Skip to content

GH-117536: GH-117894: fix athrow().throw(...) unawaited warning #117851

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 15 commits into from
Apr 24, 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
83 changes: 71 additions & 12 deletions Lib/test/test_asyncgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,8 @@ async def gen():

with self.assertWarns(DeprecationWarning):
x = gen().athrow(GeneratorExit, GeneratorExit(), None)
with self.assertWarnsRegex(RuntimeWarning,
f"coroutine method 'athrow' of '{gen.__qualname__}' "
f"was never awaited"):
with self.assertRaises(GeneratorExit):
x.send(None)
del x
gc_collect()

Expand Down Expand Up @@ -1572,11 +1571,6 @@ async def main():
self.assertIsInstance(message['exception'], ZeroDivisionError)
self.assertIn('unhandled exception during asyncio.run() shutdown',
message['message'])
with self.assertWarnsRegex(RuntimeWarning,
f"coroutine method 'aclose' of '{async_iterate.__qualname__}' "
f"was never awaited"):
del message, messages
gc_collect()

def test_async_gen_expression_01(self):
async def arange(n):
Expand Down Expand Up @@ -1630,10 +1624,6 @@ async def main():
asyncio.run(main())

self.assertEqual([], messages)
with self.assertWarnsRegex(RuntimeWarning,
f"coroutine method 'aclose' of '{async_iterate.__qualname__}' "
f"was never awaited"):
gc_collect()

def test_async_gen_await_same_anext_coro_twice(self):
async def async_iterate():
Expand Down Expand Up @@ -1671,6 +1661,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 Expand Up @@ -1752,6 +1798,19 @@ async def gen():
g.aclose()
gc_collect()

def test_aclose_throw(self):
async def gen():
return
yield

class MyException(Exception):
pass

g = gen()
with self.assertRaises(MyException):
g.aclose().throw(MyException)
del g
gc_collect()


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a :exc:`RuntimeWarning` when calling ``agen.aclose().throw(Exception)``.
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 @@ -2208,7 +2208,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 @@ -2218,6 +2222,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