Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-46672: fix NameError in asyncio.gather if type check fails #31187

Merged
merged 2 commits into from
Feb 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ def _ensure_future(coro_or_future, *, loop=None):
loop = events._get_event_loop(stacklevel=4)
try:
return loop.create_task(coro_or_future)
except RuntimeError:
except RuntimeError:
JelleZijlstra marked this conversation as resolved.
Show resolved Hide resolved
if not called_wrap_awaitable:
coro_or_future.close()
raise
Expand Down Expand Up @@ -721,7 +721,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 @@ -777,6 +777,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 @@ -3190,6 +3190,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 @@
Fixes ``NameError`` in :func:`asyncio.gather` when initial type check fails.
sobolevn marked this conversation as resolved.
Show resolved Hide resolved