Skip to content

Commit

Permalink
Merge pull request #99 from niklasf/get-running-loop
Browse files Browse the repository at this point in the history
Fix asyncio.get_running_loop() monkey-patching (#83)
  • Loading branch information
oremanj authored Jan 7, 2021
2 parents 9451387 + ebf62ac commit b93c320
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions newsfragments/99.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:func:`asyncio.get_running_loop()` will now return the trio-asyncio event loop
(if running), instead of failing with :exc:`RuntimeError`.
11 changes: 11 additions & 0 deletions tests/test_trio_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,14 @@ async def test_half_fixtured_asyncpg_conn(asyncio_fixture_own_loop):
@pytest.mark.trio
async def test_fixtured_asyncpg_conn(asyncio_fixture_with_fixtured_loop):
await use_asyncio()


@pytest.mark.trio
async def test_get_running_loop():
async with trio_asyncio.open_loop() as loop:
try:
from asyncio import get_running_loop
except ImportError:
pass # Python 3.6
else:
assert get_running_loop() == loop
2 changes: 2 additions & 0 deletions trio_asyncio/_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ def _new_run_get_or_throw():

_aio_event._get_running_loop = _new_run_get
_aio_event.get_running_loop = _new_run_get_or_throw
asyncio._get_running_loop = _new_run_get
asyncio.get_running_loop = _new_run_get_or_throw

#####

Expand Down

0 comments on commit b93c320

Please sign in to comment.