bpo-33505: Optimize asyncio.ensure_future by reordering if conditions… · python/cpython@e549c4b

Original file line numberDiff line numberDiff line change

@@ -542,17 +542,17 @@ def ensure_future(coro_or_future, *, loop=None):

542542
543543

If the argument is a Future, it is returned directly.

544544

"""

545-

if futures.isfuture(coro_or_future):

546-

if loop is not None and loop is not futures._get_loop(coro_or_future):

547-

raise ValueError('loop argument must agree with Future')

548-

return coro_or_future

549-

elif coroutines.iscoroutine(coro_or_future):

545+

if coroutines.iscoroutine(coro_or_future):

550546

if loop is None:

551547

loop = events.get_event_loop()

552548

task = loop.create_task(coro_or_future)

553549

if task._source_traceback:

554550

del task._source_traceback[-1]

555551

return task

552+

elif futures.isfuture(coro_or_future):

553+

if loop is not None and loop is not futures._get_loop(coro_or_future):

554+

raise ValueError('loop argument must agree with Future')

555+

return coro_or_future

556556

elif inspect.isawaitable(coro_or_future):

557557

return ensure_future(_wrap_awaitable(coro_or_future), loop=loop)

558558

else: