[Python-checkins] bpo-35409: Ignore GeneratorExit in async_gen_athrow_throw (GH-14755)
Miss Islington (bot)
webhook-mailer at python.org
Tue Nov 19 09:12:24 EST 2019
https://github.com/python/cpython/commit/4ffc569b47bef9f95e443f3c56f7e7e32cb440c0
commit: 4ffc569b47bef9f95e443f3c56f7e7e32cb440c0
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-11-19T06:12:12-08:00
summary:
bpo-35409: Ignore GeneratorExit in async_gen_athrow_throw (GH-14755)
Ignore `GeneratorExit` exceptions when throwing an exception into the `aclose` coroutine of an asynchronous generator.
https://bugs.python.org/issue35409
(cherry picked from commit 8e0de2a4808d7c2f4adedabff89ee64e0338790a)
Co-authored-by: Vincent Michel <vxgmichel at gmail.com>
files:
A Misc/NEWS.d/next/Core and Builtins/2019-07-13-18-01-13.bpo-35409.ozbcsR.rst
M Lib/test/test_asyncgen.py
M Objects/genobject.c
diff --git a/Lib/test/test_asyncgen.py b/Lib/test/test_asyncgen.py
index 94c2633f330a4..cca6d58417ac4 100644
--- a/Lib/test/test_asyncgen.py
+++ b/Lib/test/test_asyncgen.py
@@ -696,6 +696,33 @@ def foo():
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
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-07-13-18-01-13.bpo-35409.ozbcsR.rst b/Misc/NEWS.d/next/Core and Builtins/2019-07-13-18-01-13.bpo-35409.ozbcsR.rst
new file mode 100644
index 0000000000000..0f35a91ec74e6
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2019-07-13-18-01-13.bpo-35409.ozbcsR.rst
@@ -0,0 +1,2 @@
+Ignore GeneratorExit exceptions when throwing an exception into the aclose
+coroutine of an asynchronous generator.
diff --git a/Objects/genobject.c b/Objects/genobject.c
index 8a978b1a1fe02..7fc7a1f2aeb3d 100644
--- a/Objects/genobject.c
+++ b/Objects/genobject.c
@@ -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;
}
}
More information about the Python-checkins
mailing list