[Python-checkins] [3.8] bpo-38148: Add slots to asyncio transports (GH-16077) (GH-16093)
Andrew Svetlov
webhook-mailer at python.org
Fri Sep 13 09:14:59 EDT 2019
https://github.com/python/cpython/commit/6638c9226066205a646e18da95b33e619d784b0b
commit: 6638c9226066205a646e18da95b33e619d784b0b
branch: 3.8
author: Andrew Svetlov <andrew.svetlov at gmail.com>
committer: GitHub <noreply at github.com>
date: 2019-09-13T16:14:55+03:00
summary:
[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 at gmail.com>
(cherry picked from commit 9eb35ab0d71a6bd680e84fa0f828cb634e72b681)
Co-authored-by: Andrew Svetlov <andrew.svetlov at gmail.com>
files:
A Misc/NEWS.d/next/Library/2019-09-13-08-55-43.bpo-38148.Lnww6D.rst
M Lib/asyncio/transports.py
M Lib/test/test_asyncio/test_transports.py
diff --git a/Lib/asyncio/transports.py b/Lib/asyncio/transports.py
index 47b37fa9b7f0..513b1c024a4d 100644
--- a/Lib/asyncio/transports.py
+++ b/Lib/asyncio/transports.py
@@ -9,6 +9,8 @@
class BaseTransport:
"""Base class for transports."""
+ __slots__ = ('_extra',)
+
def __init__(self, extra=None):
if extra is None:
extra = {}
@@ -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
@@ -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.
@@ -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.
@@ -180,6 +190,8 @@ def abort(self):
class SubprocessTransport(BaseTransport):
+ __slots__ = ()
+
def get_pid(self):
"""Get subprocess id."""
raise NotImplementedError
@@ -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
diff --git a/Lib/test/test_asyncio/test_transports.py b/Lib/test/test_asyncio/test_transports.py
index 7de9b3e81a34..df448557a7b7 100644
--- a/Lib/test/test_asyncio/test_transports.py
+++ b/Lib/test/test_asyncio/test_transports.py
@@ -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()
diff --git a/Misc/NEWS.d/next/Library/2019-09-13-08-55-43.bpo-38148.Lnww6D.rst b/Misc/NEWS.d/next/Library/2019-09-13-08-55-43.bpo-38148.Lnww6D.rst
new file mode 100644
index 000000000000..88c0809b1c2a
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-09-13-08-55-43.bpo-38148.Lnww6D.rst
@@ -0,0 +1 @@
+Add slots to :mod:`asyncio` transport classes, which can reduce memory usage.
More information about the Python-checkins
mailing list