|
8 | 8 | from typing import TYPE_CHECKING, Any, NewType, Protocol, cast |
9 | 9 |
|
10 | 10 | from py_mini_racer._dll import init_mini_racer |
11 | | -from py_mini_racer._exc import JSEvalException, JSPromiseError, JSTimeoutException |
| 11 | +from py_mini_racer._exc import JSEvalException, JSPromiseError |
12 | 12 | from py_mini_racer._types import ( |
13 | 13 | CancelableJSFunction, |
14 | 14 | JSArray, |
@@ -93,7 +93,7 @@ class Context: |
93 | 93 |
|
94 | 94 | _dll: ctypes.CDLL |
95 | 95 | _ctx: ContextType |
96 | | - _event_loop: asyncio.AbstractEventLoop |
| 96 | + event_loop: asyncio.AbstractEventLoop |
97 | 97 | _object_factory: ObjectFactory |
98 | 98 | _next_async_callback_id: Iterator[int] |
99 | 99 | _active_cancelable_mr_task_callbacks: dict[int, Callable[[ValueHandle], None]] = ( |
@@ -126,7 +126,7 @@ def handle_callback_from_v8( |
126 | 126 | if callback_id == _UNCANCELABLE_TASK_CALLBACK_ID: |
127 | 127 | self._non_cancelable_mr_task_results_queue.put(val_handle) |
128 | 128 | else: |
129 | | - self._event_loop.call_soon_threadsafe( |
| 129 | + self.event_loop.call_soon_threadsafe( |
130 | 130 | self._handle_callback_from_v8_on_event_loop, callback_id, val_handle |
131 | 131 | ) |
132 | 132 |
|
@@ -179,9 +179,7 @@ async def await_promise(self, promise: JSPromise) -> PythonJSConvertedTypes: |
179 | 179 | ), |
180 | 180 | ) |
181 | 181 |
|
182 | | - future: asyncio.Future[PythonJSConvertedTypes] = ( |
183 | | - self._event_loop.create_future() |
184 | | - ) |
| 182 | + future: asyncio.Future[PythonJSConvertedTypes] = self.event_loop.create_future() |
185 | 183 |
|
186 | 184 | def on_resolved(val_handle: ValueHandle) -> None: |
187 | 185 | if future.cancelled(): |
@@ -315,24 +313,7 @@ def array_push(self, arr: JSArray, new_val: PythonJSConvertedTypes) -> None: |
315 | 313 | ) |
316 | 314 |
|
317 | 315 | def are_we_running_on_the_mini_racer_event_loop(self) -> bool: |
318 | | - return get_running_loop_or_none() is self._event_loop |
319 | | - |
320 | | - def run_coro( |
321 | | - self, |
322 | | - coro: Coroutine[Any, Any, PythonJSConvertedTypes], |
323 | | - timeout_sec: float | None = None, |
324 | | - ) -> PythonJSConvertedTypes: |
325 | | - assert not self.are_we_running_on_the_mini_racer_event_loop(), ( |
326 | | - "Cannot run a blocking operation from our own event loop" |
327 | | - ) |
328 | | - |
329 | | - async def run() -> PythonJSConvertedTypes: |
330 | | - try: |
331 | | - return await asyncio.wait_for(coro, timeout=timeout_sec) |
332 | | - except asyncio.TimeoutError as e: |
333 | | - raise JSTimeoutException from e |
334 | | - |
335 | | - return asyncio.run_coroutine_threadsafe(run(), self._event_loop).result() |
| 316 | + return get_running_loop_or_none() is self.event_loop |
336 | 317 |
|
337 | 318 | async def call_function_cancelable( |
338 | 319 | self, |
@@ -391,14 +372,22 @@ def was_soft_memory_limit_reached(self) -> bool: |
391 | 372 | def low_memory_notification(self) -> None: |
392 | 373 | self._dll.mr_low_memory_notification(self._ctx) |
393 | 374 |
|
394 | | - async def heap_stats(self) -> str: |
395 | | - return cast("str", await self._run_cancelable_mr_task(self._dll.mr_heap_stats)) |
| 375 | + def heap_stats(self) -> str: |
| 376 | + return cast( |
| 377 | + "str", |
| 378 | + self._value_handle_to_python( |
| 379 | + self._wrap_raw_handle(self._dll.mr_heap_stats(self._ctx)) |
| 380 | + ), |
| 381 | + ) |
396 | 382 |
|
397 | | - async def heap_snapshot(self) -> str: |
| 383 | + def heap_snapshot(self) -> str: |
398 | 384 | """Return a snapshot of the V8 isolate heap.""" |
399 | 385 |
|
400 | 386 | return cast( |
401 | | - "str", await self._run_cancelable_mr_task(self._dll.mr_heap_snapshot) |
| 387 | + "str", |
| 388 | + self._value_handle_to_python( |
| 389 | + self._wrap_raw_handle(self._dll.mr_heap_snapshot(self._ctx)) |
| 390 | + ), |
402 | 391 | ) |
403 | 392 |
|
404 | 393 | def value_count(self) -> int: |
@@ -444,7 +433,7 @@ async def await_into_js_promise_resolvers(val_handle: ValueHandle) -> None: |
444 | 433 | err_maker(f"Error running Python function:\n{format_exc()}") |
445 | 434 | ) |
446 | 435 |
|
447 | | - async with _make_task_set(self._event_loop) as task_set: |
| 436 | + async with _make_task_set(self.event_loop) as task_set: |
448 | 437 | with self._register_js_notification( |
449 | 438 | lambda val_handle: task_set.start_task( |
450 | 439 | await_into_js_promise_resolvers(val_handle) |
|
0 commit comments