From 154a5bd78a8359e28124c2cf6d4d2cfabf235de4 Mon Sep 17 00:00:00 2001 From: An Long Date: Sun, 25 Oct 2026 00:12:39 +0900 Subject: [PATCH] gh-160596: Fix error message in asyncio.proactor_events.sendto() (GH-160597) (cherry picked from commit 11598362a3ebbc8bfd1c4c37ec63ee565136868c) Co-authored-by: An Long --- Lib/asyncio/proactor_events.py | 4 ++-- Lib/test/test_asyncio/test_proactor_events.py | 4 +++- .../Library/2026-10-24-22-32-23.gh-issue-160596.RKll-Z.rst | 2 ++ 3 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-10-24-22-32-23.gh-issue-160596.RKll-Z.rst diff --git a/Lib/asyncio/proactor_events.py b/Lib/asyncio/proactor_events.py index 0128a739b07c614..da1af53b1224f4c 100644 --- a/Lib/asyncio/proactor_events.py +++ b/Lib/asyncio/proactor_events.py @@ -489,8 +489,8 @@ def _force_close(self, exc): def sendto(self, data, addr=None): if not isinstance(data, (bytes, bytearray, memoryview)): - raise TypeError('data argument must be bytes-like object (%r)', - type(data)) + raise TypeError(f'data argument must be a bytes-like object, ' + f'not {type(data).__name__!r}') if self._address is not None and addr not in (None, self._address): raise ValueError( diff --git a/Lib/test/test_asyncio/test_proactor_events.py b/Lib/test/test_asyncio/test_proactor_events.py index edfad5e11db35e4..2e9e0828f405628 100644 --- a/Lib/test/test_asyncio/test_proactor_events.py +++ b/Lib/test/test_asyncio/test_proactor_events.py @@ -691,7 +691,9 @@ def test_sendto_error_received_connected(self): def test_sendto_str(self): transport = self.datagram_transport() - self.assertRaises(TypeError, transport.sendto, 'str', ()) + self.assertRaisesRegex( + TypeError, "^data argument must be a bytes-like object, not 'str'$", + transport.sendto, 'str', ()) def test_sendto_connected_addr(self): transport = self.datagram_transport(address=('0.0.0.0', 1)) diff --git a/Misc/NEWS.d/next/Library/2026-10-24-22-32-23.gh-issue-160596.RKll-Z.rst b/Misc/NEWS.d/next/Library/2026-10-24-22-32-23.gh-issue-160596.RKll-Z.rst new file mode 100644 index 000000000000000..c1db616f01d17a4 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-24-22-32-23.gh-issue-160596.RKll-Z.rst @@ -0,0 +1,2 @@ +Fix the error message for invalid data passed to +:meth:`!asyncio.proactor_events.sendto`.