From 8800552ea5cff9bfdb1681ad2128efd059e32702 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 21 Dec 2022 17:49:53 +0200 Subject: [PATCH 1/4] gh-100160: Deprecate implicit creation of an event loop Partially revert changes made in GH-93453. asyncio.DefaultEventLoopPolicy.get_event_loop() now emits a DeprecationWarning and creates and sets a new event loop instead of raising a RuntimeError if there is no current event loop set. --- Doc/library/asyncio-eventloop.rst | 10 +++--- Doc/library/asyncio-policy.rst | 7 ++-- Doc/whatsnew/3.12.rst | 18 ++++------ Lib/asyncio/events.py | 22 +++++++++++- Lib/test/test_asyncio/test_events.py | 35 ++++++++++++++++--- Lib/test/test_asyncio/test_unix_events.py | 8 +++-- ...-12-21-17-49-50.gh-issue-100160.N0NHRj.rst | 3 ++ 7 files changed, 75 insertions(+), 28 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2022-12-21-17-49-50.gh-issue-100160.N0NHRj.rst diff --git a/Doc/library/asyncio-eventloop.rst b/Doc/library/asyncio-eventloop.rst index 470d1aa130e40f..b70dcef64c9a21 100644 --- a/Doc/library/asyncio-eventloop.rst +++ b/Doc/library/asyncio-eventloop.rst @@ -48,7 +48,7 @@ an event loop: running event loop. If there is no running event loop set, the function will return - the result of calling ``get_event_loop_policy().get_event_loop()``. + the result of ``get_event_loop_policy().get_event_loop()`` call. Because this function has rather complex behavior (especially when custom event loop policies are in use), using the @@ -59,11 +59,9 @@ an event loop: instead of using these lower level functions to manually create and close an event loop. - .. note:: - In Python versions 3.10.0--3.10.8 and 3.11.0 this function - (and other functions which used it implicitly) emitted a - :exc:`DeprecationWarning` if there was no running event loop, even if - the current loop was set. + .. deprecated:: 3.12 + Deprecation warning is emitted if there is no current event loop. + In future Python releases it will be an error. .. function:: set_event_loop(loop) diff --git a/Doc/library/asyncio-policy.rst b/Doc/library/asyncio-policy.rst index ccd95244947534..f55f8022d730b6 100644 --- a/Doc/library/asyncio-policy.rst +++ b/Doc/library/asyncio-policy.rst @@ -116,9 +116,10 @@ asyncio ships with the following built-in policies: On Windows, :class:`ProactorEventLoop` is now used by default. - .. versionchanged:: 3.12 - :meth:`get_event_loop` now raises a :exc:`RuntimeError` if there is no - current event loop set. + .. deprecated:: 3.12 + :meth:`get_event_loop` now emits a :exc:`DeprecationWarning` if there + is no current event loop set and a new event loop has been implicitly + created. In future Python releases it will be an error. .. class:: WindowsSelectorEventLoopPolicy diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index 0cc4471364b671..d137d4019f71da 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -392,6 +392,12 @@ Deprecated :exc:`ImportWarning`). (Contributed by Brett Cannon in :gh:`65961`.) +* The :meth:`~asyncio.DefaultEventLoopPolicy.get_event_loop` of the default + event loop policy now emits a :exc:`DeprecationWarning` if there + is no current event loop set and a new event loop has been implicitly + created. + (Contributed by Serhiy Storchaka and Łukasz Langa in :gh:`100160`.) + Pending Removal in Python 3.13 ------------------------------ @@ -692,18 +698,6 @@ Changes in the Python API around process-global resources, which are best managed from the main interpreter. (Contributed by Dong-hee Na in :gh:`99127`.) -* :func:`asyncio.get_event_loop` and many other :mod:`asyncio` functions like - :func:`~asyncio.ensure_future`, :func:`~asyncio.shield` or - :func:`~asyncio.gather`, and also the - :meth:`~asyncio.BaseDefaultEventLoopPolicy.get_event_loop` method of - :class:`~asyncio.BaseDefaultEventLoopPolicy` now raise a :exc:`RuntimeError` - if called when there is no running event loop and the current event loop was - not set. - Previously they implicitly created and set a new current event loop. - :exc:`DeprecationWarning` is no longer emitted if there is no running - event loop but the current event loop is set in the policy. - (Contributed by Serhiy Storchaka in :gh:`93453`.) - Build Changes ============= diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py index 6cff8c59ea4779..d85e0feed6b14a 100644 --- a/Lib/asyncio/events.py +++ b/Lib/asyncio/events.py @@ -619,7 +619,7 @@ def get_event_loop(self): Returns an event loop object implementing the BaseEventLoop interface, or raises an exception in case no event loop has been set for the - current context. + current context and the current policy does not specify to create one. It should never return None.""" raise NotImplementedError @@ -672,6 +672,26 @@ def get_event_loop(self): Returns an instance of EventLoop or raises an exception. """ + if (self._local._loop is None and + not self._local._set_called and + threading.current_thread() is threading.main_thread()): + stacklevel = 2 + try: + f = sys._getframe(1) + except AttributeError: + pass + else: + while f: + module = f.f_globals.get('__name__') + if not (module == 'asyncio' or module.startswith('asyncio.')): + break + f = f.f_back + stacklevel += 1 + import warnings + warnings.warn('There is no current event loop', + DeprecationWarning, stacklevel=stacklevel) + self.set_event_loop(self.new_event_loop()) + if self._local._loop is None: raise RuntimeError('There is no current event loop in thread %r.' % threading.current_thread().name) diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py index 153b2de8172273..4b8c245f91a92e 100644 --- a/Lib/test/test_asyncio/test_events.py +++ b/Lib/test/test_asyncio/test_events.py @@ -2550,8 +2550,33 @@ def test_event_loop_policy(self): def test_get_event_loop(self): policy = asyncio.DefaultEventLoopPolicy() self.assertIsNone(policy._local._loop) - with self.assertRaisesRegex(RuntimeError, 'no current event loop'): - policy.get_event_loop() + with self.assertWarns(DeprecationWarning) as cm: + loop = policy.get_event_loop() + self.assertEqual(cm.filename, __file__) + self.assertIsInstance(loop, asyncio.AbstractEventLoop) + + self.assertIs(policy._local._loop, loop) + self.assertIs(loop, policy.get_event_loop()) + loop.close() + + def test_get_event_loop_calls_set_event_loop(self): + policy = asyncio.DefaultEventLoopPolicy() + + with mock.patch.object( + policy, "set_event_loop", + wraps=policy.set_event_loop) as m_set_event_loop: + + with self.assertWarns(DeprecationWarning) as cm: + loop = policy.get_event_loop() + self.addCleanup(loop.close) + self.assertEqual(cm.filename, __file__) + + # policy._local._loop must be set through .set_event_loop() + # (the unix DefaultEventLoopPolicy needs this call to attach + # the child watcher correctly) + m_set_event_loop.assert_called_with(loop) + + loop.close() def test_get_event_loop_after_set_none(self): policy = asyncio.DefaultEventLoopPolicy() @@ -2737,8 +2762,10 @@ def test_get_event_loop_returns_running_loop2(self): loop = asyncio.new_event_loop() self.addCleanup(loop.close) - with self.assertRaisesRegex(RuntimeError, 'no current'): - asyncio.get_event_loop() + with self.assertWarns(DeprecationWarning) as cm: + loop2 = asyncio.get_event_loop() + self.addCleanup(loop2.close) + self.assertEqual(cm.filename, __file__) asyncio.set_event_loop(None) with self.assertRaisesRegex(RuntimeError, 'no current'): asyncio.get_event_loop() diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py index 600a5900da088d..33d0ea15c6de0e 100644 --- a/Lib/test/test_asyncio/test_unix_events.py +++ b/Lib/test/test_asyncio/test_unix_events.py @@ -1884,7 +1884,9 @@ async def test_fork_not_share_event_loop(self): if pid == 0: # child try: - loop = asyncio.get_event_loop_policy().get_event_loop() + with self.assertWarns(DeprecationWarning): + loop = asyncio.get_event_loop_policy().get_event_loop() + os.write(w, b'LOOP:' + str(id(loop)).encode()) except RuntimeError: os.write(w, b'NO LOOP') except: @@ -1893,7 +1895,9 @@ async def test_fork_not_share_event_loop(self): os._exit(0) else: # parent - self.assertEqual(os.read(r, 100), b'NO LOOP') + result = os.read(r, 100) + self.assertEqual(result[:5], b'LOOP:', result) + self.assertNotEqual(int(result[5:]), id(loop)) wait_process(pid, exitcode=0) @hashlib_helper.requires_hashdigest('md5') diff --git a/Misc/NEWS.d/next/Library/2022-12-21-17-49-50.gh-issue-100160.N0NHRj.rst b/Misc/NEWS.d/next/Library/2022-12-21-17-49-50.gh-issue-100160.N0NHRj.rst new file mode 100644 index 00000000000000..9a290057d0fb62 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-12-21-17-49-50.gh-issue-100160.N0NHRj.rst @@ -0,0 +1,3 @@ +Emit a deprecation warning in +:meth:`asyncio.DefaultEventLoopPolicy.get_event_loop` if there is no current +event loop set and a new event loop has been implicitly created. From 711efbf5d7d731945e1cfed04ca27f9cf6ba505b Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 22 Dec 2022 09:30:48 +0200 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: Guido van Rossum --- Doc/library/asyncio-policy.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/asyncio-policy.rst b/Doc/library/asyncio-policy.rst index f55f8022d730b6..02dbcc10b45d87 100644 --- a/Doc/library/asyncio-policy.rst +++ b/Doc/library/asyncio-policy.rst @@ -118,8 +118,8 @@ asyncio ships with the following built-in policies: .. deprecated:: 3.12 :meth:`get_event_loop` now emits a :exc:`DeprecationWarning` if there - is no current event loop set and a new event loop has been implicitly - created. In future Python releases it will be an error. + is no current event loop set and it decides to create one. + In future Python releases it will be an error. .. class:: WindowsSelectorEventLoopPolicy From 0feec4c0f5e3cd8a2fe013f6437b1d5ff0fb7736 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 22 Dec 2022 09:31:23 +0200 Subject: [PATCH 3/4] Apply suggestions from the code review. --- Doc/library/asyncio-eventloop.rst | 4 ++-- Doc/library/asyncio-policy.rst | 3 ++- Doc/whatsnew/3.12.rst | 3 +-- Lib/asyncio/events.py | 2 ++ .../Library/2022-12-21-17-49-50.gh-issue-100160.N0NHRj.rst | 2 +- 5 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Doc/library/asyncio-eventloop.rst b/Doc/library/asyncio-eventloop.rst index b70dcef64c9a21..22348c9f4f710c 100644 --- a/Doc/library/asyncio-eventloop.rst +++ b/Doc/library/asyncio-eventloop.rst @@ -48,7 +48,7 @@ an event loop: running event loop. If there is no running event loop set, the function will return - the result of ``get_event_loop_policy().get_event_loop()`` call. + the result of the ``get_event_loop_policy().get_event_loop()`` call. Because this function has rather complex behavior (especially when custom event loop policies are in use), using the @@ -61,7 +61,7 @@ an event loop: .. deprecated:: 3.12 Deprecation warning is emitted if there is no current event loop. - In future Python releases it will be an error. + In some future Python release it will become an error. .. function:: set_event_loop(loop) diff --git a/Doc/library/asyncio-policy.rst b/Doc/library/asyncio-policy.rst index f55f8022d730b6..5904dc65772478 100644 --- a/Doc/library/asyncio-policy.rst +++ b/Doc/library/asyncio-policy.rst @@ -119,7 +119,8 @@ asyncio ships with the following built-in policies: .. deprecated:: 3.12 :meth:`get_event_loop` now emits a :exc:`DeprecationWarning` if there is no current event loop set and a new event loop has been implicitly - created. In future Python releases it will be an error. + created. + In some future Python release it will become an error. .. class:: WindowsSelectorEventLoopPolicy diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index d137d4019f71da..e4a933c82e49b5 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -394,8 +394,7 @@ Deprecated * The :meth:`~asyncio.DefaultEventLoopPolicy.get_event_loop` of the default event loop policy now emits a :exc:`DeprecationWarning` if there - is no current event loop set and a new event loop has been implicitly - created. + is no current event loop set and it decides to create one. (Contributed by Serhiy Storchaka and Łukasz Langa in :gh:`100160`.) diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py index d85e0feed6b14a..ce44942186b272 100644 --- a/Lib/asyncio/events.py +++ b/Lib/asyncio/events.py @@ -681,6 +681,8 @@ def get_event_loop(self): except AttributeError: pass else: + # Move up the call stack so that the warning is attached + # to the line outside asyncio itself. while f: module = f.f_globals.get('__name__') if not (module == 'asyncio' or module.startswith('asyncio.')): diff --git a/Misc/NEWS.d/next/Library/2022-12-21-17-49-50.gh-issue-100160.N0NHRj.rst b/Misc/NEWS.d/next/Library/2022-12-21-17-49-50.gh-issue-100160.N0NHRj.rst index 9a290057d0fb62..d5cc785722d7fd 100644 --- a/Misc/NEWS.d/next/Library/2022-12-21-17-49-50.gh-issue-100160.N0NHRj.rst +++ b/Misc/NEWS.d/next/Library/2022-12-21-17-49-50.gh-issue-100160.N0NHRj.rst @@ -1,3 +1,3 @@ Emit a deprecation warning in :meth:`asyncio.DefaultEventLoopPolicy.get_event_loop` if there is no current -event loop set and a new event loop has been implicitly created. +event loop set and it decides to create one. From 1e22c6f1c1c1b64e9432704a786e484982862ede Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 12 Jan 2023 11:40:54 +0200 Subject: [PATCH 4/4] Tweak the documentation. --- Doc/library/asyncio-eventloop.rst | 2 +- Doc/library/asyncio-policy.rst | 7 ++++--- Doc/whatsnew/3.10.rst | 13 ------------- Doc/whatsnew/3.12.rst | 6 +++--- 4 files changed, 8 insertions(+), 20 deletions(-) diff --git a/Doc/library/asyncio-eventloop.rst b/Doc/library/asyncio-eventloop.rst index 22348c9f4f710c..db63a5dd11ad6e 100644 --- a/Doc/library/asyncio-eventloop.rst +++ b/Doc/library/asyncio-eventloop.rst @@ -61,7 +61,7 @@ an event loop: .. deprecated:: 3.12 Deprecation warning is emitted if there is no current event loop. - In some future Python release it will become an error. + In some future Python release this will become an error. .. function:: set_event_loop(loop) diff --git a/Doc/library/asyncio-policy.rst b/Doc/library/asyncio-policy.rst index 497e7aab54a585..0d7821e608ec98 100644 --- a/Doc/library/asyncio-policy.rst +++ b/Doc/library/asyncio-policy.rst @@ -117,9 +117,10 @@ asyncio ships with the following built-in policies: On Windows, :class:`ProactorEventLoop` is now used by default. .. deprecated:: 3.12 - :meth:`get_event_loop` now emits a :exc:`DeprecationWarning` if there - is no current event loop set and it decides to create one. - In some future Python release it will become an error. + The :meth:`get_event_loop` method of the default asyncio policy now emits + a :exc:`DeprecationWarning` if there is no current event loop set and it + decides to create one. + In some future Python release this will become an error. .. class:: WindowsSelectorEventLoopPolicy diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index 1c21caf355f082..c74f8b23b88e6a 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -1711,19 +1711,6 @@ Deprecated scheduled for removal in Python 3.12. (Contributed by Erlend E. Aasland in :issue:`42264`.) -* :func:`asyncio.get_event_loop` now emits a deprecation warning if there is - no running event loop. In the future it will be an alias of - :func:`~asyncio.get_running_loop`. - :mod:`asyncio` functions which implicitly create :class:`~asyncio.Future` - or :class:`~asyncio.Task` objects now emit - a deprecation warning if there is no running event loop and no explicit - *loop* argument is passed: :func:`~asyncio.ensure_future`, - :func:`~asyncio.wrap_future`, :func:`~asyncio.gather`, - :func:`~asyncio.shield`, :func:`~asyncio.as_completed` and constructors of - :class:`~asyncio.Future`, :class:`~asyncio.Task`, - :class:`~asyncio.StreamReader`, :class:`~asyncio.StreamReaderProtocol`. - (Contributed by Serhiy Storchaka in :issue:`39529`.) - * The undocumented built-in function ``sqlite3.enable_shared_cache`` is now deprecated, scheduled for removal in Python 3.12. Its use is strongly discouraged by the SQLite3 documentation. See `the SQLite3 docs diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index e4a933c82e49b5..c6536ceda83aab 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -392,10 +392,10 @@ Deprecated :exc:`ImportWarning`). (Contributed by Brett Cannon in :gh:`65961`.) -* The :meth:`~asyncio.DefaultEventLoopPolicy.get_event_loop` of the default - event loop policy now emits a :exc:`DeprecationWarning` if there +* The :meth:`~asyncio.DefaultEventLoopPolicy.get_event_loop` method of the + default event loop policy now emits a :exc:`DeprecationWarning` if there is no current event loop set and it decides to create one. - (Contributed by Serhiy Storchaka and Łukasz Langa in :gh:`100160`.) + (Contributed by Serhiy Storchaka and Guido van Rossum in :gh:`100160`.) Pending Removal in Python 3.13