Skip to content

Commit 94d9c5e

Browse files
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 (cherry picked from commit 442634c) Co-authored-by: Kjell Braden <[email protected]>
1 parent c1f1ddf commit 94d9c5e

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
@@ -1204,7 +1204,7 @@ def test_server_close(self):
12041204
ConnectionRefusedError, client.connect, ('127.0.0.1', port))
12051205
client.close()
12061206

1207-
def test_create_datagram_endpoint(self):
1207+
def _test_create_datagram_endpoint(self, local_addr, family):
12081208
class TestMyDatagramProto(MyDatagramProto):
12091209
def __init__(inner_self):
12101210
super().__init__(loop=self.loop)
@@ -1214,9 +1214,11 @@ def datagram_received(self, data, addr):
12141214
self.transport.sendto(b'resp:'+data, addr)
12151215

12161216
coro = self.loop.create_datagram_endpoint(
1217-
TestMyDatagramProto, local_addr=('127.0.0.1', 0))
1217+
TestMyDatagramProto, local_addr=local_addr, family=family)
12181218
s_transport, server = self.loop.run_until_complete(coro)
1219-
host, port = s_transport.get_extra_info('sockname')
1219+
sockname = s_transport.get_extra_info('sockname')
1220+
host, port = socket.getnameinfo(
1221+
sockname, socket.NI_NUMERICHOST|socket.NI_NUMERICSERV)
12201222

12211223
self.assertIsInstance(s_transport, asyncio.Transport)
12221224
self.assertIsInstance(server, TestMyDatagramProto)
@@ -1250,6 +1252,13 @@ def datagram_received(self, data, addr):
12501252
self.assertEqual('CLOSED', client.state)
12511253
server.transport.close()
12521254

1255+
def test_create_datagram_endpoint(self):
1256+
self._test_create_datagram_endpoint(('127.0.0.1', 0), socket.AF_INET)
1257+
1258+
@unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 not supported or enabled')
1259+
def test_create_datagram_endpoint_ipv6(self):
1260+
self._test_create_datagram_endpoint(('::1', 0), socket.AF_INET6)
1261+
12531262
def test_create_datagram_endpoint_sock(self):
12541263
sock = None
12551264
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)