]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46827: pass sock.type to getaddrinfo in sock_connect (GH-31499)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 22 Feb 2022 21:10:04 +0000 (13:10 -0800)
committerGitHub <noreply@github.com>
Tue, 22 Feb 2022 21:10:04 +0000 (13:10 -0800)
Co-authored-by: Thomas Grainger <tagrain@gmail.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
(cherry picked from commit 8fb94893e4a870ed3533e80c4bc2f1ebf1cfa9e7)

Co-authored-by: Thomas Grainger <tagrain@gmail.com>
Lib/asyncio/selector_events.py
Lib/test/test_asyncio/test_selector_events.py
Misc/NEWS.d/next/Library/2022-02-22-15-08-30.bpo-46827.hvj38S.rst [new file with mode: 0644]

index 6f764450ae3bebbe476a708565c2f25c76c02496..71080b8ad16c619f678c7f49292c116350f258d6 100644 (file)
@@ -489,7 +489,9 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
 
         if not hasattr(socket, 'AF_UNIX') or sock.family != socket.AF_UNIX:
             resolved = await self._ensure_resolved(
-                address, family=sock.family, proto=sock.proto, loop=self)
+                address, family=sock.family, type=sock.type, proto=sock.proto,
+                loop=self,
+            )
             _, _, _, _, address = resolved[0]
 
         fut = self.create_future()
index b684fab2771f20d6edbaaaabfb8c6d7f077092df..dfd954f807c76ccc97f3fbe18b0e01dcd0dfafa5 100644 (file)
@@ -189,6 +189,24 @@ class BaseSelectorEventLoopTests(test_utils.TestCase):
         self.loop._csock.send.side_effect = RuntimeError()
         self.assertRaises(RuntimeError, self.loop._write_to_self)
 
+    @mock.patch('socket.getaddrinfo')
+    def test_sock_connect_resolve_using_socket_params(self, m_gai):
+        addr = ('need-resolution.com', 8080)
+        for sock_type in [socket.SOCK_STREAM, socket.SOCK_DGRAM]:
+            with self.subTest(sock_type):
+                sock = test_utils.mock_nonblocking_socket(type=sock_type)
+
+                m_gai.side_effect = \
+                    lambda *args: [(None, None, None, None, ('127.0.0.1', 0))]
+
+                con = self.loop.create_task(self.loop.sock_connect(sock, addr))
+                self.loop.run_until_complete(con)
+                m_gai.assert_called_with(
+                    addr[0], addr[1], sock.family, sock.type, sock.proto, 0)
+
+                self.loop.run_until_complete(con)
+                sock.connect.assert_called_with(('127.0.0.1', 0))
+
     def test_add_reader(self):
         self.loop._selector.get_key.side_effect = KeyError
         cb = lambda: True
diff --git a/Misc/NEWS.d/next/Library/2022-02-22-15-08-30.bpo-46827.hvj38S.rst b/Misc/NEWS.d/next/Library/2022-02-22-15-08-30.bpo-46827.hvj38S.rst
new file mode 100644 (file)
index 0000000..259686a
--- /dev/null
@@ -0,0 +1 @@
+Support UDP sockets in  :meth:`asyncio.loop.sock_connect` for selector-based event loops.  Patch by Thomas Grainger.