summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorKumar Aditya <59607654+kumaraditya303@users.noreply.github.com>2023-03-16 03:50:43 (GMT)
committerGitHub <noreply@github.com>2023-03-16 03:50:43 (GMT)
commita44553ea9f7745a1119148082edb1fb0372ac0e2 (patch)
tree6cabbca3384325fb6e0c2a821e0ae37998315241 /Lib
parent1c9f3391b916939e5ad18213e553f8d6bfbec25e (diff)
downloadcpython-a44553ea9f7745a1119148082edb1fb0372ac0e2.zip
cpython-a44553ea9f7745a1119148082edb1fb0372ac0e2.tar.gz
cpython-a44553ea9f7745a1119148082edb1fb0372ac0e2.tar.bz2
GH-100112: avoid using iterable coroutines in asyncio internally (#100128)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/asyncio/tasks.py22
-rw-r--r--Lib/test/test_asyncio/test_tasks.py15
2 files changed, 21 insertions, 16 deletions
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
index 1c20754..c90d32c 100644
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -25,7 +25,6 @@ from . import events
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
@@ -635,11 +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
- called_wrap_awaitable = False
+ should_close = True
if not coroutines.iscoroutine(coro_or_future):
if inspect.isawaitable(coro_or_future):
+ async def _wrap_awaitable(awaitable):
+ return await awaitable
+
coro_or_future = _wrap_awaitable(coro_or_future)
- called_wrap_awaitable = True
+ should_close = False
else:
raise TypeError('An asyncio.Future, a coroutine or an awaitable '
'is required')
@@ -649,23 +651,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
-@types.coroutine
-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
-
-
class _GatheringFuture(futures.Future):
"""Helper for gather().
diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py
index e533d52..5b935b5 100644
--- a/Lib/test/test_asyncio/test_tasks.py
+++ b/Lib/test/test_asyncio/test_tasks.py
@@ -8,6 +8,7 @@ import random
import re
import sys
import traceback
+import types
import unittest
from unittest import mock
from types import GenericAlias
@@ -274,6 +275,20 @@ class BaseTaskTests:
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')