]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-128078: Clear exception in `anext` before calling `_PyGen_SetStopIterationValue...
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>
Mon, 13 Jan 2025 12:55:09 +0000 (13:55 +0100)
committerGitHub <noreply@github.com>
Mon, 13 Jan 2025 12:55:09 +0000 (18:25 +0530)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Lib/test/test_asyncgen.py
Misc/NEWS.d/next/Core_and_Builtins/2025-01-13-12-48-30.gh-issue-128078.qOsl9B.rst [new file with mode: 0644]
Objects/genobject.c
Objects/iterobject.c

index 5bfd789185c6750b6c302d6ddabc4f621ecaf999..3f631a03c9bf720dce607839cf8ad05e81d97132 100644 (file)
@@ -1152,6 +1152,23 @@ class AsyncGenAsyncioTest(unittest.TestCase):
 
         self.loop.run_until_complete(run())
 
+    def test_async_gen_asyncio_anext_tuple_no_exceptions(self):
+        # StopAsyncIteration exceptions should be cleared.
+        # See: https://github.com/python/cpython/issues/128078.
+
+        async def foo():
+            if False:
+                yield (1, 2)
+
+        async def run():
+            it = foo().__aiter__()
+            with self.assertRaises(StopAsyncIteration):
+                await it.__anext__()
+            res = await anext(it, ('a', 'b'))
+            self.assertEqual(res, ('a', 'b'))
+
+        self.loop.run_until_complete(run())
+
     def test_async_gen_asyncio_anext_stopiteration(self):
         async def foo():
             try:
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-01-13-12-48-30.gh-issue-128078.qOsl9B.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-01-13-12-48-30.gh-issue-128078.qOsl9B.rst
new file mode 100644 (file)
index 0000000..498864a
--- /dev/null
@@ -0,0 +1,2 @@
+Fix a :exc:`SystemError` when using :func:`anext` with a default tuple
+value. Patch by Bénédikt Tran.
index e87f199c2504ba67863de93916df4548127aade7..a4872de296e2cf593c68a416df7bed67c28a6d02 100644 (file)
@@ -633,6 +633,7 @@ gen_iternext(PyObject *self)
 int
 _PyGen_SetStopIterationValue(PyObject *value)
 {
+    assert(!PyErr_Occurred());
     PyObject *e;
 
     if (value == NULL ||
index 135ced9ea1f2685ec5af9a70d356721fc6ca4dcd..ebb342ff1092224611e0d830ad9c07ab0ca277a5 100644 (file)
@@ -384,6 +384,7 @@ anextawaitable_iternext(anextawaitableobject *obj)
         return result;
     }
     if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration)) {
+        PyErr_Clear();
         _PyGen_SetStopIterationValue(obj->default_value);
     }
     return NULL;
@@ -407,6 +408,7 @@ anextawaitable_proxy(anextawaitableobject *obj, char *meth, PyObject *arg) {
          * exception we replace it with a `StopIteration(default)`, as if
          * it was the return value of `__anext__()` coroutine.
          */
+        PyErr_Clear();
         _PyGen_SetStopIterationValue(obj->default_value);
     }
     return NULL;