mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
gh-128078: Clear exception in anext
before calling _PyGen_SetStopIterationValue
(#128780)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
This commit is contained in:
parent
517dc65ffc
commit
76ffaef729
4 changed files with 22 additions and 0 deletions
|
@ -1152,6 +1152,23 @@ class AsyncGenAsyncioTest(unittest.TestCase):
|
||||||
|
|
||||||
self.loop.run_until_complete(run())
|
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):
|
def test_async_gen_asyncio_anext_stopiteration(self):
|
||||||
async def foo():
|
async def foo():
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix a :exc:`SystemError` when using :func:`anext` with a default tuple
|
||||||
|
value. Patch by Bénédikt Tran.
|
|
@ -633,6 +633,7 @@ gen_iternext(PyObject *self)
|
||||||
int
|
int
|
||||||
_PyGen_SetStopIterationValue(PyObject *value)
|
_PyGen_SetStopIterationValue(PyObject *value)
|
||||||
{
|
{
|
||||||
|
assert(!PyErr_Occurred());
|
||||||
PyObject *e;
|
PyObject *e;
|
||||||
|
|
||||||
if (value == NULL ||
|
if (value == NULL ||
|
||||||
|
|
|
@ -384,6 +384,7 @@ anextawaitable_iternext(anextawaitableobject *obj)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration)) {
|
if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration)) {
|
||||||
|
PyErr_Clear();
|
||||||
_PyGen_SetStopIterationValue(obj->default_value);
|
_PyGen_SetStopIterationValue(obj->default_value);
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -407,6 +408,7 @@ anextawaitable_proxy(anextawaitableobject *obj, char *meth, PyObject *arg) {
|
||||||
* exception we replace it with a `StopIteration(default)`, as if
|
* exception we replace it with a `StopIteration(default)`, as if
|
||||||
* it was the return value of `__anext__()` coroutine.
|
* it was the return value of `__anext__()` coroutine.
|
||||||
*/
|
*/
|
||||||
|
PyErr_Clear();
|
||||||
_PyGen_SetStopIterationValue(obj->default_value);
|
_PyGen_SetStopIterationValue(obj->default_value);
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue