diff options
author | Irit Katriel <1055913+iritkatriel@users.noreply.github.com> | 2022-04-18 22:15:41 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-18 22:15:41 (GMT) |
commit | 39a54ba63850e081a4a5551a773df5b4d5b1d3cd (patch) | |
tree | d3d67a17c4f4a5c881f2288603dad435ac46829f /Lib/socket.py | |
parent | 0860b26a4f7abeb61aad9f4b96de8e78eed8c12a (diff) | |
download | cpython-39a54ba63850e081a4a5551a773df5b4d5b1d3cd.zip cpython-39a54ba63850e081a4a5551a773df5b4d5b1d3cd.tar.gz cpython-39a54ba63850e081a4a5551a773df5b4d5b1d3cd.tar.bz2 |
gh-74166: Add option to get all errors from socket.create_connection (GH-91586)
Diffstat (limited to 'Lib/socket.py')
-rwxr-xr-x | Lib/socket.py | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/Lib/socket.py b/Lib/socket.py index ef82c49..97362d9 100755 --- a/Lib/socket.py +++ b/Lib/socket.py @@ -806,7 +806,7 @@ def getfqdn(name=''): _GLOBAL_DEFAULT_TIMEOUT = object() def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT, - source_address=None): + source_address=None, all_errors=False): """Connect to *address* and return the socket object. Convenience function. Connect to *address* (a 2-tuple ``(host, @@ -816,11 +816,13 @@ def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT, global default timeout setting returned by :func:`getdefaulttimeout` is used. If *source_address* is set it must be a tuple of (host, port) for the socket to bind as a source address before making the connection. - A host of '' or port 0 tells the OS to use the default. + A host of '' or port 0 tells the OS to use the default. When a connection + cannot be created, raises the last error if *all_errors* is False, + and an ExceptionGroup of all errors if *all_errors* is True. """ host, port = address - err = None + exceptions = [] for res in getaddrinfo(host, port, 0, SOCK_STREAM): af, socktype, proto, canonname, sa = res sock = None @@ -832,20 +834,24 @@ def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT, sock.bind(source_address) sock.connect(sa) # Break explicitly a reference cycle - err = None + exceptions.clear() return sock - except error as _: - err = _ + except error as exc: + if not all_errors: + exceptions.clear() # raise only the last error + exceptions.append(exc) if sock is not None: sock.close() - if err is not None: + if len(exceptions): try: - raise err + if not all_errors: + raise exceptions[0] + raise ExceptionGroup("create_connection failed", exceptions) finally: # Break explicitly a reference cycle - err = None + exceptions = None else: raise error("getaddrinfo returns an empty list") |