From a1fa65776c3f0cefc9a67a0463251029a2deb6bf Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 11 Oct 2023 16:52:10 +0100 Subject: [PATCH 01/13] deprecate asyncio.set_event_loop_policy --- Lib/asyncio/events.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py index 0ccf85105e6673..6b3e20d9635bf5 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}.", 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__}'") From b8256f763fee45f8cca18b79b83a719dc3f3a145 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 11 Oct 2023 16:54:06 +0100 Subject: [PATCH 02/13] update set_event_loop_policy docs --- Doc/library/asyncio-policy.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Doc/library/asyncio-policy.rst b/Doc/library/asyncio-policy.rst index 0d7821e608ec98..fc5ae27ebae521 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. 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: From f56d368cd91c02918e18448095011ce1fffca970 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 11 Oct 2023 15:55:57 +0000 Subject: [PATCH 03/13] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-10-11-15-55-53.gh-issue-94597.qXo8Q4.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-10-11-15-55-53.gh-issue-94597.qXo8Q4.rst 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..6a9a4a8ade40bd --- /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` From 8700b61f29887cdc7ed1fc1c184dbfd74349729e Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 11 Oct 2023 17:00:01 +0100 Subject: [PATCH 04/13] test the deprecation warning --- Lib/test/test_asyncio/test_events.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py index b25c0975736e20..b8379f6a1bde1d 100644 --- a/Lib/test/test_asyncio/test_events.py +++ b/Lib/test/test_asyncio/test_events.py @@ -2690,7 +2690,8 @@ def test_set_event_loop_policy(self): old_policy = asyncio.get_event_loop_policy() policy = asyncio.DefaultEventLoopPolicy() - asyncio.set_event_loop_policy(policy) + with self.assertWarns(DeprecationWarning) as cm: + asyncio.set_event_loop_policy(policy) self.assertIs(policy, asyncio.get_event_loop_policy()) self.assertIsNot(policy, old_policy) From 491b42e529f4ca264ea95547ce36e591d9e0e5ab Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 11 Oct 2023 17:32:59 +0100 Subject: [PATCH 05/13] add support.set_event_loop_policy for setting the event loop policy in tests --- Lib/test/libregrtest/save_env.py | 2 +- Lib/test/support/__init__.py | 7 +++++++ Lib/test/test_asyncgen.py | 3 ++- Lib/test/test_asyncio/test_base_events.py | 2 +- Lib/test/test_asyncio/test_buffered_proto.py | 3 ++- Lib/test/test_asyncio/test_context.py | 3 ++- .../test_asyncio/test_eager_task_factory.py | 3 ++- Lib/test/test_asyncio/test_events.py | 17 ++++++++++------- Lib/test/test_asyncio/test_futures.py | 2 +- Lib/test/test_asyncio/test_futures2.py | 3 ++- Lib/test/test_asyncio/test_locks.py | 3 ++- Lib/test/test_asyncio/test_pep492.py | 3 ++- Lib/test/test_asyncio/test_proactor_events.py | 3 ++- Lib/test/test_asyncio/test_protocols.py | 3 ++- Lib/test/test_asyncio/test_queues.py | 3 ++- Lib/test/test_asyncio/test_runners.py | 9 +++++---- Lib/test/test_asyncio/test_selector_events.py | 3 ++- Lib/test/test_asyncio/test_sendfile.py | 2 +- Lib/test/test_asyncio/test_server.py | 3 ++- Lib/test/test_asyncio/test_sock_lowlevel.py | 2 +- Lib/test/test_asyncio/test_ssl.py | 2 +- Lib/test/test_asyncio/test_sslproto.py | 2 +- Lib/test/test_asyncio/test_streams.py | 3 ++- Lib/test/test_asyncio/test_subprocess.py | 2 +- Lib/test/test_asyncio/test_taskgroups.py | 3 ++- Lib/test/test_asyncio/test_tasks.py | 2 +- Lib/test/test_asyncio/test_threads.py | 3 ++- Lib/test/test_asyncio/test_timeouts.py | 3 ++- Lib/test/test_asyncio/test_transports.py | 3 ++- Lib/test/test_asyncio/test_unix_events.py | 2 +- Lib/test/test_asyncio/test_waitfor.py | 2 +- Lib/test/test_asyncio/test_windows_events.py | 11 ++++++----- Lib/test/test_asyncio/test_windows_utils.py | 2 +- Lib/test/test_builtin.py | 4 ++-- Lib/test/test_contextlib_async.py | 2 +- Lib/test/test_coroutines.py | 2 +- Lib/test/test_inspect/test_inspect.py | 5 ++--- Lib/test/test_logging.py | 6 +++--- Lib/test/test_os.py | 2 +- Lib/test/test_pdb.py | 8 ++++---- Lib/test/test_sys_settrace.py | 2 +- Lib/test/test_type_params.py | 3 ++- Lib/test/test_unittest/test_async_case.py | 2 +- Lib/test/test_unittest/testmock/testasync.py | 2 +- 44 files changed, 93 insertions(+), 64 deletions(-) diff --git a/Lib/test/libregrtest/save_env.py b/Lib/test/libregrtest/save_env.py index b2cc381344b2ef..eac6629064c0b2 100644 --- a/Lib/test/libregrtest/save_env.py +++ b/Lib/test/libregrtest/save_env.py @@ -97,7 +97,7 @@ def get_asyncio_events__event_loop_policy(self): 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 21e8770ab31180..0d5bb7eb1c2cd4 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -2089,6 +2089,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 abcb6f55c4b04e..d4be9f4a449b61 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 b8379f6a1bde1d..0fbbc68e011f29 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,8 +2684,9 @@ 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() @@ -2790,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): @@ -2818,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() @@ -2830,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) @@ -2861,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 1eb5641914f2a4..14f3c842859490 100644 --- a/Lib/test/test_asyncio/test_runners.py +++ b/Lib/test/test_asyncio/test_runners.py @@ -6,12 +6,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(): @@ -60,7 +61,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() @@ -68,7 +69,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() @@ -258,7 +259,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 0198da21d77028..e1d9ef37e6fc92 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 06d8b60f219f1a..6e33eb7910abc5 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 6a0231f2859a62..57cf522a7cf3aa 100644 --- a/Lib/test/test_asyncio/test_taskgroups.py +++ b/Lib/test/test_asyncio/test_taskgroups.py @@ -7,11 +7,12 @@ import contextlib from asyncio import taskgroups import unittest +from test import support # 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 e9b59b953518b3..97c755ebfeefdc 100644 --- a/Lib/test/test_asyncio/test_timeouts.py +++ b/Lib/test/test_asyncio/test_timeouts.py @@ -4,10 +4,11 @@ import time import asyncio +from test import support 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 31ac6b1070a0a2..af7caa5cdd0d24 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -72,7 +72,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(): @@ -1010,8 +1010,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 398393b2333a85..4cb9752a96e3ad 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -101,7 +101,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 ff677aeb795442..02d4da356ee542 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 f02169602e4925..8e884a74a15483 100644 --- a/Lib/test/test_sys_settrace.py +++ b/Lib/test/test_sys_settrace.py @@ -2024,7 +2024,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 a465103b59b6ec..0a020cdd9a70b5 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: From c081c6765a1ef1ad423f09ba133485605600b871 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 26 Oct 2023 20:16:30 +0100 Subject: [PATCH 06/13] Update Misc/NEWS.d/next/Library/2023-10-11-15-55-53.gh-issue-94597.qXo8Q4.rst Co-authored-by: Guido van Rossum --- .../next/Library/2023-10-11-15-55-53.gh-issue-94597.qXo8Q4.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 6a9a4a8ade40bd..c343610edd4c06 100644 --- 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 @@ -1 +1 @@ -deprecate :func:`asyncio.set_event_loop_policy` +Deprecate :func:`asyncio.set_event_loop_policy`. From 1e326bd1adef3d8760a16f6f32fceeaa375882dd Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Sun, 29 Oct 2023 09:02:35 +0000 Subject: [PATCH 07/13] Update Lib/test/test_asyncio/test_events.py Co-authored-by: Guido van Rossum --- Lib/test/test_asyncio/test_events.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py index 0fbbc68e011f29..4df3c2e818d903 100644 --- a/Lib/test/test_asyncio/test_events.py +++ b/Lib/test/test_asyncio/test_events.py @@ -2691,7 +2691,7 @@ def test_set_event_loop_policy(self): old_policy = asyncio.get_event_loop_policy() policy = asyncio.DefaultEventLoopPolicy() - with self.assertWarns(DeprecationWarning) as cm: + with self.assertWarns(DeprecationWarning): asyncio.set_event_loop_policy(policy) self.assertIs(policy, asyncio.get_event_loop_policy()) self.assertIsNot(policy, old_policy) From f0db489dc3bea8fa459430e761630f5a4e2b05e1 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Sun, 29 Oct 2023 09:02:44 +0000 Subject: [PATCH 08/13] Update Doc/library/asyncio-policy.rst Co-authored-by: Guido van Rossum --- Doc/library/asyncio-policy.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/asyncio-policy.rst b/Doc/library/asyncio-policy.rst index fc5ae27ebae521..9683550c3a9039 100644 --- a/Doc/library/asyncio-policy.rst +++ b/Doc/library/asyncio-policy.rst @@ -8,7 +8,7 @@ Policies ======== The policy system is deprecated as of Python 3.13 and will -be removed in Python 3.15. +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. From 180166bdaed2f91e83629c04221e2c0c522b1589 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Tue, 31 Oct 2023 16:28:00 +0000 Subject: [PATCH 09/13] Update Lib/test/libregrtest/save_env.py --- Lib/test/libregrtest/save_env.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/libregrtest/save_env.py b/Lib/test/libregrtest/save_env.py index eac6629064c0b2..d97ef6b5953662 100644 --- a/Lib/test/libregrtest/save_env.py +++ b/Lib/test/libregrtest/save_env.py @@ -96,7 +96,6 @@ 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') support.set_event_loop_policy(policy) def get_sys_argv(self): From b41756d05758bf89fe7ee2e4ca8f3f33e3ef739c Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Tue, 31 Oct 2023 16:28:45 +0000 Subject: [PATCH 10/13] Update Lib/asyncio/events.py --- Lib/asyncio/events.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py index 6b3e20d9635bf5..fe7d71b850d57a 100644 --- a/Lib/asyncio/events.py +++ b/Lib/asyncio/events.py @@ -790,7 +790,7 @@ def set_event_loop_policy(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}.", remove=(3, 15)) + "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__}'") From ec3907de006226cd82087a58002870574dc0eee8 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Tue, 31 Oct 2023 16:31:23 +0000 Subject: [PATCH 11/13] doc set_event_loop_policy removal schedule --- Doc/whatsnew/3.13.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index bbc1fecf4964d8..ce0dd2b694388b 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -508,6 +508,9 @@ Pending Removal in Python 3.15 All arguments will be removed from :func:`threading.RLock` in Python 3.15. (Contributed by Nikita Sobolev in :gh:`102029`.) +* :func:`asyncio.set_event_loop_policy` will be removed along with the + rest of the asyncio policy system. + Pending Removal in Python 3.16 ------------------------------ From c81b51ae1d0738577deeb3e7ba90a5b1d0863812 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Tue, 31 Oct 2023 16:42:38 +0000 Subject: [PATCH 12/13] Update Doc/whatsnew/3.13.rst Co-authored-by: Hugo van Kemenade --- Doc/whatsnew/3.13.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index ce0dd2b694388b..3c7f46732fcb72 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -509,7 +509,7 @@ Pending Removal in Python 3.15 (Contributed by Nikita Sobolev in :gh:`102029`.) * :func:`asyncio.set_event_loop_policy` will be removed along with the - rest of the asyncio policy system. + rest of the asyncio policy system in Python 3.15 or later. Pending Removal in Python 3.16 ------------------------------ From dc257ae589b8606d096c99726304bbda7477b962 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 1 Nov 2023 18:55:46 +0000 Subject: [PATCH 13/13] move set_event_loop_policy deprecation notice to asyncio section --- Doc/whatsnew/3.13.rst | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 3c7f46732fcb72..a34043ebc107e4 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -334,6 +334,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. @@ -508,9 +513,6 @@ Pending Removal in Python 3.15 All arguments will be removed from :func:`threading.RLock` in Python 3.15. (Contributed by Nikita Sobolev in :gh:`102029`.) -* :func:`asyncio.set_event_loop_policy` will be removed along with the - rest of the asyncio policy system in Python 3.15 or later. - Pending Removal in Python 3.16 ------------------------------