Skip to content

Commit f375819

Browse files
committed
CRF: cythonize transport
1 parent 4ec0d0a commit f375819

File tree

3 files changed

+40
-34
lines changed

3 files changed

+40
-34
lines changed

tests/test_tcp.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2543,9 +2543,8 @@ async def client(addr):
25432543
self.assertEqual(data, b'pong')
25442544

25452545
# fill write backlog in a hacky way - renegotiation won't help
2546-
ssl_protocol = writer.transport._ssl_protocol
25472546
for _ in range(SIZE):
2548-
ssl_protocol._test__append_write_backlog(b'x' * CHUNK)
2547+
writer.transport._test__append_write_backlog(b'x' * CHUNK)
25492548

25502549
try:
25512550
data = await reader.read()

uvloop/sslproto.pxd

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ cdef enum SSLProtocolState:
66
SHUTDOWN = 4
77

88

9+
cdef class _SSLProtocolTransport:
10+
cdef:
11+
object _loop
12+
SSLProtocol _ssl_protocol
13+
bint _closed
14+
15+
916
cdef class SSLProtocol:
1017
cdef:
1118
bint _server_side
@@ -19,7 +26,7 @@ cdef class SSLProtocol:
1926

2027
object _waiter
2128
object _loop
22-
object _app_transport
29+
_SSLProtocolTransport _app_transport
2330

2431
object _transport
2532
bint _call_connection_made

uvloop/sslproto.pyx

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,26 @@ cdef _create_transport_context(server_side, server_hostname):
1515
cdef object READ_MAX_SIZE = 256 * 1024
1616

1717

18-
class _SSLProtocolTransport:
18+
cdef class _SSLProtocolTransport:
1919

2020
# TODO:
2121
# _sendfile_compatible = constants._SendfileMode.FALLBACK
2222

23-
def __init__(self, loop, ssl_protocol):
23+
def __cinit__(self, loop, ssl_protocol):
2424
self._loop = loop
2525
# SSLProtocol instance
2626
self._ssl_protocol = ssl_protocol
2727
self._closed = False
2828

2929
def get_extra_info(self, name, default=None):
3030
"""Get optional transport information."""
31-
return (<SSLProtocol>self._ssl_protocol)._get_extra_info(name, default)
31+
return self._ssl_protocol._get_extra_info(name, default)
3232

3333
def set_protocol(self, protocol):
34-
(<SSLProtocol>self._ssl_protocol)._set_app_protocol(protocol)
34+
self._ssl_protocol._set_app_protocol(protocol)
3535

3636
def get_protocol(self):
37-
return (<SSLProtocol>self._ssl_protocol)._app_protocol
37+
return self._ssl_protocol._app_protocol
3838

3939
def is_closing(self):
4040
return self._closed
@@ -48,33 +48,33 @@ class _SSLProtocolTransport:
4848
with None as its argument.
4949
"""
5050
self._closed = True
51-
(<SSLProtocol>self._ssl_protocol)._start_shutdown()
51+
self._ssl_protocol._start_shutdown()
5252

53-
def __del__(self):
53+
def __dealloc__(self):
5454
if not self._closed:
5555
_warn_with_source(
5656
"unclosed transport {!r}".format(self),
5757
ResourceWarning, self)
58-
self.close()
58+
self._closed = True
5959

6060
def is_reading(self):
61-
return not (<SSLProtocol>self._ssl_protocol)._app_reading_paused
61+
return not self._ssl_protocol._app_reading_paused
6262

6363
def pause_reading(self):
6464
"""Pause the receiving end.
6565
6666
No data will be passed to the protocol's data_received()
6767
method until resume_reading() is called.
6868
"""
69-
(<SSLProtocol>self._ssl_protocol)._pause_reading()
69+
self._ssl_protocol._pause_reading()
7070

7171
def resume_reading(self):
7272
"""Resume the receiving end.
7373
7474
Data received will once again be passed to the protocol's
7575
data_received() method.
7676
"""
77-
(<SSLProtocol>self._ssl_protocol)._resume_reading()
77+
self._ssl_protocol._resume_reading()
7878

7979
def set_write_buffer_limits(self, high=None, low=None):
8080
"""Set the high- and low-water limits for write flow control.
@@ -95,16 +95,16 @@ class _SSLProtocolTransport:
9595
reduces opportunities for doing I/O and computation
9696
concurrently.
9797
"""
98-
(<SSLProtocol>self._ssl_protocol)._set_write_buffer_limits(high, low)
99-
(<SSLProtocol>self._ssl_protocol)._control_app_writing()
98+
self._ssl_protocol._set_write_buffer_limits(high, low)
99+
self._ssl_protocol._control_app_writing()
100100

101101
def get_write_buffer_limits(self):
102-
return ((<SSLProtocol>self._ssl_protocol)._outgoing_low_water,
103-
(<SSLProtocol>self._ssl_protocol)._outgoing_high_water)
102+
return (self._ssl_protocol._outgoing_low_water,
103+
self._ssl_protocol._outgoing_high_water)
104104

105105
def get_write_buffer_size(self):
106106
"""Return the current size of the write buffers."""
107-
return (<SSLProtocol>self._ssl_protocol)._get_write_buffer_size()
107+
return self._ssl_protocol._get_write_buffer_size()
108108

109109
def set_read_buffer_limits(self, high=None, low=None):
110110
"""Set the high- and low-water limits for read flow control.
@@ -125,21 +125,21 @@ class _SSLProtocolTransport:
125125
reduces opportunities for doing I/O and computation
126126
concurrently.
127127
"""
128-
(<SSLProtocol>self._ssl_protocol)._set_read_buffer_limits(high, low)
129-
(<SSLProtocol>self._ssl_protocol)._control_ssl_reading()
128+
self._ssl_protocol._set_read_buffer_limits(high, low)
129+
self._ssl_protocol._control_ssl_reading()
130130

131131
def get_read_buffer_limits(self):
132-
return ((<SSLProtocol>self._ssl_protocol)._incoming_low_water,
133-
(<SSLProtocol>self._ssl_protocol)._incoming_high_water)
132+
return (self._ssl_protocol._incoming_low_water,
133+
self._ssl_protocol._incoming_high_water)
134134

135135
def get_read_buffer_size(self):
136136
"""Return the current size of the read buffer."""
137-
return (<SSLProtocol>self._ssl_protocol)._get_read_buffer_size()
137+
return self._ssl_protocol._get_read_buffer_size()
138138

139139
@property
140140
def _protocol_paused(self):
141141
# Required for sendfile fallback pause_writing/resume_writing logic
142-
return (<SSLProtocol>self._ssl_protocol)._app_writing_paused
142+
return self._ssl_protocol._app_writing_paused
143143

144144
def write(self, data):
145145
"""Write some data bytes to the transport.
@@ -152,15 +152,15 @@ class _SSLProtocolTransport:
152152
f"got {type(data).__name__}")
153153
if not data:
154154
return
155-
(<SSLProtocol>self._ssl_protocol)._write_appdata((data,))
155+
self._ssl_protocol._write_appdata((data,))
156156

157157
def writelines(self, list_of_data):
158158
"""Write a list (or any iterable) of data bytes to the transport.
159159
160160
The default implementation concatenates the arguments and
161161
calls write() on the result.
162162
"""
163-
(<SSLProtocol>self._ssl_protocol)._write_appdata(list_of_data)
163+
self._ssl_protocol._write_appdata(list_of_data)
164164

165165
def write_eof(self):
166166
"""Close the write end after flushing buffered data.
@@ -184,7 +184,12 @@ class _SSLProtocolTransport:
184184

185185
def _force_close(self, exc):
186186
self._closed = True
187-
(<SSLProtocol>self._ssl_protocol)._abort(exc)
187+
self._ssl_protocol._abort(exc)
188+
189+
def _test__append_write_backlog(self, data):
190+
# for test only
191+
self._ssl_protocol._write_backlog.append(data)
192+
self._ssl_protocol._write_buffer_size += len(data)
188193

189194

190195
cdef class SSLProtocol:
@@ -318,7 +323,7 @@ cdef class SSLProtocol:
318323
self._outgoing_read()
319324
self._conn_lost += 1
320325

321-
# Just mark the app transport as closed so that its __del__
326+
# Just mark the app transport as closed so that its __dealloc__
322327
# doesn't complain.
323328
if self._app_transport is not None:
324329
self._app_transport._closed = True
@@ -600,11 +605,6 @@ cdef class SSLProtocol:
600605
except Exception as ex:
601606
self._fatal_error(ex, 'Fatal error on SSL protocol')
602607

603-
def _test__append_write_backlog(self, data):
604-
# for test only
605-
self._write_backlog.append(data)
606-
self._write_buffer_size += len(data)
607-
608608
cdef _do_write(self):
609609
cdef size_t data_len, count
610610
try:

0 commit comments

Comments
 (0)