]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-127949: deprecate asyncio policy classes (#128216)
authorKumar Aditya <kumaraditya@python.org>
Tue, 24 Dec 2024 12:00:26 +0000 (17:30 +0530)
committerGitHub <noreply@github.com>
Tue, 24 Dec 2024 12:00:26 +0000 (17:30 +0530)
Doc/library/asyncio-policy.rst
Lib/asyncio/__init__.py
Lib/asyncio/events.py
Lib/asyncio/unix_events.py
Lib/asyncio/windows_events.py
Lib/test/test_asyncio/test_events.py
Lib/test/test_asyncio/test_runners.py
Lib/test/test_asyncio/test_windows_events.py

index 2d05c3a9f7f15704d5cf3b9fbbaf0a0483bef238..ea7fe957da7c405d97728154c2fe9cc740ecebf8 100644 (file)
@@ -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:
 
index 03165a425eb7d2580657de69d13e844837b11aa5..edb615b1b6b1c65c23d840c267d71c9263377724 100644 (file)
@@ -45,3 +45,19 @@ if sys.platform == 'win32':  # pragma: no cover
 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}")
index 1449245edc7c7e0959e1e41ff20a5f8b4eb16cfc..3ade7747149b32e0ec27f6fb79cbff15361c4960 100644 (file)
@@ -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 @@ class AbstractEventLoop:
         raise NotImplementedError
 
 
-class AbstractEventLoopPolicy:
+class _AbstractEventLoopPolicy:
     """Abstract policy for accessing the event loop."""
 
     def get_event_loop(self):
@@ -655,7 +655,7 @@ class AbstractEventLoopPolicy:
         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 @@ if hasattr(os, 'fork'):
     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)
 
index 0227eb506c6016857978df6933cf78c8c9583765..f69c6a64c39ae6e307d4cce087331bab8c0c58a1 100644 (file)
@@ -28,7 +28,7 @@ from .log import logger
 
 __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
index bf99bc271c7acd6073eb6dda1fffbaec1eca0ec5..5f75b17d8ca649be6256f8ce9815c7fb6e0dda62 100644 (file)
@@ -29,8 +29,8 @@ from .log import logger
 
 __all__ = (
     'SelectorEventLoop', 'ProactorEventLoop', 'IocpProactor',
-    'DefaultEventLoopPolicy', 'WindowsSelectorEventLoopPolicy',
-    'WindowsProactorEventLoopPolicy', 'EventLoop',
+    '_DefaultEventLoopPolicy', '_WindowsSelectorEventLoopPolicy',
+    '_WindowsProactorEventLoopPolicy', 'EventLoop',
 )
 
 
@@ -891,13 +891,13 @@ class _WindowsSubprocessTransport(base_subprocess.BaseSubprocessTransport):
 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
index d43f66c13d2f96fcf06afdc54faa10ef5e4a946d..c626670f72a084c67900ddf808a9fd7ed7882dc4 100644 (file)
@@ -2695,14 +2695,26 @@ class AbstractEventLoopTests(unittest.TestCase):
 
 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 @@ class PolicyTests(unittest.TestCase):
         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 @@ class PolicyTests(unittest.TestCase):
             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 @@ class PolicyTests(unittest.TestCase):
     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 @@ class PolicyTests(unittest.TestCase):
         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 @@ class PolicyTests(unittest.TestCase):
         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 @@ class PolicyTests(unittest.TestCase):
                 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 @@ class GetEventLoopTestsMixin:
         class TestError(Exception):
             pass
 
-        class Policy(asyncio.DefaultEventLoopPolicy):
+        class Policy(asyncio._DefaultEventLoopPolicy):
             def get_event_loop(self):
                 raise TestError
 
@@ -2908,7 +2920,7 @@ class GetEventLoopTestsMixin:
     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)
 
index e1f82f7f7bec0ca84aa26b571d5f2f111f6a81ba..21f277bc2d8d5f199d3152730508e59304c02a3c 100644 (file)
@@ -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
index 28b05d24dc25a1e669247eb5f822c790b7582fab..69e9905205eee0a1bbd3432ec772ddcd29222e25 100644 (file)
@@ -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 @@ class WinPolicyTests(WindowsEventsTestCase):
 
         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)