diff --git a/Doc/library/asyncio-policy.rst b/Doc/library/asyncio-policy.rst index 0d7821e608ec98..9683550c3a9039 100644 --- a/Doc/library/asyncio-policy.rst +++ b/Doc/library/asyncio-policy.rst @@ -7,6 +7,8 @@ Policies ======== +The policy system is deprecated as of Python 3.13 and will +be removed in Python 3.15 or later. An event loop policy is a global object used to get and set the current :ref:`event loop `, as well as create new event loops. @@ -46,6 +48,8 @@ for the current process: If *policy* is set to ``None``, the default policy is restored. + .. deprecated-removed:: 3.13 3.15 + .. _asyncio-policy-objects: diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index e5f39c58490b85..274d532ec828e2 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -358,6 +358,11 @@ Deprecated They will be removed in Python 3.15. (Contributed by Victor Stinner in :gh:`105096`.) +* :mod:`asyncio`: + + * :func:`asyncio.set_event_loop_policy` will be removed along with the + rest of the asyncio policy system in Python 3.15 or later. + * Passing more than one positional argument to :func:`sqlite3.connect` and the :class:`sqlite3.Connection` constructor is deprecated. The remaining parameters will become keyword-only in Python 3.15. diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py index 0ccf85105e6673..fe7d71b850d57a 100644 --- a/Lib/asyncio/events.py +++ b/Lib/asyncio/events.py @@ -18,6 +18,7 @@ import subprocess import sys import threading +import warnings from . import format_helpers @@ -787,6 +788,9 @@ def set_event_loop_policy(policy): """Set the current event loop policy. If policy is None, the default policy is restored.""" + warnings._deprecated("set_event_loop_policy", + "{name!r} is deprecated as of Python 3.13 and will be " + "removed in Python {remove} or later.", remove=(3, 15)) global _event_loop_policy if policy is not None and not isinstance(policy, AbstractEventLoopPolicy): raise TypeError(f"policy must be an instance of AbstractEventLoopPolicy or None, not '{type(policy).__name__}'") diff --git a/Lib/test/libregrtest/save_env.py b/Lib/test/libregrtest/save_env.py index b2cc381344b2ef..d97ef6b5953662 100644 --- a/Lib/test/libregrtest/save_env.py +++ b/Lib/test/libregrtest/save_env.py @@ -96,8 +96,7 @@ def get_asyncio_events__event_loop_policy(self): self.try_get_module('asyncio') return support.maybe_get_event_loop_policy() def restore_asyncio_events__event_loop_policy(self, policy): - asyncio = self.get_module('asyncio') - asyncio.set_event_loop_policy(policy) + support.set_event_loop_policy(policy) def get_sys_argv(self): return id(sys.argv), sys.argv, sys.argv[:] diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index f64f88dcd2c6f3..e36353f994e908 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -1892,6 +1892,13 @@ def maybe_get_event_loop_policy(): import asyncio.events return asyncio.events._event_loop_policy +def set_event_loop_policy(policy): + """Set the global event loop policy ignoring deprecation warnings""" + import asyncio + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + asyncio.set_event_loop_policy(policy) + # Helpers for testing hashing. NHASHBITS = sys.hash_info.width # number of bits in hash() result assert NHASHBITS in (32, 64) diff --git a/Lib/test/test_asyncgen.py b/Lib/test/test_asyncgen.py index a49630112af510..1d23507ef61af7 100644 --- a/Lib/test/test_asyncgen.py +++ b/Lib/test/test_asyncgen.py @@ -5,6 +5,7 @@ from test.support.import_helper import import_module from test.support import gc_collect, requires_working_socket +from test import support asyncio = import_module("asyncio") @@ -429,7 +430,7 @@ def setUp(self): def tearDown(self): self.loop.close() self.loop = None - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) def check_async_iterator_anext(self, ait_class): with self.subTest(anext="pure-Python"): diff --git a/Lib/test/test_asyncio/test_base_events.py b/Lib/test/test_asyncio/test_base_events.py index c2080977e9d587..d298c9fd3a5f4f 100644 --- a/Lib/test/test_asyncio/test_base_events.py +++ b/Lib/test/test_asyncio/test_base_events.py @@ -24,7 +24,7 @@ def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) def mock_socket_module(): diff --git a/Lib/test/test_asyncio/test_buffered_proto.py b/Lib/test/test_asyncio/test_buffered_proto.py index f24e363ebfcfa3..fade54df48e324 100644 --- a/Lib/test/test_asyncio/test_buffered_proto.py +++ b/Lib/test/test_asyncio/test_buffered_proto.py @@ -2,10 +2,11 @@ import unittest from test.test_asyncio import functional as func_tests +from test import support def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class ReceiveStuffProto(asyncio.BufferedProtocol): diff --git a/Lib/test/test_asyncio/test_context.py b/Lib/test/test_asyncio/test_context.py index 6b80721873d95c..382c0fe5956efb 100644 --- a/Lib/test/test_asyncio/test_context.py +++ b/Lib/test/test_asyncio/test_context.py @@ -1,10 +1,11 @@ import asyncio import decimal import unittest +from test import support def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) @unittest.skipUnless(decimal.HAVE_CONTEXTVAR, "decimal is built with a thread-local context") diff --git a/Lib/test/test_asyncio/test_eager_task_factory.py b/Lib/test/test_asyncio/test_eager_task_factory.py index 0f8212dbec47be..74cc888b5384c2 100644 --- a/Lib/test/test_asyncio/test_eager_task_factory.py +++ b/Lib/test/test_asyncio/test_eager_task_factory.py @@ -8,12 +8,13 @@ from asyncio import tasks from test.test_asyncio import utils as test_utils from test.support.script_helper import assert_python_ok +from test import support MOCK_ANY = mock.ANY def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class EagerTaskFactoryLoopTests: diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py index b25c0975736e20..4df3c2e818d903 100644 --- a/Lib/test/test_asyncio/test_events.py +++ b/Lib/test/test_asyncio/test_events.py @@ -39,7 +39,7 @@ def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) def broken_unix_getsockname(): @@ -2684,13 +2684,15 @@ def test_get_event_loop_policy(self): self.assertIs(policy, asyncio.get_event_loop_policy()) def test_set_event_loop_policy(self): - self.assertRaises( - TypeError, asyncio.set_event_loop_policy, object()) + with self.assertWarns(DeprecationWarning): + self.assertRaises( + TypeError, asyncio.set_event_loop_policy, object()) old_policy = asyncio.get_event_loop_policy() policy = asyncio.DefaultEventLoopPolicy() - asyncio.set_event_loop_policy(policy) + with self.assertWarns(DeprecationWarning): + asyncio.set_event_loop_policy(policy) self.assertIs(policy, asyncio.get_event_loop_policy()) self.assertIsNot(policy, old_policy) @@ -2789,7 +2791,8 @@ def get_event_loop(self): old_policy = asyncio.get_event_loop_policy() try: - asyncio.set_event_loop_policy(Policy()) + with self.assertWarns(DeprecationWarning): + asyncio.set_event_loop_policy(Policy()) loop = asyncio.new_event_loop() with self.assertRaises(TestError): @@ -2817,7 +2820,7 @@ async def func(): asyncio.get_event_loop() finally: - asyncio.set_event_loop_policy(old_policy) + support.set_event_loop_policy(old_policy) if loop is not None: loop.close() @@ -2829,7 +2832,8 @@ async def func(): def test_get_event_loop_returns_running_loop2(self): old_policy = asyncio.get_event_loop_policy() try: - asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy()) + with self.assertWarns(DeprecationWarning): + asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy()) loop = asyncio.new_event_loop() self.addCleanup(loop.close) @@ -2860,7 +2864,7 @@ async def func(): asyncio.get_event_loop() finally: - asyncio.set_event_loop_policy(old_policy) + support.set_event_loop_policy(old_policy) if loop is not None: loop.close() diff --git a/Lib/test/test_asyncio/test_futures.py b/Lib/test/test_asyncio/test_futures.py index 2184b2091f84ee..9a16cf4ef632bc 100644 --- a/Lib/test/test_asyncio/test_futures.py +++ b/Lib/test/test_asyncio/test_futures.py @@ -16,7 +16,7 @@ def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) def _fakefunc(f): diff --git a/Lib/test/test_asyncio/test_futures2.py b/Lib/test/test_asyncio/test_futures2.py index b7cfffb76bd8f1..56897371d3afd1 100644 --- a/Lib/test/test_asyncio/test_futures2.py +++ b/Lib/test/test_asyncio/test_futures2.py @@ -4,10 +4,11 @@ import traceback import unittest from asyncio import tasks +from test import support def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class FutureTests: diff --git a/Lib/test/test_asyncio/test_locks.py b/Lib/test/test_asyncio/test_locks.py index f6c6a282429a21..b9834b96b23266 100644 --- a/Lib/test/test_asyncio/test_locks.py +++ b/Lib/test/test_asyncio/test_locks.py @@ -6,6 +6,7 @@ import asyncio import collections +from test import support STR_RGX_REPR = ( r'^<(?P.*?) object at (?P
.*?)' @@ -20,7 +21,7 @@ def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class LockTests(unittest.IsolatedAsyncioTestCase): diff --git a/Lib/test/test_asyncio/test_pep492.py b/Lib/test/test_asyncio/test_pep492.py index dc25a46985e349..b6365351a8df0f 100644 --- a/Lib/test/test_asyncio/test_pep492.py +++ b/Lib/test/test_asyncio/test_pep492.py @@ -8,10 +8,11 @@ import asyncio from test.test_asyncio import utils as test_utils +from test import support def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) # Test that asyncio.iscoroutine() uses collections.abc.Coroutine diff --git a/Lib/test/test_asyncio/test_proactor_events.py b/Lib/test/test_asyncio/test_proactor_events.py index c42856e578b8cc..ecc140da360dfe 100644 --- a/Lib/test/test_asyncio/test_proactor_events.py +++ b/Lib/test/test_asyncio/test_proactor_events.py @@ -15,10 +15,11 @@ from test.support import os_helper from test.support import socket_helper from test.test_asyncio import utils as test_utils +from test import support def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) def close_transport(transport): diff --git a/Lib/test/test_asyncio/test_protocols.py b/Lib/test/test_asyncio/test_protocols.py index 0f232631867db5..e295423acd9dac 100644 --- a/Lib/test/test_asyncio/test_protocols.py +++ b/Lib/test/test_asyncio/test_protocols.py @@ -2,12 +2,13 @@ from unittest import mock import asyncio +from test import support def tearDownModule(): # not needed for the test file but added for uniformness with all other # asyncio test files for the sake of unified cleanup - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class ProtocolsAbsTests(unittest.TestCase): diff --git a/Lib/test/test_asyncio/test_queues.py b/Lib/test/test_asyncio/test_queues.py index 2d058ccf6a8c72..74da812d2f0395 100644 --- a/Lib/test/test_asyncio/test_queues.py +++ b/Lib/test/test_asyncio/test_queues.py @@ -3,10 +3,11 @@ import asyncio import unittest from types import GenericAlias +from test import support def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class QueueBasicTests(unittest.IsolatedAsyncioTestCase): diff --git a/Lib/test/test_asyncio/test_runners.py b/Lib/test/test_asyncio/test_runners.py index 13493d3c806d6a..0e08d4abb10531 100644 --- a/Lib/test/test_asyncio/test_runners.py +++ b/Lib/test/test_asyncio/test_runners.py @@ -7,12 +7,13 @@ import threading import unittest from test.test_asyncio import utils as test_utils +from test import support from unittest import mock from unittest.mock import patch def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) def interrupt_self(): @@ -61,7 +62,7 @@ def setUp(self): super().setUp() policy = TestPolicy(self.new_loop) - asyncio.set_event_loop_policy(policy) + support.set_event_loop_policy(policy) def tearDown(self): policy = asyncio.get_event_loop_policy() @@ -69,7 +70,7 @@ def tearDown(self): self.assertTrue(policy.loop.is_closed()) self.assertTrue(policy.loop.shutdown_ag_run) - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) super().tearDown() @@ -259,7 +260,7 @@ def new_event_loop(): loop.set_task_factory(Task) return loop - asyncio.set_event_loop_policy(TestPolicy(new_event_loop)) + support.set_event_loop_policy(TestPolicy(new_event_loop)) with self.assertRaises(asyncio.CancelledError): asyncio.run(main()) diff --git a/Lib/test/test_asyncio/test_selector_events.py b/Lib/test/test_asyncio/test_selector_events.py index c22b780b5edcb8..677e37a1453c3b 100644 --- a/Lib/test/test_asyncio/test_selector_events.py +++ b/Lib/test/test_asyncio/test_selector_events.py @@ -19,12 +19,13 @@ _SelectorSocketTransport, _SelectorTransport) from test.test_asyncio import utils as test_utils +from test import support MOCK_ANY = mock.ANY def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class TestBaseSelectorEventLoop(BaseSelectorEventLoop): diff --git a/Lib/test/test_asyncio/test_sendfile.py b/Lib/test/test_asyncio/test_sendfile.py index d33ff197bbfa1d..1e819e59297717 100644 --- a/Lib/test/test_asyncio/test_sendfile.py +++ b/Lib/test/test_asyncio/test_sendfile.py @@ -22,7 +22,7 @@ def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class MySendfileProto(asyncio.Protocol): diff --git a/Lib/test/test_asyncio/test_server.py b/Lib/test/test_asyncio/test_server.py index 7ff3f55f4f0c5a..ac7737444df4cd 100644 --- a/Lib/test/test_asyncio/test_server.py +++ b/Lib/test/test_asyncio/test_server.py @@ -6,10 +6,11 @@ from test.support import socket_helper from test.test_asyncio import utils as test_utils from test.test_asyncio import functional as func_tests +from test import support def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class BaseStartServer(func_tests.FunctionalTestCaseMixin): diff --git a/Lib/test/test_asyncio/test_sock_lowlevel.py b/Lib/test/test_asyncio/test_sock_lowlevel.py index 075113cbe8e4a6..1126fa80a085b5 100644 --- a/Lib/test/test_asyncio/test_sock_lowlevel.py +++ b/Lib/test/test_asyncio/test_sock_lowlevel.py @@ -15,7 +15,7 @@ def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class MyProto(asyncio.Protocol): diff --git a/Lib/test/test_asyncio/test_ssl.py b/Lib/test/test_asyncio/test_ssl.py index e9cc735613fb8e..d27d1343f6d586 100644 --- a/Lib/test/test_asyncio/test_ssl.py +++ b/Lib/test/test_asyncio/test_ssl.py @@ -25,7 +25,7 @@ def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class MyBaseProto(asyncio.Protocol): diff --git a/Lib/test/test_asyncio/test_sslproto.py b/Lib/test/test_asyncio/test_sslproto.py index 37d015339761c6..516902c137f7d4 100644 --- a/Lib/test/test_asyncio/test_sslproto.py +++ b/Lib/test/test_asyncio/test_sslproto.py @@ -21,7 +21,7 @@ def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) @unittest.skipIf(ssl is None, 'No ssl module') diff --git a/Lib/test/test_asyncio/test_streams.py b/Lib/test/test_asyncio/test_streams.py index 9c92e75886c593..aaf8b438a1d26e 100644 --- a/Lib/test/test_asyncio/test_streams.py +++ b/Lib/test/test_asyncio/test_streams.py @@ -18,10 +18,11 @@ import asyncio from test.test_asyncio import utils as test_utils +from test import support def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class StreamTests(test_utils.TestCase): diff --git a/Lib/test/test_asyncio/test_subprocess.py b/Lib/test/test_asyncio/test_subprocess.py index 179c8cb8cc17cf..ba1fa5c3103ade 100644 --- a/Lib/test/test_asyncio/test_subprocess.py +++ b/Lib/test/test_asyncio/test_subprocess.py @@ -35,7 +35,7 @@ def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class TestSubprocessTransport(base_subprocess.BaseSubprocessTransport): diff --git a/Lib/test/test_asyncio/test_taskgroups.py b/Lib/test/test_asyncio/test_taskgroups.py index 7a18362b54e469..b336a49e96a537 100644 --- a/Lib/test/test_asyncio/test_taskgroups.py +++ b/Lib/test/test_asyncio/test_taskgroups.py @@ -7,13 +7,14 @@ import contextlib from asyncio import taskgroups import unittest +from test import support from test.test_asyncio.utils import await_without_task # To prevent a warning "test altered the execution environment" def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class MyExc(Exception): diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 4dfaff847edb90..1c2fcfdde0dacb 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -22,7 +22,7 @@ def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) async def coroutine_function(): diff --git a/Lib/test/test_asyncio/test_threads.py b/Lib/test/test_asyncio/test_threads.py index 1138a93e0f78ec..70eebdc15f57d2 100644 --- a/Lib/test/test_asyncio/test_threads.py +++ b/Lib/test/test_asyncio/test_threads.py @@ -5,10 +5,11 @@ from contextvars import ContextVar from unittest import mock +from test import support def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class ToThreadTests(unittest.IsolatedAsyncioTestCase): diff --git a/Lib/test/test_asyncio/test_timeouts.py b/Lib/test/test_asyncio/test_timeouts.py index f54e79e4d8e600..e2dea71c374bfd 100644 --- a/Lib/test/test_asyncio/test_timeouts.py +++ b/Lib/test/test_asyncio/test_timeouts.py @@ -4,12 +4,13 @@ import time import asyncio +from test import support from test.test_asyncio.utils import await_without_task def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class TimeoutTests(unittest.IsolatedAsyncioTestCase): diff --git a/Lib/test/test_asyncio/test_transports.py b/Lib/test/test_asyncio/test_transports.py index bbdb218efaa3b6..77b5a19cb6baa0 100644 --- a/Lib/test/test_asyncio/test_transports.py +++ b/Lib/test/test_asyncio/test_transports.py @@ -5,12 +5,13 @@ import asyncio from asyncio import transports +from test import support def tearDownModule(): # not needed for the test file but added for uniformness with all other # asyncio test files for the sake of unified cleanup - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class TransportTests(unittest.TestCase): diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py index d2c8cba6acfa31..4d01b6af00c302 100644 --- a/Lib/test/test_asyncio/test_unix_events.py +++ b/Lib/test/test_asyncio/test_unix_events.py @@ -34,7 +34,7 @@ def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) MOCK_ANY = mock.ANY diff --git a/Lib/test/test_asyncio/test_waitfor.py b/Lib/test/test_asyncio/test_waitfor.py index d52f32534a0cfe..a99bc48731df23 100644 --- a/Lib/test/test_asyncio/test_waitfor.py +++ b/Lib/test/test_asyncio/test_waitfor.py @@ -5,7 +5,7 @@ def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) # The following value can be used as a very small timeout: diff --git a/Lib/test/test_asyncio/test_windows_events.py b/Lib/test/test_asyncio/test_windows_events.py index 6e6c90a247b291..5024768fb1fa65 100644 --- a/Lib/test/test_asyncio/test_windows_events.py +++ b/Lib/test/test_asyncio/test_windows_events.py @@ -6,6 +6,7 @@ import threading import unittest from unittest import mock +from test import support if sys.platform != 'win32': raise unittest.SkipTest('Windows only') @@ -19,7 +20,7 @@ def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class UpperProto(asyncio.Protocol): @@ -294,11 +295,11 @@ async def main(): old_policy = asyncio.get_event_loop_policy() try: - asyncio.set_event_loop_policy( + support.set_event_loop_policy( asyncio.WindowsSelectorEventLoopPolicy()) asyncio.run(main()) finally: - asyncio.set_event_loop_policy(old_policy) + support.set_event_loop_policy(old_policy) def test_proactor_win_policy(self): async def main(): @@ -308,11 +309,11 @@ async def main(): old_policy = asyncio.get_event_loop_policy() try: - asyncio.set_event_loop_policy( + support.set_event_loop_policy( asyncio.WindowsProactorEventLoopPolicy()) asyncio.run(main()) finally: - asyncio.set_event_loop_policy(old_policy) + support.set_event_loop_policy(old_policy) if __name__ == '__main__': diff --git a/Lib/test/test_asyncio/test_windows_utils.py b/Lib/test/test_asyncio/test_windows_utils.py index eafa5be3829682..e97a9ae7fd3ba6 100644 --- a/Lib/test/test_asyncio/test_windows_utils.py +++ b/Lib/test/test_asyncio/test_windows_utils.py @@ -16,7 +16,7 @@ def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class PipeTests(unittest.TestCase): diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index b7966f8f03875b..bb13398c0254bf 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -462,7 +462,7 @@ async def arange(n): asyncio.run(eval(co, globals_)) self.assertEqual(globals_['a'], 1) finally: - asyncio.set_event_loop_policy(policy) + support.set_event_loop_policy(policy) def test_compile_top_level_await_invalid_cases(self): # helper function just to check we can run top=level async-for @@ -499,7 +499,7 @@ async def arange(n): mode, flags=ast.PyCF_ALLOW_TOP_LEVEL_AWAIT) finally: - asyncio.set_event_loop_policy(policy) + support.set_event_loop_policy(policy) def test_compile_async_generator(self): diff --git a/Lib/test/test_contextlib_async.py b/Lib/test/test_contextlib_async.py index ca7315783b9674..f1b494f0e328b6 100644 --- a/Lib/test/test_contextlib_async.py +++ b/Lib/test/test_contextlib_async.py @@ -11,7 +11,7 @@ support.requires_working_socket(module=True) def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class TestAbstractAsyncContextManager(unittest.IsolatedAsyncioTestCase): diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py index 47145782c0f04f..8d28cc907855c6 100644 --- a/Lib/test/test_coroutines.py +++ b/Lib/test/test_coroutines.py @@ -2281,7 +2281,7 @@ async def f(): pass finally: loop.close() - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) self.assertEqual(buffer, [1, 2, 'MyException']) diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index 33eae8505d5d24..44ee5976c34023 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -74,7 +74,7 @@ def revise(filename, *args): def tearDownModule(): if support.has_socket_support: - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) def signatures_with_lexicographic_keyword_only_parameters(): @@ -1012,8 +1012,7 @@ def f(self): "socket.accept is broken" ) def test_nested_class_definition_inside_async_function(self): - import asyncio - self.addCleanup(asyncio.set_event_loop_policy, None) + self.addCleanup(support.set_event_loop_policy, None) self.assertSourceEqual(asyncio.run(mod2.func225()), 226, 227) self.assertSourceEqual(mod2.cls226, 231, 235) self.assertSourceEqual(asyncio.run(mod2.cls226().func232()), 233, 234) diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index ab969ce26a69e7..5024f26be450c3 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -5002,7 +5002,7 @@ def test_taskName_with_asyncio_imported(self): logging.logAsyncioTasks = False runner.run(make_record(self.assertIsNone)) finally: - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) @support.requires_working_socket() def test_taskName_without_asyncio_imported(self): @@ -5014,7 +5014,7 @@ def test_taskName_without_asyncio_imported(self): logging.logAsyncioTasks = False runner.run(make_record(self.assertIsNone)) finally: - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class BasicConfigTest(unittest.TestCase): @@ -5318,7 +5318,7 @@ async def log_record(): data = f.read().strip() self.assertRegex(data, r'Task-\d+ - hello world') finally: - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) if handler: handler.close() diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 22e40d9557be7c..12c11a8016366e 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -102,7 +102,7 @@ def create_file(filename, content=b'content'): def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class MiscTests(unittest.TestCase): diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 5fef8365f59710..38dfa35c68c8fb 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -1627,7 +1627,7 @@ def test_pdb_next_command_for_coroutine(): ... loop = asyncio.new_event_loop() ... loop.run_until_complete(test_main()) ... loop.close() - ... asyncio.set_event_loop_policy(None) + ... support.set_event_loop_policy(None) ... print("finished") >>> with PdbTestInput(['step', @@ -1687,7 +1687,7 @@ def test_pdb_next_command_for_asyncgen(): ... loop = asyncio.new_event_loop() ... loop.run_until_complete(test_main()) ... loop.close() - ... asyncio.set_event_loop_policy(None) + ... support.set_event_loop_policy(None) ... print("finished") >>> with PdbTestInput(['step', @@ -1799,7 +1799,7 @@ def test_pdb_return_command_for_coroutine(): ... loop = asyncio.new_event_loop() ... loop.run_until_complete(test_main()) ... loop.close() - ... asyncio.set_event_loop_policy(None) + ... support.set_event_loop_policy(None) ... print("finished") >>> with PdbTestInput(['step', @@ -1890,7 +1890,7 @@ def test_pdb_until_command_for_coroutine(): ... loop = asyncio.new_event_loop() ... loop.run_until_complete(test_main()) ... loop.close() - ... asyncio.set_event_loop_policy(None) + ... support.set_event_loop_policy(None) ... print("finished") >>> with PdbTestInput(['step', diff --git a/Lib/test/test_sys_settrace.py b/Lib/test/test_sys_settrace.py index df7dd36274df2e..8235403c951e2d 100644 --- a/Lib/test/test_sys_settrace.py +++ b/Lib/test/test_sys_settrace.py @@ -2028,7 +2028,7 @@ def run_async_test(self, func, jumpFrom, jumpTo, expected, error=None, asyncio.run(func(output)) sys.settrace(None) - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) self.compare_jump_output(expected, output) def jump_test(jumpFrom, jumpTo, expected, error=None, event='line', warning=None): diff --git a/Lib/test/test_type_params.py b/Lib/test/test_type_params.py index 25ee188731f31f..63427bee5a8577 100644 --- a/Lib/test/test_type_params.py +++ b/Lib/test/test_type_params.py @@ -5,6 +5,7 @@ import pickle import weakref from test.support import requires_working_socket, check_syntax_error, run_code +from test import support from typing import Generic, Sequence, TypeVar, TypeVarTuple, ParamSpec, get_args @@ -850,7 +851,7 @@ async def coroutine[B](): co = get_coroutine() - self.addCleanup(asyncio.set_event_loop_policy, None) + self.addCleanup(support.set_event_loop_policy, None) a, b = asyncio.run(co()) self.assertIsInstance(a, TypeVar) diff --git a/Lib/test/test_unittest/test_async_case.py b/Lib/test/test_unittest/test_async_case.py index ba1ab838cd4a22..5c71bbacd99e70 100644 --- a/Lib/test/test_unittest/test_async_case.py +++ b/Lib/test/test_unittest/test_async_case.py @@ -11,7 +11,7 @@ class MyException(Exception): def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class TestCM: diff --git a/Lib/test/test_unittest/testmock/testasync.py b/Lib/test/test_unittest/testmock/testasync.py index f57b83f457f279..b8bb6a274fe790 100644 --- a/Lib/test/test_unittest/testmock/testasync.py +++ b/Lib/test/test_unittest/testmock/testasync.py @@ -15,7 +15,7 @@ def tearDownModule(): - asyncio.set_event_loop_policy(None) + support.set_event_loop_policy(None) class AsyncClass: diff --git a/Misc/NEWS.d/next/Library/2023-10-11-15-55-53.gh-issue-94597.qXo8Q4.rst b/Misc/NEWS.d/next/Library/2023-10-11-15-55-53.gh-issue-94597.qXo8Q4.rst new file mode 100644 index 00000000000000..c343610edd4c06 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-10-11-15-55-53.gh-issue-94597.qXo8Q4.rst @@ -0,0 +1 @@ +Deprecate :func:`asyncio.set_event_loop_policy`.