https://github.com/python/cpython/commit/76e911a040aaf16f0c76b7d2a2f29582cb98c32d
commit: 76e911a040aaf16f0c76b7d2a2f29582cb98c32d
branch: 3.12
author: Miss Islington (bot) <31488909+miss-isling...@users.noreply.github.com>
committer: sobolevn <m...@sobolevn.me>
date: 2025-03-24T19:18:29Z
summary:

[3.12] gh-131670: Fix crash in `anext()` when `__anext__` is sync and raises 
(GH-131682) (#131687)

gh-131670: Fix crash in `anext()` when `__anext__` is sync and raises 
(GH-131682)
(cherry picked from commit 929afd1d6ee4fb89ac818037effe6577947103de)

Co-authored-by: sobolevn <m...@sobolevn.me>
Co-authored-by: Bénédikt Tran <10796600+picn...@users.noreply.github.com>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2025-03-24-19-38-53.gh-issue-131670.IffOZj.rst
M Lib/test/test_asyncgen.py
M Python/bltinmodule.c

diff --git a/Lib/test/test_asyncgen.py b/Lib/test/test_asyncgen.py
index b5a786eda4e1a6..07aa088ecf31a5 100644
--- a/Lib/test/test_asyncgen.py
+++ b/Lib/test/test_asyncgen.py
@@ -1117,6 +1117,26 @@ 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_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(exc_type, msg):
+                    anext(A())
+                with self.assertRaisesRegex(exc_type, 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..812a75abe1bab8
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-03-24-19-38-53.gh-issue-131670.IffOZj.rst
@@ -0,0 +1 @@
+Fix :func:`anext` failing on sync :meth:`~object.__anext__` raising an 
exception.
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index ab87ee4fc4dfe9..2ae28693bee05c 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -1685,6 +1685,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;
     }

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to