Skip to content

Commit 738f899

Browse files
address review
1 parent 3bc9a38 commit 738f899

2 files changed

Lines changed: 71 additions & 8 deletions

File tree

‎Lib/test/test_asyncgen.py‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,33 @@ async def agenfn():
619619
with self.assertRaisesRegex(RuntimeError, "coroutine ignored GeneratorExit"):
620620
gen.close()
621621

622+
def test_async_gen_athrow_send_non_none(self):
623+
# gh-120321: sending a non-None value to a just-started athrow()
624+
# awaitable must not claim the generator, so the generator stays
625+
# usable and the awaitable can still be awaited afterwards.
626+
class MyExc(Exception):
627+
pass
628+
629+
async def agenfn():
630+
try:
631+
yield 1
632+
except MyExc:
633+
yield 2
634+
635+
agen = agenfn()
636+
with self.assertRaises(StopIteration):
637+
agen.asend(None).send(None)
638+
639+
gen = agen.athrow(MyExc)
640+
with self.assertRaisesRegex(RuntimeError, "non-None value"):
641+
gen.send(42)
642+
self.assertFalse(agen.ag_running)
643+
644+
# The awaitable is still in its initial state and works normally.
645+
with self.assertRaises(StopIteration) as cm:
646+
gen.send(None)
647+
self.assertEqual(cm.exception.value, 2)
648+
622649

623650
class AsyncGenAsyncioTest(unittest.TestCase):
624651

@@ -1950,6 +1977,41 @@ class MyException(Exception):
19501977
):
19511978
nxt.throw(MyException)
19521979

1980+
def test_async_gen_send_same_athrow_coro_after_completion(self):
1981+
# gh-120321: an athrow() awaitable that needs more than one send()
1982+
# to complete must be closed on completion; sending to it again
1983+
# must raise instead of resuming the generator.
1984+
class YieldOnce:
1985+
def __await__(self):
1986+
yield
1987+
1988+
async def async_iterate():
1989+
try:
1990+
yield 1
1991+
except ValueError:
1992+
await YieldOnce()
1993+
yield 2
1994+
1995+
it = async_iterate()
1996+
with self.assertRaises(StopIteration):
1997+
it.__anext__().send(None)
1998+
1999+
nxt = it.athrow(ValueError)
2000+
# The exception handler suspends before the operation completes.
2001+
nxt.send(None)
2002+
with self.assertRaises(StopIteration) as cm:
2003+
nxt.send(None)
2004+
self.assertEqual(cm.exception.value, 2)
2005+
2006+
with self.assertRaisesRegex(
2007+
RuntimeError,
2008+
r"cannot reuse already awaited aclose\(\)/athrow\(\)"
2009+
):
2010+
nxt.send(None)
2011+
2012+
with self.assertRaises(StopIteration):
2013+
it.aclose().send(None)
2014+
19532015
def test_async_gen_aclose_twice_with_different_coros(self):
19542016
# Regression test for https://bugs.python.org/issue39606
19552017
async def async_iterate():

‎Objects/genobject.c‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,7 +2374,10 @@ async_gen_athrow_send(PyObject *self, PyObject *arg)
23742374
}
23752375

23762376
if (FRAME_STATE_FINISHED(FT_ATOMIC_LOAD_INT8_RELAXED(gen->gi_frame_state))) {
2377-
FT_ATOMIC_STORE_INT8_RELAXED(o->agt_state, AWAITABLE_STATE_CLOSED);
2377+
// Close the awaitable, unless another thread transitioned it
2378+
// to a different state in the meantime.
2379+
(void)_Py_ASYNC_GEN_TRY_SET_STATE(o->agt_state, state,
2380+
AWAITABLE_STATE_CLOSED);
23782381
PyErr_SetNone(PyExc_StopIteration);
23792382
return NULL;
23802383
}
@@ -2390,6 +2393,10 @@ async_gen_athrow_send(PyObject *self, PyObject *arg)
23902393
goto do_send;
23912394
}
23922395
assert(state == AWAITABLE_STATE_INIT);
2396+
if (arg != Py_None) {
2397+
PyErr_SetString(PyExc_RuntimeError, NON_INIT_CORO_MSG);
2398+
return NULL;
2399+
}
23932400
} while (!_Py_ASYNC_GEN_TRY_SET_STATE(o->agt_state, state,
23942401
AWAITABLE_STATE_ITER));
23952402

@@ -2418,13 +2425,6 @@ async_gen_athrow_send(PyObject *self, PyObject *arg)
24182425
return NULL;
24192426
}
24202427

2421-
if (arg != Py_None) {
2422-
FT_ATOMIC_STORE_INT8_RELAXED(o->agt_state, AWAITABLE_STATE_INIT);
2423-
FT_ATOMIC_STORE_INT8_RELEASE(o->agt_gen->ag_running_async, 0);
2424-
PyErr_SetString(PyExc_RuntimeError, NON_INIT_CORO_MSG);
2425-
return NULL;
2426-
}
2427-
24282428
if (o->agt_typ == NULL) {
24292429
/* aclose() mode */
24302430
FT_ATOMIC_STORE_INT8_RELAXED(o->agt_gen->ag_closed, 1);
@@ -2455,6 +2455,7 @@ async_gen_athrow_send(PyObject *self, PyObject *arg)
24552455
if (o->agt_typ) {
24562456
retval = async_gen_unwrap_value(o->agt_gen, retval);
24572457
if (retval == NULL) {
2458+
FT_ATOMIC_STORE_INT8_RELAXED(o->agt_state, AWAITABLE_STATE_CLOSED);
24582459
FT_ATOMIC_STORE_INT8_RELEASE(o->agt_gen->ag_running_async, 0);
24592460
}
24602461
return retval;

0 commit comments

Comments
 (0)