Skip to content

Commit a6116a9

Browse files
asvetlovsobolevn
andauthored
[3.9] bpo-46672: fix NameError in asyncio.gather if type check fails (GH-31187) (GH-31441)
Co-authored-by: Nikita Sobolev <[email protected]>
1 parent cf19932 commit a6116a9

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Lib/asyncio/tasks.py

+2-1
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,7 @@ def _done_callback(fut):
823823
children = []
824824
nfuts = 0
825825
nfinished = 0
826+
outer = None # bpo-46672
826827
for arg in coros_or_futures:
827828
if arg not in arg_to_fut:
828829
fut = ensure_future(arg, loop=loop)

Lib/test/test_asyncio/test_tasks.py

+19
Original file line numberDiff line numberDiff line change
@@ -3407,6 +3407,11 @@ async def coro(fut=fut):
34073407
coros.append(coro())
34083408
return coros
34093409

3410+
def _gather(self, *args, **kwargs):
3411+
async def coro():
3412+
return asyncio.gather(*args, **kwargs)
3413+
return self.one_loop.run_until_complete(coro())
3414+
34103415
def test_constructor_loop_selection(self):
34113416
async def coro():
34123417
return 'abc'
@@ -3488,6 +3493,20 @@ async def outer():
34883493
test_utils.run_briefly(self.one_loop)
34893494
self.assertIsInstance(f.exception(), RuntimeError)
34903495

3496+
def test_issue46672(self):
3497+
with mock.patch(
3498+
'asyncio.base_events.BaseEventLoop.call_exception_handler',
3499+
):
3500+
async def coro(s):
3501+
return s
3502+
c = coro('abc')
3503+
3504+
with self.assertRaises(TypeError):
3505+
self._gather(c, {})
3506+
self._run_loop(self.one_loop)
3507+
# NameError should not happen:
3508+
self.one_loop.call_exception_handler.assert_not_called()
3509+
34913510

34923511
class RunCoroutineThreadsafeTests(test_utils.TestCase):
34933512
"""Test case for asyncio.run_coroutine_threadsafe."""
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)