Issue 33678: selector_events.BaseSelectorEventLoop.sock_connect should preserve socket type (original) (raw)

The current behavior causes an exception to be raised when trying to create a datagram socket and _ipaddr_info returns None (since asyncio then calls loop.getaddrinfo with SOCK_STREAM and IPPROTO_UDP).

Preserving socket type is made difficult by https://bugs.python.org/issue21327; the proposed patch includes a dirty workaround. I do not know yet if it works on Windows.

--- selector_events.py 2018-05-11 10:01:42.011164837 +0800 +++ selector_events.py.new 2018-05-29 17:41:03.103155480 +0800 @@ -439,8 +439,9 @@ raise ValueError("the socket must be non-blocking")

     if not hasattr(socket, 'AF_UNIX') or sock.family != socket.AF_UNIX:

This error seems to come from base_events.py, lines 142-145, and since it's an OS specific limitation, I can't see Sebastien's suggestion as a feasible solution. Maybe we could come up with something along the lines of the following patch. What do you think about it?

diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py index 3726c556d4..15ee4d9d1b 100644 --- a/Lib/asyncio/base_events.py +++ b/Lib/asyncio/base_events.py @@ -142,6 +142,9 @@ def _ipaddr_info(host, port, family, type, proto): if '%' in host: # Linux's inet_pton doesn't accept an IPv6 zone index after host, # like '::1%lo0'.

diff --git a/Lib/test/test_asyncio/test_base_events.py b/Lib/test/test_asyncio/test_base_events.py index 6d544d1eda..1b944ff89e 100644 --- a/Lib/test/test_asyncio/test_base_events.py +++ b/Lib/test/test_asyncio/test_base_events.py @@ -109,6 +109,9 @@ class BaseEventTests(test_utils.TestCase): self.assertIsNone( base_events._ipaddr_info('::3', 1, INET, STREAM, TCP))