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.
This commit is contained in:
Kumar Aditya 2024-12-18 11:35:29 +05:30 committed by GitHub
parent 559b0e7013
commit 5892853fb7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
46 changed files with 81 additions and 67 deletions

View file

@ -46,6 +46,10 @@ for the current process:
If *policy* is set to ``None``, the default policy is restored.
.. deprecated:: next
The :func:`set_event_loop_policy` function is deprecated and
will be removed in Python 3.16.
.. _asyncio-policy-objects:

View file

@ -8,7 +8,9 @@ __all__ = (
'AbstractEventLoopPolicy',
'AbstractEventLoop', 'AbstractServer',
'Handle', 'TimerHandle',
'get_event_loop_policy', 'set_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',
@ -21,6 +23,7 @@ import socket
import subprocess
import sys
import threading
import warnings
from . import format_helpers
@ -765,7 +768,7 @@ def get_event_loop_policy():
return _event_loop_policy
def set_event_loop_policy(policy):
def _set_event_loop_policy(policy):
"""Set the current event loop policy.
If policy is None, the default policy is restored."""
@ -774,6 +777,9 @@ def set_event_loop_policy(policy):
raise TypeError(f"policy must be an instance of AbstractEventLoopPolicy or None, not '{type(policy).__name__}'")
_event_loop_policy = policy
def set_event_loop_policy(policy):
warnings._deprecated('set_event_loop_policy', remove=(3,16))
_set_event_loop_policy(policy)
def get_event_loop():
"""Return an asyncio event loop.

View file

@ -97,7 +97,7 @@ class saved_test_environment:
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)
asyncio._set_event_loop_policy(policy)
def get_sys_argv(self):
return id(sys.argv), sys.argv, sys.argv[:]

View file

@ -629,7 +629,7 @@ class AsyncGenAsyncioTest(unittest.TestCase):
def tearDown(self):
self.loop.close()
self.loop = None
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
def check_async_iterator_anext(self, ait_class):
with self.subTest(anext="pure-Python"):

View file

@ -25,7 +25,7 @@ MOCK_ANY = mock.ANY
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
def mock_socket_module():

View file

@ -5,7 +5,7 @@ from test.test_asyncio import functional as func_tests
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class ReceiveStuffProto(asyncio.BufferedProtocol):

View file

@ -4,7 +4,7 @@ import unittest
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
@unittest.skipUnless(decimal.HAVE_CONTEXTVAR, "decimal is built with a thread-local context")

View file

@ -13,7 +13,7 @@ MOCK_ANY = mock.ANY
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class EagerTaskFactoryLoopTests:

View file

@ -36,10 +36,10 @@ from test import support
from test.support import socket_helper
from test.support import threading_helper
from test.support import ALWAYS_EQ, LARGEST, SMALLEST
from test.support import warnings_helper
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
def broken_unix_getsockname():
@ -2764,12 +2764,16 @@ class PolicyTests(unittest.TestCase):
self.assertIs(policy, asyncio.get_event_loop_policy())
def test_set_event_loop_policy(self):
with self.assertWarnsRegex(
DeprecationWarning, "'set_event_loop_policy' is deprecated"):
self.assertRaises(
TypeError, asyncio.set_event_loop_policy, object())
old_policy = asyncio.get_event_loop_policy()
policy = asyncio.DefaultEventLoopPolicy()
with self.assertWarnsRegex(
DeprecationWarning, "'set_event_loop_policy' is deprecated"):
asyncio.set_event_loop_policy(policy)
self.assertIs(policy, asyncio.get_event_loop_policy())
self.assertIsNot(policy, old_policy)
@ -2857,7 +2861,7 @@ class GetEventLoopTestsMixin:
old_policy = asyncio.get_event_loop_policy()
try:
asyncio.set_event_loop_policy(Policy())
asyncio._set_event_loop_policy(Policy())
loop = asyncio.new_event_loop()
with self.assertRaises(TestError):
@ -2885,7 +2889,7 @@ class GetEventLoopTestsMixin:
asyncio.get_event_loop()
finally:
asyncio.set_event_loop_policy(old_policy)
asyncio._set_event_loop_policy(old_policy)
if loop is not None:
loop.close()
@ -2897,7 +2901,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)
@ -2923,7 +2927,7 @@ class GetEventLoopTestsMixin:
asyncio.get_event_loop()
finally:
asyncio.set_event_loop_policy(old_policy)
asyncio._set_event_loop_policy(old_policy)
if loop is not None:
loop.close()

View file

@ -17,7 +17,7 @@ from test import support
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
def _fakefunc(f):

View file

@ -7,7 +7,7 @@ from asyncio import tasks
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class FutureTests:

View file

@ -20,7 +20,7 @@ RGX_REPR = re.compile(STR_RGX_REPR)
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class LockTests(unittest.IsolatedAsyncioTestCase):

View file

@ -11,7 +11,7 @@ from test.test_asyncio import utils as test_utils
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
# Test that asyncio.iscoroutine() uses collections.abc.Coroutine

View file

@ -18,7 +18,7 @@ from test.test_asyncio import utils as test_utils
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
def close_transport(transport):

View file

@ -7,7 +7,7 @@ import asyncio
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)
asyncio._set_event_loop_policy(None)
class ProtocolsAbsTests(unittest.TestCase):

View file

@ -6,7 +6,7 @@ from types import GenericAlias
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class QueueBasicTests(unittest.IsolatedAsyncioTestCase):

View file

@ -12,7 +12,7 @@ from unittest.mock import patch
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
def interrupt_self():
@ -61,7 +61,7 @@ class BaseTest(unittest.TestCase):
super().setUp()
policy = TestPolicy(self.new_loop)
asyncio.set_event_loop_policy(policy)
asyncio._set_event_loop_policy(policy)
def tearDown(self):
policy = asyncio.get_event_loop_policy()
@ -69,7 +69,7 @@ class BaseTest(unittest.TestCase):
self.assertTrue(policy.loop.is_closed())
self.assertTrue(policy.loop.shutdown_ag_run)
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
super().tearDown()
@ -259,7 +259,7 @@ class RunTests(BaseTest):
loop.set_task_factory(Task)
return loop
asyncio.set_event_loop_policy(TestPolicy(new_event_loop))
asyncio._set_event_loop_policy(TestPolicy(new_event_loop))
with self.assertRaises(asyncio.CancelledError):
asyncio.run(main())

View file

@ -24,7 +24,7 @@ MOCK_ANY = mock.ANY
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class TestBaseSelectorEventLoop(BaseSelectorEventLoop):

View file

@ -22,7 +22,7 @@ except ImportError:
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class MySendfileProto(asyncio.Protocol):

View file

@ -11,7 +11,7 @@ from test.test_asyncio import functional as func_tests
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class BaseStartServer(func_tests.FunctionalTestCaseMixin):

View file

@ -15,7 +15,7 @@ if socket_helper.tcp_blackhole():
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class MyProto(asyncio.Protocol):

View file

@ -29,7 +29,7 @@ BUF_MULTIPLIER = 1024 if not MACOS else 64
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class MyBaseProto(asyncio.Protocol):

View file

@ -21,7 +21,7 @@ from test.test_asyncio import functional as func_tests
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
@unittest.skipIf(ssl is None, 'No ssl module')

View file

@ -8,7 +8,7 @@ support.requires_working_socket(module=True)
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class StaggeredTests(unittest.IsolatedAsyncioTestCase):

View file

@ -21,7 +21,7 @@ from test.support import requires_subprocess, socket_helper
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class StreamTests(test_utils.TestCase):

View file

@ -37,7 +37,7 @@ PROGRAM_CAT = [
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class TestSubprocessTransport(base_subprocess.BaseSubprocessTransport):

View file

@ -14,7 +14,7 @@ from test.test_asyncio.utils import await_without_task
# To prevent a warning "test altered the execution environment"
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class MyExc(Exception):

View file

@ -24,7 +24,7 @@ from test.support.warnings_helper import ignore_warnings
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
async def coroutine_function():

View file

@ -8,7 +8,7 @@ from unittest import mock
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class ToThreadTests(unittest.IsolatedAsyncioTestCase):

View file

@ -9,7 +9,7 @@ from test.test_asyncio.utils import await_without_task
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class TimeoutTests(unittest.IsolatedAsyncioTestCase):

View file

@ -10,7 +10,7 @@ from asyncio import transports
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)
asyncio._set_event_loop_policy(None)
class TransportTests(unittest.TestCase):

View file

@ -33,7 +33,7 @@ from test.test_asyncio import utils as test_utils
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
MOCK_ANY = mock.ANY

View file

@ -5,7 +5,7 @@ from test import support
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
# The following value can be used as a very small timeout:

View file

@ -19,7 +19,7 @@ from test.test_asyncio import utils as test_utils
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class UpperProto(asyncio.Protocol):
@ -334,11 +334,11 @@ class WinPolicyTests(WindowsEventsTestCase):
old_policy = asyncio.get_event_loop_policy()
try:
asyncio.set_event_loop_policy(
asyncio._set_event_loop_policy(
asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(main())
finally:
asyncio.set_event_loop_policy(old_policy)
asyncio._set_event_loop_policy(old_policy)
def test_proactor_win_policy(self):
async def main():
@ -348,11 +348,11 @@ class WinPolicyTests(WindowsEventsTestCase):
old_policy = asyncio.get_event_loop_policy()
try:
asyncio.set_event_loop_policy(
asyncio._set_event_loop_policy(
asyncio.WindowsProactorEventLoopPolicy())
asyncio.run(main())
finally:
asyncio.set_event_loop_policy(old_policy)
asyncio._set_event_loop_policy(old_policy)
if __name__ == '__main__':

View file

@ -16,7 +16,7 @@ from test import support
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class PipeTests(unittest.TestCase):

View file

@ -493,7 +493,7 @@ class BuiltinTest(ComplexesAreIdenticalMixin, unittest.TestCase):
asyncio.run(eval(co, globals_))
self.assertEqual(globals_['a'], 1)
finally:
asyncio.set_event_loop_policy(policy)
asyncio._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
@ -530,7 +530,7 @@ class BuiltinTest(ComplexesAreIdenticalMixin, unittest.TestCase):
mode,
flags=ast.PyCF_ALLOW_TOP_LEVEL_AWAIT)
finally:
asyncio.set_event_loop_policy(policy)
asyncio._set_event_loop_policy(policy)
def test_compile_async_generator(self):

View file

@ -311,7 +311,7 @@ class AsyncioTest(InterpretersMixin, testasyncio_utils.TestCase):
# tests left a policy in place, just in case.
policy = support.maybe_get_event_loop_policy()
assert policy is None, policy
cls.addClassCleanup(lambda: asyncio.set_event_loop_policy(None))
cls.addClassCleanup(lambda: asyncio._set_event_loop_policy(None))
def setUp(self):
super().setUp()

View file

@ -11,7 +11,7 @@ from test.test_contextlib import TestBaseExitStack
support.requires_working_socket(module=True)
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class TestAbstractAsyncContextManager(unittest.IsolatedAsyncioTestCase):

View file

@ -2294,7 +2294,7 @@ class CoroAsyncIOCompatTest(unittest.TestCase):
pass
finally:
loop.close()
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
self.assertEqual(buffer, [1, 2, 'MyException'])

View file

@ -75,7 +75,7 @@ git = mod.StupidGit()
def tearDownModule():
if support.has_socket_support:
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
def signatures_with_lexicographic_keyword_only_parameters():

View file

@ -5352,7 +5352,7 @@ class LogRecordTest(BaseTest):
logging.logAsyncioTasks = False
runner.run(make_record(self.assertIsNone))
finally:
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
@support.requires_working_socket()
def test_taskName_without_asyncio_imported(self):
@ -5364,7 +5364,7 @@ class LogRecordTest(BaseTest):
logging.logAsyncioTasks = False
runner.run(make_record(self.assertIsNone))
finally:
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class BasicConfigTest(unittest.TestCase):
@ -5668,7 +5668,7 @@ class BasicConfigTest(unittest.TestCase):
data = f.read().strip()
self.assertRegex(data, r'Task-\d+ - hello world')
finally:
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
if handler:
handler.close()

View file

@ -105,7 +105,7 @@ requires_splice_pipe = unittest.skipIf(sys.platform.startswith("aix"),
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class MiscTests(unittest.TestCase):

View file

@ -2132,7 +2132,7 @@ if not SKIP_ASYNCIO_TESTS:
... loop = asyncio.new_event_loop()
... loop.run_until_complete(test_main())
... loop.close()
... asyncio.set_event_loop_policy(None)
... asyncio._set_event_loop_policy(None)
... print("finished")
>>> with PdbTestInput(['step',
@ -2253,7 +2253,7 @@ if not SKIP_ASYNCIO_TESTS:
... loop = asyncio.new_event_loop()
... loop.run_until_complete(test_main())
... loop.close()
... asyncio.set_event_loop_policy(None)
... asyncio._set_event_loop_policy(None)
... print("finished")
>>> with PdbTestInput(['step',
@ -2353,7 +2353,7 @@ if not SKIP_ASYNCIO_TESTS:
... loop = asyncio.new_event_loop()
... loop.run_until_complete(test_main())
... loop.close()
... asyncio.set_event_loop_policy(None)
... asyncio._set_event_loop_policy(None)
... print("finished")
>>> with PdbTestInput(['step',

View file

@ -2070,7 +2070,7 @@ class JumpTestCase(unittest.TestCase):
asyncio.run(func(output))
sys.settrace(None)
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
self.compare_jump_output(expected, output)
def jump_test(jumpFrom, jumpTo, expected, error=None, event='line', warning=None):

View file

@ -12,7 +12,7 @@ class MyException(Exception):
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class TestCM:
@ -490,7 +490,7 @@ class TestAsyncCase(unittest.TestCase):
self.assertTrue(result.wasSuccessful())
def test_loop_factory(self):
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class TestCase1(unittest.IsolatedAsyncioTestCase):
loop_factory = asyncio.EventLoop

View file

@ -15,7 +15,7 @@ from unittest.mock import (ANY, call, AsyncMock, patch, MagicMock, Mock,
def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)
class AsyncClass: