Skip to content

Commit da89a69

Browse files
committed
pythonGH-117536: fix athrow().throw(...) unawaited warning
1 parent 56ed979 commit da89a69

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

Lib/test/test_asyncgen.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,17 @@ async def gen():
17521752
g.aclose()
17531753
gc_collect()
17541754

1755+
def test_aclose_throw(self):
1756+
async def gen():
1757+
return
1758+
yield
1759+
1760+
class MyException(Exception):
1761+
pass
1762+
1763+
g = gen()
1764+
with self.assertRaises(MyException):
1765+
gen.aclose().throw(MyException)
17551766

17561767

17571768
if __name__ == "__main__":

Objects/genobject.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2208,7 +2208,11 @@ async_gen_athrow_throw(PyAsyncGenAThrow *o, PyObject *const *args, Py_ssize_t na
22082208

22092209
retval = gen_throw((PyGenObject*)o->agt_gen, args, nargs);
22102210
if (o->agt_args) {
2211-
return async_gen_unwrap_value(o->agt_gen, retval);
2211+
retval = async_gen_unwrap_value(o->agt_gen, retval);
2212+
if (retval == NULL) {
2213+
o->agt_state = AWAITABLE_STATE_CLOSED;
2214+
}
2215+
return retval;
22122216
} else {
22132217
/* aclose() mode */
22142218
if (retval && _PyAsyncGenWrappedValue_CheckExact(retval)) {
@@ -2228,6 +2232,7 @@ async_gen_athrow_throw(PyAsyncGenAThrow *o, PyObject *const *args, Py_ssize_t na
22282232
*/
22292233
PyErr_Clear();
22302234
PyErr_SetNone(PyExc_StopIteration);
2235+
o->agt_state = AWAITABLE_STATE_CLOSED;
22312236
}
22322237
return retval;
22332238
}

0 commit comments

Comments
 (0)