From c275312a6284bd319ea33c9abd7e15c230eca43f Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Tue, 17 Sep 2019 15:59:49 +0300 Subject: 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 --- Lib/test/test_asyncgen.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'Lib/test/test_asyncgen.py') diff --git a/Lib/test/test_asyncgen.py b/Lib/test/test_asyncgen.py index 71b0968c794..3a8d5fd4007 100644 --- a/Lib/test/test_asyncgen.py +++ b/Lib/test/test_asyncgen.py @@ -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() -- cgit v1.2.3