Skip to content

Commit 89524c5

Browse files
add test
1 parent 7fecb6f commit 89524c5

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

Lib/asyncio/tasks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -637,13 +637,13 @@ def ensure_future(coro_or_future, *, loop=None):
637637
should_close = False
638638
if not coroutines.iscoroutine(coro_or_future):
639639
if inspect.isawaitable(coro_or_future):
640-
async def _wrap_awaitable():
640+
async def _wrap_awaitable(awaitable):
641641
@types.coroutine
642642
def wrapper():
643-
return (yield from coro_or_future.__await__())
643+
return (yield from awaitable.__await__())
644644
return await wrapper()
645645

646-
coro_or_future = _wrap_awaitable()
646+
coro_or_future = _wrap_awaitable(coro_or_future)
647647
should_close = True
648648
else:
649649
raise TypeError('An asyncio.Future, a coroutine or an awaitable '

Lib/test/test_asyncio/test_tasks.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import re
99
import sys
1010
import traceback
11+
import types
1112
import unittest
1213
from unittest import mock
1314
from types import GenericAlias
@@ -274,6 +275,20 @@ async def coro():
274275
loop.run_until_complete(fut)
275276
self.assertEqual(fut.result(), 'ok')
276277

278+
def test_ensure_future_task_awaitable(self):
279+
class Aw:
280+
def __await__(self):
281+
return asyncio.sleep(0, result='ok').__await__()
282+
283+
loop = asyncio.new_event_loop()
284+
self.set_event_loop(loop)
285+
task = asyncio.ensure_future(Aw(), loop=loop)
286+
loop.run_until_complete(task)
287+
self.assertTrue(task.done())
288+
self.assertEqual(task.result(), 'ok')
289+
self.assertIsInstance(task.get_coro(), types.CoroutineType)
290+
loop.close()
291+
277292
def test_ensure_future_neither(self):
278293
with self.assertRaises(TypeError):
279294
asyncio.ensure_future('ok')

0 commit comments

Comments
 (0)