Browse Source

bpo-46672: fix `NameError` in `asyncio.gather` if type check fails (GH-31187) (GH-31440)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
(cherry picked from commit 4ab8167b9c)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
pull/31451/head
Miss Islington (bot) 4 years ago
committed by GitHub
parent
commit
f1916cde24
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      Lib/asyncio/tasks.py
  2. 14
      Lib/test/test_asyncio/test_tasks.py
  3. 1
      Misc/NEWS.d/next/Library/2022-02-07-13-15-16.bpo-46672.4swIjx.rst

3
Lib/asyncio/tasks.py

@ -721,7 +721,7 @@ def gather(*coros_or_futures, return_exceptions=False):
nonlocal nfinished
nfinished += 1
if outer.done():
if outer is None or outer.done():
if not fut.cancelled():
# Mark exception retrieved.
fut.exception()
@ -777,6 +777,7 @@ def gather(*coros_or_futures, return_exceptions=False):
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)

14
Lib/test/test_asyncio/test_tasks.py

@ -3593,6 +3593,20 @@ class CoroutineGatherTests(GatherTestsBase, test_utils.TestCase):
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."""

1
Misc/NEWS.d/next/Library/2022-02-07-13-15-16.bpo-46672.4swIjx.rst

@ -0,0 +1 @@
Fix ``NameError`` in :func:`asyncio.gather` when initial type check fails.
Loading…
Cancel
Save