1
0

vengine_cpy.py 43 KB

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