Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 98c745a

Browse files
committed
Issue #18643: Add socket.socketpair() on Windows.
1 parent 987f3dd commit 98c745a

4 files changed

Lines changed: 56 additions & 3 deletions

File tree

Doc/library/socket.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,6 @@ The following functions all create :ref:`socket objects <socket-objects>`.
350350
type, and protocol number. Address family, socket type, and protocol number are
351351
as for the :func:`.socket` function above. The default family is :const:`AF_UNIX`
352352
if defined on the platform; otherwise, the default is :const:`AF_INET`.
353-
Availability: Unix.
354353

355354
The newly created sockets are :ref:`non-inheritable <fd_inheritance>`.
356355

@@ -361,6 +360,9 @@ The following functions all create :ref:`socket objects <socket-objects>`.
361360
.. versionchanged:: 3.4
362361
The returned sockets are now non-inheritable.
363362

363+
.. versionchanged:: 3.5
364+
Windows support added.
365+
364366

365367
.. function:: create_connection(address[, timeout[, source_address]])
366368

Lib/socket.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@
7676
if name.isupper() and name.startswith('SOCK_')})
7777
globals().update(SocketType.__members__)
7878

79+
80+
_LOCALHOST = '127.0.0.1'
81+
_LOCALHOST_V6 = '::1'
82+
83+
7984
def _intenum_converter(value, enum_klass):
8085
"""Convert a numeric family value to an IntEnum member.
8186
@@ -468,6 +473,52 @@ def socketpair(family=None, type=SOCK_STREAM, proto=0):
468473
b = socket(family, type, proto, b.detach())
469474
return a, b
470475

476+
else:
477+
478+
# Origin: https://gist.github.com/4325783, by Geert Jansen. Public domain.
479+
def socketpair(family=AF_INET, type=SOCK_STREAM, proto=0):
480+
if family == AF_INET:
481+
host = _LOCALHOST
482+
elif family == AF_INET6:
483+
host = _LOCALHOST_V6
484+
else:
485+
raise ValueError("Only AF_INET and AF_INET6 socket address families "
486+
"are supported")
487+
if type != SOCK_STREAM:
488+
raise ValueError("Only SOCK_STREAM socket type is supported")
489+
if proto != 0:
490+
raise ValueError("Only protocol zero is supported")
491+
492+
# We create a connected TCP socket. Note the trick with
493+
# setblocking(False) that prevents us from having to create a thread.
494+
lsock = socket(family, type, proto)
495+
try:
496+
lsock.bind((host, 0))
497+
lsock.listen()
498+
# On IPv6, ignore flow_info and scope_id
499+
addr, port = lsock.getsockname()[:2]
500+
csock = socket(family, type, proto)
501+
try:
502+
csock.setblocking(False)
503+
try:
504+
csock.connect((addr, port))
505+
except (BlockingIOError, InterruptedError):
506+
pass
507+
csock.setblocking(True)
508+
ssock, _ = lsock.accept()
509+
except:
510+
csock.close()
511+
raise
512+
finally:
513+
lsock.close()
514+
return (ssock, csock)
515+
516+
socketpair.__doc__ = """socketpair([family[, type[, proto]]]) -> (socket object, socket object)
517+
Create a pair of socket objects from the sockets returned by the platform
518+
socketpair() function.
519+
The arguments are the same as for socket() except the default family is AF_UNIX
520+
if defined on the platform; otherwise, the default is AF_INET.
521+
"""
471522

472523
_blocking_errnos = { EAGAIN, EWOULDBLOCK }
473524

Lib/test/test_socket.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3728,8 +3728,6 @@ def _testClose(self):
37283728
self.cli.connect((HOST, self.port))
37293729
time.sleep(1.0)
37303730

3731-
@unittest.skipUnless(hasattr(socket, 'socketpair'),
3732-
'test needs socket.socketpair()')
37333731
@unittest.skipUnless(thread, 'Threading required for this test.')
37343732
class BasicSocketPairTest(SocketPairTest):
37353733

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ Core and Builtins
177177
Library
178178
-------
179179

180+
- Issue #18643: Add socket.socketpair() on Windows.
181+
180182
- Issue #22435: Fix a file descriptor leak when SocketServer bind fails.
181183

182184
- Issue #13096: Fixed segfault in CTypes POINTER handling of large

0 commit comments

Comments
 (0)