-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpected_binding.py
214 lines (167 loc) · 5.1 KB
/
expected_binding.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# This code was generated by FFIG <http://ffig.org>.
# Manual edits will be lost.
import os
from ctypes import *
c_object_p = POINTER(c_void_p)
class c_interop_string(c_char_p):
def __init__(self, p=None):
if p is None:
p = ""
if isinstance(p, str):
p = p.encode("utf8")
super(c_char_p, self).__init__(p)
def __str__(self):
return self.value
@property
def value(self):
if super(c_char_p, self).value is None:
return None
return super(c_char_p, self).value.decode("utf8")
@classmethod
def from_param(cls, param):
if isinstance(param, str):
return cls(param)
if isinstance(param, bytes):
return cls(param)
raise TypeError("Cannot convert '{}' to '{}'".format(type(param).__name__, cls.__name__))
@staticmethod
def to_python_string(x, *args):
return x.value
class test_error(Exception):
def __init__(self):
self.value = conf.lib.test_error()
conf.lib.test_clear_error()
def __str__(self):
return self.value
class Asset:
def value(self) -> 'float':
rv = c_double()
rc = conf.lib.test_Asset_value(self, byref(rv))
if rc == 0:
return rv.value
raise test_error()
@property
def name(self) -> 'str':
rv = c_interop_string()
rc = conf.lib.test_Asset_name(self, byref(rv))
if rc == 0:
return rv.value
raise test_error()
@classmethod
def from_param(k, x):
assert isinstance(x, k)
return x.ptr
def __del__(self):
conf.lib.test_Asset_dispose(self)
class CDO(Asset):
@classmethod
def from_capi(cls, ptr):
assert(isinstance(ptr, c_object_p))
if not bool(ptr):
return None
return cls(_p=ptr)
def __init__(self, _p=None) -> 'CDO':
if _p:
self.ptr = _p
else:
self.ptr = c_object_p()
rc = conf.lib.test_CDO_create(byref(self.ptr))
if rc != 0:
raise test_error()
methodList = [
("test_error",
[],
c_interop_string,
c_interop_string.to_python_string),
("test_clear_error",
[],
None),
("test_Asset_dispose",
[Asset],
None),
("test_CDO_create",
[POINTER(c_object_p)],
c_int),
("test_Asset_value",
[Asset, POINTER(c_double)],
c_int),
("test_Asset_name",
[Asset, POINTER(c_interop_string)],
c_int),
]
# library loading and method registrations
# based on clang python bindings approach
def register_method(lib, item):
func = getattr(lib, item[0])
if len(item) >= 2:
func.argtypes = item[1]
if len(item) >= 3:
func.restype = item[2]
if len(item) == 4:
func.errcheck = item[3]
class CachedProperty(object):
def __init__(self, wrapped):
self.wrapped = wrapped
try:
self.__doc__ = wrapped.__doc__
except Exception:
pass
def __get__(self, instance, instance_type=None):
if instance is None:
return self
value = self.wrapped(instance)
setattr(instance, self.wrapped.__name__, value)
return value
class Config:
_library_path = None
_loaded = False
@property
def library_path(self):
return type(self)._library_path
@library_path.setter
def library_path(self, path):
if type(self)._loaded:
raise Exception("library path is already set.")
type(self).library_path = path
@CachedProperty
def lib(self):
lib = self._get_library()
for m in methodList:
register_method(lib, m)
Config._loaded = True
return lib
def _get_filename(self):
import platform
name = platform.system()
if name == 'Darwin':
file = 'libtest_c.dylib'
elif name == 'Windows':
file = 'test_c.dll'
else:
file = 'libtest_c.so'
return file
def _get_filepath(self):
filename = self._get_filename()
if not Config._library_path:
return filename
return os.path.join(Config._library_path, filename)
def _get_library(self):
try:
# Use user-specified library path.
if Config._library_path:
library = cdll.LoadLibrary(self._get_filepath())
else:
# Use local file
try:
lib_file_dir = os.path.abspath(
os.path.dirname(os.path.dirname(__file__)))
library = cdll.LoadLibrary(
os.path.join(lib_file_dir, self._get_filename()))
except Exception:
# Use system library path (last).
library = cdll.LoadLibrary(self._get_filename())
except OSError as e:
msg = str(e) + ". To provide a path to {} set the property Config.library_path".format(self._get_filename())
raise Exception(msg)
return library
conf = Config()