]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46672: fix `NameError` in `asyncio.gather` if type check fails (GH-31187)
authorNikita Sobolev <mail@sobolevn.me>
Sun, 20 Feb 2022 10:24:00 +0000 (13:24 +0300)
committerGitHub <noreply@github.com>
Sun, 20 Feb 2022 10:24:00 +0000 (12:24 +0200)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Lib/asyncio/tasks.py
Lib/test/test_asyncio/test_tasks.py
Misc/NEWS.d/next/Library/2022-02-07-13-15-16.bpo-46672.4swIjx.rst [new file with mode: 0644]

index c11d0daaefea7e5e7876c9a5cb863c5340b362df..25a650ffbb7446033248465b2212293bc3196b8f 100644 (file)
@@ -735,7 +735,7 @@ def gather(*coros_or_futures, return_exceptions=False):
         nonlocal nfinished
         nfinished += 1
 
-        if outer.done():
+        if outer is None or outer.done():
             if not fut.cancelled():
                 # Mark exception retrieved.
                 fut.exception()
@@ -791,6 +791,7 @@ def gather(*coros_or_futures, return_exceptions=False):
     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)
index fe6bfb363f1c67ebddb412d5e18e988c5f29769f..40b33deb17ee02306704576cbf7f277f008f8a38 100644 (file)
@@ -3235,6 +3235,20 @@ class CoroutineGatherTests(GatherTestsBase, test_utils.TestCase):
         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 (file)
index 0000000..9a76c29
--- /dev/null
@@ -0,0 +1 @@
+Fix ``NameError`` in :func:`asyncio.gather` when initial type check fails.