Skip to content

Commit d327517

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 a7af34d commit d327517

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
@@ -188,6 +188,24 @@ def test_write_to_self_exception(self):
188188
self.loop._csock.send.side_effect = RuntimeError()
189189
self.assertRaises(RuntimeError, self.loop._write_to_self)
190190

191+
@mock.patch('socket.getaddrinfo')
192+
def test_sock_connect_resolve_using_socket_params(self, m_gai):
193+
addr = ('need-resolution.com', 8080)
194+
for sock_type in [socket.SOCK_STREAM, socket.SOCK_DGRAM]:
195+
with self.subTest(sock_type):
196+
sock = test_utils.mock_nonblocking_socket(type=sock_type)
197+
198+
m_gai.side_effect = \
199+
lambda *args: [(None, None, None, None, ('127.0.0.1', 0))]
200+
201+
con = self.loop.create_task(self.loop.sock_connect(sock, addr))
202+
self.loop.run_until_complete(con)
203+
m_gai.assert_called_with(
204+
addr[0], addr[1], sock.family, sock.type, sock.proto, 0)
205+
206+
self.loop.run_until_complete(con)
207+
sock.connect.assert_called_with(('127.0.0.1', 0))
208+
191209
def test_add_reader(self):
192210
self.loop._selector.get_key.side_effect = KeyError
193211
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)