summaryrefslogtreecommitdiffstats
path: root/Lib/socket.py
diff options
context:
space:
mode:
authorCharles-François Natali <cf.natali@gmail.com>2014-10-14 20:22:44 (GMT)
committerCharles-François Natali <cf.natali@gmail.com>2014-10-14 20:22:44 (GMT)
commit98c745a773f0536001d85ebe39372570bb303c4b (patch)
tree62b0435055818f81be3de2bb8e8b0538314d94c6 /Lib/socket.py
parent987f3dd161c4390d5e458298b93f73fdb984ad6c (diff)
downloadcpython-98c745a773f0536001d85ebe39372570bb303c4b.zip
cpython-98c745a773f0536001d85ebe39372570bb303c4b.tar.gz
cpython-98c745a773f0536001d85ebe39372570bb303c4b.tar.bz2
Issue #18643: Add socket.socketpair() on Windows.
Diffstat (limited to 'Lib/socket.py')
-rw-r--r--Lib/socket.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/Lib/socket.py b/Lib/socket.py
index 72aa220..ca3d21c 100644
--- 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:
+
+ # Origin: https://gist.github.com/4325783, by Geert Jansen. Public domain.
+ def socketpair(family=AF_INET, type=SOCK_STREAM, proto=0):
+ if family == AF_INET:
+ host = _LOCALHOST
+ elif family == AF_INET6:
+ host = _LOCALHOST_V6
+ else:
+ raise ValueError("Only AF_INET and AF_INET6 socket address families "
+ "are supported")
+ if type != SOCK_STREAM:
+ raise ValueError("Only SOCK_STREAM socket type is supported")
+ if proto != 0:
+ raise ValueError("Only protocol zero is supported")
+
+ # We create a connected TCP socket. Note the trick with
+ # setblocking(False) that prevents us from having to create a thread.
+ lsock = socket(family, type, proto)
+ try:
+ lsock.bind((host, 0))
+ lsock.listen()
+ # On IPv6, ignore flow_info and scope_id
+ addr, port = lsock.getsockname()[:2]
+ csock = socket(family, type, proto)
+ try:
+ csock.setblocking(False)
+ try:
+ csock.connect((addr, port))
+ except (BlockingIOError, InterruptedError):
+ pass
+ csock.setblocking(True)
+ ssock, _ = lsock.accept()
+ except:
+ csock.close()
+ raise
+ finally:
+ lsock.close()
+ return (ssock, csock)
+
+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 }