MCPcopy Index your code
hub / github.com/RustPython/RustPython / _fallback_socketpair

Function _fallback_socketpair

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

Source from the content-addressed store, hash-verified

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