Skip to content
Draft
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
28 changes: 28 additions & 0 deletions uvloop/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio as __asyncio
from types import FrameType
import typing as _typing
import sys as _sys
import warnings as _warnings
Expand Down Expand Up @@ -202,6 +203,33 @@ def get_event_loop(self) -> _AbstractEventLoop:
Returns an instance of EventLoop or raises an exception.
"""
if (
self._local._loop is None
and threading.current_thread() is threading.main_thread()
):
# Replicate the behavior of asyncio.get_event_loop() as closely
# as possible, including the warning and stack level.
stacklevel = 2
try:
f: _typing.Optional[FrameType] = _sys._getframe(1)
except AttributeError:
pass
else:
# Move up the call stack so that the warning is attached
# to the line outside uvloop itself.
while f is not None:
module = f.f_globals['__name__']
if not (module == 'uvloop' or module.startswith('uvloop.')):
break
f = f.f_back
stacklevel += 1
_warnings.warn(
'There is no current event loop',
DeprecationWarning,
stacklevel=stacklevel
)
self._local._loop = self._loop_factory()

if self._local._loop is None:
raise RuntimeError(
'There is no current event loop in thread %r.'
Expand Down
Loading