]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-128078: Clear exception in `anext` before calling `_PyGen_SetStopIterationV...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 13 Jan 2025 13:15:20 +0000 (14:15 +0100)
committerGitHub <noreply@github.com>
Mon, 13 Jan 2025 13:15:20 +0000 (18:45 +0530)
gh-128078: Clear exception in `anext` before calling `_PyGen_SetStopIterationValue` (GH-128780)
(cherry picked from commit 76ffaef729c91bb79da6df2ade48f3ec51118300)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
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 9e199806da6b1538ca1186443634798757b1208b..7c868f367b3e8c682d56e8435f2efa13adf43780 100644 (file)
@@ -1100,6 +1100,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 474abe1094be7208521b165233d357fe9059814a..d56a9ad5d73ef5180cce73c7fa2bb5ce85e97189 100644 (file)
@@ -618,6 +618,7 @@ gen_iternext(PyGenObject *gen)
 int
 _PyGen_SetStopIterationValue(PyObject *value)
 {
+    assert(!PyErr_Occurred());
     PyObject *e;
 
     if (value == NULL ||
index 7cb17a6ca4ab56a5d9abb9f8df7f092f2855b50f..66e4490766abbdf610d65e7d0f715289ea04e12f 100644 (file)
@@ -382,6 +382,7 @@ anextawaitable_iternext(anextawaitableobject *obj)
         return result;
     }
     if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration)) {
+        PyErr_Clear();
         _PyGen_SetStopIterationValue(obj->default_value);
     }
     return NULL;
@@ -405,6 +406,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;