8000 GH-100112: avoid using iterable coroutines in asyncio internally (#1… · python/cpython@a44553e · GitHub
[go: up one dir, main page]

Skip to content

Commit a44553e

Browse files
GH-100112: avoid using iterable coroutines in asyncio internally (#100128)
1 parent 1c9f339 commit a44553e

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

Lib/asyncio/tasks.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from . import exceptions
2626
from . import futures
2727
from . import timeouts
28-
from .coroutines import _is_coroutine
2928

3029
# Helper to generate new task names
3130
# This uses itertools.count() instead of a "+= 1" operation because the latter
@@ -635,11 +634,14 @@ def ensure_future(coro_or_future, *, loop=None):
635634
raise ValueError('The future belongs to a different loop than '
636635
'the one specified as the loop argument')
637636
return coro_or_future
638-
called_wrap_awaitable = False
637+
should_close = True
639638
if not coroutines.iscoroutine(coro_or_future):
640639
if inspect.isawaitable(coro_or_future):
640+
async def _wrap_awaitable(awaitable):
641+
return await awaitable
642+
641643
coro_or_future = _wrap_awaitable(coro_or_future)
642-
called_wrap_awaitable = True
644+
should_close = False
643645
else:
644646
raise TypeError('An asyncio.Future, a coroutine or an awaitable '
645647
'is required')
@@ -649,23 +651,11 @@ def ensure_future(coro_or_future, *, loop=None):
649651
try:
650652
return loop.create_task(coro_or_future)
651653
except RuntimeError:
652-
if not called_wrap_awaitable:
654+
if should_close:
653655
coro_or_future.close()
654656
raise
655657

656658

657-
@types.coroutine
658-
def _wrap_awaitable(awaitable):
659-
"""Helper for asyncio.ensure_future().
660-
661-
Wraps awaitable (an object with __await__) into a coroutine
662-
that will later be wrapped in a Task by ensure_future().
663-
"""
664-
return (yield from awaitable.__await__())
665-
666-
_wrap_awaitable._is_coroutine = _is_coroutine
667-
668-
669659
class _GatheringFuture(futures.Future):
670660
"""Helper for gather().
671661

Lib/test/test_asyncio/test_tasks.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import re
99
import sys
1010
import traceback
11+
import types
1112
import unittest
1213
from unittest import mock
1314
from types import GenericAlias
@@ -274,6 +275,20 @@ async def coro():
274275
loop.run_until_complete(fut)
275276
self.assertEqual(fut.result(), 'ok')
276277

278+
def test_ensure_future_task_awaitable(self):
279+
class Aw:
280+
def __await__(self):
281+
return asyncio.sleep(0, result='ok').__await__()
282+
283+
loop = asyncio.new_event_loop()
284+
self.set_event_loop(loop)
285+
task = asyncio.ensure_future(Aw(), loop=loop)
286+
loop.run_until_complete(task)
287+
self.assertTrue(task.done())
288+
self.assertEqual(task.result(), 'ok')
289+
self.assertIsInstance(task.get_coro(), types.CoroutineType)
290+
loop.close()
291+
277292
def test_ensure_future_neither(self):
278293
with self.assertRaises(TypeError):
279294
asyncio.ensure_future('ok')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:meth:`asyncio.Task.get_coro` now always returns a coroutine when wrapping an awaitable object. Patch by Kumar Aditya.

0 commit comments

Comments
 (0)
0