| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
import sys |
| 5 |
from . import model |
| 6 |
from .error import VerificationError |
| 7 |
from . import _imp_emulation as imp |
| 8 |
|
| 9 |
|
| 10 |
class VCPythonEngine(object): |
| 11 |
_class_key = 'x' |
| 12 |
_gen_python_module = True |
| 13 |
|
| 14 |
def __init__(self, verifier): |
| 15 |
self.verifier = verifier |
| 16 |
self.ffi = verifier.ffi |
| 17 |
self._struct_pending_verification = {} |
| 18 |
self._types_of_builtin_functions = {} |
| 19 |
|
| 20 |
def patch_extension_kwds(self, kwds): |
| 21 |
pass |
| 22 |
|
| 23 |
def find_module(self, module_name, path, so_suffixes): |
| 24 |
try: |
| 25 |
f, filename, descr = imp.find_module(module_name, path) |
| 26 |
except ImportError: |
| 27 |
return None |
| 28 |
if f is not None: |
| 29 |
f.close() |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
if descr[0] not in so_suffixes: |
| 34 |
return None |
| 35 |
return filename |
| 36 |
|
| 37 |
def collect_types(self): |
| 38 |
self._typesdict = {} |
| 39 |
self._generate("collecttype") |
| 40 |
|
| 41 |
def _prnt(self, what=''): |
| 42 |
self._f.write(what + '\n') |
| 43 |
|
| 44 |
def _gettypenum(self, type): |
| 45 |
|
| 46 |
return self._typesdict[type] |
| 47 |
|
| 48 |
def _do_collect_type(self, tp): |
| 49 |
if ((not isinstance(tp, model.PrimitiveType) |
| 50 |
or tp.name == 'long double') |
| 51 |
and tp not in self._typesdict): |
| 52 |
num = len(self._typesdict) |
| 53 |
self._typesdict[tp] = num |
| 54 |
|
| 55 |
def write_source_to_f(self): |
| 56 |
self.collect_types() |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
self._chained_list_constants = ['((void)lib,0)', '((void)lib,0)'] |
| 74 |
|
| 75 |
prnt = self._prnt |
| 76 |
|
| 77 |
prnt(cffimod_header) |
| 78 |
prnt() |
| 79 |
|
| 80 |
prnt(self.verifier.preamble) |
| 81 |
prnt() |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
self._generate("decl") |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
self._generate_setup_custom() |
| 90 |
prnt() |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
prnt('static PyMethodDef _cffi_methods[] = {') |
| 96 |
self._generate("method") |
| 97 |
prnt(' {"_cffi_setup", _cffi_setup, METH_VARARGS, NULL},') |
| 98 |
prnt(' {NULL, NULL, 0, NULL} /* Sentinel */') |
| 99 |
prnt('};') |
| 100 |
prnt() |
| 101 |
|
| 102 |
|
| 103 |
modname = self.verifier.get_module_name() |
| 104 |
constants = self._chained_list_constants[False] |
| 105 |
prnt('#if PY_MAJOR_VERSION >= 3') |
| 106 |
prnt() |
| 107 |
prnt('static struct PyModuleDef _cffi_module_def = {') |
| 108 |
prnt(' PyModuleDef_HEAD_INIT,') |
| 109 |
prnt(' "%s",' % modname) |
| 110 |
prnt(' NULL,') |
| 111 |
prnt(' -1,') |
| 112 |
prnt(' _cffi_methods,') |
| 113 |
prnt(' NULL, NULL, NULL, NULL') |
| 114 |
prnt('};') |
| 115 |
prnt() |
| 116 |
prnt('PyMODINIT_FUNC') |
| 117 |
prnt('PyInit_%s(void)' % modname) |
| 118 |
prnt('{') |
| 119 |
prnt(' PyObject *lib;') |
| 120 |
prnt(' lib = PyModule_Create(&_cffi_module_def);') |
| 121 |
prnt(' if (lib == NULL)') |
| 122 |
prnt(' return NULL;') |
| 123 |
prnt(' if (%s < 0 || _cffi_init() < 0) {' % (constants,)) |
| 124 |
prnt(' Py_DECREF(lib);') |
| 125 |
prnt(' return NULL;') |
| 126 |
prnt(' }') |
| 127 |
prnt(' return lib;') |
| 128 |
prnt('}') |
| 129 |
prnt() |
| 130 |
prnt('#else') |
| 131 |
prnt() |
| 132 |
prnt('PyMODINIT_FUNC') |
| 133 |
prnt('init%s(void)' % modname) |
| 134 |
prnt('{') |
| 135 |
prnt(' PyObject *lib;') |
| 136 |
prnt(' lib = Py_InitModule("%s", _cffi_methods);' % modname) |
| 137 |
prnt(' if (lib == NULL)') |
| 138 |
prnt(' return;') |
| 139 |
prnt(' if (%s < 0 || _cffi_init() < 0)' % (constants,)) |
| 140 |
prnt(' return;') |
| 141 |
prnt(' return;') |
| 142 |
prnt('}') |
| 143 |
prnt() |
| 144 |
prnt('#endif') |
| 145 |
|
| 146 |
def load_library(self, flags=None): |
| 147 |
|
| 148 |
|
| 149 |
imp.acquire_lock() |
| 150 |
try: |
| 151 |
if hasattr(sys, "getdlopenflags"): |
| 152 |
previous_flags = sys.getdlopenflags() |
| 153 |
try: |
| 154 |
if hasattr(sys, "setdlopenflags") and flags is not None: |
| 155 |
sys.setdlopenflags(flags) |
| 156 |
module = imp.load_dynamic(self.verifier.get_module_name(), |
| 157 |
self.verifier.modulefilename) |
| 158 |
except ImportError as e: |
| 159 |
error = "importing %r: %s" % (self.verifier.modulefilename, e) |
| 160 |
raise VerificationError(error) |
| 161 |
finally: |
| 162 |
if hasattr(sys, "setdlopenflags"): |
| 163 |
sys.setdlopenflags(previous_flags) |
| 164 |
finally: |
| 165 |
imp.release_lock() |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
self._load(module, 'loading') |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
revmapping = dict([(value, key) |
| 174 |
for (key, value) in self._typesdict.items()]) |
| 175 |
lst = [revmapping[i] for i in range(len(revmapping))] |
| 176 |
lst = list(map(self.ffi._get_cached_btype, lst)) |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
class FFILibrary(object): |
| 184 |
_cffi_python_module = module |
| 185 |
_cffi_ffi = self.ffi |
| 186 |
_cffi_dir = [] |
| 187 |
def __dir__(self): |
| 188 |
return FFILibrary._cffi_dir + list(self.__dict__) |
| 189 |
library = FFILibrary() |
| 190 |
if module._cffi_setup(lst, VerificationError, library): |
| 191 |
import warnings |
| 192 |
warnings.warn("reimporting %r might overwrite older definitions" |
| 193 |
% (self.verifier.get_module_name())) |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
self._load(module, 'loaded', library=library) |
| 200 |
module._cffi_original_ffi = self.ffi |
| 201 |
module._cffi_types_of_builtin_funcs = self._types_of_builtin_functions |
| 202 |
return library |
| 203 |
|
| 204 |
def _get_declarations(self): |
| 205 |
lst = [(key, tp) for (key, (tp, qual)) in |
| 206 |
self.ffi._parser._declarations.items()] |
| 207 |
lst.sort() |
| 208 |
return lst |
| 209 |
|
| 210 |
def _generate(self, step_name): |
| 211 |
for name, tp in self._get_declarations(): |
| 212 |
kind, realname = name.split(' ', 1) |
| 213 |
try: |
| 214 |
method = getattr(self, '_generate_cpy_%s_%s' % (kind, |
| 215 |
step_name)) |
| 216 |
except AttributeError: |
| 217 |
raise VerificationError( |
| 218 |
"not implemented in verify(): %r" % name) |
| 219 |
try: |
| 220 |
method(tp, realname) |
| 221 |
except Exception as e: |
| 222 |
model.attach_exception_info(e, name) |
| 223 |
raise |
| 224 |
|
| 225 |
def _load(self, module, step_name, **kwds): |
| 226 |
for name, tp in self._get_declarations(): |
| 227 |
kind, realname = name.split(' ', 1) |
| 228 |
method = getattr(self, '_%s_cpy_%s' % (step_name, kind)) |
| 229 |
try: |
| 230 |
method(tp, realname, module, **kwds) |
| 231 |
except Exception as e: |
| 232 |
model.attach_exception_info(e, name) |
| 233 |
raise |
| 234 |
|
| 235 |
def _generate_nothing(self, tp, name): |
| 236 |
pass |
| 237 |
|
| 238 |
def _loaded_noop(self, tp, name, module, **kwds): |
| 239 |
pass |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
def _convert_funcarg_to_c(self, tp, fromvar, tovar, errcode): |
| 244 |
extraarg = '' |
| 245 |
if isinstance(tp, model.PrimitiveType): |
| 246 |
if tp.is_integer_type() and tp.name != '_Bool': |
| 247 |
converter = '_cffi_to_c_int' |
| 248 |
extraarg = ', %s' % tp.name |
| 249 |
elif tp.is_complex_type(): |
| 250 |
raise VerificationError( |
| 251 |
"not implemented in verify(): complex types") |
| 252 |
else: |
| 253 |
converter = '(%s)_cffi_to_c_%s' % (tp.get_c_name(''), |
| 254 |
tp.name.replace(' ', '_')) |
| 255 |
errvalue = '-1' |
| 256 |
|
| 257 |
elif isinstance(tp, model.PointerType): |
| 258 |
self._convert_funcarg_to_c_ptr_or_array(tp, fromvar, |
| 259 |
tovar, errcode) |
| 260 |
return |
| 261 |
|
| 262 |
elif isinstance(tp, (model.StructOrUnion, model.EnumType)): |
| 263 |
|
| 264 |
self._prnt(' if (_cffi_to_c((char *)&%s, _cffi_type(%d), %s) < 0)' |
| 265 |
% (tovar, self._gettypenum(tp), fromvar)) |
| 266 |
self._prnt(' %s;' % errcode) |
| 267 |
return |
| 268 |
|
| 269 |
elif isinstance(tp, model.FunctionPtrType): |
| 270 |
converter = '(%s)_cffi_to_c_pointer' % tp.get_c_name('') |
| 271 |
extraarg = ', _cffi_type(%d)' % self._gettypenum(tp) |
| 272 |
errvalue = 'NULL' |
| 273 |
|
| 274 |
else: |
| 275 |
raise NotImplementedError(tp) |
| 276 |
|
| 277 |
self._prnt(' %s = %s(%s%s);' % (tovar, converter, fromvar, extraarg)) |
| 278 |
self._prnt(' if (%s == (%s)%s && PyErr_Occurred())' % ( |
| 279 |
tovar, tp.get_c_name(''), errvalue)) |
| 280 |
self._prnt(' %s;' % errcode) |
| 281 |
|
| 282 |
def _extra_local_variables(self, tp, localvars, freelines): |
| 283 |
if isinstance(tp, model.PointerType): |
| 284 |
localvars.add('Py_ssize_t datasize') |
| 285 |
localvars.add('struct _cffi_freeme_s *large_args_free = NULL') |
| 286 |
freelines.add('if (large_args_free != NULL)' |
| 287 |
' _cffi_free_array_arguments(large_args_free);') |
| 288 |
|
| 289 |
def _convert_funcarg_to_c_ptr_or_array(self, tp, fromvar, tovar, errcode): |
| 290 |
self._prnt(' datasize = _cffi_prepare_pointer_call_argument(') |
| 291 |
self._prnt(' _cffi_type(%d), %s, (char **)&%s);' % ( |
| 292 |
self._gettypenum(tp), fromvar, tovar)) |
| 293 |
self._prnt(' if (datasize != 0) {') |
| 294 |
self._prnt(' %s = ((size_t)datasize) <= 640 ? ' |
| 295 |
'alloca((size_t)datasize) : NULL;' % (tovar,)) |
| 296 |
self._prnt(' if (_cffi_convert_array_argument(_cffi_type(%d), %s, ' |
| 297 |
'(char **)&%s,' % (self._gettypenum(tp), fromvar, tovar)) |
| 298 |
self._prnt(' datasize, &large_args_free) < 0)') |
| 299 |
self._prnt(' %s;' % errcode) |
| 300 |
self._prnt(' }') |
| 301 |
|
| 302 |
def _convert_expr_from_c(self, tp, var, context): |
| 303 |
if isinstance(tp, model.PrimitiveType): |
| 304 |
if tp.is_integer_type() and tp.name != '_Bool': |
| 305 |
return '_cffi_from_c_int(%s, %s)' % (var, tp.name) |
| 306 |
elif tp.name != 'long double': |
| 307 |
return '_cffi_from_c_%s(%s)' % (tp.name.replace(' ', '_'), var) |
| 308 |
else: |
| 309 |
return '_cffi_from_c_deref((char *)&%s, _cffi_type(%d))' % ( |
| 310 |
var, self._gettypenum(tp)) |
| 311 |
elif isinstance(tp, (model.PointerType, model.FunctionPtrType)): |
| 312 |
return '_cffi_from_c_pointer((char *)%s, _cffi_type(%d))' % ( |
| 313 |
var, self._gettypenum(tp)) |
| 314 |
elif isinstance(tp, model.ArrayType): |
| 315 |
return '_cffi_from_c_pointer((char *)%s, _cffi_type(%d))' % ( |
| 316 |
var, self._gettypenum(model.PointerType(tp.item))) |
| 317 |
elif isinstance(tp, model.StructOrUnion): |
| 318 |
if tp.fldnames is None: |
| 319 |
raise TypeError("'%s' is used as %s, but is opaque" % ( |
| 320 |
tp._get_c_name(), context)) |
| 321 |
return '_cffi_from_c_struct((char *)&%s, _cffi_type(%d))' % ( |
| 322 |
var, self._gettypenum(tp)) |
| 323 |
elif isinstance(tp, model.EnumType): |
| 324 |
return '_cffi_from_c_deref((char *)&%s, _cffi_type(%d))' % ( |
| 325 |
var, self._gettypenum(tp)) |
| 326 |
else: |
| 327 |
raise NotImplementedError(tp) |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
_generate_cpy_typedef_collecttype = _generate_nothing |
| 333 |
_generate_cpy_typedef_decl = _generate_nothing |
| 334 |
_generate_cpy_typedef_method = _generate_nothing |
| 335 |
_loading_cpy_typedef = _loaded_noop |
| 336 |
_loaded_cpy_typedef = _loaded_noop |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
def _generate_cpy_function_collecttype(self, tp, name): |
| 342 |
assert isinstance(tp, model.FunctionPtrType) |
| 343 |
if tp.ellipsis: |
| 344 |
self._do_collect_type(tp) |
| 345 |
else: |
| 346 |
|
| 347 |
|
| 348 |
for type in tp.args: |
| 349 |
self._do_collect_type(type) |
| 350 |
self._do_collect_type(tp.result) |
| 351 |
|
| 352 |
def _generate_cpy_function_decl(self, tp, name): |
| 353 |
assert isinstance(tp, model.FunctionPtrType) |
| 354 |
if tp.ellipsis: |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
self._generate_cpy_const(False, name, tp) |
| 359 |
return |
| 360 |
prnt = self._prnt |
| 361 |
numargs = len(tp.args) |
| 362 |
if numargs == 0: |
| 363 |
argname = 'noarg' |
| 364 |
elif numargs == 1: |
| 365 |
argname = 'arg0' |
| 366 |
else: |
| 367 |
argname = 'args' |
| 368 |
prnt('static PyObject *') |
| 369 |
prnt('_cffi_f_%s(PyObject *self, PyObject *%s)' % (name, argname)) |
| 370 |
prnt('{') |
| 371 |
|
| 372 |
context = 'argument of %s' % name |
| 373 |
for i, type in enumerate(tp.args): |
| 374 |
prnt(' %s;' % type.get_c_name(' x%d' % i, context)) |
| 375 |
|
| 376 |
localvars = set() |
| 377 |
freelines = set() |
| 378 |
for type in tp.args: |
| 379 |
self._extra_local_variables(type, localvars, freelines) |
| 380 |
for decl in sorted(localvars): |
| 381 |
prnt(' %s;' % (decl,)) |
| 382 |
|
| 383 |
if not isinstance(tp.result, model.VoidType): |
| 384 |
result_code = 'result = ' |
| 385 |
context = 'result of %s' % name |
| 386 |
prnt(' %s;' % tp.result.get_c_name(' result', context)) |
| 387 |
prnt(' PyObject *pyresult;') |
| 388 |
else: |
| 389 |
result_code = '' |
| 390 |
|
| 391 |
if len(tp.args) > 1: |
| 392 |
rng = range(len(tp.args)) |
| 393 |
for i in rng: |
| 394 |
prnt(' PyObject *arg%d;' % i) |
| 395 |
prnt() |
| 396 |
prnt(' if (!PyArg_ParseTuple(args, "%s:%s", %s))' % ( |
| 397 |
'O' * numargs, name, ', '.join(['&arg%d' % i for i in rng]))) |
| 398 |
prnt(' return NULL;') |
| 399 |
prnt() |
| 400 |
|
| 401 |
for i, type in enumerate(tp.args): |
| 402 |
self._convert_funcarg_to_c(type, 'arg%d' % i, 'x%d' % i, |
| 403 |
'return NULL') |
| 404 |
prnt() |
| 405 |
|
| 406 |
prnt(' Py_BEGIN_ALLOW_THREADS') |
| 407 |
prnt(' _cffi_restore_errno();') |
| 408 |
prnt(' { %s%s(%s); }' % ( |
| 409 |
result_code, name, |
| 410 |
', '.join(['x%d' % i for i in range(len(tp.args))]))) |
| 411 |
prnt(' _cffi_save_errno();') |
| 412 |
prnt(' Py_END_ALLOW_THREADS') |
| 413 |
prnt() |
| 414 |
|
| 415 |
prnt(' (void)self; /* unused */') |
| 416 |
if numargs == 0: |
| 417 |
prnt(' (void)noarg; /* unused */') |
| 418 |
if result_code: |
| 419 |
prnt(' pyresult = %s;' % |
| 420 |
self._convert_expr_from_c(tp.result, 'result', 'result type')) |
| 421 |
for freeline in freelines: |
| 422 |
prnt(' ' + freeline) |
| 423 |
prnt(' return pyresult;') |
| 424 |
else: |
| 425 |
for freeline in freelines: |
| 426 |
prnt(' ' + freeline) |
| 427 |
prnt(' Py_INCREF(Py_None);') |
| 428 |
prnt(' return Py_None;') |
| 429 |
prnt('}') |
| 430 |
prnt() |
| 431 |
|
| 432 |
def _generate_cpy_function_method(self, tp, name): |
| 433 |
if tp.ellipsis: |
| 434 |
return |
| 435 |
numargs = len(tp.args) |
| 436 |
if numargs == 0: |
| 437 |
meth = 'METH_NOARGS' |
| 438 |
elif numargs == 1: |
| 439 |
meth = 'METH_O' |
| 440 |
else: |
| 441 |
meth = 'METH_VARARGS' |
| 442 |
self._prnt(' {"%s", _cffi_f_%s, %s, NULL},' % (name, name, meth)) |
| 443 |
|
| 444 |
_loading_cpy_function = _loaded_noop |
| 445 |
|
| 446 |
def _loaded_cpy_function(self, tp, name, module, library): |
| 447 |
if tp.ellipsis: |
| 448 |
return |
| 449 |
func = getattr(module, name) |
| 450 |
setattr(library, name, func) |
| 451 |
self._types_of_builtin_functions[func] = tp |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
_generate_cpy_struct_collecttype = _generate_nothing |
| 457 |
def _generate_cpy_struct_decl(self, tp, name): |
| 458 |
assert name == tp.name |
| 459 |
self._generate_struct_or_union_decl(tp, 'struct', name) |
| 460 |
def _generate_cpy_struct_method(self, tp, name): |
| 461 |
self._generate_struct_or_union_method(tp, 'struct', name) |
| 462 |
def _loading_cpy_struct(self, tp, name, module): |
| 463 |
self._loading_struct_or_union(tp, 'struct', name, module) |
| 464 |
def _loaded_cpy_struct(self, tp, name, module, **kwds): |
| 465 |
self._loaded_struct_or_union(tp) |
| 466 |
|
| 467 |
_generate_cpy_union_collecttype = _generate_nothing |
| 468 |
def _generate_cpy_union_decl(self, tp, name): |
| 469 |
assert name == tp.name |
| 470 |
self._generate_struct_or_union_decl(tp, 'union', name) |
| 471 |
def _generate_cpy_union_method(self, tp, name): |
| 472 |
self._generate_struct_or_union_method(tp, 'union', name) |
| 473 |
def _loading_cpy_union(self, tp, name, module): |
| 474 |
self._loading_struct_or_union(tp, 'union', name, module) |
| 475 |
def _loaded_cpy_union(self, tp, name, module, **kwds): |
| 476 |
self._loaded_struct_or_union(tp) |
| 477 |
|
| 478 |
def _generate_struct_or_union_decl(self, tp, prefix, name): |
| 479 |
if tp.fldnames is None: |
| 480 |
return |
| 481 |
checkfuncname = '_cffi_check_%s_%s' % (prefix, name) |
| 482 |
layoutfuncname = '_cffi_layout_%s_%s' % (prefix, name) |
| 483 |
cname = ('%s %s' % (prefix, name)).strip() |
| 484 |
|
| 485 |
prnt = self._prnt |
| 486 |
prnt('static void %s(%s *p)' % (checkfuncname, cname)) |
| 487 |
prnt('{') |
| 488 |
prnt(' /* only to generate compile-time warnings or errors */') |
| 489 |
prnt(' (void)p;') |
| 490 |
for fname, ftype, fbitsize, fqual in tp.enumfields(): |
| 491 |
if (isinstance(ftype, model.PrimitiveType) |
| 492 |
and ftype.is_integer_type()) or fbitsize >= 0: |
| 493 |
|
| 494 |
prnt(' (void)((p->%s) << 1);' % fname) |
| 495 |
else: |
| 496 |
|
| 497 |
try: |
| 498 |
prnt(' { %s = &p->%s; (void)tmp; }' % ( |
| 499 |
ftype.get_c_name('*tmp', 'field %r'%fname, quals=fqual), |
| 500 |
fname)) |
| 501 |
except VerificationError as e: |
| 502 |
prnt(' /* %s */' % str(e)) |
| 503 |
prnt('}') |
| 504 |
prnt('static PyObject *') |
| 505 |
prnt('%s(PyObject *self, PyObject *noarg)' % (layoutfuncname,)) |
| 506 |
prnt('{') |
| 507 |
prnt(' struct _cffi_aligncheck { char x; %s y; };' % cname) |
| 508 |
prnt(' static Py_ssize_t nums[] = {') |
| 509 |
prnt(' sizeof(%s),' % cname) |
| 510 |
prnt(' offsetof(struct _cffi_aligncheck, y),') |
| 511 |
for fname, ftype, fbitsize, fqual in tp.enumfields(): |
| 512 |
if fbitsize >= 0: |
| 513 |
continue |
| 514 |
prnt(' offsetof(%s, %s),' % (cname, fname)) |
| 515 |
if isinstance(ftype, model.ArrayType) and ftype.length is None: |
| 516 |
prnt(' 0, /* %s */' % ftype._get_c_name()) |
| 517 |
else: |
| 518 |
prnt(' sizeof(((%s *)0)->%s),' % (cname, fname)) |
| 519 |
prnt(' -1') |
| 520 |
prnt(' };') |
| 521 |
prnt(' (void)self; /* unused */') |
| 522 |
prnt(' (void)noarg; /* unused */') |
| 523 |
prnt(' return _cffi_get_struct_layout(nums);') |
| 524 |
prnt(' /* the next line is not executed, but compiled */') |
| 525 |
prnt(' %s(0);' % (checkfuncname,)) |
| 526 |
prnt('}') |
| 527 |
prnt() |
| 528 |
|
| 529 |
def _generate_struct_or_union_method(self, tp, prefix, name): |
| 530 |
if tp.fldnames is None: |
| 531 |
return |
| 532 |
layoutfuncname = '_cffi_layout_%s_%s' % (prefix, name) |
| 533 |
self._prnt(' {"%s", %s, METH_NOARGS, NULL},' % (layoutfuncname, |
| 534 |
layoutfuncname)) |
| 535 |
|
| 536 |
def _loading_struct_or_union(self, tp, prefix, name, module): |
| 537 |
if tp.fldnames is None: |
| 538 |
return |
| 539 |
layoutfuncname = '_cffi_layout_%s_%s' % (prefix, name) |
| 540 |
|
| 541 |
function = getattr(module, layoutfuncname) |
| 542 |
layout = function() |
| 543 |
if isinstance(tp, model.StructOrUnion) and tp.partial: |
| 544 |
|
| 545 |
|
| 546 |
totalsize = layout[0] |
| 547 |
totalalignment = layout[1] |
| 548 |
fieldofs = layout[2::2] |
| 549 |
fieldsize = layout[3::2] |
| 550 |
tp.force_flatten() |
| 551 |
assert len(fieldofs) == len(fieldsize) == len(tp.fldnames) |
| 552 |
tp.fixedlayout = fieldofs, fieldsize, totalsize, totalalignment |
| 553 |
else: |
| 554 |
cname = ('%s %s' % (prefix, name)).strip() |
| 555 |
self._struct_pending_verification[tp] = layout, cname |
| 556 |
|
| 557 |
def _loaded_struct_or_union(self, tp): |
| 558 |
if tp.fldnames is None: |
| 559 |
return |
| 560 |
self.ffi._get_cached_btype(tp) |
| 561 |
|
| 562 |
if tp in self._struct_pending_verification: |
| 563 |
|
| 564 |
def check(realvalue, expectedvalue, msg): |
| 565 |
if realvalue != expectedvalue: |
| 566 |
raise VerificationError( |
| 567 |
"%s (we have %d, but C compiler says %d)" |
| 568 |
% (msg, expectedvalue, realvalue)) |
| 569 |
ffi = self.ffi |
| 570 |
BStruct = ffi._get_cached_btype(tp) |
| 571 |
layout, cname = self._struct_pending_verification.pop(tp) |
| 572 |
check(layout[0], ffi.sizeof(BStruct), "wrong total size") |
| 573 |
check(layout[1], ffi.alignof(BStruct), "wrong total alignment") |
| 574 |
i = 2 |
| 575 |
for fname, ftype, fbitsize, fqual in tp.enumfields(): |
| 576 |
if fbitsize >= 0: |
| 577 |
continue |
| 578 |
check(layout[i], ffi.offsetof(BStruct, fname), |
| 579 |
"wrong offset for field %r" % (fname,)) |
| 580 |
if layout[i+1] != 0: |
| 581 |
BField = ffi._get_cached_btype(ftype) |
| 582 |
check(layout[i+1], ffi.sizeof(BField), |
| 583 |
"wrong size for field %r" % (fname,)) |
| 584 |
i += 2 |
| 585 |
assert i == len(layout) |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
_generate_cpy_anonymous_collecttype = _generate_nothing |
| 592 |
|
| 593 |
def _generate_cpy_anonymous_decl(self, tp, name): |
| 594 |
if isinstance(tp, model.EnumType): |
| 595 |
self._generate_cpy_enum_decl(tp, name, '') |
| 596 |
else: |
| 597 |
self._generate_struct_or_union_decl(tp, '', name) |
| 598 |
|
| 599 |
def _generate_cpy_anonymous_method(self, tp, name): |
| 600 |
if not isinstance(tp, model.EnumType): |
| 601 |
self._generate_struct_or_union_method(tp, '', name) |
| 602 |
|
| 603 |
def _loading_cpy_anonymous(self, tp, name, module): |
| 604 |
if isinstance(tp, model.EnumType): |
| 605 |
self._loading_cpy_enum(tp, name, module) |
| 606 |
else: |
| 607 |
self._loading_struct_or_union(tp, '', name, module) |
| 608 |
|
| 609 |
def _loaded_cpy_anonymous(self, tp, name, module, **kwds): |
| 610 |
if isinstance(tp, model.EnumType): |
| 611 |
self._loaded_cpy_enum(tp, name, module, **kwds) |
| 612 |
else: |
| 613 |
self._loaded_struct_or_union(tp) |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
def _generate_cpy_const(self, is_int, name, tp=None, category='const', |
| 619 |
vartp=None, delayed=True, size_too=False, |
| 620 |
check_value=None): |
| 621 |
prnt = self._prnt |
| 622 |
funcname = '_cffi_%s_%s' % (category, name) |
| 623 |
prnt('static int %s(PyObject *lib)' % funcname) |
| 624 |
prnt('{') |
| 625 |
prnt(' PyObject *o;') |
| 626 |
prnt(' int res;') |
| 627 |
if not is_int: |
| 628 |
prnt(' %s;' % (vartp or tp).get_c_name(' i', name)) |
| 629 |
else: |
| 630 |
assert category == 'const' |
| 631 |
|
| 632 |
if check_value is not None: |
| 633 |
self._check_int_constant_value(name, check_value) |
| 634 |
|
| 635 |
if not is_int: |
| 636 |
if category == 'var': |
| 637 |
realexpr = '&' + name |
| 638 |
else: |
| 639 |
realexpr = name |
| 640 |
prnt(' i = (%s);' % (realexpr,)) |
| 641 |
prnt(' o = %s;' % (self._convert_expr_from_c(tp, 'i', |
| 642 |
'variable type'),)) |
| 643 |
assert delayed |
| 644 |
else: |
| 645 |
prnt(' o = _cffi_from_c_int_const(%s);' % name) |
| 646 |
prnt(' if (o == NULL)') |
| 647 |
prnt(' return -1;') |
| 648 |
if size_too: |
| 649 |
prnt(' {') |
| 650 |
prnt(' PyObject *o1 = o;') |
| 651 |
prnt(' o = Py_BuildValue("On", o1, (Py_ssize_t)sizeof(%s));' |
| 652 |
% (name,)) |
| 653 |
prnt(' Py_DECREF(o1);') |
| 654 |
prnt(' if (o == NULL)') |
| 655 |
prnt(' return -1;') |
| 656 |
prnt(' }') |
| 657 |
prnt(' res = PyObject_SetAttrString(lib, "%s", o);' % name) |
| 658 |
prnt(' Py_DECREF(o);') |
| 659 |
prnt(' if (res < 0)') |
| 660 |
prnt(' return -1;') |
| 661 |
prnt(' return %s;' % self._chained_list_constants[delayed]) |
| 662 |
self._chained_list_constants[delayed] = funcname + '(lib)' |
| 663 |
prnt('}') |
| 664 |
prnt() |
| 665 |
|
| 666 |
def _generate_cpy_constant_collecttype(self, tp, name): |
| 667 |
is_int = isinstance(tp, model.PrimitiveType) and tp.is_integer_type() |
| 668 |
if not is_int: |
| 669 |
self._do_collect_type(tp) |
| 670 |
|
| 671 |
def _generate_cpy_constant_decl(self, tp, name): |
| 672 |
is_int = isinstance(tp, model.PrimitiveType) and tp.is_integer_type() |
| 673 |
self._generate_cpy_const(is_int, name, tp) |
| 674 |
|
| 675 |
_generate_cpy_constant_method = _generate_nothing |
| 676 |
_loading_cpy_constant = _loaded_noop |
| 677 |
_loaded_cpy_constant = _loaded_noop |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
def _check_int_constant_value(self, name, value, err_prefix=''): |
| 683 |
prnt = self._prnt |
| 684 |
if value <= 0: |
| 685 |
prnt(' if ((%s) > 0 || (long)(%s) != %dL) {' % ( |
| 686 |
name, name, value)) |
| 687 |
else: |
| 688 |
prnt(' if ((%s) <= 0 || (unsigned long)(%s) != %dUL) {' % ( |
| 689 |
name, name, value)) |
| 690 |
prnt(' char buf[64];') |
| 691 |
prnt(' if ((%s) <= 0)' % name) |
| 692 |
prnt(' snprintf(buf, 63, "%%ld", (long)(%s));' % name) |
| 693 |
prnt(' else') |
| 694 |
prnt(' snprintf(buf, 63, "%%lu", (unsigned long)(%s));' % |
| 695 |
name) |
| 696 |
prnt(' PyErr_Format(_cffi_VerificationError,') |
| 697 |
prnt(' "%s%s has the real value %s, not %s",') |
| 698 |
prnt(' "%s", "%s", buf, "%d");' % ( |
| 699 |
err_prefix, name, value)) |
| 700 |
prnt(' return -1;') |
| 701 |
prnt(' }') |
| 702 |
|
| 703 |
def _enum_funcname(self, prefix, name): |
| 704 |
|
| 705 |
name = name.replace('$', '___D_') |
| 706 |
return '_cffi_e_%s_%s' % (prefix, name) |
| 707 |
|
| 708 |
def _generate_cpy_enum_decl(self, tp, name, prefix='enum'): |
| 709 |
if tp.partial: |
| 710 |
for enumerator in tp.enumerators: |
| 711 |
self._generate_cpy_const(True, enumerator, delayed=False) |
| 712 |
return |
| 713 |
|
| 714 |
funcname = self._enum_funcname(prefix, name) |
| 715 |
prnt = self._prnt |
| 716 |
prnt('static int %s(PyObject *lib)' % funcname) |
| 717 |
prnt('{') |
| 718 |
for enumerator, enumvalue in zip(tp.enumerators, tp.enumvalues): |
| 719 |
self._check_int_constant_value(enumerator, enumvalue, |
| 720 |
"enum %s: " % name) |
| 721 |
prnt(' return %s;' % self._chained_list_constants[True]) |
| 722 |
self._chained_list_constants[True] = funcname + '(lib)' |
| 723 |
prnt('}') |
| 724 |
prnt() |
| 725 |
|
| 726 |
_generate_cpy_enum_collecttype = _generate_nothing |
| 727 |
_generate_cpy_enum_method = _generate_nothing |
| 728 |
|
| 729 |
def _loading_cpy_enum(self, tp, name, module): |
| 730 |
if tp.partial: |
| 731 |
enumvalues = [getattr(module, enumerator) |
| 732 |
for enumerator in tp.enumerators] |
| 733 |
tp.enumvalues = tuple(enumvalues) |
| 734 |
tp.partial_resolved = True |
| 735 |
|
| 736 |
def _loaded_cpy_enum(self, tp, name, module, library): |
| 737 |
for enumerator, enumvalue in zip(tp.enumerators, tp.enumvalues): |
| 738 |
setattr(library, enumerator, enumvalue) |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
def _generate_cpy_macro_decl(self, tp, name): |
| 744 |
if tp == '...': |
| 745 |
check_value = None |
| 746 |
else: |
| 747 |
check_value = tp |
| 748 |
self._generate_cpy_const(True, name, check_value=check_value) |
| 749 |
|
| 750 |
_generate_cpy_macro_collecttype = _generate_nothing |
| 751 |
_generate_cpy_macro_method = _generate_nothing |
| 752 |
_loading_cpy_macro = _loaded_noop |
| 753 |
_loaded_cpy_macro = _loaded_noop |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
def _generate_cpy_variable_collecttype(self, tp, name): |
| 759 |
if isinstance(tp, model.ArrayType): |
| 760 |
tp_ptr = model.PointerType(tp.item) |
| 761 |
else: |
| 762 |
tp_ptr = model.PointerType(tp) |
| 763 |
self._do_collect_type(tp_ptr) |
| 764 |
|
| 765 |
def _generate_cpy_variable_decl(self, tp, name): |
| 766 |
if isinstance(tp, model.ArrayType): |
| 767 |
tp_ptr = model.PointerType(tp.item) |
| 768 |
self._generate_cpy_const(False, name, tp, vartp=tp_ptr, |
| 769 |
size_too = tp.length_is_unknown()) |
| 770 |
else: |
| 771 |
tp_ptr = model.PointerType(tp) |
| 772 |
self._generate_cpy_const(False, name, tp_ptr, category='var') |
| 773 |
|
| 774 |
_generate_cpy_variable_method = _generate_nothing |
| 775 |
_loading_cpy_variable = _loaded_noop |
| 776 |
|
| 777 |
def _loaded_cpy_variable(self, tp, name, module, library): |
| 778 |
value = getattr(library, name) |
| 779 |
if isinstance(tp, model.ArrayType): |
| 780 |
|
| 781 |
if tp.length_is_unknown(): |
| 782 |
assert isinstance(value, tuple) |
| 783 |
(value, size) = value |
| 784 |
BItemType = self.ffi._get_cached_btype(tp.item) |
| 785 |
length, rest = divmod(size, self.ffi.sizeof(BItemType)) |
| 786 |
if rest != 0: |
| 787 |
raise VerificationError( |
| 788 |
"bad size: %r does not seem to be an array of %s" % |
| 789 |
(name, tp.item)) |
| 790 |
tp = tp.resolve_length(length) |
| 791 |
|
| 792 |
|
| 793 |
if tp.length is not None: |
| 794 |
BArray = self.ffi._get_cached_btype(tp) |
| 795 |
value = self.ffi.cast(BArray, value) |
| 796 |
setattr(library, name, value) |
| 797 |
return |
| 798 |
|
| 799 |
|
| 800 |
ptr = value |
| 801 |
delattr(library, name) |
| 802 |
def getter(library): |
| 803 |
return ptr[0] |
| 804 |
def setter(library, value): |
| 805 |
ptr[0] = value |
| 806 |
setattr(type(library), name, property(getter, setter)) |
| 807 |
type(library)._cffi_dir.append(name) |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
def _generate_setup_custom(self): |
| 812 |
prnt = self._prnt |
| 813 |
prnt('static int _cffi_setup_custom(PyObject *lib)') |
| 814 |
prnt('{') |
| 815 |
prnt(' return %s;' % self._chained_list_constants[True]) |
| 816 |
prnt('}') |
| 817 |
|
| 818 |
cffimod_header = r''' |
| 819 |
#include <Python.h> |
| 820 |
#include <stddef.h> |
| 821 |
|
| 822 |
/* this block of #ifs should be kept exactly identical between |
| 823 |
c/_cffi_backend.c, cffi/vengine_cpy.py, cffi/vengine_gen.py |
| 824 |
and cffi/_cffi_include.h */ |
| 825 |
#if defined(_MSC_VER) |
| 826 |
# include <malloc.h> /* for alloca() */ |
| 827 |
# if _MSC_VER < 1600 /* MSVC < 2010 */ |
| 828 |
typedef __int8 int8_t; |
| 829 |
typedef __int16 int16_t; |
| 830 |
typedef __int32 int32_t; |
| 831 |
typedef __int64 int64_t; |
| 832 |
typedef unsigned __int8 uint8_t; |
| 833 |
typedef unsigned __int16 uint16_t; |
| 834 |
typedef unsigned __int32 uint32_t; |
| 835 |
typedef unsigned __int64 uint64_t; |
| 836 |
typedef __int8 int_least8_t; |
| 837 |
typedef __int16 int_least16_t; |
| 838 |
typedef __int32 int_least32_t; |
| 839 |
typedef __int64 int_least64_t; |
| 840 |
typedef unsigned __int8 uint_least8_t; |
| 841 |
typedef unsigned __int16 uint_least16_t; |
| 842 |
typedef unsigned __int32 uint_least32_t; |
| 843 |
typedef unsigned __int64 uint_least64_t; |
| 844 |
typedef __int8 int_fast8_t; |
| 845 |
typedef __int16 int_fast16_t; |
| 846 |
typedef __int32 int_fast32_t; |
| 847 |
typedef __int64 int_fast64_t; |
| 848 |
typedef unsigned __int8 uint_fast8_t; |
| 849 |
typedef unsigned __int16 uint_fast16_t; |
| 850 |
typedef unsigned __int32 uint_fast32_t; |
| 851 |
typedef unsigned __int64 uint_fast64_t; |
| 852 |
typedef __int64 intmax_t; |
| 853 |
typedef unsigned __int64 uintmax_t; |
| 854 |
# else |
| 855 |
# include <stdint.h> |
| 856 |
# endif |
| 857 |
# if _MSC_VER < 1800 /* MSVC < 2013 */ |
| 858 |
# ifndef __cplusplus |
| 859 |
typedef unsigned char _Bool; |
| 860 |
# endif |
| 861 |
# endif |
| 862 |
# define _cffi_float_complex_t _Fcomplex /* include <complex.h> for it */ |
| 863 |
# define _cffi_double_complex_t _Dcomplex /* include <complex.h> for it */ |
| 864 |
#else |
| 865 |
# include <stdint.h> |
| 866 |
# if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux) |
| 867 |
# include <alloca.h> |
| 868 |
# endif |
| 869 |
# define _cffi_float_complex_t float _Complex |
| 870 |
# define _cffi_double_complex_t double _Complex |
| 871 |
#endif |
| 872 |
|
| 873 |
#if PY_MAJOR_VERSION < 3 |
| 874 |
# undef PyCapsule_CheckExact |
| 875 |
# undef PyCapsule_GetPointer |
| 876 |
# define PyCapsule_CheckExact(capsule) (PyCObject_Check(capsule)) |
| 877 |
# define PyCapsule_GetPointer(capsule, name) \ |
| 878 |
(PyCObject_AsVoidPtr(capsule)) |
| 879 |
#endif |
| 880 |
|
| 881 |
#if PY_MAJOR_VERSION >= 3 |
| 882 |
# define PyInt_FromLong PyLong_FromLong |
| 883 |
#endif |
| 884 |
|
| 885 |
#define _cffi_from_c_double PyFloat_FromDouble |
| 886 |
#define _cffi_from_c_float PyFloat_FromDouble |
| 887 |
#define _cffi_from_c_long PyInt_FromLong |
| 888 |
#define _cffi_from_c_ulong PyLong_FromUnsignedLong |
| 889 |
#define _cffi_from_c_longlong PyLong_FromLongLong |
| 890 |
#define _cffi_from_c_ulonglong PyLong_FromUnsignedLongLong |
| 891 |
#define _cffi_from_c__Bool PyBool_FromLong |
| 892 |
|
| 893 |
#define _cffi_to_c_double PyFloat_AsDouble |
| 894 |
#define _cffi_to_c_float PyFloat_AsDouble |
| 895 |
|
| 896 |
#define _cffi_from_c_int_const(x) \ |
| 897 |
(((x) > 0) ? \ |
| 898 |
((unsigned long long)(x) <= (unsigned long long)LONG_MAX) ? \ |
| 899 |
PyInt_FromLong((long)(x)) : \ |
| 900 |
PyLong_FromUnsignedLongLong((unsigned long long)(x)) : \ |
| 901 |
((long long)(x) >= (long long)LONG_MIN) ? \ |
| 902 |
PyInt_FromLong((long)(x)) : \ |
| 903 |
PyLong_FromLongLong((long long)(x))) |
| 904 |
|
| 905 |
#define _cffi_from_c_int(x, type) \ |
| 906 |
(((type)-1) > 0 ? /* unsigned */ \ |
| 907 |
(sizeof(type) < sizeof(long) ? \ |
| 908 |
PyInt_FromLong((long)x) : \ |
| 909 |
sizeof(type) == sizeof(long) ? \ |
| 910 |
PyLong_FromUnsignedLong((unsigned long)x) : \ |
| 911 |
PyLong_FromUnsignedLongLong((unsigned long long)x)) : \ |
| 912 |
(sizeof(type) <= sizeof(long) ? \ |
| 913 |
PyInt_FromLong((long)x) : \ |
| 914 |
PyLong_FromLongLong((long long)x))) |
| 915 |
|
| 916 |
#define _cffi_to_c_int(o, type) \ |
| 917 |
((type)( \ |
| 918 |
sizeof(type) == 1 ? (((type)-1) > 0 ? (type)_cffi_to_c_u8(o) \ |
| 919 |
: (type)_cffi_to_c_i8(o)) : \ |
| 920 |
sizeof(type) == 2 ? (((type)-1) > 0 ? (type)_cffi_to_c_u16(o) \ |
| 921 |
: (type)_cffi_to_c_i16(o)) : \ |
| 922 |
sizeof(type) == 4 ? (((type)-1) > 0 ? (type)_cffi_to_c_u32(o) \ |
| 923 |
: (type)_cffi_to_c_i32(o)) : \ |
| 924 |
sizeof(type) == 8 ? (((type)-1) > 0 ? (type)_cffi_to_c_u64(o) \ |
| 925 |
: (type)_cffi_to_c_i64(o)) : \ |
| 926 |
(Py_FatalError("unsupported size for type " #type), (type)0))) |
| 927 |
|
| 928 |
#define _cffi_to_c_i8 \ |
| 929 |
((int(*)(PyObject *))_cffi_exports[1]) |
| 930 |
#define _cffi_to_c_u8 \ |
| 931 |
((int(*)(PyObject *))_cffi_exports[2]) |
| 932 |
#define _cffi_to_c_i16 \ |
| 933 |
((int(*)(PyObject *))_cffi_exports[3]) |
| 934 |
#define _cffi_to_c_u16 \ |
| 935 |
((int(*)(PyObject *))_cffi_exports[4]) |
| 936 |
#define _cffi_to_c_i32 \ |
| 937 |
((int(*)(PyObject *))_cffi_exports[5]) |
| 938 |
#define _cffi_to_c_u32 \ |
| 939 |
((unsigned int(*)(PyObject *))_cffi_exports[6]) |
| 940 |
#define _cffi_to_c_i64 \ |
| 941 |
((long long(*)(PyObject *))_cffi_exports[7]) |
| 942 |
#define _cffi_to_c_u64 \ |
| 943 |
((unsigned long long(*)(PyObject *))_cffi_exports[8]) |
| 944 |
#define _cffi_to_c_char \ |
| 945 |
((int(*)(PyObject *))_cffi_exports[9]) |
| 946 |
#define _cffi_from_c_pointer \ |
| 947 |
((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[10]) |
| 948 |
#define _cffi_to_c_pointer \ |
| 949 |
((char *(*)(PyObject *, CTypeDescrObject *))_cffi_exports[11]) |
| 950 |
#define _cffi_get_struct_layout \ |
| 951 |
((PyObject *(*)(Py_ssize_t[]))_cffi_exports[12]) |
| 952 |
#define _cffi_restore_errno \ |
| 953 |
((void(*)(void))_cffi_exports[13]) |
| 954 |
#define _cffi_save_errno \ |
| 955 |
((void(*)(void))_cffi_exports[14]) |
| 956 |
#define _cffi_from_c_char \ |
| 957 |
((PyObject *(*)(char))_cffi_exports[15]) |
| 958 |
#define _cffi_from_c_deref \ |
| 959 |
((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[16]) |
| 960 |
#define _cffi_to_c \ |
| 961 |
((int(*)(char *, CTypeDescrObject *, PyObject *))_cffi_exports[17]) |
| 962 |
#define _cffi_from_c_struct \ |
| 963 |
((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[18]) |
| 964 |
#define _cffi_to_c_wchar_t \ |
| 965 |
((wchar_t(*)(PyObject *))_cffi_exports[19]) |
| 966 |
#define _cffi_from_c_wchar_t \ |
| 967 |
((PyObject *(*)(wchar_t))_cffi_exports[20]) |
| 968 |
#define _cffi_to_c_long_double \ |
| 969 |
((long double(*)(PyObject *))_cffi_exports[21]) |
| 970 |
#define _cffi_to_c__Bool \ |
| 971 |
((_Bool(*)(PyObject *))_cffi_exports[22]) |
| 972 |
#define _cffi_prepare_pointer_call_argument \ |
| 973 |
((Py_ssize_t(*)(CTypeDescrObject *, PyObject *, char **))_cffi_exports[23]) |
| 974 |
#define _cffi_convert_array_from_object \ |
| 975 |
((int(*)(char *, CTypeDescrObject *, PyObject *))_cffi_exports[24]) |
| 976 |
#define _CFFI_NUM_EXPORTS 25 |
| 977 |
|
| 978 |
typedef struct _ctypedescr CTypeDescrObject; |
| 979 |
|
| 980 |
static void *_cffi_exports[_CFFI_NUM_EXPORTS]; |
| 981 |
static PyObject *_cffi_types, *_cffi_VerificationError; |
| 982 |
|
| 983 |
static int _cffi_setup_custom(PyObject *lib); /* forward */ |
| 984 |
|
| 985 |
static PyObject *_cffi_setup(PyObject *self, PyObject *args) |
| 986 |
{ |
| 987 |
PyObject *library; |
| 988 |
int was_alive = (_cffi_types != NULL); |
| 989 |
(void)self; /* unused */ |
| 990 |
if (!PyArg_ParseTuple(args, "OOO", &_cffi_types, &_cffi_VerificationError, |
| 991 |
&library)) |
| 992 |
return NULL; |
| 993 |
Py_INCREF(_cffi_types); |
| 994 |
Py_INCREF(_cffi_VerificationError); |
| 995 |
if (_cffi_setup_custom(library) < 0) |
| 996 |
return NULL; |
| 997 |
return PyBool_FromLong(was_alive); |
| 998 |
} |
| 999 |
|
| 1000 |
union _cffi_union_alignment_u { |
| 1001 |
unsigned char m_char; |
| 1002 |
unsigned short m_short; |
| 1003 |
unsigned int m_int; |
| 1004 |
unsigned long m_long; |
| 1005 |
unsigned long long m_longlong; |
| 1006 |
float m_float; |
| 1007 |
double m_double; |
| 1008 |
long double m_longdouble; |
| 1009 |
}; |
| 1010 |
|
| 1011 |
struct _cffi_freeme_s { |
| 1012 |
struct _cffi_freeme_s *next; |
| 1013 |
union _cffi_union_alignment_u alignment; |
| 1014 |
}; |
| 1015 |
|
| 1016 |
#ifdef __GNUC__ |
| 1017 |
__attribute__((unused)) |
| 1018 |
#endif |
| 1019 |
static int _cffi_convert_array_argument(CTypeDescrObject *ctptr, PyObject *arg, |
| 1020 |
char **output_data, Py_ssize_t datasize, |
| 1021 |
struct _cffi_freeme_s **freeme) |
| 1022 |
{ |
| 1023 |
char *p; |
| 1024 |
if (datasize < 0) |
| 1025 |
return -1; |
| 1026 |
|
| 1027 |
p = *output_data; |
| 1028 |
if (p == NULL) { |
| 1029 |
struct _cffi_freeme_s *fp = (struct _cffi_freeme_s *)PyObject_Malloc( |
| 1030 |
offsetof(struct _cffi_freeme_s, alignment) + (size_t)datasize); |
| 1031 |
if (fp == NULL) |
| 1032 |
return -1; |
| 1033 |
fp->next = *freeme; |
| 1034 |
*freeme = fp; |
| 1035 |
p = *output_data = (char *)&fp->alignment; |
| 1036 |
} |
| 1037 |
memset((void *)p, 0, (size_t)datasize); |
| 1038 |
return _cffi_convert_array_from_object(p, ctptr, arg); |
| 1039 |
} |
| 1040 |
|
| 1041 |
#ifdef __GNUC__ |
| 1042 |
__attribute__((unused)) |
| 1043 |
#endif |
| 1044 |
static void _cffi_free_array_arguments(struct _cffi_freeme_s *freeme) |
| 1045 |
{ |
| 1046 |
do { |
| 1047 |
void *p = (void *)freeme; |
| 1048 |
freeme = freeme->next; |
| 1049 |
PyObject_Free(p); |
| 1050 |
} while (freeme != NULL); |
| 1051 |
} |
| 1052 |
|
| 1053 |
static int _cffi_init(void) |
| 1054 |
{ |
| 1055 |
PyObject *module, *c_api_object = NULL; |
| 1056 |
|
| 1057 |
module = PyImport_ImportModule("_cffi_backend"); |
| 1058 |
if (module == NULL) |
| 1059 |
goto failure; |
| 1060 |
|
| 1061 |
c_api_object = PyObject_GetAttrString(module, "_C_API"); |
| 1062 |
if (c_api_object == NULL) |
| 1063 |
goto failure; |
| 1064 |
if (!PyCapsule_CheckExact(c_api_object)) { |
| 1065 |
PyErr_SetNone(PyExc_ImportError); |
| 1066 |
goto failure; |
| 1067 |
} |
| 1068 |
memcpy(_cffi_exports, PyCapsule_GetPointer(c_api_object, "cffi"), |
| 1069 |
_CFFI_NUM_EXPORTS * sizeof(void *)); |
| 1070 |
|
| 1071 |
Py_DECREF(module); |
| 1072 |
Py_DECREF(c_api_object); |
| 1073 |
return 0; |
| 1074 |
|
| 1075 |
failure: |
| 1076 |
Py_XDECREF(module); |
| 1077 |
Py_XDECREF(c_api_object); |
| 1078 |
return -1; |
| 1079 |
} |
| 1080 |
|
| 1081 |
#define _cffi_type(num) ((CTypeDescrObject *)PyList_GET_ITEM(_cffi_types, num)) |
| 1082 |
|
| 1083 |
/**********/ |
| 1084 |
''' |