From a67cc6f2449d3dd7bf436756d9e8a923e6bdd0c8 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Wed, 6 Jul 2022 05:58:06 +0000 Subject: [PATCH 1/5] always set event loop --- Lib/asyncio/runners.py | 6 +++++- Lib/unittest/async_case.py | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Lib/asyncio/runners.py b/Lib/asyncio/runners.py index f524c3b8e424f5..1aaff95e721616 100644 --- a/Lib/asyncio/runners.py +++ b/Lib/asyncio/runners.py @@ -177,7 +177,11 @@ async def main(): "asyncio.run() cannot be called from a running event loop") with Runner(debug=debug) as runner: - return runner.run(main) + try: + events.set_event_loop(runner.get_loop()) + return runner.run(main) + finally: + events.set_event_loop(None) def _cancel_all_tasks(loop): diff --git a/Lib/unittest/async_case.py b/Lib/unittest/async_case.py index a90eed98f87140..b2b765455e5ecd 100644 --- a/Lib/unittest/async_case.py +++ b/Lib/unittest/async_case.py @@ -115,11 +115,13 @@ def _callMaybeAsync(self, func, /, *args, **kwargs): def _setupAsyncioRunner(self): assert self._asyncioRunner is None, 'asyncio runner is already initialized' runner = asyncio.Runner(debug=True) + asyncio.set_event_loop(runner.get_loop()) self._asyncioRunner = runner def _tearDownAsyncioRunner(self): runner = self._asyncioRunner runner.close() + asyncio.set_event_loop(None) def run(self, result=None): self._setupAsyncioRunner() From 9576a912cacf5f27d49ebbf634be3904a748774e Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 6 Jul 2022 06:02:04 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lib/asyncio/runners.py | 8 ++++---- Lib/unittest/async_case.py | 1 - .../Library/2022-07-06-06-02-02.gh-issue-93896.vIgWGr.rst | 1 + 3 files changed, 5 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2022-07-06-06-02-02.gh-issue-93896.vIgWGr.rst diff --git a/Lib/asyncio/runners.py b/Lib/asyncio/runners.py index 1aaff95e721616..7d0d90fe31ce13 100644 --- a/Lib/asyncio/runners.py +++ b/Lib/asyncio/runners.py @@ -176,12 +176,12 @@ async def main(): raise RuntimeError( "asyncio.run() cannot be called from a running event loop") - with Runner(debug=debug) as runner: - try: + try: + with Runner(debug=debug) as runner: events.set_event_loop(runner.get_loop()) return runner.run(main) - finally: - events.set_event_loop(None) + finally: + events.set_event_loop(None) def _cancel_all_tasks(loop): diff --git a/Lib/unittest/async_case.py b/Lib/unittest/async_case.py index b2b765455e5ecd..1b3c4e5e70d46c 100644 --- a/Lib/unittest/async_case.py +++ b/Lib/unittest/async_case.py @@ -121,7 +121,6 @@ def _setupAsyncioRunner(self): def _tearDownAsyncioRunner(self): runner = self._asyncioRunner runner.close() - asyncio.set_event_loop(None) def run(self, result=None): self._setupAsyncioRunner() diff --git a/Misc/NEWS.d/next/Library/2022-07-06-06-02-02.gh-issue-93896.vIgWGr.rst b/Misc/NEWS.d/next/Library/2022-07-06-06-02-02.gh-issue-93896.vIgWGr.rst new file mode 100644 index 00000000000000..2283e14de9a9ad --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-07-06-06-02-02.gh-issue-93896.vIgWGr.rst @@ -0,0 +1 @@ +Fix :func:`asyncio.run` and :class:`unittest.IsolatedAsyncioTestCase` to always the set event loop as it was done in Python 3.10 and earlier. Patch by Kumar Aditya. From f3c1caf2fc7998514669e1e2f88e06d5d3bdd7dd Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Wed, 6 Jul 2022 07:16:32 +0000 Subject: [PATCH 3/5] wip --- Lib/asyncio/runners.py | 14 ++++++++------ Lib/unittest/async_case.py | 1 - 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Lib/asyncio/runners.py b/Lib/asyncio/runners.py index 7d0d90fe31ce13..7489a50f8e48da 100644 --- a/Lib/asyncio/runners.py +++ b/Lib/asyncio/runners.py @@ -52,6 +52,7 @@ def __init__(self, *, debug=None, loop_factory=None): self._loop = None self._context = None self._interrupt_count = 0 + self._set_event_loop = False def __enter__(self): self._lazy_init() @@ -70,6 +71,8 @@ def close(self): loop.run_until_complete(loop.shutdown_asyncgens()) loop.run_until_complete(loop.shutdown_default_executor()) finally: + if self._set_event_loop: + events.set_event_loop(None) loop.close() self._loop = None self._state = _State.CLOSED @@ -111,6 +114,8 @@ def run(self, coro, *, context=None): self._interrupt_count = 0 try: + if self._set_event_loop: + events.set_event_loop(self._loop) return self._loop.run_until_complete(task) except exceptions.CancelledError: if self._interrupt_count > 0 and task.uncancel() == 0: @@ -130,6 +135,7 @@ def _lazy_init(self): return if self._loop_factory is None: self._loop = events.new_event_loop() + self._set_event_loop = True else: self._loop = self._loop_factory() if self._debug is not None: @@ -176,12 +182,8 @@ async def main(): raise RuntimeError( "asyncio.run() cannot be called from a running event loop") - try: - with Runner(debug=debug) as runner: - events.set_event_loop(runner.get_loop()) - return runner.run(main) - finally: - events.set_event_loop(None) + with Runner(debug=debug) as runner: + return runner.run(main) def _cancel_all_tasks(loop): diff --git a/Lib/unittest/async_case.py b/Lib/unittest/async_case.py index 1b3c4e5e70d46c..a90eed98f87140 100644 --- a/Lib/unittest/async_case.py +++ b/Lib/unittest/async_case.py @@ -115,7 +115,6 @@ def _callMaybeAsync(self, func, /, *args, **kwargs): def _setupAsyncioRunner(self): assert self._asyncioRunner is None, 'asyncio runner is already initialized' runner = asyncio.Runner(debug=True) - asyncio.set_event_loop(runner.get_loop()) self._asyncioRunner = runner def _tearDownAsyncioRunner(self): From d01fd0137dbf914d4548b8a27f5f2c8d1d4addac Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Wed, 6 Jul 2022 08:07:29 +0000 Subject: [PATCH 4/5] add a test --- Lib/test/test_asyncio/test_runners.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Lib/test/test_asyncio/test_runners.py b/Lib/test/test_asyncio/test_runners.py index 7780a5f15e1b44..0fdb2f454563a3 100644 --- a/Lib/test/test_asyncio/test_runners.py +++ b/Lib/test/test_asyncio/test_runners.py @@ -1,6 +1,7 @@ import _thread import asyncio import contextvars +from email import policy import re import signal import threading @@ -198,6 +199,18 @@ async def main(): self.assertIsNone(spinner.ag_frame) self.assertFalse(spinner.ag_running) + def test_asyncio_run_set_event_loop(self): + #See https://github.com/python/cpython/issues/93896 + + async def main(): + await asyncio.sleep(0) + return 42 + + policy = asyncio.get_event_loop_policy() + policy.set_event_loop = mock.Mock() + asyncio.run(main()) + self.assertTrue(policy.set_event_loop.called) + class RunnerTests(BaseTest): From 16be2ec4abba51580b5d42714a537cbbad928a18 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Wed, 6 Jul 2022 08:12:13 +0000 Subject: [PATCH 5/5] fix import --- Lib/test/test_asyncio/test_runners.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_asyncio/test_runners.py b/Lib/test/test_asyncio/test_runners.py index 0fdb2f454563a3..1f6a4983690cdc 100644 --- a/Lib/test/test_asyncio/test_runners.py +++ b/Lib/test/test_asyncio/test_runners.py @@ -1,7 +1,6 @@ import _thread import asyncio import contextvars -from email import policy import re import signal import threading