python: check rv == 0 first. read_cb return data or status code

This commit is contained in:
Tatsuhiro Tsujikawa 2012-08-17 23:19:50 +09:00
parent d8ffa70532
commit 2a5b7bc2a2
2 changed files with 48 additions and 28 deletions

View File

@ -334,8 +334,8 @@ cdef ssize_t read_callback(cspdylay.spdylay_session *session,
data_prd = <object>source.ptr data_prd = <object>source.ptr
try: try:
data, status = data_prd.read_cb(pysession, stream_id, length, res = data_prd.read_cb(pysession, stream_id, length,
data_prd.source) data_prd.source)
except TemporalCallbackFailureError as e: except TemporalCallbackFailureError as e:
return cspdylay.SPDYLAY_ERR_TEMPORAL_CALLBACK_FAILURE return cspdylay.SPDYLAY_ERR_TEMPORAL_CALLBACK_FAILURE
except CallbackFailureError as e: except CallbackFailureError as e:
@ -348,16 +348,15 @@ cdef ssize_t read_callback(cspdylay.spdylay_session *session,
pysession.base_error = e pysession.base_error = e
return cspdylay.SPDYLAY_ERR_CALLBACK_FAILURE return cspdylay.SPDYLAY_ERR_CALLBACK_FAILURE
if status == cspdylay.SPDYLAY_ERR_EOF: if res == cspdylay.SPDYLAY_ERR_DEFERRED:
eof[0] = 1 return res
elif status == cspdylay.SPDYLAY_ERR_DEFERRED: elif res:
return status if len(res) > length:
if data:
if len(data) > length:
return cspdylay.SPDYLAY_ERR_CALLBACK_FAILURE return cspdylay.SPDYLAY_ERR_CALLBACK_FAILURE
memcpy(buf, <char*>data, len(data)) memcpy(buf, <char*>res, len(res))
return len(data) return len(res)
else: else:
eof[0] = 1
return 0 return 0
cdef class Session: cdef class Session:
@ -427,7 +426,9 @@ cdef class Session:
else: else:
raise InvalidArgumentError('side must be either CLIENT or SERVER') raise InvalidArgumentError('side must be either CLIENT or SERVER')
if rv == cspdylay.SPDYLAY_ERR_NOMEM: if rv == 0:
return
elif rv == cspdylay.SPDYLAY_ERR_NOMEM:
raise MemoryError() raise MemoryError()
elif rv == cspdylay.SPDYLAY_ERR_ZLIB: elif rv == cspdylay.SPDYLAY_ERR_ZLIB:
raise ZlibError(cspdylay.spdylay_strerror(rv)) raise ZlibError(cspdylay.spdylay_strerror(rv))
@ -458,7 +459,10 @@ cdef class Session:
raise self.base_error raise self.base_error
if self.error: if self.error:
raise self.error raise self.error
if rv == cspdylay.SPDYLAY_ERR_EOF:
if rv >= 0:
return
elif rv == cspdylay.SPDYLAY_ERR_EOF:
raise EOFError() raise EOFError()
elif rv == cspdylay.SPDYLAY_ERR_NOMEM: elif rv == cspdylay.SPDYLAY_ERR_NOMEM:
raise MemoryError() raise MemoryError()
@ -473,7 +477,10 @@ cdef class Session:
raise self.base_error raise self.base_error
elif self.error: elif self.error:
raise self.error raise self.error
if rv == cspdylay.SPDYLAY_ERR_NOMEM:
if rv == 0:
return
elif rv == cspdylay.SPDYLAY_ERR_NOMEM:
raise MemoryError() raise MemoryError()
elif rv == cspdylay.SPDYLAY_ERR_CALLBACK_FAILURE: elif rv == cspdylay.SPDYLAY_ERR_CALLBACK_FAILURE:
raise CallbackFailureError() raise CallbackFailureError()
@ -481,7 +488,9 @@ cdef class Session:
cpdef resume_data(self, stream_id): cpdef resume_data(self, stream_id):
cpdef int rv cpdef int rv
rv = cspdylay.spdylay_session_resume_data(self._c_session, stream_id) rv = cspdylay.spdylay_session_resume_data(self._c_session, stream_id)
if rv == cspdylay.SPDYLAY_ERR_INVALID_ARGUMENT: if rv == 0:
return
elif rv == cspdylay.SPDYLAY_ERR_INVALID_ARGUMENT:
raise InvalidArgumentError(cspdylay.spdylay_strerror(rv)) raise InvalidArgumentError(cspdylay.spdylay_strerror(rv))
elif rv == cspdylay.SPDYLAY_ERR_NOMEM: elif rv == cspdylay.SPDYLAY_ERR_NOMEM:
raise MemoryError() raise MemoryError()
@ -508,7 +517,9 @@ cdef class Session:
cdef int rv cdef int rv
rv = cspdylay.spdylay_session_fail_session(self._c_session, rv = cspdylay.spdylay_session_fail_session(self._c_session,
status_code) status_code)
if rv == cspdylay.SPDYLAY_ERR_NOMEM: if rv == 0:
return
elif rv == cspdylay.SPDYLAY_ERR_NOMEM:
raise MemoryError() raise MemoryError()
cpdef submit_request(self, pri, nv, data_prd=None, stream_user_data=None): cpdef submit_request(self, pri, nv, data_prd=None, stream_user_data=None):
@ -539,7 +550,9 @@ cdef class Session:
c_data_prd_ptr, c_data_prd_ptr,
<void*>stream_user_data) <void*>stream_user_data)
free(cnv) free(cnv)
if rv == cspdylay.SPDYLAY_ERR_INVALID_ARGUMENT: if rv == 0:
return
elif rv == cspdylay.SPDYLAY_ERR_INVALID_ARGUMENT:
raise InvalidArgumentError(cspdylay.spdylay_strerror(rv)) raise InvalidArgumentError(cspdylay.spdylay_strerror(rv))
elif rv == cspdylay.SPDYLAY_ERR_NOMEM: elif rv == cspdylay.SPDYLAY_ERR_NOMEM:
raise MemoryError() raise MemoryError()
@ -558,7 +571,9 @@ cdef class Session:
rv = cspdylay.spdylay_submit_response(self._c_session, stream_id, rv = cspdylay.spdylay_submit_response(self._c_session, stream_id,
cnv, c_data_prd_ptr) cnv, c_data_prd_ptr)
free(cnv) free(cnv)
if rv == cspdylay.SPDYLAY_ERR_NOMEM: if rv == 0:
return
elif rv == cspdylay.SPDYLAY_ERR_NOMEM:
raise MemoryError() raise MemoryError()
cpdef submit_syn_stream(self, flags, assoc_stream_id, pri, nv, cpdef submit_syn_stream(self, flags, assoc_stream_id, pri, nv,
@ -572,7 +587,9 @@ cdef class Session:
cnv, cnv,
<void*>stream_user_data) <void*>stream_user_data)
free(cnv) free(cnv)
if rv == cspdylay.SPDYLAY_ERR_INVALID_ARGUMENT: if rv == 0:
return
elif rv == cspdylay.SPDYLAY_ERR_INVALID_ARGUMENT:
raise InvalidArgumentError(cspdylay.spdylay_strerror(rv)) raise InvalidArgumentError(cspdylay.spdylay_strerror(rv))
elif rv == cspdylay.SPDYLAY_ERR_NOMEM: elif rv == cspdylay.SPDYLAY_ERR_NOMEM:
raise MemoryError() raise MemoryError()
@ -583,20 +600,26 @@ cdef class Session:
rv = cspdylay.spdylay_submit_syn_reply(self._c_session, rv = cspdylay.spdylay_submit_syn_reply(self._c_session,
flags, stream_id, cnv) flags, stream_id, cnv)
free(cnv) free(cnv)
if rv == cspdylay.SPDYLAY_ERR_NOMEM: if rv == 0:
return
elif rv == cspdylay.SPDYLAY_ERR_NOMEM:
raise MemoryError() raise MemoryError()
cpdef submit_rst_stream(self, stream_id, status_code): cpdef submit_rst_stream(self, stream_id, status_code):
cdef int rv cdef int rv
rv = cspdylay.spdylay_submit_rst_stream(self._c_session, stream_id, rv = cspdylay.spdylay_submit_rst_stream(self._c_session, stream_id,
status_code) status_code)
if rv == cspdylay.SPDYLAY_ERR_NOMEM: if rv == 0:
return
elif rv == cspdylay.SPDYLAY_ERR_NOMEM:
raise MemoryError() raise MemoryError()
cpdef submit_goaway(self, status_code): cpdef submit_goaway(self, status_code):
cdef int rv cdef int rv
rv = cspdylay.spdylay_submit_goaway(self._c_session, status_code) rv = cspdylay.spdylay_submit_goaway(self._c_session, status_code)
if rv == cspdylay.SPDYLAY_ERR_NOMEM: if rv == 0:
return
elif rv == cspdylay.SPDYLAY_ERR_NOMEM:
raise MemoryError() raise MemoryError()
cpdef submit_settings(self, flags, iv): cpdef submit_settings(self, flags, iv):
@ -608,7 +631,9 @@ cdef class Session:
rv = cspdylay.spdylay_submit_settings(self._c_session, flags, rv = cspdylay.spdylay_submit_settings(self._c_session, flags,
civ, len(iv)) civ, len(iv))
free(civ) free(civ)
if rv == cspdylay.SPDYLAY_ERR_INVALID_ARGUMENT: if rv == 0:
return
elif rv == cspdylay.SPDYLAY_ERR_INVALID_ARGUMENT:
raise InvalidArgumentError(cspdylay.spdylay_strerror(rv)) raise InvalidArgumentError(cspdylay.spdylay_strerror(rv))
elif rv == cspdylay.SPDYLAY_ERR_NOMEM: elif rv == cspdylay.SPDYLAY_ERR_NOMEM:
raise MemoryError() raise MemoryError()

View File

@ -23,12 +23,7 @@ def send_cb(session, data):
return len(data) return len(data)
def read_cb(session, stream_id, length, source): def read_cb(session, stream_id, length, source):
data = source.read(length) return source.read(length)
if data:
status = spdylay.ERR_OK
else:
status = spdylay.ERR_EOF
return data, status
def on_ctrl_recv_cb(session, frame): def on_ctrl_recv_cb(session, frame):
ssctrl = session.user_data ssctrl = session.user_data