mirror of
https://github.com/python/cpython.git
synced 2025-08-16 14:50:43 +00:00
[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 <aeros167@gmail.com>
(cherry picked from commit 9eb35ab0d7
)
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
This commit is contained in:
parent
4556b1d35c
commit
6638c92260
3 changed files with 24 additions and 4 deletions
|
@ -9,6 +9,8 @@ __all__ = (
|
||||||
class BaseTransport:
|
class BaseTransport:
|
||||||
"""Base class for transports."""
|
"""Base class for transports."""
|
||||||
|
|
||||||
|
__slots__ = ('_extra',)
|
||||||
|
|
||||||
def __init__(self, extra=None):
|
def __init__(self, extra=None):
|
||||||
if extra is None:
|
if extra is None:
|
||||||
extra = {}
|
extra = {}
|
||||||
|
@ -44,6 +46,8 @@ class BaseTransport:
|
||||||
class ReadTransport(BaseTransport):
|
class ReadTransport(BaseTransport):
|
||||||
"""Interface for read-only transports."""
|
"""Interface for read-only transports."""
|
||||||
|
|
||||||
|
__slots__ = ()
|
||||||
|
|
||||||
def is_reading(self):
|
def is_reading(self):
|
||||||
"""Return True if the transport is receiving."""
|
"""Return True if the transport is receiving."""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
@ -68,6 +72,8 @@ class ReadTransport(BaseTransport):
|
||||||
class WriteTransport(BaseTransport):
|
class WriteTransport(BaseTransport):
|
||||||
"""Interface for write-only transports."""
|
"""Interface for write-only transports."""
|
||||||
|
|
||||||
|
__slots__ = ()
|
||||||
|
|
||||||
def set_write_buffer_limits(self, high=None, low=None):
|
def set_write_buffer_limits(self, high=None, low=None):
|
||||||
"""Set the high- and low-water limits for write flow control.
|
"""Set the high- and low-water limits for write flow control.
|
||||||
|
|
||||||
|
@ -154,10 +160,14 @@ class Transport(ReadTransport, WriteTransport):
|
||||||
except writelines(), which calls write() in a loop.
|
except writelines(), which calls write() in a loop.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
__slots__ = ()
|
||||||
|
|
||||||
|
|
||||||
class DatagramTransport(BaseTransport):
|
class DatagramTransport(BaseTransport):
|
||||||
"""Interface for datagram (UDP) transports."""
|
"""Interface for datagram (UDP) transports."""
|
||||||
|
|
||||||
|
__slots__ = ()
|
||||||
|
|
||||||
def sendto(self, data, addr=None):
|
def sendto(self, data, addr=None):
|
||||||
"""Send data to the transport.
|
"""Send data to the transport.
|
||||||
|
|
||||||
|
@ -180,6 +190,8 @@ class DatagramTransport(BaseTransport):
|
||||||
|
|
||||||
class SubprocessTransport(BaseTransport):
|
class SubprocessTransport(BaseTransport):
|
||||||
|
|
||||||
|
__slots__ = ()
|
||||||
|
|
||||||
def get_pid(self):
|
def get_pid(self):
|
||||||
"""Get subprocess id."""
|
"""Get subprocess id."""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
@ -247,6 +259,8 @@ class _FlowControlMixin(Transport):
|
||||||
resume_writing() may be called.
|
resume_writing() may be called.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
__slots__ = ('_loop', '_protocol_paused', '_high_water', '_low_water')
|
||||||
|
|
||||||
def __init__(self, extra=None, loop=None):
|
def __init__(self, extra=None, loop=None):
|
||||||
super().__init__(extra)
|
super().__init__(extra)
|
||||||
assert loop is not None
|
assert loop is not None
|
||||||
|
|
|
@ -22,14 +22,19 @@ class TransportTests(unittest.TestCase):
|
||||||
self.assertIs(default, transport.get_extra_info('unknown', default))
|
self.assertIs(default, transport.get_extra_info('unknown', default))
|
||||||
|
|
||||||
def test_writelines(self):
|
def test_writelines(self):
|
||||||
transport = asyncio.Transport()
|
writer = mock.Mock()
|
||||||
transport.write = mock.Mock()
|
|
||||||
|
class MyTransport(asyncio.Transport):
|
||||||
|
def write(self, data):
|
||||||
|
writer(data)
|
||||||
|
|
||||||
|
transport = MyTransport()
|
||||||
|
|
||||||
transport.writelines([b'line1',
|
transport.writelines([b'line1',
|
||||||
bytearray(b'line2'),
|
bytearray(b'line2'),
|
||||||
memoryview(b'line3')])
|
memoryview(b'line3')])
|
||||||
self.assertEqual(1, transport.write.call_count)
|
self.assertEqual(1, writer.call_count)
|
||||||
transport.write.assert_called_with(b'line1line2line3')
|
writer.assert_called_with(b'line1line2line3')
|
||||||
|
|
||||||
def test_not_implemented(self):
|
def test_not_implemented(self):
|
||||||
transport = asyncio.Transport()
|
transport = asyncio.Transport()
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Add slots to :mod:`asyncio` transport classes, which can reduce memory usage.
|
Loading…
Add table
Add a link
Reference in a new issue