From 4fa91ec296811ed2e092327c29227b5f9f7c79cf Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Sun, 20 Feb 2022 13:24:00 +0300 Subject: [PATCH 1/3] [3.9] bpo-46672: fix `NameError` in `asyncio.gather` if type check fails (GH-31187) Co-authored-by: Alex Waygood . (cherry picked from commit 4ab8167b9c60d1a04b2e3116d0c52db254b68cda) Co-authored-by: Nikita Sobolev --- Lib/asyncio/tasks.py | 4 +++- Lib/test/test_asyncio/test_tasks.py | 14 ++++++++++++++ .../2022-02-07-13-15-16.bpo-46672.4swIjx.rst | 1 + 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2022-02-07-13-15-16.bpo-46672.4swIjx.rst diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 39bd0685356685..67f5d2a7def7a4 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -768,7 +768,7 @@ def _done_callback(fut): nonlocal nfinished nfinished += 1 - if outer.done(): + if outer is None or outer.done(): if not fut.cancelled(): # Mark exception retrieved. fut.exception() @@ -823,6 +823,8 @@ def _done_callback(fut): children = [] nfuts = 0 nfinished = 0 + loop = None + outer = None # bpo-46672 for arg in coros_or_futures: if arg not in arg_to_fut: fut = ensure_future(arg, loop=loop) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 2f016740b9e769..ade453f9b1f147 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -3488,6 +3488,20 @@ async def outer(): test_utils.run_briefly(self.one_loop) self.assertIsInstance(f.exception(), RuntimeError) + def test_issue46672(self): + with mock.patch( + 'asyncio.base_events.BaseEventLoop.call_exception_handler', + ): + async def coro(s): + return s + c = coro('abc') + + with self.assertRaises(TypeError): + self._gather(c, {}) + self._run_loop(self.one_loop) + # NameError should not happen: + self.one_loop.call_exception_handler.assert_not_called() + class RunCoroutineThreadsafeTests(test_utils.TestCase): """Test case for asyncio.run_coroutine_threadsafe.""" diff --git a/Misc/NEWS.d/next/Library/2022-02-07-13-15-16.bpo-46672.4swIjx.rst b/Misc/NEWS.d/next/Library/2022-02-07-13-15-16.bpo-46672.4swIjx.rst new file mode 100644 index 00000000000000..9a76c29a334d8c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-02-07-13-15-16.bpo-46672.4swIjx.rst @@ -0,0 +1 @@ +Fix ``NameError`` in :func:`asyncio.gather` when initial type check fails. From 9028eec9799ace18b55ee5dde6033a9b7be59566 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Sun, 20 Feb 2022 13:10:21 +0200 Subject: [PATCH 2/3] Fix --- Lib/asyncio/tasks.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 67f5d2a7def7a4..53252f2079d7ce 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -823,7 +823,6 @@ def _done_callback(fut): children = [] nfuts = 0 nfinished = 0 - loop = None outer = None # bpo-46672 for arg in coros_or_futures: if arg not in arg_to_fut: From 150ef068c77abc6a5e7ba97397ac65113dba355a Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Sun, 20 Feb 2022 14:14:24 +0200 Subject: [PATCH 3/3] Add missing helper --- Lib/test/test_asyncio/test_tasks.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index ade453f9b1f147..0e538edcd9678e 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -3407,6 +3407,11 @@ async def coro(fut=fut): coros.append(coro()) return coros + def _gather(self, *args, **kwargs): + async def coro(): + return asyncio.gather(*args, **kwargs) + return self.one_loop.run_until_complete(coro()) + def test_constructor_loop_selection(self): async def coro(): return 'abc'