Skip to content

Commit 8fb9489

Browse files
graingertblurb-it[bot]asvetlov
authored
bpo-46827: pass sock.type to getaddrinfo in sock_connect (pythonGH-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]>
1 parent 38b5acf commit 8fb9489

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
@@ -498,7 +498,9 @@ async def sock_connect(self, sock, address):
498498

499499
if not hasattr(socket, 'AF_UNIX') or sock.family != socket.AF_UNIX:
500500
resolved = await self._ensure_resolved(
501-
address, family=sock.family, proto=sock.proto, loop=self)
501+
address, family=sock.family, type=sock.type, proto=sock.proto,
502+
loop=self,
503+
)
502504
_, _, _, _, address = resolved[0]
503505

504506
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
@@ -151,6 +151,24 @@ def test_write_to_self_exception(self):
151151
self.loop._csock.send.side_effect = RuntimeError()
152152
self.assertRaises(RuntimeError, self.loop._write_to_self)
153153

154+
@mock.patch('socket.getaddrinfo')
155+
def test_sock_connect_resolve_using_socket_params(self, m_gai):
156+
addr = ('need-resolution.com', 8080)
157+
for sock_type in [socket.SOCK_STREAM, socket.SOCK_DGRAM]:
158+
with self.subTest(sock_type):
159+
sock = test_utils.mock_nonblocking_socket(type=sock_type)
160+
161+
m_gai.side_effect = \
162+
lambda *args: [(None, None, None, None, ('127.0.0.1', 0))]
163+
164+
con = self.loop.create_task(self.loop.sock_connect(sock, addr))
165+
self.loop.run_until_complete(con)
166+
m_gai.assert_called_with(
167+
addr[0], addr[1], sock.family, sock.type, sock.proto, 0)
168+
169+
self.loop.run_until_complete(con)
170+
sock.connect.assert_called_with(('127.0.0.1', 0))
171+
154172
def test_add_reader(self):
155173
self.loop._selector.get_key.side_effect = KeyError
156174
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)