From a6492e9149ca871ecf1ae7738828c48aeb43d90c Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Fri, 9 Dec 2022 09:28:48 +0530 Subject: [PATCH 1/5] add wrapper --- Lib/asyncio/tasks.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 1c20754b839b69..3f76332d45d6ce 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -25,7 +25,6 @@ from . import exceptions from . import futures from . import timeouts -from .coroutines import _is_coroutine # Helper to generate new task names # This uses itertools.count() instead of a "+= 1" operation because the latter @@ -654,17 +653,18 @@ def ensure_future(coro_or_future, *, loop=None): raise -@types.coroutine -def _wrap_awaitable(awaitable): +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(). """ - return (yield from awaitable.__await__()) -_wrap_awaitable._is_coroutine = _is_coroutine + @types.coroutine + def wrapper(awaitable): + return (yield from awaitable.__await__()) + return await wrapper(awaitable) class _GatheringFuture(futures.Future): """Helper for gather(). From 7fecb6f253ac2706a099764b770ee61f3a590352 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Fri, 10 Mar 2023 09:57:01 +0000 Subject: [PATCH 2/5] inline it --- Lib/asyncio/tasks.py | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 3f76332d45d6ce..69d16ce199370f 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -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') @@ -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(). From 89524c5ba1115f20411563d1c59e255385dc7c7e Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Fri, 10 Mar 2023 10:05:35 +0000 Subject: [PATCH 3/5] add test --- Lib/asyncio/tasks.py | 6 +++--- Lib/test/test_asyncio/test_tasks.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 69d16ce199370f..2c90dc2f3d39e2 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -637,13 +637,13 @@ def ensure_future(coro_or_future, *, loop=None): should_close = False if not coroutines.iscoroutine(coro_or_future): if inspect.isawaitable(coro_or_future): - async def _wrap_awaitable(): + async def _wrap_awaitable(awaitable): @types.coroutine def wrapper(): - return (yield from coro_or_future.__await__()) + return (yield from awaitable.__await__()) return await wrapper() - coro_or_future = _wrap_awaitable() + coro_or_future = _wrap_awaitable(coro_or_future) should_close = True else: raise TypeError('An asyncio.Future, a coroutine or an awaitable ' diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index e533d5273e9f38..5b935b526541a1 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -8,6 +8,7 @@ import re import sys import traceback +import types import unittest from unittest import mock from types import GenericAlias @@ -274,6 +275,20 @@ async def coro(): loop.run_until_complete(fut) self.assertEqual(fut.result(), 'ok') + def test_ensure_future_task_awaitable(self): + class Aw: + def __await__(self): + return asyncio.sleep(0, result='ok').__await__() + + loop = asyncio.new_event_loop() + self.set_event_loop(loop) + task = asyncio.ensure_future(Aw(), loop=loop) + loop.run_until_complete(task) + self.assertTrue(task.done()) + self.assertEqual(task.result(), 'ok') + self.assertIsInstance(task.get_coro(), types.CoroutineType) + loop.close() + def test_ensure_future_neither(self): with self.assertRaises(TypeError): asyncio.ensure_future('ok') From 046458fb9f93f1b41fe2038f6bee82857d770894 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Fri, 10 Mar 2023 13:12:56 +0000 Subject: [PATCH 4/5] fix tests --- Lib/asyncio/tasks.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 2c90dc2f3d39e2..c90d32c97add78 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -634,17 +634,14 @@ 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 - should_close = False + should_close = True if not coroutines.iscoroutine(coro_or_future): if inspect.isawaitable(coro_or_future): async def _wrap_awaitable(awaitable): - @types.coroutine - def wrapper(): - return (yield from awaitable.__await__()) - return await wrapper() + return await awaitable coro_or_future = _wrap_awaitable(coro_or_future) - should_close = True + should_close = False else: raise TypeError('An asyncio.Future, a coroutine or an awaitable ' 'is required') From dddb7bddc3928b07bc74e98be7343162803bcebe Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 10 Mar 2023 13:51:24 +0000 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-03-10-13-51-21.gh-issue-100112.VHh4mw.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-03-10-13-51-21.gh-issue-100112.VHh4mw.rst diff --git a/Misc/NEWS.d/next/Library/2023-03-10-13-51-21.gh-issue-100112.VHh4mw.rst b/Misc/NEWS.d/next/Library/2023-03-10-13-51-21.gh-issue-100112.VHh4mw.rst new file mode 100644 index 00000000000000..eff77c40e30c48 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-03-10-13-51-21.gh-issue-100112.VHh4mw.rst @@ -0,0 +1 @@ +:meth:`asyncio.Task.get_coro` now always returns a coroutine when wrapping an awaitable object. Patch by Kumar Aditya.