Skip to content

bpo-46827: pass sock.type to getaddrinfo in sock_connect #31499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Lib/asyncio/selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,9 @@ async def sock_connect(self, sock, address):

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()
Expand Down
18 changes: 18 additions & 0 deletions Lib/test/test_asyncio/test_selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,24 @@ def test_write_to_self_exception(self):
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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support UDP sockets in :meth:`asyncio.loop.sock_connect` for selector-based event loops. Patch by Thomas Grainger.