Skip to content

Commit 4ab8167

Browse files
bpo-46672: fix NameError in asyncio.gather if type check fails (pythonGH-31187)
Co-authored-by: Alex Waygood <[email protected]>
1 parent e7130c2 commit 4ab8167

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
@@ -735,7 +735,7 @@ def _done_callback(fut):
735735
nonlocal nfinished
736736
nfinished += 1
737737

738-
if outer.done():
738+
if outer is None or outer.done():
739739
if not fut.cancelled():
740740
# Mark exception retrieved.
741741
fut.exception()
@@ -791,6 +791,7 @@ def _done_callback(fut):
791791
nfuts = 0
792792
nfinished = 0
793793
loop = None
794+
outer = None # bpo-46672
794795
for arg in coros_or_futures:
795796
if arg not in arg_to_fut:
796797
fut = _ensure_future(arg, loop=loop)

Lib/test/test_asyncio/test_tasks.py

+14
Original file line numberDiff line numberDiff line change
@@ -3235,6 +3235,20 @@ async def outer():
32353235
test_utils.run_briefly(self.one_loop)
32363236
self.assertIsInstance(f.exception(), RuntimeError)
32373237

3238+
def test_issue46672(self):
3239+
with mock.patch(
3240+
'asyncio.base_events.BaseEventLoop.call_exception_handler',
3241+
):
3242+
async def coro(s):
3243+
return s
3244+
c = coro('abc')
3245+
3246+
with self.assertRaises(TypeError):
3247+
self._gather(c, {})
3248+
self._run_loop(self.one_loop)
3249+
# NameError should not happen:
3250+
self.one_loop.call_exception_handler.assert_not_called()
3251+
32383252

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