forked from jupyter/jupyter_core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
206 lines (165 loc) · 6.97 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
from __future__ import annotations
import asyncio
import atexit
import errno
import inspect
import sys
import threading
import warnings
from contextvars import ContextVar
from pathlib import Path
from types import FrameType
from typing import Any, Awaitable, Callable, TypeVar, cast
def ensure_dir_exists(path: str | Path, mode: int = 0o777) -> None:
"""Ensure that a directory exists
If it doesn't exist, try to create it, protecting against a race condition
if another process is doing the same.
The default permissions are determined by the current umask.
"""
try:
Path(path).mkdir(parents=True, mode=mode)
except OSError as e:
if e.errno != errno.EEXIST:
raise
if not Path(path).is_dir():
raise OSError("%r exists but is not a directory" % path)
def _get_frame(level: int) -> FrameType | None:
"""Get the frame at the given stack level."""
# sys._getframe is much faster than inspect.stack, but isn't guaranteed to
# exist in all python implementations, so we fall back to inspect.stack()
# We need to add one to level to account for this get_frame call.
if hasattr(sys, "_getframe"):
frame = sys._getframe(level + 1)
else:
frame = inspect.stack(context=0)[level + 1].frame
return frame
# This function is from https://github.com/python/cpython/issues/67998
# (https://bugs.python.org/file39550/deprecated_module_stacklevel.diff) and
# calculates the appropriate stacklevel for deprecations to target the
# deprecation for the caller, no matter how many internal stack frames we have
# added in the process. For example, with the deprecation warning in the
# __init__ below, the appropriate stacklevel will change depending on how deep
# the inheritance hierarchy is.
def _external_stacklevel(internal: list[str]) -> int:
"""Find the stacklevel of the first frame that doesn't contain any of the given internal strings
The depth will be 1 at minimum in order to start checking at the caller of
the function that called this utility method.
"""
# Get the level of my caller's caller
level = 2
frame = _get_frame(level)
# Normalize the path separators:
normalized_internal = [str(Path(s)) for s in internal]
# climb the stack frames while we see internal frames
while frame and any(s in str(Path(frame.f_code.co_filename)) for s in normalized_internal):
level += 1
frame = frame.f_back
# Return the stack level from the perspective of whoever called us (i.e., one level up)
return level - 1
def deprecation(message: str, internal: str | list[str] = "jupyter_core/") -> None:
"""Generate a deprecation warning targeting the first frame that is not 'internal'
internal is a string or list of strings, which if they appear in filenames in the
frames, the frames will be considered internal. Changing this can be useful if, for example,
we know that our internal code is calling out to another library.
"""
_internal: list[str]
_internal = [internal] if isinstance(internal, str) else internal
# stack level of the first external frame from here
stacklevel = _external_stacklevel(_internal)
# The call to .warn adds one frame, so bump the stacklevel up by one
warnings.warn(message, DeprecationWarning, stacklevel=stacklevel + 1)
T = TypeVar("T")
class _TaskRunner:
"""A task runner that runs an asyncio event loop on a background thread."""
def __init__(self) -> None:
self.__io_loop: asyncio.AbstractEventLoop | None = None
self.__runner_thread: threading.Thread | None = None
self.__lock = threading.Lock()
atexit.register(self._close)
def _close(self) -> None:
if self.__io_loop:
self.__io_loop.stop()
def _runner(self) -> None:
loop = self.__io_loop
assert loop is not None
try:
loop.run_forever()
finally:
loop.close()
def run(self, coro: Any) -> Any:
"""Synchronously run a coroutine on a background thread."""
with self.__lock:
name = f"{threading.current_thread().name} - runner"
if self.__io_loop is None:
self.__io_loop = asyncio.new_event_loop()
self.__runner_thread = threading.Thread(target=self._runner, daemon=True, name=name)
self.__runner_thread.start()
fut = asyncio.run_coroutine_threadsafe(coro, self.__io_loop)
return fut.result(None)
_runner_map: dict[str, _TaskRunner] = {}
_loop: ContextVar[asyncio.AbstractEventLoop | None] = ContextVar("_loop", default=None)
def run_sync(coro: Callable[..., Awaitable[T]]) -> Callable[..., T]:
"""Wraps coroutine in a function that blocks until it has executed.
Parameters
----------
coro : coroutine-function
The coroutine-function to be executed.
Returns
-------
result :
Whatever the coroutine-function returns.
"""
if not inspect.iscoroutinefunction(coro):
raise AssertionError
def wrapped(*args: Any, **kwargs: Any) -> Any:
name = threading.current_thread().name
inner = coro(*args, **kwargs)
try:
# If a loop is currently running in this thread,
# use a task runner.
asyncio.get_running_loop()
if name not in _runner_map:
_runner_map[name] = _TaskRunner()
return _runner_map[name].run(inner)
except RuntimeError:
pass
# Run the loop for this thread.
loop = ensure_event_loop()
return loop.run_until_complete(inner)
wrapped.__doc__ = coro.__doc__
return wrapped
def ensure_event_loop(prefer_selector_loop: bool = False) -> asyncio.AbstractEventLoop:
# Get the loop for this thread, or create a new one.
loop = _loop.get()
if loop is not None and not loop.is_closed():
return loop
try:
loop = asyncio.get_running_loop()
except RuntimeError:
if sys.platform == "win32" and prefer_selector_loop:
loop = asyncio.WindowsSelectorEventLoopPolicy().new_event_loop()
else:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
_loop.set(loop)
return loop
async def ensure_async(obj: Awaitable[T] | T) -> T:
"""Convert a non-awaitable object to a coroutine if needed,
and await it if it was not already awaited.
This function is meant to be called on the result of calling a function,
when that function could either be asynchronous or not.
"""
if inspect.isawaitable(obj):
obj = cast(Awaitable[T], obj)
try:
result = await obj
except RuntimeError as e:
if str(e) == "cannot reuse already awaited coroutine":
# obj is already the coroutine's result
return cast(T, obj)
raise
return result
# obj doesn't need to be awaited
return cast(T, obj)