Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-127949: deprecate asyncio.set_event_loop_policy #128024

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Doc/library/asyncio-policy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ for the current process:

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

.. deprecated:: 3.14
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved
The :func:`set_event_loop_policy` function is deprecated and
will be removed in Python 3.16.


.. _asyncio-policy-objects:

Expand Down
10 changes: 8 additions & 2 deletions Lib/asyncio/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
'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',
Expand All @@ -21,6 +23,7 @@
import subprocess
import sys
import threading
import warnings
picnixz marked this conversation as resolved.
Show resolved Hide resolved

from . import format_helpers

Expand Down Expand Up @@ -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."""
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/libregrtest/save_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def get_asyncio_events__event_loop_policy(self):
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[:]
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ def setUp(self):
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"):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)


def mock_socket_module():
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_buffered_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)


class ReceiveStuffProto(asyncio.BufferedProtocol):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


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")
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_eager_task_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)


class EagerTaskFactoryLoopTests:
Expand Down
22 changes: 13 additions & 9 deletions Lib/test/test_asyncio/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
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():
Expand Down Expand Up @@ -2764,13 +2764,17 @@ def test_get_event_loop_policy(self):
self.assertIs(policy, asyncio.get_event_loop_policy())

def test_set_event_loop_policy(self):
self.assertRaises(
TypeError, asyncio.set_event_loop_policy, object())
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()
asyncio.set_event_loop_policy(policy)
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)

Expand Down Expand Up @@ -2857,7 +2861,7 @@ def get_event_loop(self):

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):
Expand Down Expand Up @@ -2885,7 +2889,7 @@ async def func():
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()

Expand All @@ -2897,7 +2901,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)

Expand All @@ -2923,7 +2927,7 @@ async def func():
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()

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)


def _fakefunc(f):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_futures2.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)


class FutureTests:
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)


class LockTests(unittest.IsolatedAsyncioTestCase):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_pep492.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)


# Test that asyncio.iscoroutine() uses collections.abc.Coroutine
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_proactor_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)


def close_transport(transport):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)


class QueueBasicTests(unittest.IsolatedAsyncioTestCase):
Expand Down
8 changes: 4 additions & 4 deletions Lib/test/test_asyncio/test_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)


def interrupt_self():
Expand Down Expand Up @@ -61,15 +61,15 @@ def setUp(self):
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()
if policy.loop is not None:
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()


Expand Down Expand Up @@ -259,7 +259,7 @@ def new_event_loop():
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())

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)


class TestBaseSelectorEventLoop(BaseSelectorEventLoop):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_sendfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)


class MySendfileProto(asyncio.Protocol):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)


class BaseStartServer(func_tests.FunctionalTestCaseMixin):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_sock_lowlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)


class MyProto(asyncio.Protocol):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)


class MyBaseProto(asyncio.Protocol):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_sslproto.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)


@unittest.skipIf(ssl is None, 'No ssl module')
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_staggered.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)


class StaggeredTests(unittest.IsolatedAsyncioTestCase):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)


class StreamTests(test_utils.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)


class TestSubprocessTransport(base_subprocess.BaseSubprocessTransport):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_taskgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

# 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):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)


async def coroutine_function():
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)


class ToThreadTests(unittest.IsolatedAsyncioTestCase):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_timeouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
asyncio._set_event_loop_policy(None)

class TimeoutTests(unittest.IsolatedAsyncioTestCase):

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_transports.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
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):
Expand Down
Loading
Loading