Skip to content

gh-94174: asyncio.get_event_loop() fails if no current event loop #94175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

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/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ Changes in the Python API
to :term:`filesystem encoding and error handler`.
Argument files should be encoded in UTF-8 instead of ANSI Codepage on Windows.

* :func:`asyncio.get_event_loop` now fails if there is no current event loop.
In Python 3.10, it emits a :exc:`DeprecationWarning` in this case.
(Contributed by Victor Stinner in :gh:`94174`.)


Build Changes
=============
Expand Down
6 changes: 2 additions & 4 deletions Lib/asyncio/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,10 +789,8 @@ def _get_event_loop(stacklevel=3):
current_loop = _get_running_loop()
if current_loop is not None:
return current_loop
import warnings
warnings.warn('There is no current event loop',
DeprecationWarning, stacklevel=stacklevel)
return get_event_loop_policy().get_event_loop()
raise RuntimeError('There is no current event loop in thread %r.'
% threading.current_thread().name)


def set_event_loop(loop):
Expand Down
47 changes: 16 additions & 31 deletions Lib/test/test_asyncio/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2708,15 +2708,11 @@ def get_event_loop(self):
asyncio.set_event_loop_policy(Policy())
loop = asyncio.new_event_loop()

with self.assertWarns(DeprecationWarning) as cm:
with self.assertRaises(TestError):
asyncio.get_event_loop()
self.assertEqual(cm.filename, __file__)
with self.assertRaisesRegex(RuntimeError, 'no current event loop'):
asyncio.get_event_loop()
asyncio.set_event_loop(None)
with self.assertWarns(DeprecationWarning) as cm:
with self.assertRaises(TestError):
asyncio.get_event_loop()
self.assertEqual(cm.filename, __file__)
with self.assertRaisesRegex(RuntimeError, 'no current event loop'):
asyncio.get_event_loop()

with self.assertRaisesRegex(RuntimeError, 'no running'):
asyncio.get_running_loop()
Expand All @@ -2730,16 +2726,12 @@ async def func():
loop.run_until_complete(func())

asyncio.set_event_loop(loop)
with self.assertWarns(DeprecationWarning) as cm:
with self.assertRaises(TestError):
asyncio.get_event_loop()
self.assertEqual(cm.filename, __file__)
with self.assertRaisesRegex(RuntimeError, 'no current event loop'):
asyncio.get_event_loop()

asyncio.set_event_loop(None)
with self.assertWarns(DeprecationWarning) as cm:
with self.assertRaises(TestError):
asyncio.get_event_loop()
self.assertEqual(cm.filename, __file__)
with self.assertRaisesRegex(RuntimeError, 'no current event loop'):
asyncio.get_event_loop()

finally:
asyncio.set_event_loop_policy(old_policy)
Expand All @@ -2758,15 +2750,11 @@ def test_get_event_loop_returns_running_loop2(self):
loop = asyncio.new_event_loop()
self.addCleanup(loop.close)

with self.assertWarns(DeprecationWarning) as cm:
loop2 = asyncio.get_event_loop()
self.addCleanup(loop2.close)
self.assertEqual(cm.filename, __file__)
with self.assertRaisesRegex(RuntimeError, 'no current event loop'):
asyncio.get_event_loop()
asyncio.set_event_loop(None)
with self.assertWarns(DeprecationWarning) as cm:
with self.assertRaisesRegex(RuntimeError, 'no current'):
asyncio.get_event_loop()
self.assertEqual(cm.filename, __file__)
with self.assertRaisesRegex(RuntimeError, 'no current event loop'):
asyncio.get_event_loop()

with self.assertRaisesRegex(RuntimeError, 'no running'):
asyncio.get_running_loop()
Expand All @@ -2780,15 +2768,12 @@ async def func():
loop.run_until_complete(func())

asyncio.set_event_loop(loop)
with self.assertWarns(DeprecationWarning) as cm:
self.assertIs(asyncio.get_event_loop(), loop)
self.assertEqual(cm.filename, __file__)
with self.assertRaisesRegex(RuntimeError, 'no current event loop'):
asyncio.get_event_loop()

asyncio.set_event_loop(None)
with self.assertWarns(DeprecationWarning) as cm:
with self.assertRaisesRegex(RuntimeError, 'no current'):
asyncio.get_event_loop()
self.assertEqual(cm.filename, __file__)
with self.assertRaisesRegex(RuntimeError, 'no current event loop'):
asyncio.get_event_loop()

finally:
asyncio.set_event_loop_policy(old_policy)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:func:`asyncio.get_event_loop` now fails if there is no current event loop.
In Python 3.10, it emits a :exc:`DeprecationWarning` in this case.
Patch by Victor Stinner
20 changes: 2 additions & 18 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ _Py_IDENTIFIER(_asyncio_future_blocking);
_Py_IDENTIFIER(add_done_callback);
_Py_IDENTIFIER(call_soon);
_Py_IDENTIFIER(cancel);
_Py_IDENTIFIER(get_event_loop);
_Py_IDENTIFIER(throw);
_Py_IDENTIFIER(_check_future);

Expand Down Expand Up @@ -330,30 +329,15 @@ static PyObject *
get_event_loop(int stacklevel)
{
PyObject *loop;
PyObject *policy;

if (get_running_loop(&loop)) {
return NULL;
}
if (loop != NULL) {
return loop;
}

if (PyErr_WarnEx(PyExc_DeprecationWarning,
"There is no current event loop",
stacklevel))
{
return NULL;
}

policy = PyObject_CallNoArgs(asyncio_get_event_loop_policy);
if (policy == NULL) {
return NULL;
}

loop = _PyObject_CallMethodIdNoArgs(policy, &PyId_get_event_loop);
Py_DECREF(policy);
return loop;
PyErr_SetString(PyExc_RuntimeError, "There is no current event loop");
return NULL;
}


Expand Down