Coverage for wasmtime/component/_enter.py: 97%
31 statements
« prev ^ index » next coverage.py v7.11.3, created at 2025-12-01 19:40 +0000
« prev ^ index » next coverage.py v7.11.3, created at 2025-12-01 19:40 +0000
1from contextlib import contextmanager
2import ctypes
3from .. import _ffi as ffi, StoreContext, WasmtimeError
4from typing import Optional, Callable
7LAST_EXCEPTION: Optional[Exception] = None
10def catch_exceptions(store_raw: 'ctypes._Pointer[ffi.wasmtime_context_t]', func: Callable[[StoreContext], None]) -> ctypes.c_size_t:
11 store = StoreContext(store_raw)
12 exception = None
13 try:
14 func(store)
15 except WasmtimeError as e:
16 exception = e
17 except Exception as e:
18 global LAST_EXCEPTION
19 LAST_EXCEPTION = e
20 exception = WasmtimeError("python exception")
21 finally:
22 store._invalidate()
24 if exception:
25 return ctypes.cast(exception._consume(), ctypes.c_void_p).value # type: ignore
26 return 0 # type: ignore
29def enter_wasm(func: Callable[[], 'ctypes._Pointer[ffi.wasmtime_error_t]']) -> None:
30 ptr = func()
31 if ptr:
32 error = WasmtimeError._from_ptr(ptr)
33 maybe_raise_last_exn()
34 raise error
37def maybe_raise_last_exn() -> None:
38 global LAST_EXCEPTION
39 if LAST_EXCEPTION is None:
40 return
41 exn = LAST_EXCEPTION
42 LAST_EXCEPTION = None
43 raise exn