Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Lib/asyncio/base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,12 +419,18 @@ def create_future(self):
"""Create a Future object attached to the loop."""
return futures.Future(loop=self)

def create_task(self, coro, *, name=None):
def create_task(self, coro, used_wrap_await=False, name=None):
"""Schedule a coroutine object.

Return a task object.
"""
self._check_closed()
try:
self._check_closed()
except RuntimeError:
if used_wrap_await:
coro.close()
raise

if self._task_factory is None:
task = tasks.Task(coro, loop=self, name=name)
if task._source_traceback:
Expand Down
2 changes: 1 addition & 1 deletion Lib/asyncio/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def create_future(self):

# Method scheduling a coroutine object: create a task.

def create_task(self, coro, *, name=None):
def create_task(self, coro, used_wrap_await=False, name=None):
raise NotImplementedError

# Methods for interacting with threads.
Expand Down
8 changes: 6 additions & 2 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def __wakeup(self, future):
Task = _CTask = _asyncio.Task


def create_task(coro, *, name=None):
def create_task(coro, used_wrap_await=False, name=None):
"""Schedule the execution of a coroutine object in a spawn task.

Return a Task object.
Expand Down Expand Up @@ -612,10 +612,13 @@ def ensure_future(coro_or_future, *, loop=None):

If the argument is a Future, it is returned directly.
"""
if loop is None:
coro_or_future.close()
return _ensure_future(coro_or_future, loop=loop)


def _ensure_future(coro_or_future, *, loop=None):
wrap_await = False
if futures.isfuture(coro_or_future):
if loop is not None and loop is not futures._get_loop(coro_or_future):
raise ValueError('The future belongs to a different loop than '
Expand All @@ -625,13 +628,14 @@ def _ensure_future(coro_or_future, *, loop=None):
if not coroutines.iscoroutine(coro_or_future):
if inspect.isawaitable(coro_or_future):
coro_or_future = _wrap_awaitable(coro_or_future)
wrap_await = True
else:
raise TypeError('An asyncio.Future, a coroutine or an awaitable '
'is required')

if loop is None:
loop = events._get_event_loop(stacklevel=4)
return loop.create_task(coro_or_future)
return loop.create_task(coro_or_future, used_wrap_await=wrap_await)


@types.coroutine
Expand Down