[Python-checkins] [3.9] bpo-46672: fix `NameError` in `asyncio.gather` if type check fails (GH-31187) (GH-31441)
asvetlov
webhook-mailer at python.org
Sun Feb 20 07:39:36 EST 2022
https://github.com/python/cpython/commit/a6116a980c9eae91c2f9af7cbd0a9727e9b887ba
commit: a6116a980c9eae91c2f9af7cbd0a9727e9b887ba
branch: 3.9
author: Andrew Svetlov <andrew.svetlov at gmail.com>
committer: asvetlov <andrew.svetlov at gmail.com>
date: 2022-02-20T14:39:21+02:00
summary:
[3.9] bpo-46672: fix `NameError` in `asyncio.gather` if type check fails (GH-31187) (GH-31441)
Co-authored-by: Nikita Sobolev <mail at sobolevn.me>
files:
A Misc/NEWS.d/next/Library/2022-02-07-13-15-16.bpo-46672.4swIjx.rst
M Lib/asyncio/tasks.py
M Lib/test/test_asyncio/test_tasks.py
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
index 39bd068535668..53252f2079d7c 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,7 @@ def _done_callback(fut):
children = []
nfuts = 0
nfinished = 0
+ 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 2f016740b9e76..0e538edcd9678 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'
@@ -3488,6 +3493,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 0000000000000..9a76c29a334d8
--- /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.
More information about the Python-checkins
mailing list