Skip to content

Commit 5ce076f

Browse files
tjhowsecorona10
andauthored
[3.11] gh-114887 Reject only sockets of type SOCK_STREAM in create_da… (#114979)
Also improve exception message. (cherry picked from commit 94ec2b9) Co-authored-by: Donghee Na <[email protected]>
1 parent 510eb4e commit 5ce076f

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

Lib/asyncio/base_events.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,9 +1281,9 @@ async def create_datagram_endpoint(self, protocol_factory,
12811281
allow_broadcast=None, sock=None):
12821282
"""Create datagram connection."""
12831283
if sock is not None:
1284-
if sock.type != socket.SOCK_DGRAM:
1284+
if sock.type == socket.SOCK_STREAM:
12851285
raise ValueError(
1286-
f'A UDP Socket was expected, got {sock!r}')
1286+
f'A datagram socket was expected, got {sock!r}')
12871287
if (local_addr or remote_addr or
12881288
family or proto or flags or
12891289
reuse_port or allow_broadcast):

Lib/test/test_asyncio/test_base_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,7 @@ def test_create_datagram_endpoint_wrong_sock(self):
11911191
with sock:
11921192
coro = self.loop.create_datagram_endpoint(MyProto, sock=sock)
11931193
with self.assertRaisesRegex(ValueError,
1194-
'A UDP Socket was expected'):
1194+
'A datagram socket was expected'):
11951195
self.loop.run_until_complete(coro)
11961196

11971197
def test_create_connection_no_host_port_sock(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Changed socket type validation in :meth:`~asyncio.loop.create_datagram_endpoint` to accept all non-stream sockets.
2+
This fixes a regression in compatibility with raw sockets.

0 commit comments

Comments
 (0)