aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib
diff options
context:
space:
mode:
authorNikita Sobolev <mail@sobolevn.me>2022-02-20 13:24:00 +0300
committerGitHub <noreply@github.com>2022-02-20 12:24:00 +0200
commit4ab8167b9c60d1a04b2e3116d0c52db254b68cda (patch)
treef7c1e79ff0941bfd8deca5e6dfccc0d527f87d8d /Lib
parente7130c2e8c6abfaf04b209bd5b239059eda024b9 (diff)
downloadcpython-4ab8167b9c60d1a04b2e3116d0c52db254b68cda.tar.gz
cpython-4ab8167b9c60d1a04b2e3116d0c52db254b68cda.zip
bpo-46672: fix `NameError` in `asyncio.gather` if type check fails (GH-31187)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/asyncio/tasks.py3
-rw-r--r--Lib/test/test_asyncio/test_tasks.py14
2 files changed, 16 insertions, 1 deletions
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
index c11d0daaefe..25a650ffbb7 100644
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -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)
diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py
index fe6bfb363f1..40b33deb17e 100644
--- a/Lib/test/test_asyncio/test_tasks.py
+++ b/Lib/test/test_asyncio/test_tasks.py
@@ -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."""