Skip to content

Commit 29e8c43

Browse files
bpo-46827: pass sock.type to getaddrinfo in sock_connect (GH-31499)
Co-authored-by: Thomas Grainger <[email protected]> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Andrew Svetlov <[email protected]> (cherry picked from commit 8fb9489) Co-authored-by: Thomas Grainger <[email protected]>
1 parent 2387aea commit 29e8c43

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Lib/asyncio/selector_events.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,9 @@ async def sock_connect(self, sock, address):
489489

490490
if not hasattr(socket, 'AF_UNIX') or sock.family != socket.AF_UNIX:
491491
resolved = await self._ensure_resolved(
492-
address, family=sock.family, proto=sock.proto, loop=self)
492+
address, family=sock.family, type=sock.type, proto=sock.proto,
493+
loop=self,
494+
)
493495
_, _, _, _, address = resolved[0]
494496

495497
fut = self.create_future()

Lib/test/test_asyncio/test_selector_events.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,24 @@ def test_write_to_self_exception(self):
189189
self.loop._csock.send.side_effect = RuntimeError()
190190
self.assertRaises(RuntimeError, self.loop._write_to_self)
191191

192+
@mock.patch('socket.getaddrinfo')
193+
def test_sock_connect_resolve_using_socket_params(self, m_gai):
194+
addr = ('need-resolution.com', 8080)
195+
for sock_type in [socket.SOCK_STREAM, socket.SOCK_DGRAM]:
196+
with self.subTest(sock_type):
197+
sock = test_utils.mock_nonblocking_socket(type=sock_type)
198+
199+
m_gai.side_effect = \
200+
lambda *args: [(None, None, None, None, ('127.0.0.1', 0))]
201+
202+
con = self.loop.create_task(self.loop.sock_connect(sock, addr))
203+
self.loop.run_until_complete(con)
204+
m_gai.assert_called_with(
205+
addr[0], addr[1], sock.family, sock.type, sock.proto, 0)
206+
207+
self.loop.run_until_complete(con)
208+
sock.connect.assert_called_with(('127.0.0.1', 0))
209+
192210
def test_add_reader(self):
193211
self.loop._selector.get_key.side_effect = KeyError
194212
cb = lambda: True
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Support UDP sockets in :meth:`asyncio.loop.sock_connect` for selector-based event loops. Patch by Thomas Grainger.

0 commit comments

Comments
 (0)