Skip to content

Commit 4fa91ec

Browse files
sobolevnasvetlov
authored andcommitted
[3.9] bpo-46672: fix NameError in asyncio.gather if type check fails (pythonGH-31187)
Co-authored-by: Alex Waygood <[email protected]>. (cherry picked from commit 4ab8167) Co-authored-by: Nikita Sobolev <[email protected]>
1 parent cf19932 commit 4fa91ec

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

Lib/asyncio/tasks.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ def _done_callback(fut):
768768
nonlocal nfinished
769769
nfinished += 1
770770

771-
if outer.done():
771+
if outer is None or outer.done():
772772
if not fut.cancelled():
773773
# Mark exception retrieved.
774774
fut.exception()
@@ -823,6 +823,8 @@ def _done_callback(fut):
823823
children = []
824824
nfuts = 0
825825
nfinished = 0
826+
loop = None
827+
outer = None # bpo-46672
826828
for arg in coros_or_futures:
827829
if arg not in arg_to_fut:
828830
fut = ensure_future(arg, loop=loop)

Lib/test/test_asyncio/test_tasks.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3488,6 +3488,20 @@ async def outer():
34883488
test_utils.run_briefly(self.one_loop)
34893489
self.assertIsInstance(f.exception(), RuntimeError)
34903490

3491+
def test_issue46672(self):
3492+
with mock.patch(
3493+
'asyncio.base_events.BaseEventLoop.call_exception_handler',
3494+
):
3495+
async def coro(s):
3496+
return s
3497+
c = coro('abc')
3498+
3499+
with self.assertRaises(TypeError):
3500+
self._gather(c, {})
3501+
self._run_loop(self.one_loop)
3502+
# NameError should not happen:
3503+
self.one_loop.call_exception_handler.assert_not_called()
3504+
34913505

34923506
class RunCoroutineThreadsafeTests(test_utils.TestCase):
34933507
"""Test case for asyncio.run_coroutine_threadsafe."""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ``NameError`` in :func:`asyncio.gather` when initial type check fails.

0 commit comments

Comments
 (0)