-
Notifications
You must be signed in to change notification settings - Fork 2
/
ffi_usage_example.py
57 lines (44 loc) · 1.44 KB
/
ffi_usage_example.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
# coding: utf-8
import ctypes
import os
# Change this based on library path
LIBTASHKEEL_PATH = os.path.abspath("../target/debug/libtashkeel.dll")
# Initialize the library
lib = ctypes.cdll.LoadLibrary(LIBTASHKEEL_PATH)
# Specify function input/return types
lib.libtashkeelTashkeel.argtypes = (ctypes.c_char_p, LibtashkeelError)
lib.libtashkeelTashkeel.restype = ctypes.c_void_p
lib.libtashkeel_free_string.argtypes = (ctypes.c_void_p, )
class LibtashkeelError(ctypes.Structure):
_fields_ = [
("err_code", ctypes.c_int32),
("err_msg_ptr", ctypes.c_void_p),
]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._message = None
@property
def message(self):
if self.err_msg_ptr:
self._message = ctypes.cast(self.err_msg_ptr, ctypes.c_char_p).value.decode('utf-8')
self.__free_err_msg_string()
def __del__(self):
try:
self.__free_err_msg_string()
except:
pass
def __free_err_msg_string(self):
lib.libtashkeel_free_string(self.err_msg_ptr)
def tashkeel(text):
err = LibtashkeelError()
ptr = lib.libtashkeelTashkeel(
ctypes.c_char_p(text.encode("utf-8")),
err
)
if err.err_code != 0:
raise RuntimeError(err.message)
try:
res = ctypes.cast(ptr, ctypes.c_char_p).value.decode('utf-8')
finally:
lib.libtashkeel_free_string(ptr)
return res