(coro_or_future, *, loop=None)
| 660 | |
| 661 | |
| 662 | def _ensure_future(coro_or_future, *, loop=None): |
| 663 | if futures.isfuture(coro_or_future): |
| 664 | if loop is not None and loop is not futures._get_loop(coro_or_future): |
| 665 | raise ValueError('The future belongs to a different loop than ' |
| 666 | 'the one specified as the loop argument') |
| 667 | return coro_or_future |
| 668 | called_wrap_awaitable = False |
| 669 | if not coroutines.iscoroutine(coro_or_future): |
| 670 | if inspect.isawaitable(coro_or_future): |
| 671 | coro_or_future = _wrap_awaitable(coro_or_future) |
| 672 | called_wrap_awaitable = True |
| 673 | else: |
| 674 | raise TypeError('An asyncio.Future, a coroutine or an awaitable ' |
| 675 | 'is required') |
| 676 | |
| 677 | if loop is None: |
| 678 | loop = events._get_event_loop(stacklevel=4) |
| 679 | try: |
| 680 | return loop.create_task(coro_or_future) |
| 681 | except RuntimeError: |
| 682 | if not called_wrap_awaitable: |
| 683 | coro_or_future.close() |
| 684 | raise |
| 685 | |
| 686 | |
| 687 | @types.coroutine |
no test coverage detected