diff -r f7643c893587 Lib/test/support/__init__.py --- a/Lib/test/support/__init__.py Wed Jul 23 19:28:13 2014 +0100 +++ b/Lib/test/support/__init__.py Wed Jul 23 21:11:16 2014 +0100 @@ -90,6 +90,7 @@ "is_jython", "check_impl_detail", # network "HOST", "IPV6_ENABLED", "find_unused_port", "bind_port", "open_urlresource", + "socketpair", # processes 'temp_umask', "reap_children", # logging @@ -674,6 +675,31 @@ port = sock.getsockname()[1] return port +if hasattr(socket, 'socketpair'): + socketpair = socket.socketpair +else: + def socketpair(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0): + """Ad-hoc socketpair implementation.""" + if family != socket.AF_INET: + raise ValueError("Invalid family: {!r}".format(family)) + + with socket.socket(family, type, proto) as server: + server.bind((HOST, 0)) + server.listen() + client = socket.socket(family, type, proto) + try: + client.connect(server.getsockname()) + client_addr = client.getsockname() + while True: + remote, remote_addr = server.accept() + # check that we've got the correct client + if remote_addr == client_addr: + return client, remote + remote.close() + except: + client.close() + raise + def _is_ipv6_enabled(): """Check whether IPv6 is enabled on this host.""" if socket.has_ipv6: diff -r f7643c893587 Lib/test/test_support.py --- a/Lib/test/test_support.py Wed Jul 23 19:28:13 2014 +0100 +++ b/Lib/test/test_support.py Wed Jul 23 21:11:16 2014 +0100 @@ -87,6 +87,17 @@ s.listen() s.close() + def test_socketpair(self): + c, s = support.socketpair() + self.addCleanup(c.close) + self.addCleanup(s.close) + c.send(b'x') + self.assertEqual(b'x', s.recv(1024)) + s.send(b'y') + self.assertEqual(b'y', c.recv(1024)) + c.close() + self.assertFalse(s.recv(1024)) + # Tests for temp_dir() def test_temp_dir(self):