Skip to content

Commit

Permalink
bpo-46672: fix NameError in asyncio.gather if type check fails (p…
Browse files Browse the repository at this point in the history
…ythonGH-31187)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
  • Loading branch information
sobolevn and AlexWaygood committed Feb 20, 2022
1 parent e7130c2 commit 4ab8167
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,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()
Expand Down Expand Up @@ -791,6 +791,7 @@ def _done_callback(fut):
nfuts = 0
nfinished = 0
loop = None
outer = None # bpo-46672
for arg in coros_or_futures:
if arg not in arg_to_fut:
fut = _ensure_future(arg, loop=loop)
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3235,6 +3235,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."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``NameError`` in :func:`asyncio.gather` when initial type check fails.

0 comments on commit 4ab8167

Please sign in to comment.