cpython: 6098141155f9 (original) (raw)
Mercurial > cpython
changeset 93053:6098141155f9
Issue #18643: Add socket.socketpair() on Windows. [#18643]
Charles-François Natali cf.natali@gmail.com | |
---|---|
date | Tue, 14 Oct 2014 21:22:44 +0100 |
parents | 424fbf011176 |
children | 1e1f275ee9a1 |
files | Doc/library/socket.rst Lib/socket.py Lib/test/test_socket.py Misc/NEWS |
diffstat | 4 files changed, 56 insertions(+), 3 deletions(-)[+] [-] Doc/library/socket.rst 4 Lib/socket.py 51 Lib/test/test_socket.py 2 Misc/NEWS 2 |
line wrap: on
line diff
--- a/Doc/library/socket.rst
+++ b/Doc/library/socket.rst
@@ -350,7 +350,6 @@ The following functions all create :ref:
type, and protocol number. Address family, socket type, and protocol number are
as for the :func:.socket
function above. The default family is :const:AF_UNIX
if defined on the platform; otherwise, the default is :const:AF_INET
.
@@ -361,6 +360,9 @@ The following functions all create :ref: .. versionchanged:: 3.4 The returned sockets are now non-inheritable.
+ .. function:: create_connection(address[, timeout[, source_address]])
--- a/Lib/socket.py +++ b/Lib/socket.py @@ -76,6 +76,11 @@ SocketType = IntEnum('SocketType', if name.isupper() and name.startswith('SOCK_')}) globals().update(SocketType.members) + +_LOCALHOST = '127.0.0.1' +_LOCALHOST_V6 = '::1' + + def _intenum_converter(value, enum_klass): """Convert a numeric family value to an IntEnum member. @@ -468,6 +473,52 @@ if hasattr(_socket, "socketpair"): b = socket(family, type, proto, b.detach()) return a, b +else: +
https://gist.github.com/4325783, by Geert Jansen. Public domain.
Origin:- def socketpair(family=AF_INET, type=SOCK_STREAM, proto=0):
if family == AF_INET:[](#l2.23)
host = _LOCALHOST[](#l2.24)
elif family == AF_INET6:[](#l2.25)
host = _LOCALHOST_V6[](#l2.26)
else:[](#l2.27)
raise ValueError("Only AF_INET and AF_INET6 socket address families "[](#l2.28)
"are supported")[](#l2.29)
if type != SOCK_STREAM:[](#l2.30)
raise ValueError("Only SOCK_STREAM socket type is supported")[](#l2.31)
if proto != 0:[](#l2.32)
raise ValueError("Only protocol zero is supported")[](#l2.33)
# We create a connected TCP socket. Note the trick with[](#l2.35)
# setblocking(False) that prevents us from having to create a thread.[](#l2.36)
lsock = socket(family, type, proto)[](#l2.37)
try:[](#l2.38)
lsock.bind((host, 0))[](#l2.39)
lsock.listen()[](#l2.40)
# On IPv6, ignore flow_info and scope_id[](#l2.41)
addr, port = lsock.getsockname()[:2][](#l2.42)
csock = socket(family, type, proto)[](#l2.43)
try:[](#l2.44)
csock.setblocking(False)[](#l2.45)
try:[](#l2.46)
csock.connect((addr, port))[](#l2.47)
except (BlockingIOError, InterruptedError):[](#l2.48)
pass[](#l2.49)
csock.setblocking(True)[](#l2.50)
ssock, _ = lsock.accept()[](#l2.51)
except:[](#l2.52)
csock.close()[](#l2.53)
raise[](#l2.54)
finally:[](#l2.55)
lsock.close()[](#l2.56)
return (ssock, csock)[](#l2.57)
+ +socketpair.doc = """socketpair([family[, type[, proto]]]) -> (socket object, socket object) +Create a pair of socket objects from the sockets returned by the platform +socketpair() function. +The arguments are the same as for socket() except the default family is AF_UNIX +if defined on the platform; otherwise, the default is AF_INET. +""" _blocking_errnos = { EAGAIN, EWOULDBLOCK }
--- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -3728,8 +3728,6 @@ class TCPCloserTest(ThreadedTCPSocketTes self.cli.connect((HOST, self.port)) time.sleep(1.0) -@unittest.skipUnless(hasattr(socket, 'socketpair'),
'test needs socket.socketpair()')[](#l3.8)
@unittest.skipUnless(thread, 'Threading required for this test.') class BasicSocketPairTest(SocketPairTest):
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -177,6 +177,8 @@ Core and Builtins Library ------- +- Issue #18643: Add socket.socketpair() on Windows. +