Skip to content

Commit 442634c

Browse files
authored
bpo-39148: enable ipv6 for datagrams in Proactor (GH-19121)
Ifdef is not necessary, as AF_INET6 is supported from Windows Vista, and other code in overlapped.c uses AF_INET6 and is not ifdef'd. Change the raised exception so users are not fooled to think it comes from Windows API. Automerge-Triggered-By: @njsmith
1 parent da742ba commit 442634c

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

Lib/test/test_asyncio/test_events.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,7 @@ def test_server_close(self):
12081208
ConnectionRefusedError, client.connect, ('127.0.0.1', port))
12091209
client.close()
12101210

1211-
def test_create_datagram_endpoint(self):
1211+
def _test_create_datagram_endpoint(self, local_addr, family):
12121212
class TestMyDatagramProto(MyDatagramProto):
12131213
def __init__(inner_self):
12141214
super().__init__(loop=self.loop)
@@ -1218,9 +1218,11 @@ def datagram_received(self, data, addr):
12181218
self.transport.sendto(b'resp:'+data, addr)
12191219

12201220
coro = self.loop.create_datagram_endpoint(
1221-
TestMyDatagramProto, local_addr=('127.0.0.1', 0))
1221+
TestMyDatagramProto, local_addr=local_addr, family=family)
12221222
s_transport, server = self.loop.run_until_complete(coro)
1223-
host, port = s_transport.get_extra_info('sockname')
1223+
sockname = s_transport.get_extra_info('sockname')
1224+
host, port = socket.getnameinfo(
1225+
sockname, socket.NI_NUMERICHOST|socket.NI_NUMERICSERV)
12241226

12251227
self.assertIsInstance(s_transport, asyncio.Transport)
12261228
self.assertIsInstance(server, TestMyDatagramProto)
@@ -1254,6 +1256,13 @@ def datagram_received(self, data, addr):
12541256
self.assertEqual('CLOSED', client.state)
12551257
server.transport.close()
12561258

1259+
def test_create_datagram_endpoint(self):
1260+
self._test_create_datagram_endpoint(('127.0.0.1', 0), socket.AF_INET)
1261+
1262+
@unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 not supported or enabled')
1263+
def test_create_datagram_endpoint_ipv6(self):
1264+
self._test_create_datagram_endpoint(('::1', 0), socket.AF_INET6)
1265+
12571266
def test_create_datagram_endpoint_sock(self):
12581267
sock = None
12591268
local_address = ('127.0.0.1', 0)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add IPv6 support to :mod:`asyncio` datagram endpoints in ProactorEventLoop.
2+
Change the raised exception for unknown address families to ValueError
3+
as it's not coming from Windows API.

Modules/overlapped.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,6 @@ make_ipv4_addr(const struct sockaddr_in *addr)
670670
return PyUnicode_FromString(buf);
671671
}
672672

673-
#ifdef ENABLE_IPV6
674673
/* Convert IPv6 sockaddr to a Python str. */
675674

676675
static PyObject *
@@ -683,7 +682,6 @@ make_ipv6_addr(const struct sockaddr_in6 *addr)
683682
}
684683
return PyUnicode_FromString(buf);
685684
}
686-
#endif
687685

688686
static PyObject*
689687
unparse_address(LPSOCKADDR Address, DWORD Length)
@@ -701,7 +699,6 @@ unparse_address(LPSOCKADDR Address, DWORD Length)
701699
}
702700
return ret;
703701
}
704-
#ifdef ENABLE_IPV6
705702
case AF_INET6: {
706703
const struct sockaddr_in6 *a = (const struct sockaddr_in6 *)Address;
707704
PyObject *addrobj = make_ipv6_addr(a);
@@ -716,9 +713,9 @@ unparse_address(LPSOCKADDR Address, DWORD Length)
716713
}
717714
return ret;
718715
}
719-
#endif /* ENABLE_IPV6 */
720716
default: {
721-
return SetFromWindowsErr(ERROR_INVALID_PARAMETER);
717+
PyErr_SetString(PyExc_ValueError, "recvfrom returned unsupported address family");
718+
return NULL;
722719
}
723720
}
724721
}

0 commit comments

Comments
 (0)