Skip to content

Commit 5892853

Browse files
gh-127949: deprecate asyncio.set_event_loop_policy (#128024)
First step towards deprecating the asyncio policy system. This deprecates `asyncio.set_event_loop_policy` and will be removed in Python 3.16.
1 parent 559b0e7 commit 5892853

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+81
-67
lines changed

Doc/library/asyncio-policy.rst

+4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ for the current process:
4646

4747
If *policy* is set to ``None``, the default policy is restored.
4848

49+
.. deprecated:: next
50+
The :func:`set_event_loop_policy` function is deprecated and
51+
will be removed in Python 3.16.
52+
4953

5054
.. _asyncio-policy-objects:
5155

Lib/asyncio/events.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
'AbstractEventLoopPolicy',
99
'AbstractEventLoop', 'AbstractServer',
1010
'Handle', 'TimerHandle',
11-
'get_event_loop_policy', 'set_event_loop_policy',
11+
'get_event_loop_policy',
12+
'_set_event_loop_policy',
13+
'set_event_loop_policy',
1214
'get_event_loop', 'set_event_loop', 'new_event_loop',
1315
'_set_running_loop', 'get_running_loop',
1416
'_get_running_loop',
@@ -21,6 +23,7 @@
2123
import subprocess
2224
import sys
2325
import threading
26+
import warnings
2427

2528
from . import format_helpers
2629

@@ -765,7 +768,7 @@ def get_event_loop_policy():
765768
return _event_loop_policy
766769

767770

768-
def set_event_loop_policy(policy):
771+
def _set_event_loop_policy(policy):
769772
"""Set the current event loop policy.
770773
771774
If policy is None, the default policy is restored."""
@@ -774,6 +777,9 @@ def set_event_loop_policy(policy):
774777
raise TypeError(f"policy must be an instance of AbstractEventLoopPolicy or None, not '{type(policy).__name__}'")
775778
_event_loop_policy = policy
776779

780+
def set_event_loop_policy(policy):
781+
warnings._deprecated('set_event_loop_policy', remove=(3,16))
782+
_set_event_loop_policy(policy)
777783

778784
def get_event_loop():
779785
"""Return an asyncio event loop.

Lib/test/libregrtest/save_env.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def get_asyncio_events__event_loop_policy(self):
9797
return support.maybe_get_event_loop_policy()
9898
def restore_asyncio_events__event_loop_policy(self, policy):
9999
asyncio = self.get_module('asyncio')
100-
asyncio.set_event_loop_policy(policy)
100+
asyncio._set_event_loop_policy(policy)
101101

102102
def get_sys_argv(self):
103103
return id(sys.argv), sys.argv, sys.argv[:]

Lib/test/test_asyncgen.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ def setUp(self):
629629
def tearDown(self):
630630
self.loop.close()
631631
self.loop = None
632-
asyncio.set_event_loop_policy(None)
632+
asyncio._set_event_loop_policy(None)
633633

634634
def check_async_iterator_anext(self, ait_class):
635635
with self.subTest(anext="pure-Python"):

Lib/test/test_asyncio/test_base_events.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626

2727
def tearDownModule():
28-
asyncio.set_event_loop_policy(None)
28+
asyncio._set_event_loop_policy(None)
2929

3030

3131
def mock_socket_module():

Lib/test/test_asyncio/test_buffered_proto.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
def tearDownModule():
8-
asyncio.set_event_loop_policy(None)
8+
asyncio._set_event_loop_policy(None)
99

1010

1111
class ReceiveStuffProto(asyncio.BufferedProtocol):

Lib/test/test_asyncio/test_context.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
def tearDownModule():
7-
asyncio.set_event_loop_policy(None)
7+
asyncio._set_event_loop_policy(None)
88

99

1010
@unittest.skipUnless(decimal.HAVE_CONTEXTVAR, "decimal is built with a thread-local context")

Lib/test/test_asyncio/test_eager_task_factory.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
def tearDownModule():
16-
asyncio.set_event_loop_policy(None)
16+
asyncio._set_event_loop_policy(None)
1717

1818

1919
class EagerTaskFactoryLoopTests:

Lib/test/test_asyncio/test_events.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@
3636
from test.support import socket_helper
3737
from test.support import threading_helper
3838
from test.support import ALWAYS_EQ, LARGEST, SMALLEST
39-
39+
from test.support import warnings_helper
4040

4141
def tearDownModule():
42-
asyncio.set_event_loop_policy(None)
42+
asyncio._set_event_loop_policy(None)
4343

4444

4545
def broken_unix_getsockname():
@@ -2764,13 +2764,17 @@ def test_get_event_loop_policy(self):
27642764
self.assertIs(policy, asyncio.get_event_loop_policy())
27652765

27662766
def test_set_event_loop_policy(self):
2767-
self.assertRaises(
2768-
TypeError, asyncio.set_event_loop_policy, object())
2767+
with self.assertWarnsRegex(
2768+
DeprecationWarning, "'set_event_loop_policy' is deprecated"):
2769+
self.assertRaises(
2770+
TypeError, asyncio.set_event_loop_policy, object())
27692771

27702772
old_policy = asyncio.get_event_loop_policy()
27712773

27722774
policy = asyncio.DefaultEventLoopPolicy()
2773-
asyncio.set_event_loop_policy(policy)
2775+
with self.assertWarnsRegex(
2776+
DeprecationWarning, "'set_event_loop_policy' is deprecated"):
2777+
asyncio.set_event_loop_policy(policy)
27742778
self.assertIs(policy, asyncio.get_event_loop_policy())
27752779
self.assertIsNot(policy, old_policy)
27762780

@@ -2857,7 +2861,7 @@ def get_event_loop(self):
28572861

28582862
old_policy = asyncio.get_event_loop_policy()
28592863
try:
2860-
asyncio.set_event_loop_policy(Policy())
2864+
asyncio._set_event_loop_policy(Policy())
28612865
loop = asyncio.new_event_loop()
28622866

28632867
with self.assertRaises(TestError):
@@ -2885,7 +2889,7 @@ async def func():
28852889
asyncio.get_event_loop()
28862890

28872891
finally:
2888-
asyncio.set_event_loop_policy(old_policy)
2892+
asyncio._set_event_loop_policy(old_policy)
28892893
if loop is not None:
28902894
loop.close()
28912895

@@ -2897,7 +2901,7 @@ async def func():
28972901
def test_get_event_loop_returns_running_loop2(self):
28982902
old_policy = asyncio.get_event_loop_policy()
28992903
try:
2900-
asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
2904+
asyncio._set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
29012905
loop = asyncio.new_event_loop()
29022906
self.addCleanup(loop.close)
29032907

@@ -2923,7 +2927,7 @@ async def func():
29232927
asyncio.get_event_loop()
29242928

29252929
finally:
2926-
asyncio.set_event_loop_policy(old_policy)
2930+
asyncio._set_event_loop_policy(old_policy)
29272931
if loop is not None:
29282932
loop.close()
29292933

Lib/test/test_asyncio/test_futures.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818

1919
def tearDownModule():
20-
asyncio.set_event_loop_policy(None)
20+
asyncio._set_event_loop_policy(None)
2121

2222

2323
def _fakefunc(f):

Lib/test/test_asyncio/test_futures2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
def tearDownModule():
10-
asyncio.set_event_loop_policy(None)
10+
asyncio._set_event_loop_policy(None)
1111

1212

1313
class FutureTests:

Lib/test/test_asyncio/test_locks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121

2222
def tearDownModule():
23-
asyncio.set_event_loop_policy(None)
23+
asyncio._set_event_loop_policy(None)
2424

2525

2626
class LockTests(unittest.IsolatedAsyncioTestCase):

Lib/test/test_asyncio/test_pep492.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
def tearDownModule():
14-
asyncio.set_event_loop_policy(None)
14+
asyncio._set_event_loop_policy(None)
1515

1616

1717
# Test that asyncio.iscoroutine() uses collections.abc.Coroutine

Lib/test/test_asyncio/test_proactor_events.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919

2020
def tearDownModule():
21-
asyncio.set_event_loop_policy(None)
21+
asyncio._set_event_loop_policy(None)
2222

2323

2424
def close_transport(transport):

Lib/test/test_asyncio/test_protocols.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
def tearDownModule():
88
# not needed for the test file but added for uniformness with all other
99
# asyncio test files for the sake of unified cleanup
10-
asyncio.set_event_loop_policy(None)
10+
asyncio._set_event_loop_policy(None)
1111

1212

1313
class ProtocolsAbsTests(unittest.TestCase):

Lib/test/test_asyncio/test_queues.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
def tearDownModule():
9-
asyncio.set_event_loop_policy(None)
9+
asyncio._set_event_loop_policy(None)
1010

1111

1212
class QueueBasicTests(unittest.IsolatedAsyncioTestCase):

Lib/test/test_asyncio/test_runners.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
def tearDownModule():
15-
asyncio.set_event_loop_policy(None)
15+
asyncio._set_event_loop_policy(None)
1616

1717

1818
def interrupt_self():
@@ -61,15 +61,15 @@ def setUp(self):
6161
super().setUp()
6262

6363
policy = TestPolicy(self.new_loop)
64-
asyncio.set_event_loop_policy(policy)
64+
asyncio._set_event_loop_policy(policy)
6565

6666
def tearDown(self):
6767
policy = asyncio.get_event_loop_policy()
6868
if policy.loop is not None:
6969
self.assertTrue(policy.loop.is_closed())
7070
self.assertTrue(policy.loop.shutdown_ag_run)
7171

72-
asyncio.set_event_loop_policy(None)
72+
asyncio._set_event_loop_policy(None)
7373
super().tearDown()
7474

7575

@@ -259,7 +259,7 @@ def new_event_loop():
259259
loop.set_task_factory(Task)
260260
return loop
261261

262-
asyncio.set_event_loop_policy(TestPolicy(new_event_loop))
262+
asyncio._set_event_loop_policy(TestPolicy(new_event_loop))
263263
with self.assertRaises(asyncio.CancelledError):
264264
asyncio.run(main())
265265

Lib/test/test_asyncio/test_selector_events.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525

2626
def tearDownModule():
27-
asyncio.set_event_loop_policy(None)
27+
asyncio._set_event_loop_policy(None)
2828

2929

3030
class TestBaseSelectorEventLoop(BaseSelectorEventLoop):

Lib/test/test_asyncio/test_sendfile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323

2424
def tearDownModule():
25-
asyncio.set_event_loop_policy(None)
25+
asyncio._set_event_loop_policy(None)
2626

2727

2828
class MySendfileProto(asyncio.Protocol):

Lib/test/test_asyncio/test_server.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
def tearDownModule():
14-
asyncio.set_event_loop_policy(None)
14+
asyncio._set_event_loop_policy(None)
1515

1616

1717
class BaseStartServer(func_tests.FunctionalTestCaseMixin):

Lib/test/test_asyncio/test_sock_lowlevel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
def tearDownModule():
18-
asyncio.set_event_loop_policy(None)
18+
asyncio._set_event_loop_policy(None)
1919

2020

2121
class MyProto(asyncio.Protocol):

Lib/test/test_asyncio/test_ssl.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030

3131
def tearDownModule():
32-
asyncio.set_event_loop_policy(None)
32+
asyncio._set_event_loop_policy(None)
3333

3434

3535
class MyBaseProto(asyncio.Protocol):

Lib/test/test_asyncio/test_sslproto.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
def tearDownModule():
24-
asyncio.set_event_loop_policy(None)
24+
asyncio._set_event_loop_policy(None)
2525

2626

2727
@unittest.skipIf(ssl is None, 'No ssl module')

Lib/test/test_asyncio/test_staggered.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
def tearDownModule():
11-
asyncio.set_event_loop_policy(None)
11+
asyncio._set_event_loop_policy(None)
1212

1313

1414
class StaggeredTests(unittest.IsolatedAsyncioTestCase):

Lib/test/test_asyncio/test_streams.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
def tearDownModule():
24-
asyncio.set_event_loop_policy(None)
24+
asyncio._set_event_loop_policy(None)
2525

2626

2727
class StreamTests(test_utils.TestCase):

Lib/test/test_asyncio/test_subprocess.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838

3939
def tearDownModule():
40-
asyncio.set_event_loop_policy(None)
40+
asyncio._set_event_loop_policy(None)
4141

4242

4343
class TestSubprocessTransport(base_subprocess.BaseSubprocessTransport):

Lib/test/test_asyncio/test_taskgroups.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
# To prevent a warning "test altered the execution environment"
1616
def tearDownModule():
17-
asyncio.set_event_loop_policy(None)
17+
asyncio._set_event_loop_policy(None)
1818

1919

2020
class MyExc(Exception):

Lib/test/test_asyncio/test_tasks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525

2626
def tearDownModule():
27-
asyncio.set_event_loop_policy(None)
27+
asyncio._set_event_loop_policy(None)
2828

2929

3030
async def coroutine_function():

Lib/test/test_asyncio/test_threads.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
def tearDownModule():
11-
asyncio.set_event_loop_policy(None)
11+
asyncio._set_event_loop_policy(None)
1212

1313

1414
class ToThreadTests(unittest.IsolatedAsyncioTestCase):

Lib/test/test_asyncio/test_timeouts.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
def tearDownModule():
12-
asyncio.set_event_loop_policy(None)
12+
asyncio._set_event_loop_policy(None)
1313

1414
class TimeoutTests(unittest.IsolatedAsyncioTestCase):
1515

Lib/test/test_asyncio/test_transports.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
def tearDownModule():
1111
# not needed for the test file but added for uniformness with all other
1212
# asyncio test files for the sake of unified cleanup
13-
asyncio.set_event_loop_policy(None)
13+
asyncio._set_event_loop_policy(None)
1414

1515

1616
class TransportTests(unittest.TestCase):

0 commit comments

Comments
 (0)