MCPcopy Index your code
hub / github.com/python/cpython / _fallback_socketpair

Function _fallback_socketpair

Lib/socket.py:613–665  ·  view source on GitHub ↗
(family=AF_INET, type=SOCK_STREAM, proto=0)

Source from the content-addressed store, hash-verified

611# This is used if _socket doesn't natively provide socketpair. It's
612# always defined so that it can be patched in for testing purposes.
613def _fallback_socketpair(family=AF_INET, type=SOCK_STREAM, proto=0):
614 if family == AF_INET:
615 host = _LOCALHOST
616 elif family == AF_INET6:
617 host = _LOCALHOST_V6
618 else:
619 raise ValueError("Only AF_INET and AF_INET6 socket address families "
620 "are supported")
621 if type != SOCK_STREAM:
622 raise ValueError("Only SOCK_STREAM socket type is supported")
623 if proto != 0:
624 raise ValueError("Only protocol zero is supported")
625
626 # We create a connected TCP socket. Note the trick with
627 # setblocking(False) that prevents us from having to create a thread.
628 lsock = socket(family, type, proto)
629 try:
630 lsock.bind((host, 0))
631 lsock.listen()
632 # On IPv6, ignore flow_info and scope_id
633 addr, port = lsock.getsockname()[:2]
634 csock = socket(family, type, proto)
635 try:
636 csock.setblocking(False)
637 try:
638 csock.connect((addr, port))
639 except (BlockingIOError, InterruptedError):
640 pass
641 csock.setblocking(True)
642 ssock, _ = lsock.accept()
643 except:
644 csock.close()
645 raise
646 finally:
647 lsock.close()
648
649 # Authenticating avoids using a connection from something else
650 # able to connect to {host}:{port} instead of us.
651 # We expect only AF_INET and AF_INET6 families.
652 try:
653 if (
654 ssock.getsockname() != csock.getpeername()
655 or csock.getsockname() != ssock.getpeername()
656 ):
657 raise ConnectionError("Unexpected peer connection")
658 except:
659 # getsockname() and getpeername() can fail
660 # if either socket isn't connected.
661 ssock.close()
662 csock.close()
663 raise
664
665 return (ssock, csock)
666
667if hasattr(_socket, "socketpair"):
668 def socketpair(family=None, type=SOCK_STREAM, proto=0):

Callers

nothing calls this directly

Calls 9

socketClass · 0.70
bindMethod · 0.45
listenMethod · 0.45
getsocknameMethod · 0.45
setblockingMethod · 0.45
connectMethod · 0.45
acceptMethod · 0.45
closeMethod · 0.45
getpeernameMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…