Skip to content
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

bpo-35409: Ignore GeneratorExit in async_gen_athrow_throw #14755

Merged
merged 1 commit into from
Nov 19, 2019
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
27 changes: 27 additions & 0 deletions Lib/test/test_asyncgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,33 @@ async def run():
self.loop.run_until_complete(run())
self.assertEqual(DONE, 10)

def test_async_gen_asyncio_aclose_12(self):
DONE = 0

async def target():
await asyncio.sleep(0.01)
1 / 0

async def foo():
nonlocal DONE
task = asyncio.create_task(target())
try:
yield 1
finally:
try:
await task
except ZeroDivisionError:
DONE = 1

async def run():
gen = foo()
it = gen.__aiter__()
await it.__anext__()
await gen.aclose()

self.loop.run_until_complete(run())
self.assertEqual(DONE, 1)

def test_async_gen_asyncio_asend_01(self):
DONE = 0

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Ignore GeneratorExit exceptions when throwing an exception into the aclose
coroutine of an asynchronous generator.
11 changes: 11 additions & 0 deletions Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1920,6 +1920,17 @@ async_gen_athrow_throw(PyAsyncGenAThrow *o, PyObject *args)
PyErr_SetString(PyExc_RuntimeError, ASYNC_GEN_IGNORED_EXIT_MSG);
return NULL;
}
if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration) ||
PyErr_ExceptionMatches(PyExc_GeneratorExit))
{
/* when aclose() is called we don't want to propagate
StopAsyncIteration or GeneratorExit; just raise
StopIteration, signalling that this 'aclose()' await
is done.
*/
PyErr_Clear();
PyErr_SetNone(PyExc_StopIteration);
}
return retval;
}
}
Expand Down