From 9e19f406f8483fa7f7cfce23c9d756b61bb4e4bf Mon Sep 17 00:00:00 2001 From: Aperence Date: Tue, 20 Aug 2024 14:01:53 +0200 Subject: [PATCH 1/3] Add proto to sock_connect Allow the selection of the protocol used by getaddrinfo by adding an additional, optional parameter proto_addr_info. If not specified, use the protocol used by the socket (= previous behaviour) This change is needed to allow custom protocols to be used with the sock_connect, because these protocols may raise an error when calling getaddrinfo (result in a "ai_proto not supported" error) An example of such protocol is MPTCP (https://mptcp.dev) --- Lib/asyncio/selector_events.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Lib/asyncio/selector_events.py b/Lib/asyncio/selector_events.py index f94bf10b4225e7..ce7f1398b67a22 100644 --- a/Lib/asyncio/selector_events.py +++ b/Lib/asyncio/selector_events.py @@ -618,10 +618,14 @@ def _sock_sendto(self, fut, sock, data, address): else: fut.set_result(n) - async def sock_connect(self, sock, address): + async def sock_connect(self, sock, address, proto_addr_info = None): """Connect to a remote socket at address. This method is a coroutine. + + The proto_addr info parameter allows to specify a protocol to use for the + resolution of the address. If not specified, default to the protocol used + by sock """ base_events._check_ssl_socket(sock) if self._debug and sock.gettimeout() != 0: @@ -629,8 +633,9 @@ async def sock_connect(self, sock, address): if sock.family == socket.AF_INET or ( base_events._HAS_IPv6 and sock.family == socket.AF_INET6): + proto = proto_addr_info if proto_addr_info is not None else sock.proto resolved = await self._ensure_resolved( - address, family=sock.family, type=sock.type, proto=sock.proto, + address, family=sock.family, type=sock.type, proto=proto, loop=self, ) _, _, _, _, address = resolved[0] From 94f7d6d32e7fa6594927a0df9a29ad9cc56e1fbd Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 20 Aug 2024 12:52:00 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2024-08-20-12-51-58.gh-issue-123174.nXCVYU.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-08-20-12-51-58.gh-issue-123174.nXCVYU.rst diff --git a/Misc/NEWS.d/next/Library/2024-08-20-12-51-58.gh-issue-123174.nXCVYU.rst b/Misc/NEWS.d/next/Library/2024-08-20-12-51-58.gh-issue-123174.nXCVYU.rst new file mode 100644 index 00000000000000..91ea137f78893c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-08-20-12-51-58.gh-issue-123174.nXCVYU.rst @@ -0,0 +1,2 @@ +Allow the selection of the protocol used by getaddrinfo by adding an additional, optional parameter proto_addr_info +If not specified, use the protocol used by the socket (= previous behaviour) From d528035e22debc8bd5a088facd8aeb9c43ba2e35 Mon Sep 17 00:00:00 2001 From: Anthony Doeraene <78789735+Aperence@users.noreply.github.com> Date: Tue, 20 Aug 2024 15:32:30 +0200 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Peter Bierma --- Lib/asyncio/selector_events.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Lib/asyncio/selector_events.py b/Lib/asyncio/selector_events.py index ce7f1398b67a22..b2866d1e6aeb75 100644 --- a/Lib/asyncio/selector_events.py +++ b/Lib/asyncio/selector_events.py @@ -618,14 +618,10 @@ def _sock_sendto(self, fut, sock, data, address): else: fut.set_result(n) - async def sock_connect(self, sock, address, proto_addr_info = None): + async def sock_connect(self, sock, address, proto_addr_info=None): """Connect to a remote socket at address. This method is a coroutine. - - The proto_addr info parameter allows to specify a protocol to use for the - resolution of the address. If not specified, default to the protocol used - by sock """ base_events._check_ssl_socket(sock) if self._debug and sock.gettimeout() != 0: