Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a391d80

Browse filesBrowse files
gh-127949: deprecate asyncio policy classes (#128216)
1 parent 3f6a618 commit a391d80
Copy full SHA for a391d80

File tree

8 files changed

+82
-34
lines changed
Filter options

8 files changed

+82
-34
lines changed

‎Doc/library/asyncio-policy.rst

Copy file name to clipboardExpand all lines: Doc/library/asyncio-policy.rst
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ The abstract event loop policy base class is defined as follows:
8787

8888
This method should never return ``None``.
8989

90+
.. deprecated:: next
91+
The :class:`AbstractEventLoopPolicy` class is deprecated and
92+
will be removed in Python 3.16.
93+
9094

9195
.. _asyncio-policy-builtin:
9296

@@ -109,6 +113,10 @@ asyncio ships with the following built-in policies:
109113
The :meth:`get_event_loop` method of the default asyncio policy now
110114
raises a :exc:`RuntimeError` if there is no set event loop.
111115

116+
.. deprecated:: next
117+
The :class:`DefaultEventLoopPolicy` class is deprecated and
118+
will be removed in Python 3.16.
119+
112120

113121
.. class:: WindowsSelectorEventLoopPolicy
114122

@@ -117,6 +125,10 @@ asyncio ships with the following built-in policies:
117125

118126
.. availability:: Windows.
119127

128+
.. deprecated:: next
129+
The :class:`WindowsSelectorEventLoopPolicy` class is deprecated and
130+
will be removed in Python 3.16.
131+
120132

121133
.. class:: WindowsProactorEventLoopPolicy
122134

@@ -125,6 +137,10 @@ asyncio ships with the following built-in policies:
125137

126138
.. availability:: Windows.
127139

140+
.. deprecated:: next
141+
The :class:`WindowsProactorEventLoopPolicy` class is deprecated and
142+
will be removed in Python 3.16.
143+
128144

129145
.. _asyncio-custom-policies:
130146

‎Lib/asyncio/__init__.py

Copy file name to clipboardExpand all lines: Lib/asyncio/__init__.py
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,19 @@
4545
else:
4646
from .unix_events import * # pragma: no cover
4747
__all__ += unix_events.__all__
48+
49+
def __getattr__(name: str):
50+
import warnings
51+
52+
deprecated = {
53+
"AbstractEventLoopPolicy",
54+
"DefaultEventLoopPolicy",
55+
"WindowsSelectorEventLoopPolicy",
56+
"WindowsProactorEventLoopPolicy",
57+
}
58+
if name in deprecated:
59+
warnings._deprecated(f"asyncio.{name}", remove=(3, 16))
60+
# deprecated things have underscores in front of them
61+
return globals()["_" + name]
62+
63+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")

‎Lib/asyncio/events.py

Copy file name to clipboardExpand all lines: Lib/asyncio/events.py
+7-7Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# SPDX-FileCopyrightText: Copyright (c) 2015-2021 MagicStack Inc. http://magic.io
66

77
__all__ = (
8-
'AbstractEventLoopPolicy',
8+
'_AbstractEventLoopPolicy',
99
'AbstractEventLoop', 'AbstractServer',
1010
'Handle', 'TimerHandle',
1111
'_get_event_loop_policy',
@@ -632,7 +632,7 @@ def set_debug(self, enabled):
632632
raise NotImplementedError
633633

634634

635-
class AbstractEventLoopPolicy:
635+
class _AbstractEventLoopPolicy:
636636
"""Abstract policy for accessing the event loop."""
637637

638638
def get_event_loop(self):
@@ -655,7 +655,7 @@ def new_event_loop(self):
655655
the current context, set_event_loop must be called explicitly."""
656656
raise NotImplementedError
657657

658-
class BaseDefaultEventLoopPolicy(AbstractEventLoopPolicy):
658+
class _BaseDefaultEventLoopPolicy(_AbstractEventLoopPolicy):
659659
"""Default policy implementation for accessing the event loop.
660660
661661
In this policy, each thread has its own event loop. However, we
@@ -758,8 +758,8 @@ def _init_event_loop_policy():
758758
global _event_loop_policy
759759
with _lock:
760760
if _event_loop_policy is None: # pragma: no branch
761-
from . import DefaultEventLoopPolicy
762-
_event_loop_policy = DefaultEventLoopPolicy()
761+
from . import _DefaultEventLoopPolicy
762+
_event_loop_policy = _DefaultEventLoopPolicy()
763763

764764

765765
def _get_event_loop_policy():
@@ -777,7 +777,7 @@ def _set_event_loop_policy(policy):
777777
778778
If policy is None, the default policy is restored."""
779779
global _event_loop_policy
780-
if policy is not None and not isinstance(policy, AbstractEventLoopPolicy):
780+
if policy is not None and not isinstance(policy, _AbstractEventLoopPolicy):
781781
raise TypeError(f"policy must be an instance of AbstractEventLoopPolicy or None, not '{type(policy).__name__}'")
782782
_event_loop_policy = policy
783783

@@ -838,7 +838,7 @@ def new_event_loop():
838838
def on_fork():
839839
# Reset the loop and wakeupfd in the forked child process.
840840
if _event_loop_policy is not None:
841-
_event_loop_policy._local = BaseDefaultEventLoopPolicy._Local()
841+
_event_loop_policy._local = _BaseDefaultEventLoopPolicy._Local()
842842
_set_running_loop(None)
843843
signal.set_wakeup_fd(-1)
844844

‎Lib/asyncio/unix_events.py

Copy file name to clipboardExpand all lines: Lib/asyncio/unix_events.py
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
__all__ = (
3030
'SelectorEventLoop',
31-
'DefaultEventLoopPolicy',
31+
'_DefaultEventLoopPolicy',
3232
'EventLoop',
3333
)
3434

@@ -963,11 +963,11 @@ def can_use_pidfd():
963963
return True
964964

965965

966-
class _UnixDefaultEventLoopPolicy(events.BaseDefaultEventLoopPolicy):
966+
class _UnixDefaultEventLoopPolicy(events._BaseDefaultEventLoopPolicy):
967967
"""UNIX event loop policy"""
968968
_loop_factory = _UnixSelectorEventLoop
969969

970970

971971
SelectorEventLoop = _UnixSelectorEventLoop
972-
DefaultEventLoopPolicy = _UnixDefaultEventLoopPolicy
972+
_DefaultEventLoopPolicy = _UnixDefaultEventLoopPolicy
973973
EventLoop = SelectorEventLoop

‎Lib/asyncio/windows_events.py

Copy file name to clipboardExpand all lines: Lib/asyncio/windows_events.py
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929

3030
__all__ = (
3131
'SelectorEventLoop', 'ProactorEventLoop', 'IocpProactor',
32-
'DefaultEventLoopPolicy', 'WindowsSelectorEventLoopPolicy',
33-
'WindowsProactorEventLoopPolicy', 'EventLoop',
32+
'_DefaultEventLoopPolicy', '_WindowsSelectorEventLoopPolicy',
33+
'_WindowsProactorEventLoopPolicy', 'EventLoop',
3434
)
3535

3636

@@ -891,13 +891,13 @@ def callback(f):
891891
SelectorEventLoop = _WindowsSelectorEventLoop
892892

893893

894-
class WindowsSelectorEventLoopPolicy(events.BaseDefaultEventLoopPolicy):
894+
class _WindowsSelectorEventLoopPolicy(events._BaseDefaultEventLoopPolicy):
895895
_loop_factory = SelectorEventLoop
896896

897897

898-
class WindowsProactorEventLoopPolicy(events.BaseDefaultEventLoopPolicy):
898+
class _WindowsProactorEventLoopPolicy(events._BaseDefaultEventLoopPolicy):
899899
_loop_factory = ProactorEventLoop
900900

901901

902-
DefaultEventLoopPolicy = WindowsProactorEventLoopPolicy
902+
_DefaultEventLoopPolicy = _WindowsProactorEventLoopPolicy
903903
EventLoop = ProactorEventLoop

‎Lib/test/test_asyncio/test_events.py

Copy file name to clipboardExpand all lines: Lib/test/test_asyncio/test_events.py
+23-11Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2695,22 +2695,34 @@ async def inner():
26952695

26962696
class PolicyTests(unittest.TestCase):
26972697

2698+
def test_abstract_event_loop_policy_deprecation(self):
2699+
with self.assertWarnsRegex(
2700+
DeprecationWarning, "'asyncio.AbstractEventLoopPolicy' is deprecated"):
2701+
policy = asyncio.AbstractEventLoopPolicy()
2702+
self.assertIsInstance(policy, asyncio.AbstractEventLoopPolicy)
2703+
2704+
def test_default_event_loop_policy_deprecation(self):
2705+
with self.assertWarnsRegex(
2706+
DeprecationWarning, "'asyncio.DefaultEventLoopPolicy' is deprecated"):
2707+
policy = asyncio.DefaultEventLoopPolicy()
2708+
self.assertIsInstance(policy, asyncio.DefaultEventLoopPolicy)
2709+
26982710
def test_event_loop_policy(self):
2699-
policy = asyncio.AbstractEventLoopPolicy()
2711+
policy = asyncio._AbstractEventLoopPolicy()
27002712
self.assertRaises(NotImplementedError, policy.get_event_loop)
27012713
self.assertRaises(NotImplementedError, policy.set_event_loop, object())
27022714
self.assertRaises(NotImplementedError, policy.new_event_loop)
27032715

27042716
def test_get_event_loop(self):
2705-
policy = asyncio.DefaultEventLoopPolicy()
2717+
policy = asyncio._DefaultEventLoopPolicy()
27062718
self.assertIsNone(policy._local._loop)
27072719

27082720
with self.assertRaises(RuntimeError):
27092721
loop = policy.get_event_loop()
27102722
self.assertIsNone(policy._local._loop)
27112723

27122724
def test_get_event_loop_does_not_call_set_event_loop(self):
2713-
policy = asyncio.DefaultEventLoopPolicy()
2725+
policy = asyncio._DefaultEventLoopPolicy()
27142726

27152727
with mock.patch.object(
27162728
policy, "set_event_loop",
@@ -2722,30 +2734,30 @@ def test_get_event_loop_does_not_call_set_event_loop(self):
27222734
m_set_event_loop.assert_not_called()
27232735

27242736
def test_get_event_loop_after_set_none(self):
2725-
policy = asyncio.DefaultEventLoopPolicy()
2737+
policy = asyncio._DefaultEventLoopPolicy()
27262738
policy.set_event_loop(None)
27272739
self.assertRaises(RuntimeError, policy.get_event_loop)
27282740

27292741
@mock.patch('asyncio.events.threading.current_thread')
27302742
def test_get_event_loop_thread(self, m_current_thread):
27312743

27322744
def f():
2733-
policy = asyncio.DefaultEventLoopPolicy()
2745+
policy = asyncio._DefaultEventLoopPolicy()
27342746
self.assertRaises(RuntimeError, policy.get_event_loop)
27352747

27362748
th = threading.Thread(target=f)
27372749
th.start()
27382750
th.join()
27392751

27402752
def test_new_event_loop(self):
2741-
policy = asyncio.DefaultEventLoopPolicy()
2753+
policy = asyncio._DefaultEventLoopPolicy()
27422754

27432755
loop = policy.new_event_loop()
27442756
self.assertIsInstance(loop, asyncio.AbstractEventLoop)
27452757
loop.close()
27462758

27472759
def test_set_event_loop(self):
2748-
policy = asyncio.DefaultEventLoopPolicy()
2760+
policy = asyncio._DefaultEventLoopPolicy()
27492761
old_loop = policy.new_event_loop()
27502762
policy.set_event_loop(old_loop)
27512763

@@ -2762,7 +2774,7 @@ def test_get_event_loop_policy(self):
27622774
with self.assertWarnsRegex(
27632775
DeprecationWarning, "'asyncio.get_event_loop_policy' is deprecated"):
27642776
policy = asyncio.get_event_loop_policy()
2765-
self.assertIsInstance(policy, asyncio.AbstractEventLoopPolicy)
2777+
self.assertIsInstance(policy, asyncio._AbstractEventLoopPolicy)
27662778
self.assertIs(policy, asyncio.get_event_loop_policy())
27672779

27682780
def test_set_event_loop_policy(self):
@@ -2775,7 +2787,7 @@ def test_set_event_loop_policy(self):
27752787
DeprecationWarning, "'asyncio.get_event_loop_policy' is deprecated"):
27762788
old_policy = asyncio.get_event_loop_policy()
27772789

2778-
policy = asyncio.DefaultEventLoopPolicy()
2790+
policy = asyncio._DefaultEventLoopPolicy()
27792791
with self.assertWarnsRegex(
27802792
DeprecationWarning, "'asyncio.set_event_loop_policy' is deprecated"):
27812793
asyncio.set_event_loop_policy(policy)
@@ -2862,7 +2874,7 @@ def test_get_event_loop_returns_running_loop(self):
28622874
class TestError(Exception):
28632875
pass
28642876

2865-
class Policy(asyncio.DefaultEventLoopPolicy):
2877+
class Policy(asyncio._DefaultEventLoopPolicy):
28662878
def get_event_loop(self):
28672879
raise TestError
28682880

@@ -2908,7 +2920,7 @@ async def func():
29082920
def test_get_event_loop_returns_running_loop2(self):
29092921
old_policy = asyncio._get_event_loop_policy()
29102922
try:
2911-
asyncio._set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
2923+
asyncio._set_event_loop_policy(asyncio._DefaultEventLoopPolicy())
29122924
loop = asyncio.new_event_loop()
29132925
self.addCleanup(loop.close)
29142926

‎Lib/test/test_asyncio/test_runners.py

Copy file name to clipboardExpand all lines: Lib/test/test_asyncio/test_runners.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def interrupt_self():
1919
_thread.interrupt_main()
2020

2121

22-
class TestPolicy(asyncio.AbstractEventLoopPolicy):
22+
class TestPolicy(asyncio._AbstractEventLoopPolicy):
2323

2424
def __init__(self, loop_factory):
2525
self.loop_factory = loop_factory

‎Lib/test/test_asyncio/test_windows_events.py

Copy file name to clipboardExpand all lines: Lib/test/test_asyncio/test_windows_events.py
+11-7Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -328,14 +328,15 @@ class WinPolicyTests(WindowsEventsTestCase):
328328

329329
def test_selector_win_policy(self):
330330
async def main():
331-
self.assertIsInstance(
332-
asyncio.get_running_loop(),
333-
asyncio.SelectorEventLoop)
331+
self.assertIsInstance(asyncio.get_running_loop(), asyncio.SelectorEventLoop)
334332

335333
old_policy = asyncio._get_event_loop_policy()
336334
try:
337-
asyncio._set_event_loop_policy(
338-
asyncio.WindowsSelectorEventLoopPolicy())
335+
with self.assertWarnsRegex(
336+
DeprecationWarning,
337+
"'asyncio.WindowsSelectorEventLoopPolicy' is deprecated",
338+
):
339+
asyncio._set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
339340
asyncio.run(main())
340341
finally:
341342
asyncio._set_event_loop_policy(old_policy)
@@ -348,8 +349,11 @@ async def main():
348349

349350
old_policy = asyncio._get_event_loop_policy()
350351
try:
351-
asyncio._set_event_loop_policy(
352-
asyncio.WindowsProactorEventLoopPolicy())
352+
with self.assertWarnsRegex(
353+
DeprecationWarning,
354+
"'asyncio.WindowsProactorEventLoopPolicy' is deprecated",
355+
):
356+
asyncio._set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
353357
asyncio.run(main())
354358
finally:
355359
asyncio._set_event_loop_policy(old_policy)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.