Skip to content

Commit 94f8283

Browse files
author
taras
committed
Implemented PR review comments
1 parent ae09ab8 commit 94f8283

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

uvloop/handles/stream.pxd

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
cdef enum ProtocolType:
2+
SIMPLE = 0 # User Protocol doesn't support asyncio.BufferedProtocol
3+
BUFFERED = 1 # User Protocol supports asyncio.BufferedProtocol
4+
SSL_PROTOCOL = 2 # Our own SSLProtocol
5+
6+
17
cdef class UVStream(UVBaseTransport):
28
cdef:
39
uv.uv_shutdown_t _shutdown_req
410
bint __shutting_down
511
bint __reading
612
bint __read_error_close
713

8-
bint __buffered
14+
ProtocolType __protocol_type
915
object _protocol_get_buffer
1016
object _protocol_buffer_updated
1117

uvloop/handles/stream.pyx

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cdef enum:
1+
cdef enum:
22
__PREALLOCED_BUFS = 4
33

44

@@ -213,14 +213,15 @@ cdef class UVStream(UVBaseTransport):
213213
self.__shutting_down = 0
214214
self.__reading = 0
215215
self.__read_error_close = 0
216-
self.__buffered = 0
217-
self._eof = 0
218-
self._buffer = []
219-
self._buffer_size = 0
220216

217+
self.__protocol_type = ProtocolType.SIMPLE
221218
self._protocol_get_buffer = None
222219
self._protocol_buffer_updated = None
223220

221+
self._eof = 0
222+
self._buffer = []
223+
self._buffer_size = 0
224+
224225
self._read_pybuf_acquired = False
225226

226227
cdef _set_protocol(self, object protocol):
@@ -230,23 +231,23 @@ cdef class UVStream(UVBaseTransport):
230231
UVBaseTransport._set_protocol(self, protocol)
231232

232233
if isinstance(protocol, SSLProtocol):
233-
self.__buffered = 1
234+
self.__protocol_type = ProtocolType.SSL_PROTOCOL
234235
elif (hasattr(protocol, 'get_buffer') and
235236
not isinstance(protocol, aio_Protocol)):
236237
try:
237238
self._protocol_get_buffer = protocol.get_buffer
238239
self._protocol_buffer_updated = protocol.buffer_updated
239-
self.__buffered = 1
240+
self.__protocol_type = ProtocolType.BUFFERED
240241
except AttributeError:
241242
pass
242243
else:
243-
self.__buffered = 0
244+
self.__protocol_type = ProtocolType.SIMPLE
244245

245246
cdef _clear_protocol(self):
246247
UVBaseTransport._clear_protocol(self)
247248
self._protocol_get_buffer = None
248249
self._protocol_buffer_updated = None
249-
self.__buffered = 0
250+
self.__protocol_type = ProtocolType.SIMPLE
250251

251252
cdef inline _shutdown(self):
252253
cdef int err
@@ -296,14 +297,14 @@ cdef class UVStream(UVBaseTransport):
296297
if self.__reading:
297298
return
298299

299-
if self.__buffered:
300-
err = uv.uv_read_start(<uv.uv_stream_t*>self._handle,
301-
__uv_stream_buffered_alloc,
302-
__uv_stream_buffered_on_read)
303-
else:
300+
if self.__protocol_type == ProtocolType.SIMPLE:
304301
err = uv.uv_read_start(<uv.uv_stream_t*>self._handle,
305302
__loop_alloc_buffer,
306303
__uv_stream_on_read)
304+
else:
305+
err = uv.uv_read_start(<uv.uv_stream_t *> self._handle,
306+
__uv_stream_buffered_alloc,
307+
__uv_stream_buffered_on_read)
307308
if err < 0:
308309
exc = convert_error(err)
309310
self._fatal_error(exc, True)
@@ -927,7 +928,7 @@ cdef void __uv_stream_buffered_alloc(
927928

928929
# Fast pass for our own SSLProtocol
929930
# avoid python calls, memoryviews, context enter/exit, etc
930-
if isinstance(sc._protocol, SSLProtocol):
931+
if sc.__protocol_type == ProtocolType.SSL_PROTOCOL:
931932
try:
932933
(<SSLProtocol>sc._protocol).get_buffer_impl(
933934
suggested_size, &uvbuf.base, &uvbuf.len)
@@ -1001,7 +1002,7 @@ cdef void __uv_stream_buffered_on_read(
10011002
return
10021003

10031004
try:
1004-
if nread > 0 and not isinstance(sc._protocol, SSLProtocol) and not sc._read_pybuf_acquired:
1005+
if nread > 0 and sc.__protocol_type != ProtocolType.SSL_PROTOCOL and not sc._read_pybuf_acquired:
10051006
# From libuv docs:
10061007
# nread is > 0 if there is data available or < 0 on error. When
10071008
# we’ve reached EOF, nread will be set to UV_EOF. When
@@ -1022,7 +1023,7 @@ cdef void __uv_stream_buffered_on_read(
10221023
if UVLOOP_DEBUG:
10231024
loop._debug_stream_read_cb_total += 1
10241025

1025-
if isinstance(sc._protocol, SSLProtocol):
1026+
if sc.__protocol_type == ProtocolType.SSL_PROTOCOL:
10261027
Context_Enter(sc.context)
10271028
try:
10281029
(<SSLProtocol>sc._protocol).buffer_updated_impl(nread)

0 commit comments

Comments
 (0)