| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -621,17 +621,23 @@ def _ensure_future(coro_or_future, *, loop=None): | |||
| 621 | 621 | raise ValueError('The future belongs to a different loop than ' | |
| 622 | 622 | 'the one specified as the loop argument') | |
| 623 | 623 | return coro_or_future | |
| 624 | - | ||
| 624 | + called_wrap_awaitable = False | ||
| 625 | 625 | if not coroutines.iscoroutine(coro_or_future): | |
| 626 | 626 | if inspect.isawaitable(coro_or_future): | |
| 627 | 627 | coro_or_future = _wrap_awaitable(coro_or_future) | |
| 628 | + called_wrap_awaitable = True | ||
| 628 | 629 | else: | |
| 629 | 630 | raise TypeError('An asyncio.Future, a coroutine or an awaitable ' | |
| 630 | 631 | 'is required') | |
| 631 | 632 | ||
| 632 | 633 | if loop is None: | |
| 633 | 634 | loop = events._get_event_loop(stacklevel=4) | |
| 634 | - return loop.create_task(coro_or_future) | ||
| 635 | + try: | ||
| 636 | + return loop.create_task(coro_or_future) | ||
| 637 | + except RuntimeError: | ||
| 638 | + if not called_wrap_awaitable: | ||
| 639 | + coro_or_future.close() | ||
| 640 | + raise | ||
| 635 | 641 | ||
| 636 | 642 | ||
| 637 | 643 | @types.coroutine | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,7 +18,7 @@ | |||
| 18 | 18 | from test.support.script_helper import assert_python_ok | |
| 19 | 19 | from test.support import os_helper | |
| 20 | 20 | from test.support import socket_helper | |
| 21 | - | ||
| 21 | + import warnings | ||
| 22 | 22 | ||
| 23 | 23 | MOCK_ANY = mock.ANY | |
| 24 | 24 | ||
@@ -796,6 +796,17 @@ def create_task(self, coro): | |||
| 796 | 796 | task._log_destroy_pending = False | |
| 797 | 797 | coro.close() | |
| 798 | 798 | ||
| 799 | + def test_create_task_error_closes_coro(self): | ||
| 800 | + async def test(): | ||
| 801 | + pass | ||
| 802 | + loop = asyncio.new_event_loop() | ||
| 803 | + loop.close() | ||
| 804 | + with warnings.catch_warnings(record=True) as w: | ||
| 805 | + with self.assertRaises(RuntimeError): | ||
| 806 | + asyncio.ensure_future(test(), loop=loop) | ||
| 807 | + self.assertEqual(len(w), 0) | ||
| 808 | + | ||
| 809 | + | ||
| 799 | 810 | def test_create_named_task_with_default_factory(self): | |
| 800 | 811 | async def test(): | |
| 801 | 812 | pass | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + Fixed case where failing :func:`asyncio.ensure_future` did not close the coroutine. Patch by Kumar Aditya. | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments