(family=AF_INET, type=SOCK_STREAM, proto=0)
| 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. |
| 613 | def _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 | |
| 667 | if hasattr(_socket, "socketpair"): |
| 668 | def socketpair(family=None, type=SOCK_STREAM, proto=0): |
nothing calls this directly
no test coverage detected
searching dependent graphs…