From 6862fb495960dbe0c5ba7fd991617811b197f166 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 24 Mar 2025 19:42:31 +0300 Subject: [PATCH 1/2] gh-131670: Fix crash in `anext()` when `__anext__` is sync and raises --- Lib/test/test_asyncgen.py | 19 +++++++++++++++++++ ...-03-24-19-38-53.gh-issue-131670.IffOZj.rst | 2 ++ Python/bltinmodule.c | 3 +++ 3 files changed, 24 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-03-24-19-38-53.gh-issue-131670.IffOZj.rst diff --git a/Lib/test/test_asyncgen.py b/Lib/test/test_asyncgen.py index b81187871753b9..6e968bc6ceb676 100644 --- a/Lib/test/test_asyncgen.py +++ b/Lib/test/test_asyncgen.py @@ -1169,6 +1169,25 @@ async def run(): self.loop.run_until_complete(run()) + def test_sync_anext_raises_exception(self): + # See: https://github.com/python/cpython/issues/131670 + msg = 'custom' + for exc in [ + StopAsyncIteration(msg), + StopIteration(msg), + ValueError(msg), + Exception(msg), + ]: + with self.subTest(exc=exc): + class A: + def __anext__(self): + raise exc + + with self.assertRaisesRegex(type(exc), msg): + anext(A()) + with self.assertRaisesRegex(type(exc), msg): + anext(A(), 1) + def test_async_gen_asyncio_anext_stopiteration(self): async def foo(): try: diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-03-24-19-38-53.gh-issue-131670.IffOZj.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-03-24-19-38-53.gh-issue-131670.IffOZj.rst new file mode 100644 index 00000000000000..5b4aee3027bc8d --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-03-24-19-38-53.gh-issue-131670.IffOZj.rst @@ -0,0 +1,2 @@ +Fix :func:`anext` failing on sync :meth:`~object.__anext__` that raises +:exc:`StopAsyncIteration`. diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 6709f306bb024d..f7761207950705 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1837,6 +1837,9 @@ builtin_anext_impl(PyObject *module, PyObject *aiterator, } awaitable = (*t->tp_as_async->am_anext)(aiterator); + if (awaitable == NULL) { + return NULL; + } if (default_value == NULL) { return awaitable; } From cd918c2b1f6dc32bbcdb425d0e15753fecec3055 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 24 Mar 2025 21:35:46 +0300 Subject: [PATCH 2/2] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/test/test_asyncgen.py | 15 ++++++++------- ...2025-03-24-19-38-53.gh-issue-131670.IffOZj.rst | 3 +-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Lib/test/test_asyncgen.py b/Lib/test/test_asyncgen.py index 6e968bc6ceb676..d7dcf619300b2b 100644 --- a/Lib/test/test_asyncgen.py +++ b/Lib/test/test_asyncgen.py @@ -1172,20 +1172,21 @@ async def run(): def test_sync_anext_raises_exception(self): # See: https://github.com/python/cpython/issues/131670 msg = 'custom' - for exc in [ - StopAsyncIteration(msg), - StopIteration(msg), - ValueError(msg), - Exception(msg), + for exc_type in [ + StopAsyncIteration, + StopIteration, + ValueError, + Exception, ]: + exc = exc_type(msg) with self.subTest(exc=exc): class A: def __anext__(self): raise exc - with self.assertRaisesRegex(type(exc), msg): + with self.assertRaisesRegex(exc_type, msg): anext(A()) - with self.assertRaisesRegex(type(exc), msg): + with self.assertRaisesRegex(exc_type, msg): anext(A(), 1) def test_async_gen_asyncio_anext_stopiteration(self): diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-03-24-19-38-53.gh-issue-131670.IffOZj.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-03-24-19-38-53.gh-issue-131670.IffOZj.rst index 5b4aee3027bc8d..812a75abe1bab8 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2025-03-24-19-38-53.gh-issue-131670.IffOZj.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-03-24-19-38-53.gh-issue-131670.IffOZj.rst @@ -1,2 +1 @@ -Fix :func:`anext` failing on sync :meth:`~object.__anext__` that raises -:exc:`StopAsyncIteration`. +Fix :func:`anext` failing on sync :meth:`~object.__anext__` raising an exception.