[Python-checkins] GH-93896: AAlways set event loop in asyncio.run and IsolatedAsyncioTestCase (GH-94593)
miss-islington
webhook-mailer at python.org
Wed Jul 6 11:48:52 EDT 2022
https://github.com/python/cpython/commit/0187b601063c09670c2f65a68e4c42231b1446e1
commit: 0187b601063c09670c2f65a68e4c42231b1446e1
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-07-06T08:48:37-07:00
summary:
GH-93896: AAlways set event loop in asyncio.run and IsolatedAsyncioTestCase (GH-94593)
(cherry picked from commit 14fea6b4d25658bc00adbb97dd40ea3d3e6843ad)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303 at users.noreply.github.com>
files:
A Misc/NEWS.d/next/Library/2022-07-06-06-02-02.gh-issue-93896.vIgWGr.rst
M Lib/asyncio/runners.py
M Lib/test/test_asyncio/test_runners.py
diff --git a/Lib/asyncio/runners.py b/Lib/asyncio/runners.py
index 065691bed9928..c58b52ed862e8 100644
--- a/Lib/asyncio/runners.py
+++ b/Lib/asyncio/runners.py
@@ -53,6 +53,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()
@@ -71,6 +72,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
@@ -112,6 +115,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:
@@ -131,6 +136,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:
diff --git a/Lib/test/test_asyncio/test_runners.py b/Lib/test/test_asyncio/test_runners.py
index 3e288d1b62ade..a27942c4f53eb 100644
--- a/Lib/test/test_asyncio/test_runners.py
+++ b/Lib/test/test_asyncio/test_runners.py
@@ -199,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):
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 0000000000000..2283e14de9a9a
--- /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.
More information about the Python-checkins
mailing list