Skip to content

Commit f1916cd

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

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

Lib/asyncio/tasks.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ def _done_callback(fut):
721721
nonlocal nfinished
722722
nfinished += 1
723723

724-
if outer.done():
724+
if outer is None or outer.done():
725725
if not fut.cancelled():
726726
# Mark exception retrieved.
727727
fut.exception()
@@ -777,6 +777,7 @@ def _done_callback(fut):
777777
nfuts = 0
778778
nfinished = 0
779779
loop = None
780+
outer = None # bpo-46672
780781
for arg in coros_or_futures:
781782
if arg not in arg_to_fut:
782783
fut = _ensure_future(arg, loop=loop)

Lib/test/test_asyncio/test_tasks.py

+14
Original file line numberDiff line numberDiff line change
@@ -3593,6 +3593,20 @@ async def outer():
35933593
test_utils.run_briefly(self.one_loop)
35943594
self.assertIsInstance(f.exception(), RuntimeError)
35953595

3596+
def test_issue46672(self):
3597+
with mock.patch(
3598+
'asyncio.base_events.BaseEventLoop.call_exception_handler',
3599+
):
3600+
async def coro(s):
3601+
return s
3602+
c = coro('abc')
3603+
3604+
with self.assertRaises(TypeError):
3605+
self._gather(c, {})
3606+
self._run_loop(self.one_loop)
3607+
# NameError should not happen:
3608+
self.one_loop.call_exception_handler.assert_not_called()
3609+
35963610

35973611
class RunCoroutineThreadsafeTests(test_utils.TestCase):
35983612
"""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)