-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy path_error.py
60 lines (48 loc) · 1.97 KB
/
_error.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
from ctypes import byref, POINTER, c_int
from . import _ffi as ffi
import ctypes
from typing import Optional
from wasmtime import Managed
class WasmtimeError(Exception, Managed["ctypes._Pointer[ffi.wasmtime_error_t]"]):
__message: Optional[str]
def __init__(self, message: str):
self.__message = message
def _delete(self, ptr: "ctypes._Pointer[ffi.wasmtime_error_t]") -> None:
ffi.wasmtime_error_delete(ptr)
@classmethod
def _from_ptr(cls, ptr: "ctypes._Pointer") -> 'WasmtimeError':
from . import _ffi as ffi
if not isinstance(ptr, POINTER(ffi.wasmtime_error_t)):
raise TypeError("wrong pointer type")
exit_code = c_int(0)
if ffi.wasmtime_error_exit_status(ptr, byref(exit_code)):
exit_trap: ExitTrap = ExitTrap.__new__(ExitTrap)
exit_trap._set_ptr(ptr)
exit_trap.__message = None
exit_trap.code = exit_code.value
return exit_trap
err: WasmtimeError = cls.__new__(cls)
err._set_ptr(ptr)
err.__message = None
return err
def __str__(self) -> str:
if self.__message:
return self.__message
message_vec = ffi.wasm_byte_vec_t()
ffi.wasmtime_error_message(self.ptr(), byref(message_vec))
message = ffi.to_str(message_vec)
ffi.wasm_byte_vec_delete(byref(message_vec))
return message
class ExitTrap(WasmtimeError):
"""
A special type of `WasmtimeError` which represents the process exiting via
WASI's `proc_exit` function call.
Whenever a WASI program exits via `proc_exit` a trap is raised, but the
trap will have this type instead of `WasmtimeError`, so you can catch just
this type instead of all traps (if desired). Exit traps have a `code`
associated with them which is the exit code provided at exit.
Note that `ExitTrap` is a subclass of `WasmtimeError`, so if you catch a
trap you'll also catch `ExitTrap`.
"""
code: int
pass