Skip to content

Commit 87f9b1d

Browse files
[3.10] gh-100160: Remove any deprecation warnings in asyncio.get_event_loop() (GH-100412) (GH-100970)
Some deprecation warnings will reappear (in a slightly different form) in 3.12. Co-authored-by: Guido van Rossum <[email protected]>. (cherry picked from commit 1b2459d)
1 parent 47b1eb9 commit 87f9b1d

File tree

7 files changed

+29
-49
lines changed

7 files changed

+29
-49
lines changed

Doc/library/asyncio-eventloop.rst

+8-8
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ an event loop:
4848
running event loop.
4949

5050
If there is no running event loop set, the function will return
51-
the result of ``get_event_loop_policy().get_event_loop()`` call.
51+
the result of the ``get_event_loop_policy().get_event_loop()`` call.
5252

5353
Because this function has rather complex behavior (especially
5454
when custom event loop policies are in use), using the
@@ -59,15 +59,15 @@ an event loop:
5959
instead of using these lower level functions to manually create and close an
6060
event loop.
6161

62-
.. deprecated:: 3.10
63-
Deprecation warning is emitted if there is no current event loop.
64-
In Python 3.12 it will be an error.
65-
6662
.. note::
67-
In Python versions 3.10.0--3.10.8 this function
68-
(and other functions which used it implicitly) emitted a
63+
In Python versions 3.10.0--3.10.8 and 3.11.0 this function
64+
(and other functions which use it implicitly) emitted a
6965
:exc:`DeprecationWarning` if there was no running event loop, even if
70-
the current loop was set.
66+
the current loop was set on the policy.
67+
In Python versions 3.10.9, 3.11.1 and 3.12 they emit a
68+
:exc:`DeprecationWarning` if there is no running event loop and no
69+
current loop is set.
70+
In some future Python release this will become an error.
7171

7272
.. function:: set_event_loop(loop)
7373

Doc/library/asyncio-policy.rst

+5-4
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,11 @@ asyncio ships with the following built-in policies:
112112

113113
On Windows, :class:`ProactorEventLoop` is now used by default.
114114

115-
.. deprecated:: 3.10.9
116-
:meth:`get_event_loop` now emits a :exc:`DeprecationWarning` if there
117-
is no current event loop set and a new event loop has been implicitly
118-
created. In Python 3.12 it will be an error.
115+
.. note::
116+
In Python versions 3.10.9, 3.11.1 and 3.12 the :meth:`get_event_loop`
117+
method of the default asyncio policy emits a :exc:`DeprecationWarning`
118+
if there is no running event loop and no current loop is set.
119+
In some future Python release this will become an error.
119120

120121

121122
.. class:: WindowsSelectorEventLoopPolicy

Doc/whatsnew/3.10.rst

-13
Original file line numberDiff line numberDiff line change
@@ -1706,19 +1706,6 @@ Deprecated
17061706
scheduled for removal in Python 3.12.
17071707
(Contributed by Erlend E. Aasland in :issue:`42264`.)
17081708
1709-
* :func:`asyncio.get_event_loop` now emits a deprecation warning if there is
1710-
no running event loop. In the future it will be an alias of
1711-
:func:`~asyncio.get_running_loop`.
1712-
:mod:`asyncio` functions which implicitly create :class:`~asyncio.Future`
1713-
or :class:`~asyncio.Task` objects now emit
1714-
a deprecation warning if there is no running event loop and no explicit
1715-
*loop* argument is passed: :func:`~asyncio.ensure_future`,
1716-
:func:`~asyncio.wrap_future`, :func:`~asyncio.gather`,
1717-
:func:`~asyncio.shield`, :func:`~asyncio.as_completed` and constructors of
1718-
:class:`~asyncio.Future`, :class:`~asyncio.Task`,
1719-
:class:`~asyncio.StreamReader`, :class:`~asyncio.StreamReaderProtocol`.
1720-
(Contributed by Serhiy Storchaka in :issue:`39529`.)
1721-
17221709
* The undocumented built-in function ``sqlite3.enable_shared_cache`` is now
17231710
deprecated, scheduled for removal in Python 3.12. Its use is strongly
17241711
discouraged by the SQLite3 documentation. See `the SQLite3 docs

Lib/asyncio/events.py

-15
Original file line numberDiff line numberDiff line change
@@ -650,21 +650,6 @@ def get_event_loop(self):
650650
if (self._local._loop is None and
651651
not self._local._set_called and
652652
threading.current_thread() is threading.main_thread()):
653-
stacklevel = 2
654-
try:
655-
f = sys._getframe(1)
656-
except AttributeError:
657-
pass
658-
else:
659-
while f:
660-
module = f.f_globals.get('__name__')
661-
if not (module == 'asyncio' or module.startswith('asyncio.')):
662-
break
663-
f = f.f_back
664-
stacklevel += 1
665-
import warnings
666-
warnings.warn('There is no current event loop',
667-
DeprecationWarning, stacklevel=stacklevel)
668653
self.set_event_loop(self.new_event_loop())
669654

670655
if self._local._loop is None:

Lib/test/test_asyncio/test_events.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -2602,9 +2602,7 @@ def test_event_loop_policy(self):
26022602
def test_get_event_loop(self):
26032603
policy = asyncio.DefaultEventLoopPolicy()
26042604
self.assertIsNone(policy._local._loop)
2605-
with self.assertWarns(DeprecationWarning) as cm:
2606-
loop = policy.get_event_loop()
2607-
self.assertEqual(cm.filename, __file__)
2605+
loop = policy.get_event_loop()
26082606
self.assertIsInstance(loop, asyncio.AbstractEventLoop)
26092607

26102608
self.assertIs(policy._local._loop, loop)
@@ -2618,10 +2616,8 @@ def test_get_event_loop_calls_set_event_loop(self):
26182616
policy, "set_event_loop",
26192617
wraps=policy.set_event_loop) as m_set_event_loop:
26202618

2621-
with self.assertWarns(DeprecationWarning) as cm:
2622-
loop = policy.get_event_loop()
2619+
loop = policy.get_event_loop()
26232620
self.addCleanup(loop.close)
2624-
self.assertEqual(cm.filename, __file__)
26252621

26262622
# policy._local._loop must be set through .set_event_loop()
26272623
# (the unix DefaultEventLoopPolicy needs this call to attach
@@ -2810,10 +2806,8 @@ def test_get_event_loop_returns_running_loop2(self):
28102806
loop = asyncio.new_event_loop()
28112807
self.addCleanup(loop.close)
28122808

2813-
with self.assertWarns(DeprecationWarning) as cm:
2814-
loop2 = asyncio.get_event_loop()
2809+
loop2 = asyncio.get_event_loop()
28152810
self.addCleanup(loop2.close)
2816-
self.assertEqual(cm.warnings[0].filename, __file__)
28172811
asyncio.set_event_loop(None)
28182812
with self.assertRaisesRegex(RuntimeError, 'no current'):
28192813
asyncio.get_event_loop()

Lib/test/test_json/test_default.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import collections
12
from test.test_json import PyTest, CTest
23

34

@@ -7,6 +8,16 @@ def test_default(self):
78
self.dumps(type, default=repr),
89
self.dumps(repr(type)))
910

11+
def test_ordereddict(self):
12+
od = collections.OrderedDict(a=1, b=2)
13+
od.move_to_end('a')
14+
self.assertEqual(
15+
self.dumps(od),
16+
'{"b": 2, "a": 1}')
17+
self.assertEqual(
18+
self.dumps(od, sort_keys=True),
19+
'{"a": 1, "b": 2}')
20+
1021

1122
class TestPyDefault(TestDefault, PyTest): pass
1223
class TestCDefault(TestDefault, CTest): pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Remove any deprecation warnings in :func:`asyncio.get_event_loop`. They are
2+
deferred to Python 3.12.

0 commit comments

Comments
 (0)