commit: f581dcf8d23625bd6c8ae807575db9feee0c2345 Author: Zac Medico <zmedico <AT> gentoo <DOT> org> AuthorDate: Tue Sep 23 03:51:27 2025 +0000 Commit: Zac Medico <zmedico <AT> gentoo <DOT> org> CommitDate: Tue Sep 23 03:57:52 2025 +0000 URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=f581dcf8
tests: Remove deprecated asyncio.set_event_loop_policy Bug: https://bugs.gentoo.org/963116 Signed-off-by: Zac Medico <zmedico <AT> gentoo.org> .../futures/asyncio/test_event_loop_in_fork.py | 32 ++++++------------- .../tests/util/futures/asyncio/test_pipe_closed.py | 20 +----------- .../futures/asyncio/test_run_until_complete.py | 37 +++++++--------------- .../util/futures/asyncio/test_subprocess_exec.py | 18 ++--------- 4 files changed, 26 insertions(+), 81 deletions(-) diff --git a/lib/portage/tests/util/futures/asyncio/test_event_loop_in_fork.py b/lib/portage/tests/util/futures/asyncio/test_event_loop_in_fork.py index b9ef473048..be8aff5665 100644 --- a/lib/portage/tests/util/futures/asyncio/test_event_loop_in_fork.py +++ b/lib/portage/tests/util/futures/asyncio/test_event_loop_in_fork.py @@ -1,4 +1,4 @@ -# Copyright 2018-2020 Gentoo Authors +# Copyright 2018-2025 Gentoo Authors # Distributed under the terms of the GNU General Public License v2 import os @@ -7,12 +7,11 @@ from portage.tests import TestCase from portage.util._async.AsyncFunction import AsyncFunction from portage.util._eventloop.global_event_loop import global_event_loop from portage.util.futures import asyncio -from portage.util.futures.unix_events import DefaultEventLoopPolicy def fork_main(): loop = asyncio._wrap_loop() - # This fails with python's default event loop policy, + # Before python 3.12 this fails with python's default event loop policy, # see https://bugs.python.org/issue22087. loop.run_until_complete(asyncio.sleep(0.1, loop=loop)) loop.close() @@ -29,26 +28,15 @@ def async_main(fork_exitcode, loop=None): class EventLoopInForkTestCase(TestCase): """ - The default asyncio event loop policy does not support loops + Before python 3.12 the default asyncio event loop policy does not support loops running in forks, see https://bugs.python.org/issue22087. - Portage's DefaultEventLoopPolicy supports forks. """ def testEventLoopInForkTestCase(self): - initial_policy = asyncio.get_event_loop_policy() - if not isinstance(initial_policy, DefaultEventLoopPolicy): - asyncio.set_event_loop_policy(DefaultEventLoopPolicy()) - loop = None - try: - loop = asyncio._wrap_loop() - fork_exitcode = loop.create_future() - # Make async_main fork while the loop is running, which would - # trigger https://bugs.python.org/issue22087 with asyncio's - # default event loop policy. - loop.call_soon(async_main, fork_exitcode) - assert loop.run_until_complete(fork_exitcode) == os.EX_OK - finally: - asyncio.set_event_loop_policy(initial_policy) - if loop not in (None, global_event_loop()): - loop.close() - self.assertFalse(global_event_loop().is_closed()) + loop = global_event_loop() + fork_exitcode = loop.create_future() + # Make async_main fork while the loop is running, which would + # trigger https://bugs.python.org/issue22087 with asyncio's + # default event loop policy before python 3.12. + loop.call_soon(async_main, fork_exitcode) + assert loop.run_until_complete(fork_exitcode) == os.EX_OK diff --git a/lib/portage/tests/util/futures/asyncio/test_pipe_closed.py b/lib/portage/tests/util/futures/asyncio/test_pipe_closed.py index 50d561df61..48766576d1 100644 --- a/lib/portage/tests/util/futures/asyncio/test_pipe_closed.py +++ b/lib/portage/tests/util/futures/asyncio/test_pipe_closed.py @@ -1,4 +1,4 @@ -# Copyright 2018-2020 Gentoo Authors +# Copyright 2018-2025 Gentoo Authors # Distributed under the terms of the GNU General Public License v2 import errno @@ -9,10 +9,8 @@ import socket import tempfile from portage.tests import TestCase -from portage.util._eventloop.global_event_loop import global_event_loop from portage.util.futures import asyncio from portage.util.futures.unix_events import ( - DefaultEventLoopPolicy, _set_nonblocking, ) @@ -53,10 +51,6 @@ class ReaderPipeClosedTestCase(_PipeClosedTestCase, TestCase): """ def _do_test(self, read_end, write_end): - initial_policy = asyncio.get_event_loop_policy() - if not isinstance(initial_policy, DefaultEventLoopPolicy): - asyncio.set_event_loop_policy(DefaultEventLoopPolicy()) - loop = asyncio._wrap_loop() read_end = os.fdopen(read_end, "rb", 0) write_end = os.fdopen(write_end, "wb", 0) @@ -82,10 +76,6 @@ class ReaderPipeClosedTestCase(_PipeClosedTestCase, TestCase): loop.remove_reader(read_end.fileno()) write_end.close() read_end.close() - asyncio.set_event_loop_policy(initial_policy) - if loop not in (None, global_event_loop()): - loop.close() - self.assertFalse(global_event_loop().is_closed()) class WriterPipeClosedTestCase(_PipeClosedTestCase, TestCase): @@ -95,10 +85,6 @@ class WriterPipeClosedTestCase(_PipeClosedTestCase, TestCase): """ def _do_test(self, read_end, write_end): - initial_policy = asyncio.get_event_loop_policy() - if not isinstance(initial_policy, DefaultEventLoopPolicy): - asyncio.set_event_loop_policy(DefaultEventLoopPolicy()) - loop = asyncio._wrap_loop() read_end = os.fdopen(read_end, "rb", 0) write_end = os.fdopen(write_end, "wb", 0) @@ -146,7 +132,3 @@ class WriterPipeClosedTestCase(_PipeClosedTestCase, TestCase): loop.remove_writer(write_end.fileno()) write_end.close() read_end.close() - asyncio.set_event_loop_policy(initial_policy) - if loop not in (None, global_event_loop()): - loop.close() - self.assertFalse(global_event_loop().is_closed()) diff --git a/lib/portage/tests/util/futures/asyncio/test_run_until_complete.py b/lib/portage/tests/util/futures/asyncio/test_run_until_complete.py index 725eb0f425..755c976863 100644 --- a/lib/portage/tests/util/futures/asyncio/test_run_until_complete.py +++ b/lib/portage/tests/util/futures/asyncio/test_run_until_complete.py @@ -1,34 +1,21 @@ -# Copyright 2018 Gentoo Foundation +# Copyright 2018-2025 Gentoo Authors # Distributed under the terms of the GNU General Public License v2 from portage.tests import TestCase from portage.util._eventloop.global_event_loop import global_event_loop -from portage.util.futures import asyncio -from portage.util.futures.unix_events import DefaultEventLoopPolicy class RunUntilCompleteTestCase(TestCase): def test_add_done_callback(self): - initial_policy = asyncio.get_event_loop_policy() - if not isinstance(initial_policy, DefaultEventLoopPolicy): - asyncio.set_event_loop_policy(DefaultEventLoopPolicy()) + loop = global_event_loop() + f1 = loop.create_future() + f2 = loop.create_future() + f1.add_done_callback(f2.set_result) + loop.call_soon(lambda: f1.set_result(None)) + loop.run_until_complete(f1) + self.assertEqual(f1.done(), True) - loop = None - try: - loop = asyncio._wrap_loop() - f1 = loop.create_future() - f2 = loop.create_future() - f1.add_done_callback(f2.set_result) - loop.call_soon(lambda: f1.set_result(None)) - loop.run_until_complete(f1) - self.assertEqual(f1.done(), True) - - # This proves that done callbacks of f1 are executed before - # loop.run_until_complete(f1) returns, which is how asyncio's - # default event loop behaves. - self.assertEqual(f2.done(), True) - finally: - asyncio.set_event_loop_policy(initial_policy) - if loop not in (None, global_event_loop()): - loop.close() - self.assertFalse(global_event_loop().is_closed()) + # This proves that done callbacks of f1 are executed before + # loop.run_until_complete(f1) returns, which is how asyncio's + # default event loop behaves. + self.assertEqual(f2.done(), True) diff --git a/lib/portage/tests/util/futures/asyncio/test_subprocess_exec.py b/lib/portage/tests/util/futures/asyncio/test_subprocess_exec.py index bb284d49f2..e7ab80d8b5 100644 --- a/lib/portage/tests/util/futures/asyncio/test_subprocess_exec.py +++ b/lib/portage/tests/util/futures/asyncio/test_subprocess_exec.py @@ -1,4 +1,4 @@ -# Copyright 2018-2021 Gentoo Authors +# Copyright 2018-2025 Gentoo Authors # Distributed under the terms of the GNU General Public License v2 import os @@ -7,25 +7,13 @@ import subprocess from portage.process import find_binary from portage.tests import TestCase from portage.util._eventloop.global_event_loop import global_event_loop -from portage.util.futures import asyncio from portage.util.futures._asyncio import create_subprocess_exec -from portage.util.futures.unix_events import DefaultEventLoopPolicy class SubprocessExecTestCase(TestCase): def _run_test(self, test): - initial_policy = asyncio.get_event_loop_policy() - if not isinstance(initial_policy, DefaultEventLoopPolicy): - asyncio.set_event_loop_policy(DefaultEventLoopPolicy()) - - loop = asyncio._wrap_loop() - try: - test(loop) - finally: - asyncio.set_event_loop_policy(initial_policy) - if loop not in (None, global_event_loop()): - loop.close() - self.assertFalse(global_event_loop().is_closed()) + loop = global_event_loop() + test(loop) def testEcho(self): args_tuple = (b"hello", b"world")
