Skip to content

Commit

Permalink
inline it
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaraditya303 committed Mar 10, 2023
1 parent a6492e9 commit 7fecb6f
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,11 +634,17 @@ def ensure_future(coro_or_future, *, loop=None):
raise ValueError('The future belongs to a different loop than '
'the one specified as the loop argument')
return coro_or_future
called_wrap_awaitable = False
should_close = False
if not coroutines.iscoroutine(coro_or_future):
if inspect.isawaitable(coro_or_future):
coro_or_future = _wrap_awaitable(coro_or_future)
called_wrap_awaitable = True
async def _wrap_awaitable():
@types.coroutine
def wrapper():
return (yield from coro_or_future.__await__())
return await wrapper()

coro_or_future = _wrap_awaitable()
should_close = True
else:
raise TypeError('An asyncio.Future, a coroutine or an awaitable '
'is required')
Expand All @@ -648,24 +654,11 @@ def ensure_future(coro_or_future, *, loop=None):
try:
return loop.create_task(coro_or_future)
except RuntimeError:
if not called_wrap_awaitable:
if should_close:
coro_or_future.close()
raise


async def _wrap_awaitable(awaitable):
"""Helper for asyncio.ensure_future().
Wraps awaitable (an object with __await__) into a coroutine
that will later be wrapped in a Task by ensure_future().
"""

@types.coroutine
def wrapper(awaitable):
return (yield from awaitable.__await__())

return await wrapper(awaitable)

class _GatheringFuture(futures.Future):
"""Helper for gather().
Expand Down

0 comments on commit 7fecb6f

Please sign in to comment.