From 5ce076fd92306a0c41cf574221b930d54374bb47 Mon Sep 17 00:00:00 2001 From: Travis Howse Date: Mon, 5 Feb 2024 14:01:37 +1000 Subject: [PATCH] =?utf8?q?[3.11]=20gh-114887=20Reject=20only=20sockets=20o?= =?utf8?q?f=20type=20SOCK=5FSTREAM=20in=20create=5Fda=E2=80=A6=20(#114979)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Also improve exception message. (cherry picked from commit 94ec2b9c9ce898723c3fe61fbc64d6c8f4f68700) Co-authored-by: Donghee Na --- Lib/asyncio/base_events.py | 4 ++-- Lib/test/test_asyncio/test_base_events.py | 2 +- .../2024-02-03-04-07-18.gh-issue-114887.uLSFmN.rst | 2 ++ 3 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-02-03-04-07-18.gh-issue-114887.uLSFmN.rst diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py index c5dfc15729a7..5fa28cae9c31 100644 --- a/Lib/asyncio/base_events.py +++ b/Lib/asyncio/base_events.py @@ -1281,9 +1281,9 @@ class BaseEventLoop(events.AbstractEventLoop): 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 f3f83ad318c9..e1fc7e636323 100644 --- a/Lib/test/test_asyncio/test_base_events.py +++ b/Lib/test/test_asyncio/test_base_events.py @@ -1191,7 +1191,7 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase): 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 000000000000..b4d8cf4089d7 --- /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. -- 2.47.3