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] 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')