Skip to content

Commit 3e0f2c2

Browse files
committed
bpo-39148: enable ipv6 for datagrams in Proactor
Ifdef is not necessary, as AF_INET6 is supported from Windows Vista. Change the raised exception so users are not fooled to think it comes from Windows API.
1 parent 2510494 commit 3e0f2c2

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
@@ -1207,7 +1207,7 @@ def test_server_close(self):
12071207
ConnectionRefusedError, client.connect, ('127.0.0.1', port))
12081208
client.close()
12091209

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

12191219
coro = self.loop.create_datagram_endpoint(
1220-
TestMyDatagramProto, local_addr=('127.0.0.1', 0))
1220+
TestMyDatagramProto, local_addr=local_addr, family=family)
12211221
s_transport, server = self.loop.run_until_complete(coro)
1222-
host, port = s_transport.get_extra_info('sockname')
1222+
sockname = s_transport.get_extra_info('sockname')
1223+
host, port = socket.getnameinfo(
1224+
sockname, socket.NI_NUMERICHOST|socket.NI_NUMERICSERV)
12231225

12241226
self.assertIsInstance(s_transport, asyncio.Transport)
12251227
self.assertIsInstance(server, TestMyDatagramProto)
@@ -1253,6 +1255,13 @@ def datagram_received(self, data, addr):
12531255
self.assertEqual('CLOSED', client.state)
12541256
server.transport.close()
12551257

1258+
def test_create_datagram_endpoint(self):
1259+
self._test_create_datagram_endpoint(('127.0.0.1', 0), socket.AF_INET)
1260+
1261+
@unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 not supported or enabled')
1262+
def test_create_datagram_endpoint_ipv6(self):
1263+
self._test_create_datagram_endpoint(('::1', 0), socket.AF_INET6)
1264+
12561265
def test_create_datagram_endpoint_sock(self):
12571266
sock = None
12581267
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)