Skip to content

[3.12] GH-117894: prevent aclose()/athrow() being re-used after StopIteration (GH-117851) #118226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
62 changes: 61 additions & 1 deletion Lib/test/test_asyncgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,11 @@ async def gen():
yield 123

with self.assertWarns(DeprecationWarning):
gen().athrow(GeneratorExit, GeneratorExit(), None)
x = gen().athrow(GeneratorExit, GeneratorExit(), None)
with self.assertRaises(GeneratorExit):
x.send(None)
del x
gc_collect()

def test_async_gen_api_01(self):
async def gen():
Expand Down Expand Up @@ -1653,6 +1657,62 @@ async def run():

self.loop.run_until_complete(run())

def test_async_gen_throw_same_aclose_coro_twice(self):
async def async_iterate():
yield 1
yield 2

it = async_iterate()
nxt = it.aclose()
with self.assertRaises(StopIteration):
nxt.throw(GeneratorExit)

with self.assertRaisesRegex(
RuntimeError,
r"cannot reuse already awaited aclose\(\)/athrow\(\)"
):
nxt.throw(GeneratorExit)

def test_async_gen_throw_custom_same_aclose_coro_twice(self):
async def async_iterate():
yield 1
yield 2

it = async_iterate()

class MyException(Exception):
pass

nxt = it.aclose()
with self.assertRaises(MyException):
nxt.throw(MyException)

with self.assertRaisesRegex(
RuntimeError,
r"cannot reuse already awaited aclose\(\)/athrow\(\)"
):
nxt.throw(MyException)

def test_async_gen_throw_custom_same_athrow_coro_twice(self):
async def async_iterate():
yield 1
yield 2

it = async_iterate()

class MyException(Exception):
pass

nxt = it.athrow(MyException)
with self.assertRaises(MyException):
nxt.throw(MyException)

with self.assertRaisesRegex(
RuntimeError,
r"cannot reuse already awaited aclose\(\)/athrow\(\)"
):
nxt.throw(MyException)

def test_async_gen_aclose_twice_with_different_coros(self):
# Regression test for https://bugs.python.org/issue39606
async def async_iterate():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prevent ``agen.aclose()`` objects being re-used after ``.throw()``.
9 changes: 8 additions & 1 deletion Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2223,7 +2223,11 @@ async_gen_athrow_throw(PyAsyncGenAThrow *o, PyObject *const *args, Py_ssize_t na

retval = gen_throw((PyGenObject*)o->agt_gen, args, nargs);
if (o->agt_args) {
return async_gen_unwrap_value(o->agt_gen, retval);
retval = async_gen_unwrap_value(o->agt_gen, retval);
if (retval == NULL) {
o->agt_state = AWAITABLE_STATE_CLOSED;
}
return retval;
} else {
/* aclose() mode */
if (retval && _PyAsyncGenWrappedValue_CheckExact(retval)) {
Expand All @@ -2233,6 +2237,9 @@ async_gen_athrow_throw(PyAsyncGenAThrow *o, PyObject *const *args, Py_ssize_t na
PyErr_SetString(PyExc_RuntimeError, ASYNC_GEN_IGNORED_EXIT_MSG);
return NULL;
}
if (retval == NULL) {
o->agt_state = AWAITABLE_STATE_CLOSED;
}
if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration) ||
PyErr_ExceptionMatches(PyExc_GeneratorExit))
{
Expand Down