Skip to content

Commit a10dc3e

Browse files
authored
asyncio: use directly socket.socketpair() (#4597)
Since Python 3.5, socket.socketpair() is also available on Windows, and so can be used directly, rather than using asyncio.windows_utils.socketpair().
1 parent 92f9339 commit a10dc3e

7 files changed

Lines changed: 9 additions & 36 deletions

File tree

Lib/asyncio/proactor_events.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,6 @@ def sock_connect(self, sock, address):
446446
def sock_accept(self, sock):
447447
return self._proactor.accept(sock)
448448

449-
def _socketpair(self):
450-
raise NotImplementedError
451-
452449
def _close_self_pipe(self):
453450
if self._self_reading_future is not None:
454451
self._self_reading_future.cancel()
@@ -461,7 +458,7 @@ def _close_self_pipe(self):
461458

462459
def _make_self_pipe(self):
463460
# A self-socket, really. :-)
464-
self._ssock, self._csock = self._socketpair()
461+
self._ssock, self._csock = socket.socketpair()
465462
self._ssock.setblocking(False)
466463
self._csock.setblocking(False)
467464
self._internal_fds += 1

Lib/asyncio/selector_events.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@ def close(self):
9696
self._selector.close()
9797
self._selector = None
9898

99-
def _socketpair(self):
100-
raise NotImplementedError
101-
10299
def _close_self_pipe(self):
103100
self._remove_reader(self._ssock.fileno())
104101
self._ssock.close()
@@ -109,7 +106,7 @@ def _close_self_pipe(self):
109106

110107
def _make_self_pipe(self):
111108
# A self-socket, really. :-)
112-
self._ssock, self._csock = self._socketpair()
109+
self._ssock, self._csock = socket.socketpair()
113110
self._ssock.setblocking(False)
114111
self._csock.setblocking(False)
115112
self._internal_fds += 1

Lib/asyncio/unix_events.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ def __init__(self, selector=None):
5555
super().__init__(selector)
5656
self._signal_handlers = {}
5757

58-
def _socketpair(self):
59-
return socket.socketpair()
60-
6158
def close(self):
6259
super().close()
6360
for sig in list(self._signal_handlers):
@@ -677,7 +674,7 @@ def _start(self, args, shell, stdin, stdout, stderr, bufsize, **kwargs):
677674
# socket (which we use in order to detect closing of the
678675
# other end). Notably this is needed on AIX, and works
679676
# just fine on other platforms.
680-
stdin, stdin_w = self._loop._socketpair()
677+
stdin, stdin_w = socket.socketpair()
681678

682679
# Mark the write end of the stdin pipe as non-inheritable,
683680
# needed by close_fds=False on Python 3.3 and older

Lib/asyncio/windows_events.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,6 @@ def close(self):
296296
class _WindowsSelectorEventLoop(selector_events.BaseSelectorEventLoop):
297297
"""Windows version of selector event loop."""
298298

299-
def _socketpair(self):
300-
return windows_utils.socketpair()
301-
302299

303300
class ProactorEventLoop(proactor_events.BaseProactorEventLoop):
304301
"""Windows version of proactor event loop using IOCP."""
@@ -308,9 +305,6 @@ def __init__(self, proactor=None):
308305
proactor = IocpProactor()
309306
super().__init__(proactor)
310307

311-
def _socketpair(self):
312-
return windows_utils.socketpair()
313-
314308
@coroutine
315309
def create_pipe_connection(self, protocol_factory, address):
316310
f = self._proactor.connect_pipe(address)

Lib/test/test_asyncio/test_proactor_events.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -444,15 +444,13 @@ def setUp(self):
444444

445445
self.ssock, self.csock = mock.Mock(), mock.Mock()
446446

447-
class EventLoop(BaseProactorEventLoop):
448-
def _socketpair(s):
449-
return (self.ssock, self.csock)
450-
451-
self.loop = EventLoop(self.proactor)
447+
with mock.patch('asyncio.proactor_events.socket.socketpair',
448+
return_value=(self.ssock, self.csock)):
449+
self.loop = BaseProactorEventLoop(self.proactor)
452450
self.set_event_loop(self.loop)
453451

454452
@mock.patch.object(BaseProactorEventLoop, 'call_soon')
455-
@mock.patch.object(BaseProactorEventLoop, '_socketpair')
453+
@mock.patch('asyncio.proactor_events.socket.socketpair')
456454
def test_ctor(self, socketpair, call_soon):
457455
ssock, csock = socketpair.return_value = (
458456
mock.Mock(), mock.Mock())
@@ -506,14 +504,6 @@ def test_sock_accept(self):
506504
self.loop.sock_accept(self.sock)
507505
self.proactor.accept.assert_called_with(self.sock)
508506

509-
def test_socketpair(self):
510-
class EventLoop(BaseProactorEventLoop):
511-
# override the destructor to not log a ResourceWarning
512-
def __del__(self):
513-
pass
514-
self.assertRaises(
515-
NotImplementedError, EventLoop, self.proactor)
516-
517507
def test_make_socket_transport(self):
518508
tr = self.loop._make_socket_transport(self.sock, asyncio.Protocol())
519509
self.assertIsInstance(tr, _ProactorSocketTransport)

Lib/test/test_asyncio/test_selector_events.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,6 @@ def test_close_no_selector(self):
154154
self.loop.close()
155155
self.assertIsNone(self.loop._selector)
156156

157-
def test_socketpair(self):
158-
self.assertRaises(NotImplementedError, self.loop._socketpair)
159-
160157
def test_read_from_self_tryagain(self):
161158
self.loop._ssock.recv.side_effect = BlockingIOError
162159
self.assertIsNone(self.loop._read_from_self())

Lib/test/test_asyncio/test_windows_events.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import socket
23
import sys
34
import unittest
45
from unittest import mock
@@ -36,7 +37,7 @@ def setUp(self):
3637
self.set_event_loop(self.loop)
3738

3839
def test_close(self):
39-
a, b = self.loop._socketpair()
40+
a, b = socket.socketpair()
4041
trans = self.loop._make_socket_transport(a, asyncio.Protocol())
4142
f = asyncio.ensure_future(self.loop.sock_recv(b, 100))
4243
trans.close()

0 commit comments

Comments
 (0)