Skip to content

[3.8] bpo-38148: Add slots to asyncio transports (GH-16077) #16093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Lib/asyncio/transports.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
class BaseTransport:
"""Base class for transports."""

__slots__ = ('_extra',)

def __init__(self, extra=None):
if extra is None:
extra = {}
Expand Down Expand Up @@ -44,6 +46,8 @@ def get_protocol(self):
class ReadTransport(BaseTransport):
"""Interface for read-only transports."""

__slots__ = ()

def is_reading(self):
"""Return True if the transport is receiving."""
raise NotImplementedError
Expand All @@ -68,6 +72,8 @@ def resume_reading(self):
class WriteTransport(BaseTransport):
"""Interface for write-only transports."""

__slots__ = ()

def set_write_buffer_limits(self, high=None, low=None):
"""Set the high- and low-water limits for write flow control.

Expand Down Expand Up @@ -154,10 +160,14 @@ class Transport(ReadTransport, WriteTransport):
except writelines(), which calls write() in a loop.
"""

__slots__ = ()


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

__slots__ = ()

def sendto(self, data, addr=None):
"""Send data to the transport.

Expand All @@ -180,6 +190,8 @@ def abort(self):

class SubprocessTransport(BaseTransport):

__slots__ = ()

def get_pid(self):
"""Get subprocess id."""
raise NotImplementedError
Expand Down Expand Up @@ -247,6 +259,8 @@ class _FlowControlMixin(Transport):
resume_writing() may be called.
"""

__slots__ = ('_loop', '_protocol_paused', '_high_water', '_low_water')

def __init__(self, extra=None, loop=None):
super().__init__(extra)
assert loop is not None
Expand Down
13 changes: 9 additions & 4 deletions Lib/test/test_asyncio/test_transports.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@ def test_get_extra_info(self):
self.assertIs(default, transport.get_extra_info('unknown', default))

def test_writelines(self):
transport = asyncio.Transport()
transport.write = mock.Mock()
writer = mock.Mock()

class MyTransport(asyncio.Transport):
def write(self, data):
writer(data)

transport = MyTransport()

transport.writelines([b'line1',
bytearray(b'line2'),
memoryview(b'line3')])
self.assertEqual(1, transport.write.call_count)
transport.write.assert_called_with(b'line1line2line3')
self.assertEqual(1, writer.call_count)
writer.assert_called_with(b'line1line2line3')

def test_not_implemented(self):
transport = asyncio.Transport()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add slots to :mod:`asyncio` transport classes, which can reduce memory usage.