Skip to content

Commit 6638c92

Browse files
authored
[3.8] bpo-38148: Add slots to asyncio transports (GH-16077) (GH-16093)
* bpo-38148: Add slots to asyncio transports * Update Misc/NEWS.d/next/Library/2019-09-13-08-55-43.bpo-38148.Lnww6D.rst Co-Authored-By: Kyle Stanley <[email protected]> (cherry picked from commit 9eb35ab) Co-authored-by: Andrew Svetlov <[email protected]>
1 parent 4556b1d commit 6638c92

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

Lib/asyncio/transports.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
class BaseTransport:
1010
"""Base class for transports."""
1111

12+
__slots__ = ('_extra',)
13+
1214
def __init__(self, extra=None):
1315
if extra is None:
1416
extra = {}
@@ -44,6 +46,8 @@ def get_protocol(self):
4446
class ReadTransport(BaseTransport):
4547
"""Interface for read-only transports."""
4648

49+
__slots__ = ()
50+
4751
def is_reading(self):
4852
"""Return True if the transport is receiving."""
4953
raise NotImplementedError
@@ -68,6 +72,8 @@ def resume_reading(self):
6872
class WriteTransport(BaseTransport):
6973
"""Interface for write-only transports."""
7074

75+
__slots__ = ()
76+
7177
def set_write_buffer_limits(self, high=None, low=None):
7278
"""Set the high- and low-water limits for write flow control.
7379
@@ -154,10 +160,14 @@ class Transport(ReadTransport, WriteTransport):
154160
except writelines(), which calls write() in a loop.
155161
"""
156162

163+
__slots__ = ()
164+
157165

158166
class DatagramTransport(BaseTransport):
159167
"""Interface for datagram (UDP) transports."""
160168

169+
__slots__ = ()
170+
161171
def sendto(self, data, addr=None):
162172
"""Send data to the transport.
163173
@@ -180,6 +190,8 @@ def abort(self):
180190

181191
class SubprocessTransport(BaseTransport):
182192

193+
__slots__ = ()
194+
183195
def get_pid(self):
184196
"""Get subprocess id."""
185197
raise NotImplementedError
@@ -247,6 +259,8 @@ class _FlowControlMixin(Transport):
247259
resume_writing() may be called.
248260
"""
249261

262+
__slots__ = ('_loop', '_protocol_paused', '_high_water', '_low_water')
263+
250264
def __init__(self, extra=None, loop=None):
251265
super().__init__(extra)
252266
assert loop is not None

Lib/test/test_asyncio/test_transports.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@ def test_get_extra_info(self):
2222
self.assertIs(default, transport.get_extra_info('unknown', default))
2323

2424
def test_writelines(self):
25-
transport = asyncio.Transport()
26-
transport.write = mock.Mock()
25+
writer = mock.Mock()
26+
27+
class MyTransport(asyncio.Transport):
28+
def write(self, data):
29+
writer(data)
30+
31+
transport = MyTransport()
2732

2833
transport.writelines([b'line1',
2934
bytearray(b'line2'),
3035
memoryview(b'line3')])
31-
self.assertEqual(1, transport.write.call_count)
32-
transport.write.assert_called_with(b'line1line2line3')
36+
self.assertEqual(1, writer.call_count)
37+
writer.assert_called_with(b'line1line2line3')
3338

3439
def test_not_implemented(self):
3540
transport = asyncio.Transport()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add slots to :mod:`asyncio` transport classes, which can reduce memory usage.

0 commit comments

Comments
 (0)