Skip to content

Commit 7fecb6f

Browse files
inline it
1 parent a6492e9 commit 7fecb6f

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

Lib/asyncio/tasks.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -634,11 +634,17 @@ def ensure_future(coro_or_future, *, loop=None):
634634
raise ValueError('The future belongs to a different loop than '
635635
'the one specified as the loop argument')
636636
return coro_or_future
637-
called_wrap_awaitable = False
637+
should_close = False
638638
if not coroutines.iscoroutine(coro_or_future):
639639
if inspect.isawaitable(coro_or_future):
640-
coro_or_future = _wrap_awaitable(coro_or_future)
641-
called_wrap_awaitable = True
640+
async def _wrap_awaitable():
641+
@types.coroutine
642+
def wrapper():
643+
return (yield from coro_or_future.__await__())
644+
return await wrapper()
645+
646+
coro_or_future = _wrap_awaitable()
647+
should_close = True
642648
else:
643649
raise TypeError('An asyncio.Future, a coroutine or an awaitable '
644650
'is required')
@@ -648,24 +654,11 @@ def ensure_future(coro_or_future, *, loop=None):
648654
try:
649655
return loop.create_task(coro_or_future)
650656
except RuntimeError:
651-
if not called_wrap_awaitable:
657+
if should_close:
652658
coro_or_future.close()
653659
raise
654660

655661

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

0 commit comments

Comments
 (0)