mirror of https://github.com/python/cpython.git
gh-127949: deprecate `asyncio.get_event_loop_policy` (#128053)
This deprecates `asyncio.get_event_loop_policy` and will be removed in Python 3.16.
This commit is contained in:
parent
bad3cdefa8
commit
dbd08fb60d
|
@ -40,6 +40,10 @@ for the current process:
|
||||||
|
|
||||||
Return the current process-wide policy.
|
Return the current process-wide policy.
|
||||||
|
|
||||||
|
.. deprecated:: next
|
||||||
|
The :func:`get_event_loop_policy` function is deprecated and
|
||||||
|
will be removed in Python 3.16.
|
||||||
|
|
||||||
.. function:: set_event_loop_policy(policy)
|
.. function:: set_event_loop_policy(policy)
|
||||||
|
|
||||||
Set the current process-wide policy to *policy*.
|
Set the current process-wide policy to *policy*.
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
'AbstractEventLoopPolicy',
|
'AbstractEventLoopPolicy',
|
||||||
'AbstractEventLoop', 'AbstractServer',
|
'AbstractEventLoop', 'AbstractServer',
|
||||||
'Handle', 'TimerHandle',
|
'Handle', 'TimerHandle',
|
||||||
|
'_get_event_loop_policy',
|
||||||
'get_event_loop_policy',
|
'get_event_loop_policy',
|
||||||
'_set_event_loop_policy',
|
'_set_event_loop_policy',
|
||||||
'set_event_loop_policy',
|
'set_event_loop_policy',
|
||||||
|
@ -761,12 +762,15 @@ def _init_event_loop_policy():
|
||||||
_event_loop_policy = DefaultEventLoopPolicy()
|
_event_loop_policy = DefaultEventLoopPolicy()
|
||||||
|
|
||||||
|
|
||||||
def get_event_loop_policy():
|
def _get_event_loop_policy():
|
||||||
"""Get the current event loop policy."""
|
"""Get the current event loop policy."""
|
||||||
if _event_loop_policy is None:
|
if _event_loop_policy is None:
|
||||||
_init_event_loop_policy()
|
_init_event_loop_policy()
|
||||||
return _event_loop_policy
|
return _event_loop_policy
|
||||||
|
|
||||||
|
def get_event_loop_policy():
|
||||||
|
warnings._deprecated('asyncio.get_event_loop_policy', remove=(3, 16))
|
||||||
|
return _get_event_loop_policy()
|
||||||
|
|
||||||
def _set_event_loop_policy(policy):
|
def _set_event_loop_policy(policy):
|
||||||
"""Set the current event loop policy.
|
"""Set the current event loop policy.
|
||||||
|
@ -778,7 +782,7 @@ def _set_event_loop_policy(policy):
|
||||||
_event_loop_policy = policy
|
_event_loop_policy = policy
|
||||||
|
|
||||||
def set_event_loop_policy(policy):
|
def set_event_loop_policy(policy):
|
||||||
warnings._deprecated('set_event_loop_policy', remove=(3,16))
|
warnings._deprecated('asyncio.set_event_loop_policy', remove=(3,16))
|
||||||
_set_event_loop_policy(policy)
|
_set_event_loop_policy(policy)
|
||||||
|
|
||||||
def get_event_loop():
|
def get_event_loop():
|
||||||
|
@ -794,17 +798,17 @@ def get_event_loop():
|
||||||
current_loop = _get_running_loop()
|
current_loop = _get_running_loop()
|
||||||
if current_loop is not None:
|
if current_loop is not None:
|
||||||
return current_loop
|
return current_loop
|
||||||
return get_event_loop_policy().get_event_loop()
|
return _get_event_loop_policy().get_event_loop()
|
||||||
|
|
||||||
|
|
||||||
def set_event_loop(loop):
|
def set_event_loop(loop):
|
||||||
"""Equivalent to calling get_event_loop_policy().set_event_loop(loop)."""
|
"""Equivalent to calling get_event_loop_policy().set_event_loop(loop)."""
|
||||||
get_event_loop_policy().set_event_loop(loop)
|
_get_event_loop_policy().set_event_loop(loop)
|
||||||
|
|
||||||
|
|
||||||
def new_event_loop():
|
def new_event_loop():
|
||||||
"""Equivalent to calling get_event_loop_policy().new_event_loop()."""
|
"""Equivalent to calling get_event_loop_policy().new_event_loop()."""
|
||||||
return get_event_loop_policy().new_event_loop()
|
return _get_event_loop_policy().new_event_loop()
|
||||||
|
|
||||||
|
|
||||||
# Alias pure-Python implementations for testing purposes.
|
# Alias pure-Python implementations for testing purposes.
|
||||||
|
|
|
@ -2397,7 +2397,7 @@ def test_handle_repr_debug(self):
|
||||||
self.assertRegex(repr(h), regex)
|
self.assertRegex(repr(h), regex)
|
||||||
|
|
||||||
def test_handle_source_traceback(self):
|
def test_handle_source_traceback(self):
|
||||||
loop = asyncio.get_event_loop_policy().new_event_loop()
|
loop = asyncio.new_event_loop()
|
||||||
loop.set_debug(True)
|
loop.set_debug(True)
|
||||||
self.set_event_loop(loop)
|
self.set_event_loop(loop)
|
||||||
|
|
||||||
|
@ -2759,24 +2759,31 @@ def test_set_event_loop(self):
|
||||||
old_loop.close()
|
old_loop.close()
|
||||||
|
|
||||||
def test_get_event_loop_policy(self):
|
def test_get_event_loop_policy(self):
|
||||||
policy = asyncio.get_event_loop_policy()
|
with self.assertWarnsRegex(
|
||||||
self.assertIsInstance(policy, asyncio.AbstractEventLoopPolicy)
|
DeprecationWarning, "'asyncio.get_event_loop_policy' is deprecated"):
|
||||||
self.assertIs(policy, asyncio.get_event_loop_policy())
|
policy = asyncio.get_event_loop_policy()
|
||||||
|
self.assertIsInstance(policy, asyncio.AbstractEventLoopPolicy)
|
||||||
|
self.assertIs(policy, asyncio.get_event_loop_policy())
|
||||||
|
|
||||||
def test_set_event_loop_policy(self):
|
def test_set_event_loop_policy(self):
|
||||||
with self.assertWarnsRegex(
|
with self.assertWarnsRegex(
|
||||||
DeprecationWarning, "'set_event_loop_policy' is deprecated"):
|
DeprecationWarning, "'asyncio.set_event_loop_policy' is deprecated"):
|
||||||
self.assertRaises(
|
self.assertRaises(
|
||||||
TypeError, asyncio.set_event_loop_policy, object())
|
TypeError, asyncio.set_event_loop_policy, object())
|
||||||
|
|
||||||
old_policy = asyncio.get_event_loop_policy()
|
with self.assertWarnsRegex(
|
||||||
|
DeprecationWarning, "'asyncio.get_event_loop_policy' is deprecated"):
|
||||||
|
old_policy = asyncio.get_event_loop_policy()
|
||||||
|
|
||||||
policy = asyncio.DefaultEventLoopPolicy()
|
policy = asyncio.DefaultEventLoopPolicy()
|
||||||
with self.assertWarnsRegex(
|
with self.assertWarnsRegex(
|
||||||
DeprecationWarning, "'set_event_loop_policy' is deprecated"):
|
DeprecationWarning, "'asyncio.set_event_loop_policy' is deprecated"):
|
||||||
asyncio.set_event_loop_policy(policy)
|
asyncio.set_event_loop_policy(policy)
|
||||||
self.assertIs(policy, asyncio.get_event_loop_policy())
|
|
||||||
self.assertIsNot(policy, old_policy)
|
with self.assertWarnsRegex(
|
||||||
|
DeprecationWarning, "'asyncio.get_event_loop_policy' is deprecated"):
|
||||||
|
self.assertIs(policy, asyncio.get_event_loop_policy())
|
||||||
|
self.assertIsNot(policy, old_policy)
|
||||||
|
|
||||||
|
|
||||||
class GetEventLoopTestsMixin:
|
class GetEventLoopTestsMixin:
|
||||||
|
@ -2859,7 +2866,7 @@ class Policy(asyncio.DefaultEventLoopPolicy):
|
||||||
def get_event_loop(self):
|
def get_event_loop(self):
|
||||||
raise TestError
|
raise TestError
|
||||||
|
|
||||||
old_policy = asyncio.get_event_loop_policy()
|
old_policy = asyncio._get_event_loop_policy()
|
||||||
try:
|
try:
|
||||||
asyncio._set_event_loop_policy(Policy())
|
asyncio._set_event_loop_policy(Policy())
|
||||||
loop = asyncio.new_event_loop()
|
loop = asyncio.new_event_loop()
|
||||||
|
@ -2899,7 +2906,7 @@ async def func():
|
||||||
self.assertIs(asyncio._get_running_loop(), None)
|
self.assertIs(asyncio._get_running_loop(), None)
|
||||||
|
|
||||||
def test_get_event_loop_returns_running_loop2(self):
|
def test_get_event_loop_returns_running_loop2(self):
|
||||||
old_policy = asyncio.get_event_loop_policy()
|
old_policy = asyncio._get_event_loop_policy()
|
||||||
try:
|
try:
|
||||||
asyncio._set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
|
asyncio._set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
|
||||||
loop = asyncio.new_event_loop()
|
loop = asyncio.new_event_loop()
|
||||||
|
|
|
@ -64,7 +64,7 @@ def setUp(self):
|
||||||
asyncio._set_event_loop_policy(policy)
|
asyncio._set_event_loop_policy(policy)
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
policy = asyncio.get_event_loop_policy()
|
policy = asyncio._get_event_loop_policy()
|
||||||
if policy.loop is not None:
|
if policy.loop is not None:
|
||||||
self.assertTrue(policy.loop.is_closed())
|
self.assertTrue(policy.loop.is_closed())
|
||||||
self.assertTrue(policy.loop.shutdown_ag_run)
|
self.assertTrue(policy.loop.shutdown_ag_run)
|
||||||
|
@ -208,7 +208,7 @@ async def main():
|
||||||
await asyncio.sleep(0)
|
await asyncio.sleep(0)
|
||||||
return 42
|
return 42
|
||||||
|
|
||||||
policy = asyncio.get_event_loop_policy()
|
policy = asyncio._get_event_loop_policy()
|
||||||
policy.set_event_loop = mock.Mock()
|
policy.set_event_loop = mock.Mock()
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
self.assertTrue(policy.set_event_loop.called)
|
self.assertTrue(policy.set_event_loop.called)
|
||||||
|
@ -495,7 +495,7 @@ def test_set_event_loop_called_once(self):
|
||||||
async def coro():
|
async def coro():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
policy = asyncio.get_event_loop_policy()
|
policy = asyncio._get_event_loop_policy()
|
||||||
policy.set_event_loop = mock.Mock()
|
policy.set_event_loop = mock.Mock()
|
||||||
runner = asyncio.Runner()
|
runner = asyncio.Runner()
|
||||||
runner.run(coro())
|
runner.run(coro())
|
||||||
|
|
|
@ -886,8 +886,7 @@ class SubprocessWatcherMixin(SubprocessMixin):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super().setUp()
|
super().setUp()
|
||||||
policy = asyncio.get_event_loop_policy()
|
self.loop = asyncio.new_event_loop()
|
||||||
self.loop = policy.new_event_loop()
|
|
||||||
self.set_event_loop(self.loop)
|
self.set_event_loop(self.loop)
|
||||||
|
|
||||||
def test_watcher_implementation(self):
|
def test_watcher_implementation(self):
|
||||||
|
|
|
@ -332,7 +332,7 @@ async def main():
|
||||||
asyncio.get_running_loop(),
|
asyncio.get_running_loop(),
|
||||||
asyncio.SelectorEventLoop)
|
asyncio.SelectorEventLoop)
|
||||||
|
|
||||||
old_policy = asyncio.get_event_loop_policy()
|
old_policy = asyncio._get_event_loop_policy()
|
||||||
try:
|
try:
|
||||||
asyncio._set_event_loop_policy(
|
asyncio._set_event_loop_policy(
|
||||||
asyncio.WindowsSelectorEventLoopPolicy())
|
asyncio.WindowsSelectorEventLoopPolicy())
|
||||||
|
@ -346,7 +346,7 @@ async def main():
|
||||||
asyncio.get_running_loop(),
|
asyncio.get_running_loop(),
|
||||||
asyncio.ProactorEventLoop)
|
asyncio.ProactorEventLoop)
|
||||||
|
|
||||||
old_policy = asyncio.get_event_loop_policy()
|
old_policy = asyncio._get_event_loop_policy()
|
||||||
try:
|
try:
|
||||||
asyncio._set_event_loop_policy(
|
asyncio._set_event_loop_policy(
|
||||||
asyncio.WindowsProactorEventLoopPolicy())
|
asyncio.WindowsProactorEventLoopPolicy())
|
||||||
|
|
|
@ -496,7 +496,7 @@ class TestAsyncExitStack(TestBaseExitStack, unittest.IsolatedAsyncioTestCase):
|
||||||
class SyncAsyncExitStack(AsyncExitStack):
|
class SyncAsyncExitStack(AsyncExitStack):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def run_coroutine(coro):
|
def run_coroutine(coro):
|
||||||
loop = asyncio.get_event_loop_policy().get_event_loop()
|
loop = asyncio.new_event_loop()
|
||||||
t = loop.create_task(coro)
|
t = loop.create_task(coro)
|
||||||
t.add_done_callback(lambda f: loop.stop())
|
t.add_done_callback(lambda f: loop.stop())
|
||||||
loop.run_forever()
|
loop.run_forever()
|
||||||
|
|
|
@ -480,7 +480,7 @@ def test_setup_get_event_loop(self):
|
||||||
|
|
||||||
class TestCase1(unittest.IsolatedAsyncioTestCase):
|
class TestCase1(unittest.IsolatedAsyncioTestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
asyncio.get_event_loop_policy().get_event_loop()
|
asyncio._get_event_loop_policy().get_event_loop()
|
||||||
|
|
||||||
async def test_demo1(self):
|
async def test_demo1(self):
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -3773,7 +3773,7 @@ module_init(asyncio_state *state)
|
||||||
}
|
}
|
||||||
|
|
||||||
WITH_MOD("asyncio.events")
|
WITH_MOD("asyncio.events")
|
||||||
GET_MOD_ATTR(state->asyncio_get_event_loop_policy, "get_event_loop_policy")
|
GET_MOD_ATTR(state->asyncio_get_event_loop_policy, "_get_event_loop_policy")
|
||||||
|
|
||||||
WITH_MOD("asyncio.base_futures")
|
WITH_MOD("asyncio.base_futures")
|
||||||
GET_MOD_ATTR(state->asyncio_future_repr_func, "_future_repr")
|
GET_MOD_ATTR(state->asyncio_future_repr_func, "_future_repr")
|
||||||
|
|
Loading…
Reference in New Issue