Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

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 @@ -7,6 +7,8 @@
Policies
========

The policy system is deprecated as of Python 3.13 and will
be removed in Python 3.15.
An event loop policy is a global object
used to get and set the current :ref:`event loop <asyncio-event-loop>`,
as well as create new event loops.
Expand Down Expand Up @@ -46,6 +48,8 @@ for the current process:

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

.. deprecated-removed:: 3.13 3.15


.. _asyncio-policy-objects:

Expand Down
4 changes: 4 additions & 0 deletions Lib/asyncio/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import subprocess
import sys
import threading
import warnings

from . import format_helpers

Expand Down Expand Up @@ -787,6 +788,9 @@ def set_event_loop_policy(policy):
"""Set the current event loop policy.

If policy is None, the default policy is restored."""
warnings._deprecated("set_event_loop_policy",
"{name!r} is deprecated as of Python 3.13 and will be "
"removed in Python {remove}.", remove=(3, 15))
global _event_loop_policy
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__}'")
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)
support.set_event_loop_policy(policy)

def get_sys_argv(self):
return id(sys.argv), sys.argv, sys.argv[:]
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2089,6 +2089,13 @@ def maybe_get_event_loop_policy():
import asyncio.events
return asyncio.events._event_loop_policy

def set_event_loop_policy(policy):
"""Set the global event loop policy ignoring deprecation warnings"""
import asyncio
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
asyncio.set_event_loop_policy(policy)

# Helpers for testing hashing.
NHASHBITS = sys.hash_info.width # number of bits in hash() result
assert NHASHBITS in (32, 64)
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from test.support.import_helper import import_module
from test.support import gc_collect, requires_working_socket
from test import support
asyncio = import_module("asyncio")


Expand Down Expand Up @@ -429,7 +430,7 @@ def setUp(self):
def tearDown(self):
self.loop.close()
self.loop = None
asyncio.set_event_loop_policy(None)
support.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 @@ -24,7 +24,7 @@


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


def mock_socket_module():
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_buffered_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import unittest

from test.test_asyncio import functional as func_tests
from test import support


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


class ReceiveStuffProto(asyncio.BufferedProtocol):
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_context.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import asyncio
import decimal
import unittest
from test import support


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


@unittest.skipUnless(decimal.HAVE_CONTEXTVAR, "decimal is built with a thread-local context")
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_eager_task_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
from asyncio import tasks
from test.test_asyncio import utils as test_utils
from test.support.script_helper import assert_python_ok
from test import support

MOCK_ANY = mock.ANY


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


class EagerTaskFactoryLoopTests:
Expand Down
20 changes: 12 additions & 8 deletions Lib/test/test_asyncio/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@


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


def broken_unix_getsockname():
Expand Down Expand Up @@ -2684,13 +2684,15 @@ 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.assertWarns(DeprecationWarning):
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.assertWarns(DeprecationWarning) as cm:
asyncio.set_event_loop_policy(policy)
self.assertIs(policy, asyncio.get_event_loop_policy())
self.assertIsNot(policy, old_policy)

Expand Down Expand Up @@ -2789,7 +2791,8 @@ def get_event_loop(self):

old_policy = asyncio.get_event_loop_policy()
try:
asyncio.set_event_loop_policy(Policy())
with self.assertWarns(DeprecationWarning):
asyncio.set_event_loop_policy(Policy())
loop = asyncio.new_event_loop()

with self.assertRaises(TestError):
Expand Down Expand Up @@ -2817,7 +2820,7 @@ async def func():
asyncio.get_event_loop()

finally:
asyncio.set_event_loop_policy(old_policy)
support.set_event_loop_policy(old_policy)
if loop is not None:
loop.close()

Expand All @@ -2829,7 +2832,8 @@ 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())
with self.assertWarns(DeprecationWarning):
asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
loop = asyncio.new_event_loop()
self.addCleanup(loop.close)

Expand Down Expand Up @@ -2860,7 +2864,7 @@ async def func():
asyncio.get_event_loop()

finally:
asyncio.set_event_loop_policy(old_policy)
support.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 @@ -16,7 +16,7 @@


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


def _fakefunc(f):
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_futures2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import traceback
import unittest
from asyncio import tasks
from test import support


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


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

import asyncio
import collections
from test import support

STR_RGX_REPR = (
r'^<(?P<class>.*?) object at (?P<address>.*?)'
Expand All @@ -20,7 +21,7 @@


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


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

import asyncio
from test.test_asyncio import utils as test_utils
from test import support


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


# Test that asyncio.iscoroutine() uses collections.abc.Coroutine
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_proactor_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
from test.support import os_helper
from test.support import socket_helper
from test.test_asyncio import utils as test_utils
from test import support


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


def close_transport(transport):
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
from unittest import mock

import asyncio
from test import support


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)
support.set_event_loop_policy(None)


class ProtocolsAbsTests(unittest.TestCase):
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import asyncio
import unittest
from types import GenericAlias
from test import support


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


class QueueBasicTests(unittest.IsolatedAsyncioTestCase):
Expand Down
9 changes: 5 additions & 4 deletions Lib/test/test_asyncio/test_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import threading
import unittest
from test.test_asyncio import utils as test_utils
from test import support
from unittest import mock
from unittest.mock import patch


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


def interrupt_self():
Expand Down Expand Up @@ -60,15 +61,15 @@ def setUp(self):
super().setUp()

policy = TestPolicy(self.new_loop)
asyncio.set_event_loop_policy(policy)
support.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)
support.set_event_loop_policy(None)
super().tearDown()


Expand Down Expand Up @@ -258,7 +259,7 @@ def new_event_loop():
loop.set_task_factory(Task)
return loop

asyncio.set_event_loop_policy(TestPolicy(new_event_loop))
support.set_event_loop_policy(TestPolicy(new_event_loop))
with self.assertRaises(asyncio.CancelledError):
asyncio.run(main())

Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
_SelectorSocketTransport,
_SelectorTransport)
from test.test_asyncio import utils as test_utils
from test import support

MOCK_ANY = mock.ANY


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.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)
support.set_event_loop_policy(None)


class MySendfileProto(asyncio.Protocol):
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
from test.support import socket_helper
from test.test_asyncio import utils as test_utils
from test.test_asyncio import functional as func_tests
from test import support


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.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)
support.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 @@ -25,7 +25,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.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)
support.set_event_loop_policy(None)


@unittest.skipIf(ssl is None, 'No ssl module')
Expand Down
Loading