summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>2025-01-13 12:55:09 (GMT)
committerGitHub <noreply@github.com>2025-01-13 12:55:09 (GMT)
commit76ffaef729c91bb79da6df2ade48f3ec51118300 (patch)
treeaa587f3ec5b3ae03c376cd806df047fc6af75f16
parent517dc65ffcea8413e1a60c4cb5d63e5fa39e7f72 (diff)
downloadcpython-76ffaef729c91bb79da6df2ade48f3ec51118300.zip
cpython-76ffaef729c91bb79da6df2ade48f3ec51118300.tar.gz
cpython-76ffaef729c91bb79da6df2ade48f3ec51118300.tar.bz2
gh-128078: Clear exception in `anext` before calling `_PyGen_SetStopIterationValue` (#128780)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
-rw-r--r--Lib/test/test_asyncgen.py17
-rw-r--r--Misc/NEWS.d/next/Core_and_Builtins/2025-01-13-12-48-30.gh-issue-128078.qOsl9B.rst2
-rw-r--r--Objects/genobject.c1
-rw-r--r--Objects/iterobject.c2
4 files changed, 22 insertions, 0 deletions
diff --git a/Lib/test/test_asyncgen.py b/Lib/test/test_asyncgen.py
index 5bfd789..3f631a0 100644
--- a/Lib/test/test_asyncgen.py
+++ b/Lib/test/test_asyncgen.py
@@ -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
index 0000000..498864a
--- /dev/null
+++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-01-13-12-48-30.gh-issue-128078.qOsl9B.rst
@@ -0,0 +1,2 @@
+Fix a :exc:`SystemError` when using :func:`anext` with a default tuple
+value. Patch by Bénédikt Tran.
diff --git a/Objects/genobject.c b/Objects/genobject.c
index e87f199..a4872de 100644
--- a/Objects/genobject.c
+++ b/Objects/genobject.c
@@ -633,6 +633,7 @@ gen_iternext(PyObject *self)
int
_PyGen_SetStopIterationValue(PyObject *value)
{
+ assert(!PyErr_Occurred());
PyObject *e;
if (value == NULL ||
diff --git a/Objects/iterobject.c b/Objects/iterobject.c
index 135ced9..ebb342f 100644
--- a/Objects/iterobject.c
+++ b/Objects/iterobject.c
@@ -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;