mirror of
https://github.com/python/cpython.git
synced 2024-12-03 23:06:43 +08:00
bpo-38013: make async_generator_athrow object tolerant to throwing exceptions (GH-16070)
Even when the helper is not started yet. This behavior follows conventional generator one. There is no reason for `async_generator_athrow` to handle `gen.throw()` differently. https://bugs.python.org/issue38013
This commit is contained in:
parent
5f5f11faf9
commit
c275312a62
@ -1125,6 +1125,28 @@ class AsyncGenAsyncioTest(unittest.TestCase):
|
||||
res = self.loop.run_until_complete(run())
|
||||
self.assertEqual(res, [i * 2 for i in range(1, 10)])
|
||||
|
||||
def test_asyncgen_nonstarted_hooks_are_cancellable(self):
|
||||
# See https://bugs.python.org/issue38013
|
||||
messages = []
|
||||
|
||||
def exception_handler(loop, context):
|
||||
messages.append(context)
|
||||
|
||||
async def async_iterate():
|
||||
yield 1
|
||||
yield 2
|
||||
|
||||
async def main():
|
||||
loop = asyncio.get_running_loop()
|
||||
loop.set_exception_handler(exception_handler)
|
||||
|
||||
async for i in async_iterate():
|
||||
break
|
||||
|
||||
asyncio.run(main())
|
||||
|
||||
self.assertEqual([], messages)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
@ -0,0 +1,3 @@
|
||||
Allow to call ``async_generator_athrow().throw(...)`` even for non-started
|
||||
async generator helper. It fixes annoying warning at the end of
|
||||
:func:`asyncio.run` call.
|
@ -1884,11 +1884,6 @@ async_gen_athrow_throw(PyAsyncGenAThrow *o, PyObject *args)
|
||||
{
|
||||
PyObject *retval;
|
||||
|
||||
if (o->agt_state == AWAITABLE_STATE_INIT) {
|
||||
PyErr_SetString(PyExc_RuntimeError, NON_INIT_CORO_MSG);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (o->agt_state == AWAITABLE_STATE_CLOSED) {
|
||||
PyErr_SetNone(PyExc_StopIteration);
|
||||
return NULL;
|
||||
|
Loading…
Reference in New Issue
Block a user