Skip to content

Commit 6a1a164

Browse files
author
taras
committed
Fix tests
1 parent d028020 commit 6a1a164

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-24
lines changed

uvloop/handles/stream.pyx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ cdef class UVStream(UVBaseTransport):
233233
UVBaseTransport._set_protocol(self, protocol)
234234

235235
if isinstance(protocol, SSLProtocol):
236-
self._protocol_buffer_updated = protocol.buffer_updated
237236
self.__buffered = 1
238237
elif (hasattr(protocol, 'get_buffer') and
239238
not isinstance(protocol, aio_Protocol)):
@@ -930,13 +929,16 @@ cdef void __uv_stream_buffered_alloc(
930929
cdef UVStream sc = <UVStream>stream.data
931930

932931
# Fast pass for our own SSLProtocol
933-
# avoid python calls, memoryviews, etc
932+
# avoid python calls, memoryviews, context enter/exit, etc
934933
if isinstance(sc._protocol, SSLProtocol):
935934
try:
936-
(<SSLProtocol>sc._protocol).get_buffer_c(
935+
(<SSLProtocol>sc._protocol).get_buffer_impl(
937936
suggested_size, &uvbuf.base, &uvbuf.len)
938937
return
939938
except BaseException as exc:
939+
# Can't call 'sc._fatal_error' or 'sc._close', libuv will SF.
940+
# We'll do it later in __uv_stream_buffered_on_read when we
941+
# receive UV_ENOBUFS.
940942
uvbuf.len = 0
941943
uvbuf.base = NULL
942944
return
@@ -1023,7 +1025,14 @@ cdef void __uv_stream_buffered_on_read(
10231025
if UVLOOP_DEBUG:
10241026
loop._debug_stream_read_cb_total += 1
10251027

1026-
run_in_context1(sc.context, sc._protocol_buffer_updated, nread)
1028+
if isinstance(sc._protocol, SSLProtocol):
1029+
Context_Enter(sc.context)
1030+
try:
1031+
(<SSLProtocol>sc._protocol).buffer_updated_impl(nread)
1032+
finally:
1033+
Context_Exit(sc.context)
1034+
else:
1035+
run_in_context1(sc.context, sc._protocol_buffer_updated, nread)
10271036
except BaseException as exc:
10281037
if UVLOOP_DEBUG:
10291038
loop._debug_stream_read_cb_errors_total += 1

uvloop/sslproto.pxd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ cdef class SSLProtocol:
8383
object _handshake_timeout_handle
8484
object _shutdown_timeout_handle
8585

86-
cdef inline get_buffer_c(self, size_t n, char** buf, size_t* buf_size)
86+
# Instead of doing python calls, c methods *_impl are called directly
87+
# from stream.pyx
88+
89+
cdef inline get_buffer_impl(self, size_t n, char** buf, size_t* buf_size)
90+
cdef inline buffer_updated_impl(self, size_t nbytes)
8791

8892
cdef inline _set_app_protocol(self, app_protocol)
8993
cdef inline _wakeup_waiter(self, exc=*)

uvloop/sslproto.pyx

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ cdef class SSLProtocol:
355355
self._handshake_timeout_handle.cancel()
356356
self._handshake_timeout_handle = None
357357

358-
cdef get_buffer_c(self, size_t n, char** buf, size_t* buf_size):
358+
cdef get_buffer_impl(self, size_t n, char** buf, size_t* buf_size):
359359
cdef size_t want = n
360360
if want > SSL_READ_MAX_SIZE:
361361
want = SSL_READ_MAX_SIZE
@@ -368,17 +368,7 @@ cdef class SSLProtocol:
368368
buf[0] = self._ssl_buffer
369369
buf_size[0] = self._ssl_buffer_len
370370

371-
def get_buffer(self, size_t n):
372-
# This pure python call is still used by some very peculiar test cases
373-
374-
cdef:
375-
char* buf
376-
size_t buf_size
377-
378-
self.get_buffer_c(n, &buf, &buf_size)
379-
return PyMemoryView_FromMemory(buf, buf_size, PyBUF_WRITE)
380-
381-
def buffer_updated(self, size_t nbytes):
371+
cdef buffer_updated_impl(self, size_t nbytes):
382372
self._incoming_write(PyMemoryView_FromMemory(
383373
self._ssl_buffer, nbytes, PyBUF_WRITE))
384374

@@ -394,6 +384,18 @@ cdef class SSLProtocol:
394384
elif self._state == SHUTDOWN:
395385
self._do_shutdown()
396386

387+
def get_buffer(self, size_t n):
388+
# This pure python call is still used by some very peculiar test cases
389+
cdef:
390+
char* buf
391+
size_t buf_size
392+
393+
self.get_buffer_impl(n, &buf, &buf_size)
394+
return PyMemoryView_FromMemory(buf, buf_size, PyBUF_WRITE)
395+
396+
def buffer_updated(self, size_t nbytes):
397+
self.buffer_updated_impl(nbytes)
398+
397399
def eof_received(self):
398400
"""Called when the other end of the low-level stream
399401
is half-closed.
@@ -546,6 +548,7 @@ cdef class SSLProtocol:
546548
if self._app_state == STATE_INIT:
547549
self._app_state = STATE_CON_MADE
548550
self._app_protocol.connection_made(self._get_app_transport())
551+
549552
self._wakeup_waiter()
550553

551554
# We should wakeup user code before sending the first data below. In
@@ -558,7 +561,7 @@ cdef class SSLProtocol:
558561
new_MethodHandle(self._loop,
559562
"SSLProtocol._do_read",
560563
<method_t> self._do_read,
561-
None, # current context is good
564+
None,
562565
self))
563566

564567
# Shutdown flow
@@ -758,7 +761,7 @@ cdef class SSLProtocol:
758761
new_MethodHandle(self._loop,
759762
"SSLProtocol._do_read",
760763
<method_t>self._do_read,
761-
None, # current context is good
764+
None,
762765
self))
763766
except ssl_SSLAgainErrors as exc:
764767
pass
@@ -794,10 +797,12 @@ cdef class SSLProtocol:
794797
data.append(chunk)
795798
except ssl_SSLAgainErrors as exc:
796799
pass
800+
797801
if one:
798802
self._app_protocol.data_received(first)
799803
elif not zero:
800804
self._app_protocol.data_received(b''.join(data))
805+
801806
if not chunk:
802807
# close_notify
803808
self._call_eof_received()
@@ -887,11 +892,12 @@ cdef class SSLProtocol:
887892
self._app_reading_paused = False
888893
if self._state == WRAPPED:
889894
self._loop._call_soon_handle(
890-
new_MethodHandle(self._loop,
891-
"SSLProtocol._do_read",
892-
<method_t>self._do_read,
893-
context,
894-
self))
895+
new_MethodHandle1(self._loop,
896+
"SSLProtocol._do_read",
897+
<method1_t>self._do_read,
898+
context,
899+
self,
900+
context))
895901

896902
# Flow control for reads from SSL socket
897903

0 commit comments

Comments
 (0)