-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patherror.py
More file actions
42 lines (31 loc) · 938 Bytes
/
error.py
File metadata and controls
42 lines (31 loc) · 938 Bytes
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
import ctypes
class Error(ctypes.Structure):
_fields_ = [('err', ctypes.c_char_p)]
def __del__(self):
# We can call del_error with a None err, but this way we can avoid
# the call overhead when it's not necessary.
if self.err is not None:
del_error(self)
def raise_if_err(self):
if self.err is not None:
raise IOError(self.err.decode())
class EvenResult(ctypes.Structure):
# Multiple return values can be grabbed in a struct.
_fields_ = [
('result', ctypes.c_bool),
('err', Error),
]
lib = ctypes.CDLL('./error.so')
del_error = lib.delError
del_error.argtypes = [Error]
even = lib.even
even.argtypes = [ctypes.c_int64]
even.restype = EvenResult
for i in (0, 1, 2, 3, -5):
e = even(i)
try:
e.err.raise_if_err()
except IOError as err:
print('Error:', err)
continue
print(i, 'even:', e.result)