From a385d204d0ec720f044cbfbbad0e4e7360f93392 Mon Sep 17 00:00:00 2001 From: Zain Nadeem Date: Sun, 28 Jun 2026 15:26:27 +0500 Subject: [PATCH] gh-152099: Raise SendfileNotAvailableError for fallback-only transports in asyncio (#152223) --- Lib/asyncio/base_events.py | 2 +- Lib/test/test_asyncio/test_sendfile.py | 3 ++- .../Library/2026-06-25-22-19-04.gh-issue-152099.L7fKq9.rst | 4 ++++ 3 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-06-25-22-19-04.gh-issue-152099.L7fKq9.rst diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py index bb736222b0b3..93bd7df993d8 100644 --- a/Lib/asyncio/base_events.py +++ b/Lib/asyncio/base_events.py @@ -1283,7 +1283,7 @@ class BaseEventLoop(events.AbstractEventLoop): raise if not fallback: - raise RuntimeError( + raise exceptions.SendfileNotAvailableError( f"fallback is disabled and native sendfile is not " f"supported for transport {transport!r}") return await self._sendfile_fallback(transport, file, diff --git a/Lib/test/test_asyncio/test_sendfile.py b/Lib/test/test_asyncio/test_sendfile.py index 2d5c06f82b3d..69ac9a367f6b 100644 --- a/Lib/test/test_asyncio/test_sendfile.py +++ b/Lib/test/test_asyncio/test_sendfile.py @@ -580,7 +580,8 @@ class SendfileMixin(SendfileBase): transport = mock.Mock() transport.is_closing.side_effect = lambda: False transport._sendfile_compatible = constants._SendfileMode.FALLBACK - with self.assertRaisesRegex(RuntimeError, 'fallback is disabled'): + with self.assertRaisesRegex(asyncio.SendfileNotAvailableError, + 'fallback is disabled'): self.loop.run_until_complete( self.loop.sendfile(transport, None, fallback=False)) diff --git a/Misc/NEWS.d/next/Library/2026-06-25-22-19-04.gh-issue-152099.L7fKq9.rst b/Misc/NEWS.d/next/Library/2026-06-25-22-19-04.gh-issue-152099.L7fKq9.rst new file mode 100644 index 000000000000..05e760bcf6ed --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-06-25-22-19-04.gh-issue-152099.L7fKq9.rst @@ -0,0 +1,4 @@ +``asyncio``'s ``loop.sendfile(..., fallback=False)`` now consistently raises +:exc:`asyncio.SendfileNotAvailableError` for fallback-only transports, such as +SSL/TLS transports, when native sendfile cannot be used. Previously, this case +raised :exc:`RuntimeError`. -- 2.47.3