https://github.com/python/cpython/commit/94ec2b9c9ce898723c3fe61fbc64d6c8f4f68700
commit: 94ec2b9c9ce898723c3fe61fbc64d6c8f4f68700
branch: main
author: Travis Howse <[email protected]>
committer: gvanrossum <[email protected]>
date: 2024-02-03T17:14:02Z
summary:

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]>

files:
A Misc/NEWS.d/next/Core and 
Builtins/2024-02-03-04-07-18.gh-issue-114887.uLSFmN.rst
M Lib/asyncio/base_events.py
M Lib/test/test_asyncio/test_base_events.py

diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py
index c60d7688ef8c77..aadc4f478f8b56 100644
--- a/Lib/asyncio/base_events.py
+++ b/Lib/asyncio/base_events.py
@@ -1340,9 +1340,9 @@ async def create_datagram_endpoint(self, protocol_factory,
                                        allow_broadcast=None, sock=None):
         """Create datagram connection."""
         if sock is not None:
-            if sock.type != socket.SOCK_DGRAM:
+            if sock.type == socket.SOCK_STREAM:
                 raise ValueError(
-                    f'A UDP Socket was expected, got {sock!r}')
+                    f'A datagram socket was expected, got {sock!r}')
             if (local_addr or remote_addr or
                     family or proto or flags or
                     reuse_port or allow_broadcast):
diff --git a/Lib/test/test_asyncio/test_base_events.py 
b/Lib/test/test_asyncio/test_base_events.py
index c2080977e9d587..82071edb252570 100644
--- a/Lib/test/test_asyncio/test_base_events.py
+++ b/Lib/test/test_asyncio/test_base_events.py
@@ -1232,7 +1232,7 @@ def test_create_datagram_endpoint_wrong_sock(self):
         with sock:
             coro = self.loop.create_datagram_endpoint(MyProto, sock=sock)
             with self.assertRaisesRegex(ValueError,
-                                        'A UDP Socket was expected'):
+                                        'A datagram socket was expected'):
                 self.loop.run_until_complete(coro)
 
     def test_create_connection_no_host_port_sock(self):
diff --git a/Misc/NEWS.d/next/Core and 
Builtins/2024-02-03-04-07-18.gh-issue-114887.uLSFmN.rst b/Misc/NEWS.d/next/Core 
and Builtins/2024-02-03-04-07-18.gh-issue-114887.uLSFmN.rst
new file mode 100644
index 00000000000000..b4d8cf4089d723
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and 
Builtins/2024-02-03-04-07-18.gh-issue-114887.uLSFmN.rst 
@@ -0,0 +1,2 @@
+Changed socket type validation in 
:meth:`~asyncio.loop.create_datagram_endpoint` to accept all non-stream sockets.
+This fixes a regression in compatibility with raw sockets.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to