From 81fc1ccda88e55ced4f48d10a720661e2fa6832a Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Thu, 10 Apr 2025 12:51:24 +0530 Subject: [PATCH 1/3] wip --- Lib/asyncio/__main__.py | 2 +- Lib/asyncio/events.py | 6 +----- Lib/test/test_asyncgen.py | 2 +- Lib/test/test_asyncio/functional.py | 4 ++-- Lib/test/test_asyncio/test_base_events.py | 8 ++++---- Lib/test/test_asyncio/test_events.py | 18 +++++++++--------- Lib/test/test_asyncio/test_futures.py | 4 ++-- Lib/test/test_asyncio/test_streams.py | 6 +++--- Lib/test/test_asyncio/test_tasks.py | 8 ++++---- Lib/test/test_asyncio/test_unix_events.py | 4 ++-- Lib/test/test_coroutines.py | 2 +- Lib/test/test_unittest/test_async_case.py | 2 +- 12 files changed, 31 insertions(+), 35 deletions(-) diff --git a/Lib/asyncio/__main__.py b/Lib/asyncio/__main__.py index e624f7632bedce..69f5a30cfe5095 100644 --- a/Lib/asyncio/__main__.py +++ b/Lib/asyncio/__main__.py @@ -149,7 +149,7 @@ def interrupt(self) -> None: return_code = 0 loop = asyncio.new_event_loop() - asyncio._set_event_loop(loop) + asyncio.set_event_loop(loop) repl_locals = {'asyncio': asyncio} for key in {'__name__', '__package__', diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py index 2e45b4fe6fa2dd..cf89d913d595e0 100644 --- a/Lib/asyncio/events.py +++ b/Lib/asyncio/events.py @@ -835,13 +835,9 @@ def get_event_loop(): return _get_event_loop_policy().get_event_loop() -def _set_event_loop(loop): - _get_event_loop_policy().set_event_loop(loop) - def set_event_loop(loop): """Equivalent to calling get_event_loop_policy().set_event_loop(loop).""" - warnings._deprecated('asyncio.set_event_loop', remove=(3,16)) - _set_event_loop(loop) + _get_event_loop_policy().set_event_loop(loop) def new_event_loop(): diff --git a/Lib/test/test_asyncgen.py b/Lib/test/test_asyncgen.py index d7dcf619300b2b..2c44647bf3e2f9 100644 --- a/Lib/test/test_asyncgen.py +++ b/Lib/test/test_asyncgen.py @@ -624,7 +624,7 @@ class AsyncGenAsyncioTest(unittest.TestCase): def setUp(self): self.loop = asyncio.new_event_loop() - asyncio._set_event_loop(None) + asyncio.set_event_loop(None) def tearDown(self): self.loop.close() diff --git a/Lib/test/test_asyncio/functional.py b/Lib/test/test_asyncio/functional.py index 555394b915de48..96dc9ab4401067 100644 --- a/Lib/test/test_asyncio/functional.py +++ b/Lib/test/test_asyncio/functional.py @@ -24,7 +24,7 @@ def loop_exception_handler(self, loop, context): def setUp(self): self.loop = self.new_loop() - asyncio._set_event_loop(None) + asyncio.set_event_loop(None) self.loop.set_exception_handler(self.loop_exception_handler) self.__unhandled_exceptions = [] @@ -39,7 +39,7 @@ def tearDown(self): self.fail('unexpected calls to loop.call_exception_handler()') finally: - asyncio._set_event_loop(None) + asyncio.set_event_loop(None) self.loop = None def tcp_server(self, server_prog, *, diff --git a/Lib/test/test_asyncio/test_base_events.py b/Lib/test/test_asyncio/test_base_events.py index 7838d1fccb88e1..2ca5c4c6719c41 100644 --- a/Lib/test/test_asyncio/test_base_events.py +++ b/Lib/test/test_asyncio/test_base_events.py @@ -336,10 +336,10 @@ def check_in_thread(loop, event, debug, create_loop, fut): if create_loop: loop2 = base_events.BaseEventLoop() try: - asyncio._set_event_loop(loop2) + asyncio.set_event_loop(loop2) self.check_thread(loop, debug) finally: - asyncio._set_event_loop(None) + asyncio.set_event_loop(None) loop2.close() else: self.check_thread(loop, debug) @@ -695,7 +695,7 @@ def default_exception_handler(self, context): loop = Loop() self.addCleanup(loop.close) - asyncio._set_event_loop(loop) + asyncio.set_event_loop(loop) def run_loop(): def zero_error(): @@ -1988,7 +1988,7 @@ def stop_loop_cb(loop): async def stop_loop_coro(loop): loop.stop() - asyncio._set_event_loop(self.loop) + asyncio.set_event_loop(self.loop) self.loop.set_debug(True) self.loop.slow_callback_duration = 0.0 diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py index a067bc98ecc2a8..cdec54f28a2b2d 100644 --- a/Lib/test/test_asyncio/test_events.py +++ b/Lib/test/test_asyncio/test_events.py @@ -57,7 +57,7 @@ async def doit(): return 'hello' with contextlib.closing(asyncio.new_event_loop()) as loop: - asyncio._set_event_loop(loop) + asyncio.set_event_loop(loop) return loop.run_until_complete(doit()) @@ -2974,14 +2974,14 @@ def setUp(self): super().setUp() self.loop = asyncio.new_event_loop() - asyncio._set_event_loop(self.loop) + asyncio.set_event_loop(self.loop) def tearDown(self): try: super().tearDown() finally: self.loop.close() - asyncio._set_event_loop(None) + asyncio.set_event_loop(None) events._get_running_loop = self._get_running_loop_saved events._set_running_loop = self._set_running_loop_saved @@ -3053,7 +3053,7 @@ def get_event_loop(self): with self.assertRaises(TestError): asyncio.get_event_loop() - asyncio._set_event_loop(None) + asyncio.set_event_loop(None) with self.assertRaises(TestError): asyncio.get_event_loop() @@ -3068,10 +3068,10 @@ async def func(): loop.run_until_complete(func()) - asyncio._set_event_loop(loop) + asyncio.set_event_loop(loop) with self.assertRaises(TestError): asyncio.get_event_loop() - asyncio._set_event_loop(None) + asyncio.set_event_loop(None) with self.assertRaises(TestError): asyncio.get_event_loop() @@ -3095,7 +3095,7 @@ def test_get_event_loop_returns_running_loop2(self): with self.assertRaisesRegex(RuntimeError, 'no current'): asyncio.get_event_loop() - asyncio._set_event_loop(None) + asyncio.set_event_loop(None) with self.assertRaisesRegex(RuntimeError, 'no current'): asyncio.get_event_loop() @@ -3106,10 +3106,10 @@ async def func(): loop.run_until_complete(func()) - asyncio._set_event_loop(loop) + asyncio.set_event_loop(loop) self.assertIs(asyncio.get_event_loop(), loop) - asyncio._set_event_loop(None) + asyncio.set_event_loop(None) with self.assertRaisesRegex(RuntimeError, 'no current'): asyncio.get_event_loop() diff --git a/Lib/test/test_asyncio/test_futures.py b/Lib/test/test_asyncio/test_futures.py index 01d6230e6dd9a3..2ed803c48b8065 100644 --- a/Lib/test/test_asyncio/test_futures.py +++ b/Lib/test/test_asyncio/test_futures.py @@ -178,7 +178,7 @@ async def test(): def test_constructor_use_global_loop(self): # Deprecated in 3.10, undeprecated in 3.12 - asyncio._set_event_loop(self.loop) + asyncio.set_event_loop(self.loop) self.addCleanup(asyncio._set_event_loop, None) f = self._new_future() self.assertIs(f._loop, self.loop) @@ -566,7 +566,7 @@ async def test(): def test_wrap_future_use_global_loop(self): # Deprecated in 3.10, undeprecated in 3.12 - asyncio._set_event_loop(self.loop) + asyncio.set_event_loop(self.loop) self.addCleanup(asyncio._set_event_loop, None) def run(arg): return (arg, threading.get_ident()) diff --git a/Lib/test/test_asyncio/test_streams.py b/Lib/test/test_asyncio/test_streams.py index 0b47b2b91a1fe5..d04288cf39888c 100644 --- a/Lib/test/test_asyncio/test_streams.py +++ b/Lib/test/test_asyncio/test_streams.py @@ -68,7 +68,7 @@ def _basetest_open_connection_no_loop_ssl(self, open_connection_fut): try: reader, writer = self.loop.run_until_complete(open_connection_fut) finally: - asyncio._set_event_loop(None) + asyncio.set_event_loop(None) writer.write(b'GET / HTTP/1.0\r\n\r\n') f = reader.read() data = self.loop.run_until_complete(f) @@ -837,7 +837,7 @@ def test_streamreader_constructor_use_global_loop(self): # retrieves the current loop if the loop parameter is not set # Deprecated in 3.10, undeprecated in 3.12 self.addCleanup(asyncio._set_event_loop, None) - asyncio._set_event_loop(self.loop) + asyncio.set_event_loop(self.loop) reader = asyncio.StreamReader() self.assertIs(reader._loop, self.loop) @@ -861,7 +861,7 @@ def test_streamreaderprotocol_constructor_use_global_loop(self): # retrieves the current loop if the loop parameter is not set # Deprecated in 3.10, undeprecated in 3.12 self.addCleanup(asyncio._set_event_loop, None) - asyncio._set_event_loop(self.loop) + asyncio.set_event_loop(self.loop) reader = mock.Mock() protocol = asyncio.StreamReaderProtocol(reader) self.assertIs(protocol._loop, self.loop) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index f1a665e5df3fd3..c8ad4abc276588 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -212,7 +212,7 @@ async def test(): self.assertEqual(t.result(), 'ok') # Deprecated in 3.10, undeprecated in 3.12 - asyncio._set_event_loop(self.loop) + asyncio.set_event_loop(self.loop) self.addCleanup(asyncio._set_event_loop, None) t = asyncio.ensure_future(notmuch()) self.assertIs(t._loop, self.loop) @@ -2202,7 +2202,7 @@ def test_shield_coroutine_use_global_loop(self): async def coro(): return 42 - asyncio._set_event_loop(self.loop) + asyncio.set_event_loop(self.loop) self.addCleanup(asyncio._set_event_loop, None) outer = asyncio.shield(coro()) self.assertEqual(outer._loop, self.loop) @@ -3322,7 +3322,7 @@ async def gather(): def test_constructor_empty_sequence_use_global_loop(self): # Deprecated in 3.10, undeprecated in 3.12 - asyncio._set_event_loop(self.one_loop) + asyncio.set_event_loop(self.one_loop) self.addCleanup(asyncio._set_event_loop, None) fut = asyncio.gather() self.assertIsInstance(fut, asyncio.Future) @@ -3430,7 +3430,7 @@ def test_constructor_use_global_loop(self): # Deprecated in 3.10, undeprecated in 3.12 async def coro(): return 'abc' - asyncio._set_event_loop(self.other_loop) + asyncio.set_event_loop(self.other_loop) self.addCleanup(asyncio._set_event_loop, None) gen1 = coro() gen2 = coro() diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py index 7f9f5a1abbcd99..e020c1f3e4f677 100644 --- a/Lib/test/test_asyncio/test_unix_events.py +++ b/Lib/test/test_asyncio/test_unix_events.py @@ -1113,11 +1113,11 @@ class TestFunctional(unittest.TestCase): def setUp(self): self.loop = asyncio.new_event_loop() - asyncio._set_event_loop(self.loop) + asyncio.set_event_loop(self.loop) def tearDown(self): self.loop.close() - asyncio._set_event_loop(None) + asyncio.set_event_loop(None) def test_add_reader_invalid_argument(self): def assert_raises(): diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py index deeaa724e79544..761cb230277bd9 100644 --- a/Lib/test/test_coroutines.py +++ b/Lib/test/test_coroutines.py @@ -2300,7 +2300,7 @@ async def f(): buffer.append('unreachable') loop = asyncio.new_event_loop() - asyncio._set_event_loop(loop) + asyncio.set_event_loop(loop) try: loop.run_until_complete(f()) except MyException: diff --git a/Lib/test/test_unittest/test_async_case.py b/Lib/test/test_unittest/test_async_case.py index fc996b42149dcb..993e6bf013cfbf 100644 --- a/Lib/test/test_unittest/test_async_case.py +++ b/Lib/test/test_unittest/test_async_case.py @@ -476,7 +476,7 @@ async def cleanup(self, fut): def test_setup_get_event_loop(self): # See https://github.com/python/cpython/issues/95736 # Make sure the default event loop is not used - asyncio._set_event_loop(None) + asyncio.set_event_loop(None) class TestCase1(unittest.IsolatedAsyncioTestCase): def setUp(self): From d96ea053d90d2808b679bb52927c8e224aa06ff8 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Thu, 10 Apr 2025 13:06:45 +0530 Subject: [PATCH 2/3] docs --- Doc/deprecations/pending-removal-in-3.16.rst | 1 - Doc/library/asyncio-eventloop.rst | 10 +++------- Lib/asyncio/events.py | 2 +- Lib/asyncio/runners.py | 4 ++-- Lib/test/test_asyncio/test_events.py | 8 -------- Lib/test/test_asyncio/test_futures.py | 4 ++-- Lib/test/test_asyncio/test_streams.py | 4 ++-- Lib/test/test_asyncio/test_tasks.py | 8 ++++---- Lib/test/test_asyncio/utils.py | 4 ++-- 9 files changed, 16 insertions(+), 29 deletions(-) diff --git a/Doc/deprecations/pending-removal-in-3.16.rst b/Doc/deprecations/pending-removal-in-3.16.rst index b408a6d72febe0..acb53198bdc07e 100644 --- a/Doc/deprecations/pending-removal-in-3.16.rst +++ b/Doc/deprecations/pending-removal-in-3.16.rst @@ -32,7 +32,6 @@ Pending removal in Python 3.16 * :class:`asyncio.WindowsProactorEventLoopPolicy` * :func:`asyncio.get_event_loop_policy` * :func:`asyncio.set_event_loop_policy` - * :func:`asyncio.set_event_loop` Users should use :func:`asyncio.run` or :class:`asyncio.Runner` with *loop_factory* to use the desired event loop implementation. diff --git a/Doc/library/asyncio-eventloop.rst b/Doc/library/asyncio-eventloop.rst index fdb75fe9e63a88..fae68803107639 100644 --- a/Doc/library/asyncio-eventloop.rst +++ b/Doc/library/asyncio-eventloop.rst @@ -65,18 +65,14 @@ an event loop: .. note:: The :mod:`!asyncio` policy system is deprecated and will be removed - in Python 3.16; from there on, this function will always return the - running event loop. - + in Python 3.16; from there on, this function will return the current + running event loop if present else it will return the + loop set by :func:`set_event_loop`. .. function:: set_event_loop(loop) Set *loop* as the current event loop for the current OS thread. - .. deprecated:: 3.14 - The :func:`set_event_loop` function is deprecated and will be removed - in Python 3.16. - .. function:: new_event_loop() Create and return a new event loop object. diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py index cf89d913d595e0..22e1dfb2212436 100644 --- a/Lib/asyncio/events.py +++ b/Lib/asyncio/events.py @@ -15,7 +15,7 @@ "_set_event_loop_policy", "set_event_loop_policy", "get_event_loop", - "_set_event_loop", + "set_event_loop", "set_event_loop", "new_event_loop", "_set_running_loop", diff --git a/Lib/asyncio/runners.py b/Lib/asyncio/runners.py index 14397b4ad0c732..ba37e003a655c0 100644 --- a/Lib/asyncio/runners.py +++ b/Lib/asyncio/runners.py @@ -74,7 +74,7 @@ def close(self): loop.shutdown_default_executor(constants.THREAD_JOIN_TIMEOUT)) finally: if self._set_event_loop: - events._set_event_loop(None) + events.set_event_loop(None) loop.close() self._loop = None self._state = _State.CLOSED @@ -147,7 +147,7 @@ def _lazy_init(self): if not self._set_event_loop: # Call set_event_loop only once to avoid calling # attach_loop multiple times on child watchers - events._set_event_loop(self._loop) + events.set_event_loop(self._loop) self._set_event_loop = True else: self._loop = self._loop_factory() diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py index cdec54f28a2b2d..873c503fa02b21 100644 --- a/Lib/test/test_asyncio/test_events.py +++ b/Lib/test/test_asyncio/test_events.py @@ -2830,14 +2830,6 @@ async def inner(): class PolicyTests(unittest.TestCase): - def test_asyncio_set_event_loop_deprecation(self): - with self.assertWarnsRegex( - DeprecationWarning, "'asyncio.set_event_loop' is deprecated"): - loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) - self.assertIs(loop, asyncio.get_event_loop()) - loop.close() - def test_abstract_event_loop_policy_deprecation(self): with self.assertWarnsRegex( DeprecationWarning, "'asyncio.AbstractEventLoopPolicy' is deprecated"): diff --git a/Lib/test/test_asyncio/test_futures.py b/Lib/test/test_asyncio/test_futures.py index 2ed803c48b8065..8b51522278aaa6 100644 --- a/Lib/test/test_asyncio/test_futures.py +++ b/Lib/test/test_asyncio/test_futures.py @@ -179,7 +179,7 @@ async def test(): def test_constructor_use_global_loop(self): # Deprecated in 3.10, undeprecated in 3.12 asyncio.set_event_loop(self.loop) - self.addCleanup(asyncio._set_event_loop, None) + self.addCleanup(asyncio.set_event_loop, None) f = self._new_future() self.assertIs(f._loop, self.loop) self.assertIs(f.get_loop(), self.loop) @@ -567,7 +567,7 @@ async def test(): def test_wrap_future_use_global_loop(self): # Deprecated in 3.10, undeprecated in 3.12 asyncio.set_event_loop(self.loop) - self.addCleanup(asyncio._set_event_loop, None) + self.addCleanup(asyncio.set_event_loop, None) def run(arg): return (arg, threading.get_ident()) ex = concurrent.futures.ThreadPoolExecutor(1) diff --git a/Lib/test/test_asyncio/test_streams.py b/Lib/test/test_asyncio/test_streams.py index d04288cf39888c..4fa4384346f9e9 100644 --- a/Lib/test/test_asyncio/test_streams.py +++ b/Lib/test/test_asyncio/test_streams.py @@ -836,7 +836,7 @@ def test_streamreader_constructor_use_global_loop(self): # asyncio issue #184: Ensure that StreamReaderProtocol constructor # retrieves the current loop if the loop parameter is not set # Deprecated in 3.10, undeprecated in 3.12 - self.addCleanup(asyncio._set_event_loop, None) + self.addCleanup(asyncio.set_event_loop, None) asyncio.set_event_loop(self.loop) reader = asyncio.StreamReader() self.assertIs(reader._loop, self.loop) @@ -860,7 +860,7 @@ def test_streamreaderprotocol_constructor_use_global_loop(self): # asyncio issue #184: Ensure that StreamReaderProtocol constructor # retrieves the current loop if the loop parameter is not set # Deprecated in 3.10, undeprecated in 3.12 - self.addCleanup(asyncio._set_event_loop, None) + self.addCleanup(asyncio.set_event_loop, None) asyncio.set_event_loop(self.loop) reader = mock.Mock() protocol = asyncio.StreamReaderProtocol(reader) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index c8ad4abc276588..8d7f17334547b3 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -213,7 +213,7 @@ async def test(): # Deprecated in 3.10, undeprecated in 3.12 asyncio.set_event_loop(self.loop) - self.addCleanup(asyncio._set_event_loop, None) + self.addCleanup(asyncio.set_event_loop, None) t = asyncio.ensure_future(notmuch()) self.assertIs(t._loop, self.loop) self.loop.run_until_complete(t) @@ -2203,7 +2203,7 @@ async def coro(): return 42 asyncio.set_event_loop(self.loop) - self.addCleanup(asyncio._set_event_loop, None) + self.addCleanup(asyncio.set_event_loop, None) outer = asyncio.shield(coro()) self.assertEqual(outer._loop, self.loop) res = self.loop.run_until_complete(outer) @@ -3323,7 +3323,7 @@ async def gather(): def test_constructor_empty_sequence_use_global_loop(self): # Deprecated in 3.10, undeprecated in 3.12 asyncio.set_event_loop(self.one_loop) - self.addCleanup(asyncio._set_event_loop, None) + self.addCleanup(asyncio.set_event_loop, None) fut = asyncio.gather() self.assertIsInstance(fut, asyncio.Future) self.assertIs(fut._loop, self.one_loop) @@ -3431,7 +3431,7 @@ def test_constructor_use_global_loop(self): async def coro(): return 'abc' asyncio.set_event_loop(self.other_loop) - self.addCleanup(asyncio._set_event_loop, None) + self.addCleanup(asyncio.set_event_loop, None) gen1 = coro() gen2 = coro() fut = asyncio.gather(gen1, gen2) diff --git a/Lib/test/test_asyncio/utils.py b/Lib/test/test_asyncio/utils.py index c61105712d3cff..ad2bde490c888a 100644 --- a/Lib/test/test_asyncio/utils.py +++ b/Lib/test/test_asyncio/utils.py @@ -540,7 +540,7 @@ def set_event_loop(self, loop, *, cleanup=True): if loop is None: raise AssertionError('loop is None') # ensure that the event loop is passed explicitly in asyncio - events._set_event_loop(None) + events.set_event_loop(None) if cleanup: self.addCleanup(self.close_loop, loop) @@ -553,7 +553,7 @@ def setUp(self): self._thread_cleanup = threading_helper.threading_setup() def tearDown(self): - events._set_event_loop(None) + events.set_event_loop(None) # Detect CPython bug #23353: ensure that yield/yield-from is not used # in an except block of a generator From 45cdadf784358556646cab439d7b34fd7682b662 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Thu, 10 Apr 2025 13:12:02 +0530 Subject: [PATCH 3/3] remove duplicate --- Lib/asyncio/events.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py index 22e1dfb2212436..2913f901dca65f 100644 --- a/Lib/asyncio/events.py +++ b/Lib/asyncio/events.py @@ -16,7 +16,6 @@ "set_event_loop_policy", "get_event_loop", "set_event_loop", - "set_event_loop", "new_event_loop", "_set_running_loop", "get_running_loop",