Commit a10dc3ef authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

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().
parent 92f9339a
...@@ -446,9 +446,6 @@ class BaseProactorEventLoop(base_events.BaseEventLoop): ...@@ -446,9 +446,6 @@ class BaseProactorEventLoop(base_events.BaseEventLoop):
def sock_accept(self, sock): def sock_accept(self, sock):
return self._proactor.accept(sock) return self._proactor.accept(sock)
def _socketpair(self):
raise NotImplementedError
def _close_self_pipe(self): def _close_self_pipe(self):
if self._self_reading_future is not None: if self._self_reading_future is not None:
self._self_reading_future.cancel() self._self_reading_future.cancel()
...@@ -461,7 +458,7 @@ class BaseProactorEventLoop(base_events.BaseEventLoop): ...@@ -461,7 +458,7 @@ class BaseProactorEventLoop(base_events.BaseEventLoop):
def _make_self_pipe(self): def _make_self_pipe(self):
# A self-socket, really. :-) # A self-socket, really. :-)
self._ssock, self._csock = self._socketpair() self._ssock, self._csock = socket.socketpair()
self._ssock.setblocking(False) self._ssock.setblocking(False)
self._csock.setblocking(False) self._csock.setblocking(False)
self._internal_fds += 1 self._internal_fds += 1
......
...@@ -96,9 +96,6 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop): ...@@ -96,9 +96,6 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
self._selector.close() self._selector.close()
self._selector = None self._selector = None
def _socketpair(self):
raise NotImplementedError
def _close_self_pipe(self): def _close_self_pipe(self):
self._remove_reader(self._ssock.fileno()) self._remove_reader(self._ssock.fileno())
self._ssock.close() self._ssock.close()
...@@ -109,7 +106,7 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop): ...@@ -109,7 +106,7 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
def _make_self_pipe(self): def _make_self_pipe(self):
# A self-socket, really. :-) # A self-socket, really. :-)
self._ssock, self._csock = self._socketpair() self._ssock, self._csock = socket.socketpair()
self._ssock.setblocking(False) self._ssock.setblocking(False)
self._csock.setblocking(False) self._csock.setblocking(False)
self._internal_fds += 1 self._internal_fds += 1
......
...@@ -55,9 +55,6 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop): ...@@ -55,9 +55,6 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop):
super().__init__(selector) super().__init__(selector)
self._signal_handlers = {} self._signal_handlers = {}
def _socketpair(self):
return socket.socketpair()
def close(self): def close(self):
super().close() super().close()
for sig in list(self._signal_handlers): for sig in list(self._signal_handlers):
...@@ -677,7 +674,7 @@ class _UnixSubprocessTransport(base_subprocess.BaseSubprocessTransport): ...@@ -677,7 +674,7 @@ class _UnixSubprocessTransport(base_subprocess.BaseSubprocessTransport):
# socket (which we use in order to detect closing of the # socket (which we use in order to detect closing of the
# other end). Notably this is needed on AIX, and works # other end). Notably this is needed on AIX, and works
# just fine on other platforms. # just fine on other platforms.
stdin, stdin_w = self._loop._socketpair() stdin, stdin_w = socket.socketpair()
# Mark the write end of the stdin pipe as non-inheritable, # Mark the write end of the stdin pipe as non-inheritable,
# needed by close_fds=False on Python 3.3 and older # needed by close_fds=False on Python 3.3 and older
......
...@@ -296,9 +296,6 @@ class PipeServer(object): ...@@ -296,9 +296,6 @@ class PipeServer(object):
class _WindowsSelectorEventLoop(selector_events.BaseSelectorEventLoop): class _WindowsSelectorEventLoop(selector_events.BaseSelectorEventLoop):
"""Windows version of selector event loop.""" """Windows version of selector event loop."""
def _socketpair(self):
return windows_utils.socketpair()
class ProactorEventLoop(proactor_events.BaseProactorEventLoop): class ProactorEventLoop(proactor_events.BaseProactorEventLoop):
"""Windows version of proactor event loop using IOCP.""" """Windows version of proactor event loop using IOCP."""
...@@ -308,9 +305,6 @@ class ProactorEventLoop(proactor_events.BaseProactorEventLoop): ...@@ -308,9 +305,6 @@ class ProactorEventLoop(proactor_events.BaseProactorEventLoop):
proactor = IocpProactor() proactor = IocpProactor()
super().__init__(proactor) super().__init__(proactor)
def _socketpair(self):
return windows_utils.socketpair()
@coroutine @coroutine
def create_pipe_connection(self, protocol_factory, address): def create_pipe_connection(self, protocol_factory, address):
f = self._proactor.connect_pipe(address) f = self._proactor.connect_pipe(address)
......
...@@ -444,15 +444,13 @@ class BaseProactorEventLoopTests(test_utils.TestCase): ...@@ -444,15 +444,13 @@ class BaseProactorEventLoopTests(test_utils.TestCase):
self.ssock, self.csock = mock.Mock(), mock.Mock() self.ssock, self.csock = mock.Mock(), mock.Mock()
class EventLoop(BaseProactorEventLoop): with mock.patch('asyncio.proactor_events.socket.socketpair',
def _socketpair(s): return_value=(self.ssock, self.csock)):
return (self.ssock, self.csock) self.loop = BaseProactorEventLoop(self.proactor)
self.loop = EventLoop(self.proactor)
self.set_event_loop(self.loop) self.set_event_loop(self.loop)
@mock.patch.object(BaseProactorEventLoop, 'call_soon') @mock.patch.object(BaseProactorEventLoop, 'call_soon')
@mock.patch.object(BaseProactorEventLoop, '_socketpair') @mock.patch('asyncio.proactor_events.socket.socketpair')
def test_ctor(self, socketpair, call_soon): def test_ctor(self, socketpair, call_soon):
ssock, csock = socketpair.return_value = ( ssock, csock = socketpair.return_value = (
mock.Mock(), mock.Mock()) mock.Mock(), mock.Mock())
...@@ -506,14 +504,6 @@ class BaseProactorEventLoopTests(test_utils.TestCase): ...@@ -506,14 +504,6 @@ class BaseProactorEventLoopTests(test_utils.TestCase):
self.loop.sock_accept(self.sock) self.loop.sock_accept(self.sock)
self.proactor.accept.assert_called_with(self.sock) self.proactor.accept.assert_called_with(self.sock)
def test_socketpair(self):
class EventLoop(BaseProactorEventLoop):
# override the destructor to not log a ResourceWarning
def __del__(self):
pass
self.assertRaises(
NotImplementedError, EventLoop, self.proactor)
def test_make_socket_transport(self): def test_make_socket_transport(self):
tr = self.loop._make_socket_transport(self.sock, asyncio.Protocol()) tr = self.loop._make_socket_transport(self.sock, asyncio.Protocol())
self.assertIsInstance(tr, _ProactorSocketTransport) self.assertIsInstance(tr, _ProactorSocketTransport)
......
...@@ -154,9 +154,6 @@ class BaseSelectorEventLoopTests(test_utils.TestCase): ...@@ -154,9 +154,6 @@ class BaseSelectorEventLoopTests(test_utils.TestCase):
self.loop.close() self.loop.close()
self.assertIsNone(self.loop._selector) self.assertIsNone(self.loop._selector)
def test_socketpair(self):
self.assertRaises(NotImplementedError, self.loop._socketpair)
def test_read_from_self_tryagain(self): def test_read_from_self_tryagain(self):
self.loop._ssock.recv.side_effect = BlockingIOError self.loop._ssock.recv.side_effect = BlockingIOError
self.assertIsNone(self.loop._read_from_self()) self.assertIsNone(self.loop._read_from_self())
......
import os import os
import socket
import sys import sys
import unittest import unittest
from unittest import mock from unittest import mock
...@@ -36,7 +37,7 @@ class ProactorTests(test_utils.TestCase): ...@@ -36,7 +37,7 @@ class ProactorTests(test_utils.TestCase):
self.set_event_loop(self.loop) self.set_event_loop(self.loop)
def test_close(self): def test_close(self):
a, b = self.loop._socketpair() a, b = socket.socketpair()
trans = self.loop._make_socket_transport(a, asyncio.Protocol()) trans = self.loop._make_socket_transport(a, asyncio.Protocol())
f = asyncio.ensure_future(self.loop.sock_recv(b, 100)) f = asyncio.ensure_future(self.loop.sock_recv(b, 100))
trans.close() trans.close()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment