From af5204498ce13a0bd9dd448b67835a1bbc74bd52 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 18 Oct 2024 10:18:28 -0600 Subject: [PATCH 1/4] Clear the default event loop policy if it might have been set. --- Lib/test/test_concurrent_futures/test_interpreter_pool.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/test/test_concurrent_futures/test_interpreter_pool.py b/Lib/test/test_concurrent_futures/test_interpreter_pool.py index 0de03c0d669399..6a65d07a4f9299 100644 --- a/Lib/test/test_concurrent_futures/test_interpreter_pool.py +++ b/Lib/test/test_concurrent_futures/test_interpreter_pool.py @@ -290,6 +290,9 @@ def setUp(self): self.executor = self.executor_type() self.addCleanup(lambda: self.executor.shutdown()) + if support.maybe_get_event_loop_policy() is None: + self.addCleanup(lambda: asyncio.set_event_loop_policy(None)) + def tearDown(self): if not self.loop.is_closed(): testasyncio_utils.run_briefly(self.loop) From b0519f9a2818277c3e40649f835a512c8ed15d47 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 18 Oct 2024 10:29:20 -0600 Subject: [PATCH 2/4] Move the cleanup to the class teardown. --- .../test_concurrent_futures/test_interpreter_pool.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_concurrent_futures/test_interpreter_pool.py b/Lib/test/test_concurrent_futures/test_interpreter_pool.py index 6a65d07a4f9299..72181ba6f9abd8 100644 --- a/Lib/test/test_concurrent_futures/test_interpreter_pool.py +++ b/Lib/test/test_concurrent_futures/test_interpreter_pool.py @@ -282,6 +282,15 @@ def test_idle_thread_reuse(self): class AsyncioTest(InterpretersMixin, testasyncio_utils.TestCase): + @classmethod + def setUpClass(cls): + policy = support.maybe_get_event_loop_policy() + assert policy is None, policy + + @classmethod + def tearDownClass(cls): + asyncio.set_event_loop_policy(None) + def setUp(self): super().setUp() self.loop = asyncio.new_event_loop() @@ -290,9 +299,6 @@ def setUp(self): self.executor = self.executor_type() self.addCleanup(lambda: self.executor.shutdown()) - if support.maybe_get_event_loop_policy() is None: - self.addCleanup(lambda: asyncio.set_event_loop_policy(None)) - def tearDown(self): if not self.loop.is_closed(): testasyncio_utils.run_briefly(self.loop) From 216cc99fbbb0439b0c52aeac911bf147478497a0 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 18 Oct 2024 14:58:58 -0600 Subject: [PATCH 3/4] Add a comment about clearing the policy. --- .../test_concurrent_futures/test_interpreter_pool.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_concurrent_futures/test_interpreter_pool.py b/Lib/test/test_concurrent_futures/test_interpreter_pool.py index 72181ba6f9abd8..109a8c2ce5b0cd 100644 --- a/Lib/test/test_concurrent_futures/test_interpreter_pool.py +++ b/Lib/test/test_concurrent_futures/test_interpreter_pool.py @@ -284,12 +284,16 @@ class AsyncioTest(InterpretersMixin, testasyncio_utils.TestCase): @classmethod def setUpClass(cls): + # Most uses of asyncio will implicitly call set_event_loop_policy() + # with the default policy if a policy hasn't been set already. + # If that happens in a test, likw here, we'll end up with a failure + # when --fail-env-changed is used. That's why the other tests that + # use asyncio are careful to set the policy back to None and why + # we're careful to do so here. We also validate that no other + # tests left a policy in place, just in case. policy = support.maybe_get_event_loop_policy() assert policy is None, policy - - @classmethod - def tearDownClass(cls): - asyncio.set_event_loop_policy(None) + cls.addClassCleanup(lambda: asyncio.set_event_loop_policy(None)) def setUp(self): super().setUp() From 0f6c50f5cf4a750144831f69d34b56c75ab3bd2c Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 18 Oct 2024 15:21:14 -0600 Subject: [PATCH 4/4] Update Lib/test/test_concurrent_futures/test_interpreter_pool.py Co-authored-by: Peter Bierma --- Lib/test/test_concurrent_futures/test_interpreter_pool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_concurrent_futures/test_interpreter_pool.py b/Lib/test/test_concurrent_futures/test_interpreter_pool.py index 109a8c2ce5b0cd..5264b1bb6e9c75 100644 --- a/Lib/test/test_concurrent_futures/test_interpreter_pool.py +++ b/Lib/test/test_concurrent_futures/test_interpreter_pool.py @@ -286,7 +286,7 @@ class AsyncioTest(InterpretersMixin, testasyncio_utils.TestCase): def setUpClass(cls): # Most uses of asyncio will implicitly call set_event_loop_policy() # with the default policy if a policy hasn't been set already. - # If that happens in a test, likw here, we'll end up with a failure + # If that happens in a test, like here, we'll end up with a failure # when --fail-env-changed is used. That's why the other tests that # use asyncio are careful to set the policy back to None and why # we're careful to do so here. We also validate that no other