Issue #18643: asyncio.windows_utils now reuse socket.socketpair() on Windows if

available

Since Python 3.5, socket.socketpair() is now also available on Windows.

Make csock blocking before calling the accept() method, and fix also a typo in
an error message.
This commit is contained in:
Victor Stinner 2014-10-14 22:56:25 +02:00
parent ee3e56105f
commit f67f460242
2 changed files with 45 additions and 39 deletions

View File

@ -28,9 +28,11 @@ STDOUT = subprocess.STDOUT
_mmap_counter = itertools.count()
if hasattr(socket, 'socketpair'):
# Since Python 3.5, socket.socketpair() is now also available on Windows
socketpair = socket.socketpair
else:
# Replacement for socket.socketpair()
def socketpair(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0):
"""A socket pair usable as a self-pipe, for Windows.
@ -41,7 +43,7 @@ def socketpair(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0):
elif family == socket.AF_INET6:
host = '::1'
else:
raise ValueError("Ony AF_INET and AF_INET6 socket address families "
raise ValueError("Only AF_INET and AF_INET6 socket address families "
"are supported")
if type != socket.SOCK_STREAM:
raise ValueError("Only SOCK_STREAM socket type is supported")
@ -63,8 +65,8 @@ def socketpair(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0):
csock.connect((addr, port))
except (BlockingIOError, InterruptedError):
pass
ssock, _ = lsock.accept()
csock.setblocking(True)
ssock, _ = lsock.accept()
except:
csock.close()
raise

View File

@ -33,6 +33,8 @@ class WinsocketpairTests(unittest.TestCase):
ssock, csock = windows_utils.socketpair(family=socket.AF_INET6)
self.check_winsocketpair(ssock, csock)
@unittest.skipIf(hasattr(socket, 'socketpair'),
'socket.socketpair is available')
@mock.patch('asyncio.windows_utils.socket')
def test_winsocketpair_exc(self, m_socket):
m_socket.AF_INET = socket.AF_INET
@ -51,6 +53,8 @@ class WinsocketpairTests(unittest.TestCase):
self.assertRaises(ValueError,
windows_utils.socketpair, proto=1)
@unittest.skipIf(hasattr(socket, 'socketpair'),
'socket.socketpair is available')
@mock.patch('asyncio.windows_utils.socket')
def test_winsocketpair_close(self, m_socket):
m_socket.AF_INET = socket.AF_INET