From 46d9cd8c715f0fca042bcccbf0f9510fe29b332d Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Tue, 24 Dec 2024 11:28:52 +0000 Subject: [PATCH 1/4] deprecate policy classes --- Doc/library/asyncio-policy.rst | 16 +++++++++ Lib/asyncio/__init__.py | 16 +++++++++ Lib/asyncio/events.py | 14 ++++---- Lib/asyncio/unix_events.py | 6 ++-- Lib/asyncio/windows_events.py | 10 +++--- Lib/test/test_asyncio/test_events.py | 34 +++++++++++++------- Lib/test/test_asyncio/test_runners.py | 2 +- Lib/test/test_asyncio/test_windows_events.py | 18 +++++++---- 8 files changed, 82 insertions(+), 34 deletions(-) diff --git a/Doc/library/asyncio-policy.rst b/Doc/library/asyncio-policy.rst index 2d05c3a9f7f157..ea7fe957da7c40 100644 --- a/Doc/library/asyncio-policy.rst +++ b/Doc/library/asyncio-policy.rst @@ -87,6 +87,10 @@ The abstract event loop policy base class is defined as follows: This method should never return ``None``. + .. deprecated:: next + The :class:`AbstractEventLoopPolicy` class is deprecated and + will be removed in Python 3.16. + .. _asyncio-policy-builtin: @@ -109,6 +113,10 @@ asyncio ships with the following built-in policies: The :meth:`get_event_loop` method of the default asyncio policy now raises a :exc:`RuntimeError` if there is no set event loop. + .. deprecated:: next + The :class:`DefaultEventLoopPolicy` class is deprecated and + will be removed in Python 3.16. + .. class:: WindowsSelectorEventLoopPolicy @@ -117,6 +125,10 @@ asyncio ships with the following built-in policies: .. availability:: Windows. + .. deprecated:: next + The :class:`WindowsSelectorEventLoopPolicy` class is deprecated and + will be removed in Python 3.16. + .. class:: WindowsProactorEventLoopPolicy @@ -125,6 +137,10 @@ asyncio ships with the following built-in policies: .. availability:: Windows. + .. deprecated:: next + The :class:`WindowsProactorEventLoopPolicy` class is deprecated and + will be removed in Python 3.16. + .. _asyncio-custom-policies: diff --git a/Lib/asyncio/__init__.py b/Lib/asyncio/__init__.py index 03165a425eb7d2..8f608affbff3d8 100644 --- a/Lib/asyncio/__init__.py +++ b/Lib/asyncio/__init__.py @@ -45,3 +45,19 @@ else: from .unix_events import * # pragma: no cover __all__ += unix_events.__all__ + +def __getattr__(name: str): + import warnings + + deprecated = { + "AbstractEventLoopPolicy", + "DefaultEventLoopPolicy", + "WindowsSelectorEventLoopPolicy", + "WindowsProactorEventLoopPolicy", + } + if name in deprecated: + warnings._deprecated(f"asyncio.{name}", remove=(3, 16)) + # deprecated things have underscores in front of them + return globals()["_" + name] + + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") \ No newline at end of file diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py index 1449245edc7c7e..3ade7747149b32 100644 --- a/Lib/asyncio/events.py +++ b/Lib/asyncio/events.py @@ -5,7 +5,7 @@ # SPDX-FileCopyrightText: Copyright (c) 2015-2021 MagicStack Inc. http://magic.io __all__ = ( - 'AbstractEventLoopPolicy', + '_AbstractEventLoopPolicy', 'AbstractEventLoop', 'AbstractServer', 'Handle', 'TimerHandle', '_get_event_loop_policy', @@ -632,7 +632,7 @@ def set_debug(self, enabled): raise NotImplementedError -class AbstractEventLoopPolicy: +class _AbstractEventLoopPolicy: """Abstract policy for accessing the event loop.""" def get_event_loop(self): @@ -655,7 +655,7 @@ def new_event_loop(self): the current context, set_event_loop must be called explicitly.""" raise NotImplementedError -class BaseDefaultEventLoopPolicy(AbstractEventLoopPolicy): +class _BaseDefaultEventLoopPolicy(_AbstractEventLoopPolicy): """Default policy implementation for accessing the event loop. In this policy, each thread has its own event loop. However, we @@ -758,8 +758,8 @@ def _init_event_loop_policy(): global _event_loop_policy with _lock: if _event_loop_policy is None: # pragma: no branch - from . import DefaultEventLoopPolicy - _event_loop_policy = DefaultEventLoopPolicy() + from . import _DefaultEventLoopPolicy + _event_loop_policy = _DefaultEventLoopPolicy() def _get_event_loop_policy(): @@ -777,7 +777,7 @@ def _set_event_loop_policy(policy): If policy is None, the default policy is restored.""" global _event_loop_policy - if policy is not None and not isinstance(policy, AbstractEventLoopPolicy): + 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__}'") _event_loop_policy = policy @@ -838,7 +838,7 @@ def new_event_loop(): def on_fork(): # Reset the loop and wakeupfd in the forked child process. if _event_loop_policy is not None: - _event_loop_policy._local = BaseDefaultEventLoopPolicy._Local() + _event_loop_policy._local = _BaseDefaultEventLoopPolicy._Local() _set_running_loop(None) signal.set_wakeup_fd(-1) diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py index 0227eb506c6016..f69c6a64c39ae6 100644 --- a/Lib/asyncio/unix_events.py +++ b/Lib/asyncio/unix_events.py @@ -28,7 +28,7 @@ __all__ = ( 'SelectorEventLoop', - 'DefaultEventLoopPolicy', + '_DefaultEventLoopPolicy', 'EventLoop', ) @@ -963,11 +963,11 @@ def can_use_pidfd(): return True -class _UnixDefaultEventLoopPolicy(events.BaseDefaultEventLoopPolicy): +class _UnixDefaultEventLoopPolicy(events._BaseDefaultEventLoopPolicy): """UNIX event loop policy""" _loop_factory = _UnixSelectorEventLoop SelectorEventLoop = _UnixSelectorEventLoop -DefaultEventLoopPolicy = _UnixDefaultEventLoopPolicy +_DefaultEventLoopPolicy = _UnixDefaultEventLoopPolicy EventLoop = SelectorEventLoop diff --git a/Lib/asyncio/windows_events.py b/Lib/asyncio/windows_events.py index bf99bc271c7acd..5f75b17d8ca649 100644 --- a/Lib/asyncio/windows_events.py +++ b/Lib/asyncio/windows_events.py @@ -29,8 +29,8 @@ __all__ = ( 'SelectorEventLoop', 'ProactorEventLoop', 'IocpProactor', - 'DefaultEventLoopPolicy', 'WindowsSelectorEventLoopPolicy', - 'WindowsProactorEventLoopPolicy', 'EventLoop', + '_DefaultEventLoopPolicy', '_WindowsSelectorEventLoopPolicy', + '_WindowsProactorEventLoopPolicy', 'EventLoop', ) @@ -891,13 +891,13 @@ def callback(f): SelectorEventLoop = _WindowsSelectorEventLoop -class WindowsSelectorEventLoopPolicy(events.BaseDefaultEventLoopPolicy): +class _WindowsSelectorEventLoopPolicy(events._BaseDefaultEventLoopPolicy): _loop_factory = SelectorEventLoop -class WindowsProactorEventLoopPolicy(events.BaseDefaultEventLoopPolicy): +class _WindowsProactorEventLoopPolicy(events._BaseDefaultEventLoopPolicy): _loop_factory = ProactorEventLoop -DefaultEventLoopPolicy = WindowsProactorEventLoopPolicy +_DefaultEventLoopPolicy = _WindowsProactorEventLoopPolicy EventLoop = ProactorEventLoop diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py index d43f66c13d2f96..c626670f72a084 100644 --- a/Lib/test/test_asyncio/test_events.py +++ b/Lib/test/test_asyncio/test_events.py @@ -2695,14 +2695,26 @@ async def inner(): class PolicyTests(unittest.TestCase): + def test_abstract_event_loop_policy_deprecation(self): + with self.assertWarnsRegex( + DeprecationWarning, "'asyncio.AbstractEventLoopPolicy' is deprecated"): + policy = asyncio.AbstractEventLoopPolicy() + self.assertIsInstance(policy, asyncio.AbstractEventLoopPolicy) + + def test_default_event_loop_policy_deprecation(self): + with self.assertWarnsRegex( + DeprecationWarning, "'asyncio.DefaultEventLoopPolicy' is deprecated"): + policy = asyncio.DefaultEventLoopPolicy() + self.assertIsInstance(policy, asyncio.DefaultEventLoopPolicy) + def test_event_loop_policy(self): - policy = asyncio.AbstractEventLoopPolicy() + policy = asyncio._AbstractEventLoopPolicy() self.assertRaises(NotImplementedError, policy.get_event_loop) self.assertRaises(NotImplementedError, policy.set_event_loop, object()) self.assertRaises(NotImplementedError, policy.new_event_loop) def test_get_event_loop(self): - policy = asyncio.DefaultEventLoopPolicy() + policy = asyncio._DefaultEventLoopPolicy() self.assertIsNone(policy._local._loop) with self.assertRaises(RuntimeError): @@ -2710,7 +2722,7 @@ def test_get_event_loop(self): self.assertIsNone(policy._local._loop) def test_get_event_loop_does_not_call_set_event_loop(self): - policy = asyncio.DefaultEventLoopPolicy() + policy = asyncio._DefaultEventLoopPolicy() with mock.patch.object( policy, "set_event_loop", @@ -2722,7 +2734,7 @@ def test_get_event_loop_does_not_call_set_event_loop(self): m_set_event_loop.assert_not_called() def test_get_event_loop_after_set_none(self): - policy = asyncio.DefaultEventLoopPolicy() + policy = asyncio._DefaultEventLoopPolicy() policy.set_event_loop(None) self.assertRaises(RuntimeError, policy.get_event_loop) @@ -2730,7 +2742,7 @@ def test_get_event_loop_after_set_none(self): def test_get_event_loop_thread(self, m_current_thread): def f(): - policy = asyncio.DefaultEventLoopPolicy() + policy = asyncio._DefaultEventLoopPolicy() self.assertRaises(RuntimeError, policy.get_event_loop) th = threading.Thread(target=f) @@ -2738,14 +2750,14 @@ def f(): th.join() def test_new_event_loop(self): - policy = asyncio.DefaultEventLoopPolicy() + policy = asyncio._DefaultEventLoopPolicy() loop = policy.new_event_loop() self.assertIsInstance(loop, asyncio.AbstractEventLoop) loop.close() def test_set_event_loop(self): - policy = asyncio.DefaultEventLoopPolicy() + policy = asyncio._DefaultEventLoopPolicy() old_loop = policy.new_event_loop() policy.set_event_loop(old_loop) @@ -2762,7 +2774,7 @@ def test_get_event_loop_policy(self): with self.assertWarnsRegex( DeprecationWarning, "'asyncio.get_event_loop_policy' is deprecated"): policy = asyncio.get_event_loop_policy() - self.assertIsInstance(policy, asyncio.AbstractEventLoopPolicy) + self.assertIsInstance(policy, asyncio._AbstractEventLoopPolicy) self.assertIs(policy, asyncio.get_event_loop_policy()) def test_set_event_loop_policy(self): @@ -2775,7 +2787,7 @@ def test_set_event_loop_policy(self): DeprecationWarning, "'asyncio.get_event_loop_policy' is deprecated"): old_policy = asyncio.get_event_loop_policy() - policy = asyncio.DefaultEventLoopPolicy() + policy = asyncio._DefaultEventLoopPolicy() with self.assertWarnsRegex( DeprecationWarning, "'asyncio.set_event_loop_policy' is deprecated"): asyncio.set_event_loop_policy(policy) @@ -2862,7 +2874,7 @@ def test_get_event_loop_returns_running_loop(self): class TestError(Exception): pass - class Policy(asyncio.DefaultEventLoopPolicy): + class Policy(asyncio._DefaultEventLoopPolicy): def get_event_loop(self): raise TestError @@ -2908,7 +2920,7 @@ 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()) + asyncio._set_event_loop_policy(asyncio._DefaultEventLoopPolicy()) loop = asyncio.new_event_loop() self.addCleanup(loop.close) diff --git a/Lib/test/test_asyncio/test_runners.py b/Lib/test/test_asyncio/test_runners.py index e1f82f7f7bec0c..21f277bc2d8d5f 100644 --- a/Lib/test/test_asyncio/test_runners.py +++ b/Lib/test/test_asyncio/test_runners.py @@ -19,7 +19,7 @@ def interrupt_self(): _thread.interrupt_main() -class TestPolicy(asyncio.AbstractEventLoopPolicy): +class TestPolicy(asyncio._AbstractEventLoopPolicy): def __init__(self, loop_factory): self.loop_factory = loop_factory diff --git a/Lib/test/test_asyncio/test_windows_events.py b/Lib/test/test_asyncio/test_windows_events.py index 28b05d24dc25a1..69e9905205eee0 100644 --- a/Lib/test/test_asyncio/test_windows_events.py +++ b/Lib/test/test_asyncio/test_windows_events.py @@ -328,14 +328,15 @@ class WinPolicyTests(WindowsEventsTestCase): def test_selector_win_policy(self): async def main(): - self.assertIsInstance( - asyncio.get_running_loop(), - asyncio.SelectorEventLoop) + self.assertIsInstance(asyncio.get_running_loop(), asyncio.SelectorEventLoop) old_policy = asyncio._get_event_loop_policy() try: - asyncio._set_event_loop_policy( - asyncio.WindowsSelectorEventLoopPolicy()) + with self.assertWarnsRegex( + DeprecationWarning, + "'asyncio.WindowsSelectorEventLoopPolicy' is deprecated", + ): + asyncio._set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) asyncio.run(main()) finally: asyncio._set_event_loop_policy(old_policy) @@ -348,8 +349,11 @@ async def main(): old_policy = asyncio._get_event_loop_policy() try: - asyncio._set_event_loop_policy( - asyncio.WindowsProactorEventLoopPolicy()) + with self.assertWarnsRegex( + DeprecationWarning, + "'asyncio.WindowsProactorEventLoopPolicy' is deprecated", + ): + asyncio._set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy()) asyncio.run(main()) finally: asyncio._set_event_loop_policy(old_policy) From 4168b5785fde7ca3e9e9b88f0685e22200e7ea66 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Tue, 24 Dec 2024 11:31:16 +0000 Subject: [PATCH 2/4] add newline --- Lib/asyncio/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/asyncio/__init__.py b/Lib/asyncio/__init__.py index 8f608affbff3d8..edb615b1b6b1c6 100644 --- a/Lib/asyncio/__init__.py +++ b/Lib/asyncio/__init__.py @@ -60,4 +60,4 @@ def __getattr__(name: str): # deprecated things have underscores in front of them return globals()["_" + name] - raise AttributeError(f"module {__name__!r} has no attribute {name!r}") \ No newline at end of file + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") From 4b29d89a7007556c459ab39c93015dc75f42537c Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Tue, 24 Dec 2024 12:19:50 +0000 Subject: [PATCH 3/4] deprecate asyncio.set_event_loop --- Doc/library/asyncio-eventloop.rst | 4 +++ Lib/asyncio/__main__.py | 2 +- Lib/asyncio/events.py | 32 +++++++++++++++-------- Lib/asyncio/runners.py | 4 +-- Lib/test/test_asyncgen.py | 2 +- Lib/test/test_asyncio/functional.py | 4 +-- Lib/test/test_asyncio/test_base_events.py | 8 +++--- Lib/test/test_asyncio/test_events.py | 18 ++++++------- Lib/test/test_asyncio/test_futures.py | 8 +++--- Lib/test/test_asyncio/test_streams.py | 10 +++---- Lib/test/test_asyncio/test_tasks.py | 18 ++++++------- Lib/test/test_asyncio/test_unix_events.py | 4 +-- Lib/test/test_asyncio/utils.py | 4 +-- Lib/test/test_coroutines.py | 2 +- Lib/test/test_type_params.py | 2 +- Lib/test/test_unittest/test_async_case.py | 2 +- 16 files changed, 69 insertions(+), 55 deletions(-) diff --git a/Doc/library/asyncio-eventloop.rst b/Doc/library/asyncio-eventloop.rst index 9f1aec148f8750..29f843123f8560 100644 --- a/Doc/library/asyncio-eventloop.rst +++ b/Doc/library/asyncio-eventloop.rst @@ -66,6 +66,10 @@ an event loop: Set *loop* as the current event loop for the current OS thread. + .. deprecated:: next + The :func:`set_event_loop` function is deprecated and will be removed + in Python 3.16. + .. function:: new_event_loop() Create and return a new event loop object. diff --git a/Lib/asyncio/__main__.py b/Lib/asyncio/__main__.py index 95c636f9e02866..662ba649aa08be 100644 --- a/Lib/asyncio/__main__.py +++ b/Lib/asyncio/__main__.py @@ -149,7 +149,7 @@ def interrupt(self) -> None: return_code = 0 loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) + asyncio._set_event_loop(loop) repl_locals = {'asyncio': asyncio} for key in {'__name__', '__package__', diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py index 3ade7747149b32..6e291d28ec81ae 100644 --- a/Lib/asyncio/events.py +++ b/Lib/asyncio/events.py @@ -5,16 +5,22 @@ # SPDX-FileCopyrightText: Copyright (c) 2015-2021 MagicStack Inc. http://magic.io __all__ = ( - '_AbstractEventLoopPolicy', - 'AbstractEventLoop', 'AbstractServer', - 'Handle', 'TimerHandle', - '_get_event_loop_policy', - 'get_event_loop_policy', - '_set_event_loop_policy', - 'set_event_loop_policy', - 'get_event_loop', 'set_event_loop', 'new_event_loop', - '_set_running_loop', 'get_running_loop', - '_get_running_loop', + "_AbstractEventLoopPolicy", + "AbstractEventLoop", + "AbstractServer", + "Handle", + "TimerHandle", + "_get_event_loop_policy", + "get_event_loop_policy", + "_set_event_loop_policy", + "set_event_loop_policy", + "get_event_loop", + "_set_event_loop", + "set_event_loop", + "new_event_loop", + "_set_running_loop", + "get_running_loop", + "_get_running_loop", ) import contextvars @@ -801,9 +807,13 @@ def get_event_loop(): return _get_event_loop_policy().get_event_loop() +def _set_event_loop(loop): + _get_event_loop_policy().set_event_loop(loop) + def set_event_loop(loop): """Equivalent to calling get_event_loop_policy().set_event_loop(loop).""" - _get_event_loop_policy().set_event_loop(loop) + warnings._deprecated('asyncio.set_event_loop', remove=(3,16)) + _set_event_loop(loop) def new_event_loop(): diff --git a/Lib/asyncio/runners.py b/Lib/asyncio/runners.py index 0e63c34f60f4d9..b9adf291d4817f 100644 --- a/Lib/asyncio/runners.py +++ b/Lib/asyncio/runners.py @@ -74,7 +74,7 @@ def close(self): loop.shutdown_default_executor(constants.THREAD_JOIN_TIMEOUT)) finally: if self._set_event_loop: - events.set_event_loop(None) + events._set_event_loop(None) loop.close() self._loop = None self._state = _State.CLOSED @@ -147,7 +147,7 @@ def _lazy_init(self): if not self._set_event_loop: # Call set_event_loop only once to avoid calling # attach_loop multiple times on child watchers - events.set_event_loop(self._loop) + events._set_event_loop(self._loop) self._set_event_loop = True else: self._loop = self._loop_factory() diff --git a/Lib/test/test_asyncgen.py b/Lib/test/test_asyncgen.py index 4bce6d5c1b1d2f..5bfd789185c675 100644 --- a/Lib/test/test_asyncgen.py +++ b/Lib/test/test_asyncgen.py @@ -624,7 +624,7 @@ class AsyncGenAsyncioTest(unittest.TestCase): def setUp(self): self.loop = asyncio.new_event_loop() - asyncio.set_event_loop(None) + asyncio._set_event_loop(None) def tearDown(self): self.loop.close() diff --git a/Lib/test/test_asyncio/functional.py b/Lib/test/test_asyncio/functional.py index d19c7a612ccf86..2934325b6dfbc7 100644 --- a/Lib/test/test_asyncio/functional.py +++ b/Lib/test/test_asyncio/functional.py @@ -24,7 +24,7 @@ def loop_exception_handler(self, loop, context): def setUp(self): self.loop = self.new_loop() - asyncio.set_event_loop(None) + asyncio._set_event_loop(None) self.loop.set_exception_handler(self.loop_exception_handler) self.__unhandled_exceptions = [] @@ -39,7 +39,7 @@ def tearDown(self): self.fail('unexpected calls to loop.call_exception_handler()') finally: - asyncio.set_event_loop(None) + asyncio._set_event_loop(None) self.loop = None def tcp_server(self, server_prog, *, diff --git a/Lib/test/test_asyncio/test_base_events.py b/Lib/test/test_asyncio/test_base_events.py index 08e38b047d519b..1e063c1352ecb9 100644 --- a/Lib/test/test_asyncio/test_base_events.py +++ b/Lib/test/test_asyncio/test_base_events.py @@ -331,10 +331,10 @@ def check_in_thread(loop, event, debug, create_loop, fut): if create_loop: loop2 = base_events.BaseEventLoop() try: - asyncio.set_event_loop(loop2) + asyncio._set_event_loop(loop2) self.check_thread(loop, debug) finally: - asyncio.set_event_loop(None) + asyncio._set_event_loop(None) loop2.close() else: self.check_thread(loop, debug) @@ -690,7 +690,7 @@ def default_exception_handler(self, context): loop = Loop() self.addCleanup(loop.close) - asyncio.set_event_loop(loop) + asyncio._set_event_loop(loop) def run_loop(): def zero_error(): @@ -1983,7 +1983,7 @@ def stop_loop_cb(loop): async def stop_loop_coro(loop): loop.stop() - asyncio.set_event_loop(self.loop) + asyncio._set_event_loop(self.loop) self.loop.set_debug(True) self.loop.slow_callback_duration = 0.0 diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py index c626670f72a084..b9843f8a157479 100644 --- a/Lib/test/test_asyncio/test_events.py +++ b/Lib/test/test_asyncio/test_events.py @@ -58,7 +58,7 @@ async def doit(): return 'hello' loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) + asyncio._set_event_loop(loop) return loop.run_until_complete(doit()) @@ -2824,14 +2824,14 @@ def setUp(self): super().setUp() self.loop = asyncio.new_event_loop() - asyncio.set_event_loop(self.loop) + asyncio._set_event_loop(self.loop) def tearDown(self): try: super().tearDown() finally: self.loop.close() - asyncio.set_event_loop(None) + asyncio._set_event_loop(None) events._get_running_loop = self._get_running_loop_saved events._set_running_loop = self._set_running_loop_saved @@ -2885,7 +2885,7 @@ def get_event_loop(self): with self.assertRaises(TestError): asyncio.get_event_loop() - asyncio.set_event_loop(None) + asyncio._set_event_loop(None) with self.assertRaises(TestError): asyncio.get_event_loop() @@ -2900,10 +2900,10 @@ async def func(): loop.run_until_complete(func()) - asyncio.set_event_loop(loop) + asyncio._set_event_loop(loop) with self.assertRaises(TestError): asyncio.get_event_loop() - asyncio.set_event_loop(None) + asyncio._set_event_loop(None) with self.assertRaises(TestError): asyncio.get_event_loop() @@ -2927,7 +2927,7 @@ def test_get_event_loop_returns_running_loop2(self): with self.assertRaisesRegex(RuntimeError, 'no current'): asyncio.get_event_loop() - asyncio.set_event_loop(None) + asyncio._set_event_loop(None) with self.assertRaisesRegex(RuntimeError, 'no current'): asyncio.get_event_loop() @@ -2938,10 +2938,10 @@ async def func(): loop.run_until_complete(func()) - asyncio.set_event_loop(loop) + asyncio._set_event_loop(loop) self.assertIs(asyncio.get_event_loop(), loop) - asyncio.set_event_loop(None) + asyncio._set_event_loop(None) with self.assertRaisesRegex(RuntimeError, 'no current'): asyncio.get_event_loop() diff --git a/Lib/test/test_asyncio/test_futures.py b/Lib/test/test_asyncio/test_futures.py index 7db70a4c81d483..84b44011b9a844 100644 --- a/Lib/test/test_asyncio/test_futures.py +++ b/Lib/test/test_asyncio/test_futures.py @@ -178,8 +178,8 @@ async def test(): def test_constructor_use_global_loop(self): # Deprecated in 3.10, undeprecated in 3.12 - asyncio.set_event_loop(self.loop) - self.addCleanup(asyncio.set_event_loop, None) + asyncio._set_event_loop(self.loop) + self.addCleanup(asyncio._set_event_loop, None) f = self._new_future() self.assertIs(f._loop, self.loop) self.assertIs(f.get_loop(), self.loop) @@ -566,8 +566,8 @@ async def test(): def test_wrap_future_use_global_loop(self): # Deprecated in 3.10, undeprecated in 3.12 - asyncio.set_event_loop(self.loop) - self.addCleanup(asyncio.set_event_loop, None) + asyncio._set_event_loop(self.loop) + self.addCleanup(asyncio._set_event_loop, None) def run(arg): return (arg, threading.get_ident()) ex = concurrent.futures.ThreadPoolExecutor(1) diff --git a/Lib/test/test_asyncio/test_streams.py b/Lib/test/test_asyncio/test_streams.py index c3ba90b309e49f..047ada8c5d23df 100644 --- a/Lib/test/test_asyncio/test_streams.py +++ b/Lib/test/test_asyncio/test_streams.py @@ -71,7 +71,7 @@ def _basetest_open_connection_no_loop_ssl(self, open_connection_fut): try: reader, writer = self.loop.run_until_complete(open_connection_fut) finally: - asyncio.set_event_loop(None) + asyncio._set_event_loop(None) writer.write(b'GET / HTTP/1.0\r\n\r\n') f = reader.read() data = self.loop.run_until_complete(f) @@ -839,8 +839,8 @@ def test_streamreader_constructor_use_global_loop(self): # asyncio issue #184: Ensure that StreamReaderProtocol constructor # retrieves the current loop if the loop parameter is not set # Deprecated in 3.10, undeprecated in 3.12 - self.addCleanup(asyncio.set_event_loop, None) - asyncio.set_event_loop(self.loop) + self.addCleanup(asyncio._set_event_loop, None) + asyncio._set_event_loop(self.loop) reader = asyncio.StreamReader() self.assertIs(reader._loop, self.loop) @@ -863,8 +863,8 @@ def test_streamreaderprotocol_constructor_use_global_loop(self): # asyncio issue #184: Ensure that StreamReaderProtocol constructor # retrieves the current loop if the loop parameter is not set # Deprecated in 3.10, undeprecated in 3.12 - self.addCleanup(asyncio.set_event_loop, None) - asyncio.set_event_loop(self.loop) + self.addCleanup(asyncio._set_event_loop, None) + asyncio._set_event_loop(self.loop) reader = mock.Mock() protocol = asyncio.StreamReaderProtocol(reader) self.assertIs(protocol._loop, self.loop) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 7d6d0564a9a9db..b5363226ad79f4 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -212,8 +212,8 @@ async def test(): self.assertEqual(t.result(), 'ok') # Deprecated in 3.10, undeprecated in 3.12 - asyncio.set_event_loop(self.loop) - self.addCleanup(asyncio.set_event_loop, None) + asyncio._set_event_loop(self.loop) + self.addCleanup(asyncio._set_event_loop, None) t = asyncio.ensure_future(notmuch()) self.assertIs(t._loop, self.loop) self.loop.run_until_complete(t) @@ -2202,8 +2202,8 @@ def test_shield_coroutine_use_global_loop(self): async def coro(): return 42 - asyncio.set_event_loop(self.loop) - self.addCleanup(asyncio.set_event_loop, None) + asyncio._set_event_loop(self.loop) + self.addCleanup(asyncio._set_event_loop, None) outer = asyncio.shield(coro()) self.assertEqual(outer._loop, self.loop) res = self.loop.run_until_complete(outer) @@ -2273,7 +2273,7 @@ async def kill_me(loop): self.assertEqual(self.all_tasks(loop=self.loop), {task}) - asyncio.set_event_loop(None) + asyncio._set_event_loop(None) # execute the task so it waits for future self.loop._run_once() @@ -3278,8 +3278,8 @@ async def gather(): def test_constructor_empty_sequence_use_global_loop(self): # Deprecated in 3.10, undeprecated in 3.12 - asyncio.set_event_loop(self.one_loop) - self.addCleanup(asyncio.set_event_loop, None) + asyncio._set_event_loop(self.one_loop) + self.addCleanup(asyncio._set_event_loop, None) fut = asyncio.gather() self.assertIsInstance(fut, asyncio.Future) self.assertIs(fut._loop, self.one_loop) @@ -3386,8 +3386,8 @@ def test_constructor_use_global_loop(self): # Deprecated in 3.10, undeprecated in 3.12 async def coro(): return 'abc' - asyncio.set_event_loop(self.other_loop) - self.addCleanup(asyncio.set_event_loop, None) + asyncio._set_event_loop(self.other_loop) + self.addCleanup(asyncio._set_event_loop, None) gen1 = coro() gen2 = coro() fut = asyncio.gather(gen1, gen2) diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py index e9ee9702248015..ebb4cc0f7b64fd 100644 --- a/Lib/test/test_asyncio/test_unix_events.py +++ b/Lib/test/test_asyncio/test_unix_events.py @@ -1116,11 +1116,11 @@ class TestFunctional(unittest.TestCase): def setUp(self): self.loop = asyncio.new_event_loop() - asyncio.set_event_loop(self.loop) + asyncio._set_event_loop(self.loop) def tearDown(self): self.loop.close() - asyncio.set_event_loop(None) + asyncio._set_event_loop(None) def test_add_reader_invalid_argument(self): def assert_raises(): diff --git a/Lib/test/test_asyncio/utils.py b/Lib/test/test_asyncio/utils.py index b8dbe7feaac3f4..35ce13896da08f 100644 --- a/Lib/test/test_asyncio/utils.py +++ b/Lib/test/test_asyncio/utils.py @@ -541,7 +541,7 @@ def set_event_loop(self, loop, *, cleanup=True): if loop is None: raise AssertionError('loop is None') # ensure that the event loop is passed explicitly in asyncio - events.set_event_loop(None) + events._set_event_loop(None) if cleanup: self.addCleanup(self.close_loop, loop) @@ -554,7 +554,7 @@ def setUp(self): self._thread_cleanup = threading_helper.threading_setup() def tearDown(self): - events.set_event_loop(None) + events._set_event_loop(None) # Detect CPython bug #23353: ensure that yield/yield-from is not used # in an except block of a generator diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py index a72c43f9b47947..840043d5271224 100644 --- a/Lib/test/test_coroutines.py +++ b/Lib/test/test_coroutines.py @@ -2287,7 +2287,7 @@ async def f(): buffer.append('unreachable') loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) + asyncio._set_event_loop(loop) try: loop.run_until_complete(f()) except MyException: diff --git a/Lib/test/test_type_params.py b/Lib/test/test_type_params.py index 433b19593bdd04..89f836cf722966 100644 --- a/Lib/test/test_type_params.py +++ b/Lib/test/test_type_params.py @@ -1060,7 +1060,7 @@ async def coroutine[B](): co = get_coroutine() - self.addCleanup(asyncio.set_event_loop_policy, None) + self.addCleanup(asyncio._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 993e6bf013cfbf..fc996b42149dcb 100644 --- a/Lib/test/test_unittest/test_async_case.py +++ b/Lib/test/test_unittest/test_async_case.py @@ -476,7 +476,7 @@ async def cleanup(self, fut): def test_setup_get_event_loop(self): # See https://github.com/python/cpython/issues/95736 # Make sure the default event loop is not used - asyncio.set_event_loop(None) + asyncio._set_event_loop(None) class TestCase1(unittest.IsolatedAsyncioTestCase): def setUp(self): From 962bd63998fbc37cf42f7b8bcaebbb839f9744ae Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Tue, 24 Dec 2024 12:25:13 +0000 Subject: [PATCH 4/4] add test --- Lib/test/test_asyncio/test_events.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py index b9843f8a157479..c8439c9af5e6ba 100644 --- a/Lib/test/test_asyncio/test_events.py +++ b/Lib/test/test_asyncio/test_events.py @@ -2695,6 +2695,14 @@ async def inner(): class PolicyTests(unittest.TestCase): + def test_asyncio_set_event_loop_deprecation(self): + with self.assertWarnsRegex( + DeprecationWarning, "'asyncio.set_event_loop' is deprecated"): + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + self.assertIs(loop, asyncio.get_event_loop()) + loop.close() + def test_abstract_event_loop_policy_deprecation(self): with self.assertWarnsRegex( DeprecationWarning, "'asyncio.AbstractEventLoopPolicy' is deprecated"):