Skip to content

Commit a882241

Browse files
committed
Avoid duplicating code from ensure_future in Runner.run
1 parent 0509c55 commit a882241

File tree

3 files changed

+10
-22
lines changed

3 files changed

+10
-22
lines changed

Lib/asyncio/runners.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
import contextvars
44
import enum
55
import functools
6-
import inspect
76
import threading
87
import signal
98
from . import coroutines
109
from . import events
1110
from . import exceptions
12-
from . import futures
1311
from . import tasks
1412
from . import constants
1513

@@ -104,20 +102,7 @@ def run(self, coro, *, context=None):
104102
if context is None:
105103
context = self._context
106104

107-
if futures.isfuture(coro):
108-
# This covers the argument being an asyncio.Future or asyncio.Task
109-
task = coro
110-
elif coroutines.iscoroutine(coro):
111-
task = self._loop.create_task(coro, context=context)
112-
elif inspect.isawaitable(coro):
113-
async def _wrap_awaitable(awaitable):
114-
return await awaitable
115-
116-
task = _wrap_awaitable(coro)
117-
else:
118-
raise ValueError("An asyncio Future, asyncio.Task, "
119-
"awaitable, or coroutine was expected, "
120-
"got {!r}".format(coro))
105+
task = tasks.ensure_future(coro, loop=self._loop, context=context)
121106

122107
if (threading.current_thread() is threading.main_thread()
123108
and signal.getsignal(signal.SIGINT) is signal.default_int_handler

Lib/asyncio/tasks.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ async def sleep(delay, result=None):
720720
h.cancel()
721721

722722

723-
def ensure_future(coro_or_future, *, loop=None):
723+
def ensure_future(coro_or_future, *, loop=None, context=None):
724724
"""Wrap a coroutine or an awaitable in a future.
725725
726726
If the argument is a Future, it is returned directly.
@@ -745,7 +745,10 @@ async def _wrap_awaitable(awaitable):
745745
if loop is None:
746746
loop = events.get_event_loop()
747747
try:
748-
return loop.create_task(coro_or_future)
748+
if context is None:
749+
return loop.create_task(coro_or_future)
750+
else:
751+
return loop.create_task(coro_or_future, context=context)
749752
except RuntimeError:
750753
if should_close:
751754
coro_or_future.close()

Lib/test/test_asyncio/test_runners.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ async def main():
9393
def test_asyncio_run_only_coro(self):
9494
for o in {1, lambda: None}:
9595
with self.subTest(obj=o), \
96-
self.assertRaisesRegex(ValueError,
97-
'coroutine was expected'):
96+
self.assertRaisesRegex(TypeError,
97+
'an awaitable is required'):
9898
asyncio.run(o)
9999

100100
def test_asyncio_run_debug(self):
@@ -319,8 +319,8 @@ async def f():
319319
def test_run_non_coro(self):
320320
with asyncio.Runner() as runner:
321321
with self.assertRaisesRegex(
322-
ValueError,
323-
"coroutine was expected"
322+
TypeError,
323+
"an awaitable is required"
324324
):
325325
runner.run(123)
326326

0 commit comments

Comments
 (0)