Skip to content

Commit 94ec2b9

Browse files
tjhowsecorona10
andauthored
gh-114887 Reject only sockets of type SOCK_STREAM in create_datagram_endpoint() (#114893)
Also improve exception message. Co-authored-by: Donghee Na <[email protected]>
1 parent 6b53d5f commit 94ec2b9

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
@@ -1340,9 +1340,9 @@ async def create_datagram_endpoint(self, protocol_factory,
13401340
allow_broadcast=None, sock=None):
13411341
"""Create datagram connection."""
13421342
if sock is not None:
1343-
if sock.type != socket.SOCK_DGRAM:
1343+
if sock.type == socket.SOCK_STREAM:
13441344
raise ValueError(
1345-
f'A UDP Socket was expected, got {sock!r}')
1345+
f'A datagram socket was expected, got {sock!r}')
13461346
if (local_addr or remote_addr or
13471347
family or proto or flags or
13481348
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
@@ -1232,7 +1232,7 @@ def test_create_datagram_endpoint_wrong_sock(self):
12321232
with sock:
12331233
coro = self.loop.create_datagram_endpoint(MyProto, sock=sock)
12341234
with self.assertRaisesRegex(ValueError,
1235-
'A UDP Socket was expected'):
1235+
'A datagram socket was expected'):
12361236
self.loop.run_until_complete(coro)
12371237

12381238
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)