diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2022-07-29 12:38:02 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-29 12:38:02 (GMT) |
commit | efeda8b4a1fd8f1c510311c40e46d5fad65512a0 (patch) | |
tree | fd69a9f3c7fd20d380ea5177baf4332844b57a40 /Lib/test/test_asyncio/test_runners.py | |
parent | c26470f0cc900dccf2a7ce4a915fc36596cdf289 (diff) | |
download | cpython-efeda8b4a1fd8f1c510311c40e46d5fad65512a0.zip cpython-efeda8b4a1fd8f1c510311c40e46d5fad65512a0.tar.gz cpython-efeda8b4a1fd8f1c510311c40e46d5fad65512a0.tar.bz2 |
GH-95097: fix `asyncio.run` for tasks without `uncancel` method (GH-95211) (GH-95387)
(cherry picked from commit 54f48844d18bc6fb98849f15a2fc08f92ad240ea)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Co-authored-by: Thomas Grainger <tagrain@gmail.com>
Diffstat (limited to 'Lib/test/test_asyncio/test_runners.py')
-rw-r--r-- | Lib/test/test_asyncio/test_runners.py | 51 |
1 files changed, 49 insertions, 2 deletions
diff --git a/Lib/test/test_asyncio/test_runners.py b/Lib/test/test_asyncio/test_runners.py index a27942c..5e1db23 100644 --- a/Lib/test/test_asyncio/test_runners.py +++ b/Lib/test/test_asyncio/test_runners.py @@ -6,10 +6,9 @@ import re import signal import threading import unittest - +from test.test_asyncio import utils as test_utils from unittest import mock from unittest.mock import patch -from test.test_asyncio import utils as test_utils def tearDownModule(): @@ -211,6 +210,54 @@ class RunTests(BaseTest): asyncio.run(main()) self.assertTrue(policy.set_event_loop.called) + def test_asyncio_run_without_uncancel(self): + # See https://github.com/python/cpython/issues/95097 + class Task: + def __init__(self, loop, coro, **kwargs): + self._task = asyncio.Task(coro, loop=loop, **kwargs) + + def cancel(self, *args, **kwargs): + return self._task.cancel(*args, **kwargs) + + def add_done_callback(self, *args, **kwargs): + return self._task.add_done_callback(*args, **kwargs) + + def remove_done_callback(self, *args, **kwargs): + return self._task.remove_done_callback(*args, **kwargs) + + @property + def _asyncio_future_blocking(self): + return self._task._asyncio_future_blocking + + def result(self, *args, **kwargs): + return self._task.result(*args, **kwargs) + + def done(self, *args, **kwargs): + return self._task.done(*args, **kwargs) + + def cancelled(self, *args, **kwargs): + return self._task.cancelled(*args, **kwargs) + + def exception(self, *args, **kwargs): + return self._task.exception(*args, **kwargs) + + def get_loop(self, *args, **kwargs): + return self._task.get_loop(*args, **kwargs) + + + async def main(): + interrupt_self() + await asyncio.Event().wait() + + def new_event_loop(): + loop = self.new_loop() + loop.set_task_factory(Task) + return loop + + asyncio.set_event_loop_policy(TestPolicy(new_event_loop)) + with self.assertRaises(asyncio.CancelledError): + asyncio.run(main()) + class RunnerTests(BaseTest): |