summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_asyncio
diff options
context:
space:
mode:
authorRon Frederick <ronf@timeheart.net>2024-09-26 06:15:08 (GMT)
committerGitHub <noreply@github.com>2024-09-26 06:15:08 (GMT)
commit1229cb8c1412d37cf3206eab407f03e21d602cbd (patch)
tree9ff9363d0b2c15026c225cb16e6305fe8cd9028c /Lib/test/test_asyncio
parent46f5cbca4c37c57f718d3de0d7f7ddfc44298535 (diff)
downloadcpython-1229cb8c1412d37cf3206eab407f03e21d602cbd.zip
cpython-1229cb8c1412d37cf3206eab407f03e21d602cbd.tar.gz
cpython-1229cb8c1412d37cf3206eab407f03e21d602cbd.tar.bz2
gh-120284: Enhance `asyncio.run` to accept awaitable objects (#120566)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Diffstat (limited to 'Lib/test/test_asyncio')
-rw-r--r--Lib/test/test_asyncio/test_runners.py29
1 files changed, 19 insertions, 10 deletions
diff --git a/Lib/test/test_asyncio/test_runners.py b/Lib/test/test_asyncio/test_runners.py
index 266f057..45f70d0 100644
--- a/Lib/test/test_asyncio/test_runners.py
+++ b/Lib/test/test_asyncio/test_runners.py
@@ -93,8 +93,8 @@ class RunTests(BaseTest):
def test_asyncio_run_only_coro(self):
for o in {1, lambda: None}:
with self.subTest(obj=o), \
- self.assertRaisesRegex(ValueError,
- 'a coroutine was expected'):
+ self.assertRaisesRegex(TypeError,
+ 'an awaitable is required'):
asyncio.run(o)
def test_asyncio_run_debug(self):
@@ -319,19 +319,28 @@ class RunnerTests(BaseTest):
def test_run_non_coro(self):
with asyncio.Runner() as runner:
with self.assertRaisesRegex(
- ValueError,
- "a coroutine was expected"
+ TypeError,
+ "an awaitable is required"
):
runner.run(123)
def test_run_future(self):
with asyncio.Runner() as runner:
- with self.assertRaisesRegex(
- ValueError,
- "a coroutine was expected"
- ):
- fut = runner.get_loop().create_future()
- runner.run(fut)
+ fut = runner.get_loop().create_future()
+ fut.set_result('done')
+ self.assertEqual('done', runner.run(fut))
+
+ def test_run_awaitable(self):
+ class MyAwaitable:
+ def __await__(self):
+ return self.run().__await__()
+
+ @staticmethod
+ async def run():
+ return 'done'
+
+ with asyncio.Runner() as runner:
+ self.assertEqual('done', runner.run(MyAwaitable()))
def test_explicit_close(self):
runner = asyncio.Runner()