Coverage for wasmtime/_error.py: 95%
38 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-20 16:25 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-20 16:25 +0000
1from ctypes import byref, POINTER, c_int
2from . import _ffi as ffi
3import ctypes
4from typing import Optional
5from wasmtime import Managed
8class WasmtimeError(Exception, Managed["ctypes._Pointer[ffi.wasmtime_error_t]"]):
9 __message: Optional[str]
11 def __init__(self, message: str):
12 self.__message = message
14 def _delete(self, ptr: "ctypes._Pointer[ffi.wasmtime_error_t]") -> None:
15 ffi.wasmtime_error_delete(ptr)
17 @classmethod
18 def _from_ptr(cls, ptr: "ctypes._Pointer") -> 'WasmtimeError':
19 from . import _ffi as ffi
20 if not isinstance(ptr, POINTER(ffi.wasmtime_error_t)):
21 raise TypeError("wrong pointer type")
23 exit_code = c_int(0)
24 if ffi.wasmtime_error_exit_status(ptr, byref(exit_code)):
25 exit_trap: ExitTrap = ExitTrap.__new__(ExitTrap)
26 exit_trap._set_ptr(ptr)
27 exit_trap.__message = None
28 exit_trap.code = exit_code.value
29 return exit_trap
31 err: WasmtimeError = cls.__new__(cls)
32 err._set_ptr(ptr)
33 err.__message = None
34 return err
36 def __str__(self) -> str:
37 if self.__message:
38 return self.__message
39 message_vec = ffi.wasm_byte_vec_t()
40 ffi.wasmtime_error_message(self.ptr(), byref(message_vec))
41 message = ffi.to_str(message_vec)
42 ffi.wasm_byte_vec_delete(byref(message_vec))
43 return message
46class ExitTrap(WasmtimeError):
47 """
48 A special type of `WasmtimeError` which represents the process exiting via
49 WASI's `proc_exit` function call.
51 Whenever a WASI program exits via `proc_exit` a trap is raised, but the
52 trap will have this type instead of `WasmtimeError`, so you can catch just
53 this type instead of all traps (if desired). Exit traps have a `code`
54 associated with them which is the exit code provided at exit.
56 Note that `ExitTrap` is a subclass of `WasmtimeError`, so if you catch a
57 trap you'll also catch `ExitTrap`.
58 """
59 code: int
60 pass