Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaraditya303 committed Nov 3, 2024
1 parent 516f097 commit 1214f07
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
8 changes: 2 additions & 6 deletions Doc/library/asyncio-eventloop.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ an event loop:
call_soon or similar API), this function will always return the
running event loop.

If there is no running event loop set, the function will return
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
:func:`get_running_loop` function is preferred to :func:`get_event_loop`
Expand All @@ -59,9 +56,8 @@ an event loop:
instead of using these lower level functions to manually create and close an
event loop.

.. deprecated:: 3.12
Deprecation warning is emitted if there is no current event loop.
In some future Python release this will become an error.
.. versionchanged:: 3.14
Raises a :exc:`RuntimeError` if there is no set event loop.

.. function:: set_event_loop(loop)

Expand Down
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,11 @@ asyncio

(Contributed by Kumar Aditya in :gh:`120804`.)

* Removed implicit creation of event loop by :func:`asyncio.get_event_loop`.
It now raises a :exc:`RuntimeError` if there is no set event loop.
(Contributed by Kumar Aditya in :gh:`126353`.)


collections.abc
---------------

Expand Down
28 changes: 28 additions & 0 deletions Lib/test/test_asyncio/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2701,6 +2701,33 @@ def test_event_loop_policy(self):
self.assertRaises(NotImplementedError, policy.set_event_loop, object())
self.assertRaises(NotImplementedError, policy.new_event_loop)

def test_get_event_loop(self):
policy = asyncio.DefaultEventLoopPolicy()
self.assertIsNone(policy._local._loop)

with self.assertRaises(RuntimeError):
loop = policy.get_event_loop()
self.assertIsNone(policy._local._loop)

loop = policy.new_event_loop()
self.assertIsNone(policy._local._loop)
policy.set_event_loop(loop)
self.assertIs(policy._local._loop, loop)
self.assertIs(loop, policy.get_event_loop())
loop.close()

def test_get_event_loop_does_not_call_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.assertRaises(RuntimeError):
loop = policy.get_event_loop()

m_set_event_loop.assert_not_called()

def test_get_event_loop_after_set_none(self):
policy = asyncio.DefaultEventLoopPolicy()
policy.set_event_loop(None)
Expand Down Expand Up @@ -2881,6 +2908,7 @@ def test_get_event_loop_returns_running_loop2(self):
loop = asyncio.new_event_loop()
self.addCleanup(loop.close)

asyncio.set_event_loop(None)
with self.assertRaisesRegex(RuntimeError, 'no current'):
asyncio.get_event_loop()

Expand Down

0 comments on commit 1214f07

Please sign in to comment.