Skip to content

Commit 5344501

Browse files
authored
bpo-35394: Add empty slots to abstract asyncio protocols (python#10889)
* bpo-35394: Add empty slots to abstract asyncio protocols * Add missing test file
1 parent 7211d30 commit 5344501

4 files changed

Lines changed: 68 additions & 24 deletions

File tree

Lib/asyncio/protocols.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class BaseProtocol:
1616
write-only transport like write pipe
1717
"""
1818

19+
__slots__ = ()
20+
1921
def connection_made(self, transport):
2022
"""Called when a connection is made.
2123
@@ -87,6 +89,8 @@ class Protocol(BaseProtocol):
8789
* CL: connection_lost()
8890
"""
8991

92+
__slots__ = ()
93+
9094
def data_received(self, data):
9195
"""Called when some data is received.
9296
@@ -130,6 +134,8 @@ class BufferedProtocol(BaseProtocol):
130134
* CL: connection_lost()
131135
"""
132136

137+
__slots__ = ()
138+
133139
def get_buffer(self, sizehint):
134140
"""Called to allocate a new receive buffer.
135141
@@ -160,6 +166,8 @@ def eof_received(self):
160166
class DatagramProtocol(BaseProtocol):
161167
"""Interface for datagram protocol."""
162168

169+
__slots__ = ()
170+
163171
def datagram_received(self, data, addr):
164172
"""Called when some datagram is received."""
165173

@@ -173,6 +181,8 @@ def error_received(self, exc):
173181
class SubprocessProtocol(BaseProtocol):
174182
"""Interface for protocol for subprocess calls."""
175183

184+
__slots__ = ()
185+
176186
def pipe_data_received(self, fd, data):
177187
"""Called when the subprocess writes data into stdout/stderr pipe.
178188

Lib/test/test_asyncio/test_events.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2486,30 +2486,6 @@ async def inner():
24862486
loop.close()
24872487

24882488

2489-
class ProtocolsAbsTests(unittest.TestCase):
2490-
2491-
def test_empty(self):
2492-
f = mock.Mock()
2493-
p = asyncio.Protocol()
2494-
self.assertIsNone(p.connection_made(f))
2495-
self.assertIsNone(p.connection_lost(f))
2496-
self.assertIsNone(p.data_received(f))
2497-
self.assertIsNone(p.eof_received())
2498-
2499-
dp = asyncio.DatagramProtocol()
2500-
self.assertIsNone(dp.connection_made(f))
2501-
self.assertIsNone(dp.connection_lost(f))
2502-
self.assertIsNone(dp.error_received(f))
2503-
self.assertIsNone(dp.datagram_received(f, f))
2504-
2505-
sp = asyncio.SubprocessProtocol()
2506-
self.assertIsNone(sp.connection_made(f))
2507-
self.assertIsNone(sp.connection_lost(f))
2508-
self.assertIsNone(sp.pipe_data_received(1, f))
2509-
self.assertIsNone(sp.pipe_connection_lost(1, f))
2510-
self.assertIsNone(sp.process_exited())
2511-
2512-
25132489
class PolicyTests(unittest.TestCase):
25142490

25152491
def test_event_loop_policy(self):
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import unittest
2+
from unittest import mock
3+
4+
import asyncio
5+
6+
7+
class ProtocolsAbsTests(unittest.TestCase):
8+
9+
def test_base_protocol(self):
10+
f = mock.Mock()
11+
p = asyncio.BaseProtocol()
12+
self.assertIsNone(p.connection_made(f))
13+
self.assertIsNone(p.connection_lost(f))
14+
self.assertIsNone(p.pause_writing())
15+
self.assertIsNone(p.resume_writing())
16+
self.assertFalse(hasattr(p, '__dict__'))
17+
18+
def test_protocol(self):
19+
f = mock.Mock()
20+
p = asyncio.Protocol()
21+
self.assertIsNone(p.connection_made(f))
22+
self.assertIsNone(p.connection_lost(f))
23+
self.assertIsNone(p.data_received(f))
24+
self.assertIsNone(p.eof_received())
25+
self.assertIsNone(p.pause_writing())
26+
self.assertIsNone(p.resume_writing())
27+
self.assertFalse(hasattr(p, '__dict__'))
28+
29+
def test_buffered_protocol(self):
30+
f = mock.Mock()
31+
p = asyncio.BufferedProtocol()
32+
self.assertIsNone(p.connection_made(f))
33+
self.assertIsNone(p.connection_lost(f))
34+
self.assertIsNone(p.get_buffer(100))
35+
self.assertIsNone(p.buffer_updated(150))
36+
self.assertIsNone(p.pause_writing())
37+
self.assertIsNone(p.resume_writing())
38+
self.assertFalse(hasattr(p, '__dict__'))
39+
40+
def test_datagram_protocol(self):
41+
f = mock.Mock()
42+
dp = asyncio.DatagramProtocol()
43+
self.assertIsNone(dp.connection_made(f))
44+
self.assertIsNone(dp.connection_lost(f))
45+
self.assertIsNone(dp.error_received(f))
46+
self.assertIsNone(dp.datagram_received(f, f))
47+
self.assertFalse(hasattr(dp, '__dict__'))
48+
49+
def test_subprocess_protocol(self):
50+
f = mock.Mock()
51+
sp = asyncio.SubprocessProtocol()
52+
self.assertIsNone(sp.connection_made(f))
53+
self.assertIsNone(sp.connection_lost(f))
54+
self.assertIsNone(sp.pipe_data_received(1, f))
55+
self.assertIsNone(sp.pipe_connection_lost(1, f))
56+
self.assertIsNone(sp.process_exited())
57+
self.assertFalse(hasattr(sp, '__dict__'))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add empty slots to asyncio abstract protocols.

0 commit comments

Comments
 (0)