Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Lib/test/test_asyncgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,24 @@ async def 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__()
a, b = await anext(it, ('a', 'b'))
self.assertEqual(a, 'a')
self.assertEqual(b, 'b')

self.loop.run_until_complete(run())

def test_async_gen_asyncio_anext_stopiteration(self):
async def foo():
try:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a :exc:`SystemError` when using :func:`anext` with a default tuple value.
Patch by Bénédikt Tran.
16 changes: 16 additions & 0 deletions Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,22 @@ _PyGen_SetStopIterationValue(PyObject *value)
PyErr_SetObject(PyExc_StopIteration, value);
return 0;
}

// Since _PyGen_SetStopIterationValue() is meant to create
// a StopItertation or substitute one for a StopAsyncItertation
// an exception of another type should not already be set.
PyObject *run_ex = PyErr_Occurred();
if (run_ex) {
if (!PyErr_GivenExceptionMatches(run_ex, PyExc_StopAsyncIteration)) {
// Replace existing bad exception with a SystemError instead.
PyErr_Format(PyExc_SystemError,
"%s:%d: unexpected caller exception: %R",
__FILE__, __LINE__, run_ex);
return -1;
}
PyErr_Clear();
}

/* Construct an exception instance manually with
* PyObject_CallOneArg and pass it to PyErr_SetObject.
*
Expand Down
Loading