From 90c16bc5a6342e30c9ba228024aacd036e5edf3f Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 7 Feb 2022 13:15:55 +0300 Subject: [PATCH 1/2] bpo-46672: fix `NameError` in `asyncio.gather` if type check fails --- Lib/asyncio/tasks.py | 5 +++-- Lib/test/test_asyncio/test_tasks.py | 14 ++++++++++++++ .../2022-02-07-13-15-16.bpo-46672.4swIjx.rst | 1 + 3 files changed, 18 insertions(+), 2 deletions(-) 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 2bee5c050ded7d..ea6bfac179f552 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -634,7 +634,7 @@ def _ensure_future(coro_or_future, *, loop=None): loop = events._get_event_loop(stacklevel=4) try: return loop.create_task(coro_or_future) - except RuntimeError: + except RuntimeError: if not called_wrap_awaitable: coro_or_future.close() raise @@ -721,7 +721,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() @@ -777,6 +777,7 @@ def _done_callback(fut): 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 8c4dceacdeec96..7d9e491647b11b 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -3190,6 +3190,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..2238c2344e2b29 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-02-07-13-15-16.bpo-46672.4swIjx.rst @@ -0,0 +1 @@ +Fixes ``NameError`` in :func:`asyncio.gather` when initial type check fails. From 7b35c037cea52d1122e79b2912809b35c46d2247 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Mon, 7 Feb 2022 13:39:35 +0300 Subject: [PATCH 2/2] Update Misc/NEWS.d/next/Library/2022-02-07-13-15-16.bpo-46672.4swIjx.rst Co-authored-by: Alex Waygood --- .../next/Library/2022-02-07-13-15-16.bpo-46672.4swIjx.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 2238c2344e2b29..9a76c29a334d8c 100644 --- 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 @@ -1 +1 @@ -Fixes ``NameError`` in :func:`asyncio.gather` when initial type check fails. +Fix ``NameError`` in :func:`asyncio.gather` when initial type check fails.