mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
gh-93973: Add all_errors to asyncio.create_connection (#93974)
Co-authored-by: Oleg Iarygin <dralife@yandex.ru>
This commit is contained in:
parent
ac18665472
commit
a0ad63e70e
4 changed files with 54 additions and 2 deletions
|
@ -377,7 +377,8 @@ Opening network connections
|
||||||
local_addr=None, server_hostname=None, \
|
local_addr=None, server_hostname=None, \
|
||||||
ssl_handshake_timeout=None, \
|
ssl_handshake_timeout=None, \
|
||||||
ssl_shutdown_timeout=None, \
|
ssl_shutdown_timeout=None, \
|
||||||
happy_eyeballs_delay=None, interleave=None)
|
happy_eyeballs_delay=None, interleave=None, \
|
||||||
|
all_errors=False)
|
||||||
|
|
||||||
Open a streaming transport connection to a given
|
Open a streaming transport connection to a given
|
||||||
address specified by *host* and *port*.
|
address specified by *host* and *port*.
|
||||||
|
@ -468,6 +469,14 @@ Opening network connections
|
||||||
to complete before aborting the connection. ``30.0`` seconds if ``None``
|
to complete before aborting the connection. ``30.0`` seconds if ``None``
|
||||||
(default).
|
(default).
|
||||||
|
|
||||||
|
* *all_errors* determines what exceptions are raised when a connection cannot
|
||||||
|
be created. By default, only a single ``Exception`` is raised: the first
|
||||||
|
exception if there is only one or all errors have same message, or a single
|
||||||
|
``OSError`` with the error messages combined. When ``all_errors`` is ``True``,
|
||||||
|
an ``ExceptionGroup`` will be raised containing all exceptions (even if there
|
||||||
|
is only one).
|
||||||
|
|
||||||
|
|
||||||
.. versionchanged:: 3.5
|
.. versionchanged:: 3.5
|
||||||
|
|
||||||
Added support for SSL/TLS in :class:`ProactorEventLoop`.
|
Added support for SSL/TLS in :class:`ProactorEventLoop`.
|
||||||
|
@ -500,6 +509,9 @@ Opening network connections
|
||||||
|
|
||||||
Added the *ssl_shutdown_timeout* parameter.
|
Added the *ssl_shutdown_timeout* parameter.
|
||||||
|
|
||||||
|
.. versionchanged:: 3.12
|
||||||
|
*all_errors* was added.
|
||||||
|
|
||||||
.. seealso::
|
.. seealso::
|
||||||
|
|
||||||
The :func:`open_connection` function is a high-level alternative
|
The :func:`open_connection` function is a high-level alternative
|
||||||
|
|
|
@ -980,7 +980,8 @@ class BaseEventLoop(events.AbstractEventLoop):
|
||||||
local_addr=None, server_hostname=None,
|
local_addr=None, server_hostname=None,
|
||||||
ssl_handshake_timeout=None,
|
ssl_handshake_timeout=None,
|
||||||
ssl_shutdown_timeout=None,
|
ssl_shutdown_timeout=None,
|
||||||
happy_eyeballs_delay=None, interleave=None):
|
happy_eyeballs_delay=None, interleave=None,
|
||||||
|
all_errors=False):
|
||||||
"""Connect to a TCP server.
|
"""Connect to a TCP server.
|
||||||
|
|
||||||
Create a streaming transport connection to a given internet host and
|
Create a streaming transport connection to a given internet host and
|
||||||
|
@ -1069,6 +1070,8 @@ class BaseEventLoop(events.AbstractEventLoop):
|
||||||
|
|
||||||
if sock is None:
|
if sock is None:
|
||||||
exceptions = [exc for sub in exceptions for exc in sub]
|
exceptions = [exc for sub in exceptions for exc in sub]
|
||||||
|
if all_errors:
|
||||||
|
raise ExceptionGroup("create_connection failed", exceptions)
|
||||||
if len(exceptions) == 1:
|
if len(exceptions) == 1:
|
||||||
raise exceptions[0]
|
raise exceptions[0]
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -1109,6 +1109,15 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase):
|
||||||
|
|
||||||
self.assertEqual(str(cm.exception), 'Multiple exceptions: err1, err2')
|
self.assertEqual(str(cm.exception), 'Multiple exceptions: err1, err2')
|
||||||
|
|
||||||
|
idx = -1
|
||||||
|
coro = self.loop.create_connection(MyProto, 'example.com', 80, all_errors=True)
|
||||||
|
with self.assertRaises(ExceptionGroup) as cm:
|
||||||
|
self.loop.run_until_complete(coro)
|
||||||
|
|
||||||
|
self.assertIsInstance(cm.exception, ExceptionGroup)
|
||||||
|
for e in cm.exception.exceptions:
|
||||||
|
self.assertIsInstance(e, OSError)
|
||||||
|
|
||||||
@patch_socket
|
@patch_socket
|
||||||
def test_create_connection_timeout(self, m_socket):
|
def test_create_connection_timeout(self, m_socket):
|
||||||
# Ensure that the socket is closed on timeout
|
# Ensure that the socket is closed on timeout
|
||||||
|
@ -1228,6 +1237,14 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase):
|
||||||
self.assertRaises(
|
self.assertRaises(
|
||||||
OSError, self.loop.run_until_complete, coro)
|
OSError, self.loop.run_until_complete, coro)
|
||||||
|
|
||||||
|
coro = self.loop.create_connection(MyProto, 'example.com', 80, all_errors=True)
|
||||||
|
with self.assertRaises(ExceptionGroup) as cm:
|
||||||
|
self.loop.run_until_complete(coro)
|
||||||
|
|
||||||
|
self.assertIsInstance(cm.exception, ExceptionGroup)
|
||||||
|
self.assertEqual(len(cm.exception.exceptions), 1)
|
||||||
|
self.assertIsInstance(cm.exception.exceptions[0], OSError)
|
||||||
|
|
||||||
def test_create_connection_multiple(self):
|
def test_create_connection_multiple(self):
|
||||||
async def getaddrinfo(*args, **kw):
|
async def getaddrinfo(*args, **kw):
|
||||||
return [(2, 1, 6, '', ('0.0.0.1', 80)),
|
return [(2, 1, 6, '', ('0.0.0.1', 80)),
|
||||||
|
@ -1245,6 +1262,15 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase):
|
||||||
with self.assertRaises(OSError):
|
with self.assertRaises(OSError):
|
||||||
self.loop.run_until_complete(coro)
|
self.loop.run_until_complete(coro)
|
||||||
|
|
||||||
|
coro = self.loop.create_connection(
|
||||||
|
MyProto, 'example.com', 80, family=socket.AF_INET, all_errors=True)
|
||||||
|
with self.assertRaises(ExceptionGroup) as cm:
|
||||||
|
self.loop.run_until_complete(coro)
|
||||||
|
|
||||||
|
self.assertIsInstance(cm.exception, ExceptionGroup)
|
||||||
|
for e in cm.exception.exceptions:
|
||||||
|
self.assertIsInstance(e, OSError)
|
||||||
|
|
||||||
@patch_socket
|
@patch_socket
|
||||||
def test_create_connection_multiple_errors_local_addr(self, m_socket):
|
def test_create_connection_multiple_errors_local_addr(self, m_socket):
|
||||||
|
|
||||||
|
@ -1276,6 +1302,16 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase):
|
||||||
self.assertTrue(str(cm.exception).startswith('Multiple exceptions: '))
|
self.assertTrue(str(cm.exception).startswith('Multiple exceptions: '))
|
||||||
self.assertTrue(m_socket.socket.return_value.close.called)
|
self.assertTrue(m_socket.socket.return_value.close.called)
|
||||||
|
|
||||||
|
coro = self.loop.create_connection(
|
||||||
|
MyProto, 'example.com', 80, family=socket.AF_INET,
|
||||||
|
local_addr=(None, 8080), all_errors=True)
|
||||||
|
with self.assertRaises(ExceptionGroup) as cm:
|
||||||
|
self.loop.run_until_complete(coro)
|
||||||
|
|
||||||
|
self.assertIsInstance(cm.exception, ExceptionGroup)
|
||||||
|
for e in cm.exception.exceptions:
|
||||||
|
self.assertIsInstance(e, OSError)
|
||||||
|
|
||||||
def _test_create_connection_ip_addr(self, m_socket, allow_inet_pton):
|
def _test_create_connection_ip_addr(self, m_socket, allow_inet_pton):
|
||||||
# Test the fallback code, even if this system has inet_pton.
|
# Test the fallback code, even if this system has inet_pton.
|
||||||
if not allow_inet_pton:
|
if not allow_inet_pton:
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Add keyword argument ``all_errors`` to ``asyncio.create_connection`` so that multiple connection errors can be raised as an ``ExceptionGroup``.
|
Loading…
Add table
Add a link
Reference in a new issue