@pairling/runtime-darwin-x64 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (159) hide show
  1. package/README.md +1 -1
  2. package/bin/pairling-connectd +0 -0
  3. package/manifest.json +4 -4
  4. package/package.json +3 -3
  5. package/python/bin/python3 +0 -0
  6. package/python/bin/python3.12 +0 -0
  7. package/python/lib/libpython3.12.dylib +0 -0
  8. package/python/lib/python3.12/lib-dynload/_crypt.cpython-312-darwin.so +0 -0
  9. package/python/lib/python3.12/lib-dynload/_dbm.cpython-312-darwin.so +0 -0
  10. package/python/lib/python3.12/site-packages/_cffi_backend.cpython-312-darwin.so +0 -0
  11. package/python/lib/python3.12/site-packages/cffi/__init__.py +14 -0
  12. package/python/lib/python3.12/site-packages/cffi/_cffi_errors.h +149 -0
  13. package/python/lib/python3.12/site-packages/cffi/_cffi_include.h +389 -0
  14. package/python/lib/python3.12/site-packages/cffi/_embedding.h +550 -0
  15. package/python/lib/python3.12/site-packages/cffi/_imp_emulation.py +83 -0
  16. package/python/lib/python3.12/site-packages/cffi/_shimmed_dist_utils.py +45 -0
  17. package/python/lib/python3.12/site-packages/cffi/api.py +967 -0
  18. package/python/lib/python3.12/site-packages/cffi/backend_ctypes.py +1121 -0
  19. package/python/lib/python3.12/site-packages/cffi/cffi_opcode.py +187 -0
  20. package/python/lib/python3.12/site-packages/cffi/commontypes.py +82 -0
  21. package/python/lib/python3.12/site-packages/cffi/cparser.py +1015 -0
  22. package/python/lib/python3.12/site-packages/cffi/error.py +31 -0
  23. package/python/lib/python3.12/site-packages/cffi/ffiplatform.py +113 -0
  24. package/python/lib/python3.12/site-packages/cffi/lock.py +30 -0
  25. package/python/lib/python3.12/site-packages/cffi/model.py +618 -0
  26. package/python/lib/python3.12/site-packages/cffi/parse_c_type.h +181 -0
  27. package/python/lib/python3.12/site-packages/cffi/pkgconfig.py +121 -0
  28. package/python/lib/python3.12/site-packages/cffi/recompiler.py +1598 -0
  29. package/python/lib/python3.12/site-packages/cffi/setuptools_ext.py +216 -0
  30. package/python/lib/python3.12/site-packages/cffi/vengine_cpy.py +1084 -0
  31. package/python/lib/python3.12/site-packages/cffi/vengine_gen.py +679 -0
  32. package/python/lib/python3.12/site-packages/cffi/verifier.py +306 -0
  33. package/python/lib/python3.12/site-packages/cffi-1.17.1.dist-info/INSTALLER +1 -0
  34. package/python/lib/python3.12/site-packages/cffi-1.17.1.dist-info/LICENSE +26 -0
  35. package/python/lib/python3.12/site-packages/cffi-1.17.1.dist-info/METADATA +40 -0
  36. package/python/lib/python3.12/site-packages/cffi-1.17.1.dist-info/RECORD +30 -0
  37. package/python/lib/python3.12/site-packages/cffi-1.17.1.dist-info/WHEEL +5 -0
  38. package/python/lib/python3.12/site-packages/cffi-1.17.1.dist-info/entry_points.txt +2 -0
  39. package/python/lib/python3.12/site-packages/cffi-1.17.1.dist-info/top_level.txt +2 -0
  40. package/python/lib/python3.12/site-packages/cryptography/__about__.py +17 -0
  41. package/python/lib/python3.12/site-packages/cryptography/__init__.py +26 -0
  42. package/python/lib/python3.12/site-packages/cryptography/exceptions.py +52 -0
  43. package/python/lib/python3.12/site-packages/cryptography/fernet.py +224 -0
  44. package/python/lib/python3.12/site-packages/cryptography/hazmat/__init__.py +13 -0
  45. package/python/lib/python3.12/site-packages/cryptography/hazmat/_oid.py +348 -0
  46. package/python/lib/python3.12/site-packages/cryptography/hazmat/backends/__init__.py +13 -0
  47. package/python/lib/python3.12/site-packages/cryptography/hazmat/backends/openssl/__init__.py +9 -0
  48. package/python/lib/python3.12/site-packages/cryptography/hazmat/backends/openssl/backend.py +308 -0
  49. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/__init__.py +3 -0
  50. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/__init__.pyi +37 -0
  51. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/_openssl.pyi +8 -0
  52. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/asn1.pyi +7 -0
  53. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/exceptions.pyi +17 -0
  54. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/ocsp.pyi +117 -0
  55. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/openssl/__init__.pyi +75 -0
  56. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/openssl/aead.pyi +107 -0
  57. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/openssl/ciphers.pyi +38 -0
  58. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/openssl/cmac.pyi +18 -0
  59. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/openssl/dh.pyi +51 -0
  60. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/openssl/dsa.pyi +41 -0
  61. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/openssl/ec.pyi +52 -0
  62. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/openssl/ed25519.pyi +13 -0
  63. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/openssl/ed448.pyi +13 -0
  64. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/openssl/hashes.pyi +28 -0
  65. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/openssl/hmac.pyi +22 -0
  66. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/openssl/kdf.pyi +49 -0
  67. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/openssl/keys.pyi +34 -0
  68. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/openssl/poly1305.pyi +15 -0
  69. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/openssl/rsa.pyi +55 -0
  70. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/openssl/x25519.pyi +13 -0
  71. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/openssl/x448.pyi +13 -0
  72. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/pkcs12.pyi +52 -0
  73. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/pkcs7.pyi +50 -0
  74. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/test_support.pyi +23 -0
  75. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust/x509.pyi +313 -0
  76. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/_rust.abi3.so +0 -0
  77. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/openssl/__init__.py +3 -0
  78. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/openssl/_conditional.py +191 -0
  79. package/python/lib/python3.12/site-packages/cryptography/hazmat/bindings/openssl/binding.py +122 -0
  80. package/python/lib/python3.12/site-packages/cryptography/hazmat/decrepit/__init__.py +5 -0
  81. package/python/lib/python3.12/site-packages/cryptography/hazmat/decrepit/ciphers/__init__.py +5 -0
  82. package/python/lib/python3.12/site-packages/cryptography/hazmat/decrepit/ciphers/algorithms.py +112 -0
  83. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/__init__.py +3 -0
  84. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/_asymmetric.py +19 -0
  85. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/_cipheralgorithm.py +60 -0
  86. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/_serialization.py +168 -0
  87. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/asymmetric/__init__.py +3 -0
  88. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/asymmetric/dh.py +147 -0
  89. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/asymmetric/dsa.py +167 -0
  90. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/asymmetric/ec.py +447 -0
  91. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/asymmetric/ed25519.py +129 -0
  92. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/asymmetric/ed448.py +131 -0
  93. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/asymmetric/padding.py +113 -0
  94. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.py +277 -0
  95. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/asymmetric/types.py +111 -0
  96. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/asymmetric/utils.py +24 -0
  97. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/asymmetric/x25519.py +122 -0
  98. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/asymmetric/x448.py +125 -0
  99. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/ciphers/__init__.py +27 -0
  100. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/ciphers/aead.py +23 -0
  101. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/ciphers/algorithms.py +183 -0
  102. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/ciphers/base.py +146 -0
  103. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/ciphers/modes.py +268 -0
  104. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/cmac.py +10 -0
  105. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/constant_time.py +14 -0
  106. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/hashes.py +246 -0
  107. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/hmac.py +13 -0
  108. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/kdf/__init__.py +23 -0
  109. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/kdf/argon2.py +13 -0
  110. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/kdf/concatkdf.py +125 -0
  111. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/kdf/hkdf.py +101 -0
  112. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/kdf/kbkdf.py +305 -0
  113. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/kdf/pbkdf2.py +62 -0
  114. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/kdf/scrypt.py +19 -0
  115. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/kdf/x963kdf.py +61 -0
  116. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/keywrap.py +177 -0
  117. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/padding.py +69 -0
  118. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/poly1305.py +11 -0
  119. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/serialization/__init__.py +65 -0
  120. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/serialization/base.py +14 -0
  121. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/serialization/pkcs12.py +176 -0
  122. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/serialization/pkcs7.py +411 -0
  123. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/serialization/ssh.py +1619 -0
  124. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/twofactor/__init__.py +9 -0
  125. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/twofactor/hotp.py +101 -0
  126. package/python/lib/python3.12/site-packages/cryptography/hazmat/primitives/twofactor/totp.py +56 -0
  127. package/python/lib/python3.12/site-packages/cryptography/py.typed +0 -0
  128. package/python/lib/python3.12/site-packages/cryptography/utils.py +139 -0
  129. package/python/lib/python3.12/site-packages/cryptography/x509/__init__.py +270 -0
  130. package/python/lib/python3.12/site-packages/cryptography/x509/base.py +848 -0
  131. package/python/lib/python3.12/site-packages/cryptography/x509/certificate_transparency.py +35 -0
  132. package/python/lib/python3.12/site-packages/cryptography/x509/extensions.py +2528 -0
  133. package/python/lib/python3.12/site-packages/cryptography/x509/general_name.py +281 -0
  134. package/python/lib/python3.12/site-packages/cryptography/x509/name.py +477 -0
  135. package/python/lib/python3.12/site-packages/cryptography/x509/ocsp.py +379 -0
  136. package/python/lib/python3.12/site-packages/cryptography/x509/oid.py +37 -0
  137. package/python/lib/python3.12/site-packages/cryptography/x509/verification.py +34 -0
  138. package/python/lib/python3.12/site-packages/cryptography-45.0.7.dist-info/INSTALLER +1 -0
  139. package/python/lib/python3.12/site-packages/cryptography-45.0.7.dist-info/METADATA +139 -0
  140. package/python/lib/python3.12/site-packages/cryptography-45.0.7.dist-info/RECORD +106 -0
  141. package/python/lib/python3.12/site-packages/cryptography-45.0.7.dist-info/REQUESTED +0 -0
  142. package/python/lib/python3.12/site-packages/cryptography-45.0.7.dist-info/WHEEL +4 -0
  143. package/python/lib/python3.12/site-packages/cryptography-45.0.7.dist-info/licenses/LICENSE +3 -0
  144. package/python/lib/python3.12/site-packages/cryptography-45.0.7.dist-info/licenses/LICENSE.APACHE +202 -0
  145. package/python/lib/python3.12/site-packages/cryptography-45.0.7.dist-info/licenses/LICENSE.BSD +27 -0
  146. package/python/lib/python3.12/site-packages/pycparser/__init__.py +99 -0
  147. package/python/lib/python3.12/site-packages/pycparser/_ast_gen.py +355 -0
  148. package/python/lib/python3.12/site-packages/pycparser/_c_ast.cfg +195 -0
  149. package/python/lib/python3.12/site-packages/pycparser/ast_transforms.py +174 -0
  150. package/python/lib/python3.12/site-packages/pycparser/c_ast.py +1341 -0
  151. package/python/lib/python3.12/site-packages/pycparser/c_generator.py +573 -0
  152. package/python/lib/python3.12/site-packages/pycparser/c_lexer.py +706 -0
  153. package/python/lib/python3.12/site-packages/pycparser/c_parser.py +2376 -0
  154. package/python/lib/python3.12/site-packages/pycparser-3.0.dist-info/INSTALLER +1 -0
  155. package/python/lib/python3.12/site-packages/pycparser-3.0.dist-info/METADATA +244 -0
  156. package/python/lib/python3.12/site-packages/pycparser-3.0.dist-info/RECORD +14 -0
  157. package/python/lib/python3.12/site-packages/pycparser-3.0.dist-info/WHEEL +5 -0
  158. package/python/lib/python3.12/site-packages/pycparser-3.0.dist-info/licenses/LICENSE +27 -0
  159. package/python/lib/python3.12/site-packages/pycparser-3.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1084 @@
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
+
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
+ # Note that after a setuptools installation, there are both .py
31
+ # and .so files with the same basename. The code here relies on
32
+ # imp.find_module() locating the .so in priority.
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
+ # a KeyError here is a bug. please report it! :-)
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
+ # The new module will have a _cffi_setup() function that receives
59
+ # objects from the ffi world, and that calls some setup code in
60
+ # the module. This setup code is split in several independent
61
+ # functions, e.g. one per constant. The functions are "chained"
62
+ # by ending in a tail call to each other.
63
+ #
64
+ # This is further split in two chained lists, depending on if we
65
+ # can do it at import-time or if we must wait for _cffi_setup() to
66
+ # provide us with the <ctype> objects. This is needed because we
67
+ # need the values of the enum constants in order to build the
68
+ # <ctype 'enum'> that we may have to pass to _cffi_setup().
69
+ #
70
+ # The following two 'chained_list_constants' items contains
71
+ # the head of these two chained lists, as a string that gives the
72
+ # call to do, if any.
73
+ self._chained_list_constants = ['((void)lib,0)', '((void)lib,0)']
74
+ #
75
+ prnt = self._prnt
76
+ # first paste some standard set of lines that are mostly '#define'
77
+ prnt(cffimod_header)
78
+ prnt()
79
+ # then paste the C source given by the user, verbatim.
80
+ prnt(self.verifier.preamble)
81
+ prnt()
82
+ #
83
+ # call generate_cpy_xxx_decl(), for every xxx found from
84
+ # ffi._parser._declarations. This generates all the functions.
85
+ self._generate("decl")
86
+ #
87
+ # implement the function _cffi_setup_custom() as calling the
88
+ # head of the chained list.
89
+ self._generate_setup_custom()
90
+ prnt()
91
+ #
92
+ # produce the method table, including the entries for the
93
+ # generated Python->C function wrappers, which are done
94
+ # by generate_cpy_function_method().
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
+ # standard init.
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
+ # XXX review all usages of 'self' here!
148
+ # import it as a new extension module
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
+ # call loading_cpy_struct() to get the struct layout inferred by
168
+ # the C compiler
169
+ self._load(module, 'loading')
170
+ #
171
+ # the C code will need the <ctype> objects. Collect them in
172
+ # order in a list.
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
+ # build the FFILibrary class and instance and call _cffi_setup().
179
+ # this will set up some fields like '_cffi_types', and only then
180
+ # it will invoke the chained list of functions that will really
181
+ # build (notably) the constant objects, as <cdata> if they are
182
+ # pointers, and store them as attributes on the 'library' object.
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
+ # finally, call the loaded_cpy_xxx() functions. This will perform
196
+ # the final adjustments, like copying the Python->C wrapper
197
+ # functions from the module to the 'library' object, and setting
198
+ # up the FFILibrary class with properties for the global C variables.
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
+ # a struct (not a struct pointer) as a function argument
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
+ # typedefs: generates no code so far
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
+ # function declarations
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
+ # don't call _do_collect_type(tp) in this common case,
347
+ # otherwise test_autofilled_struct_as_argument fails
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
+ # cannot support vararg functions better than this: check for its
356
+ # exact type (including the fixed arguments), and build it as a
357
+ # constant function pointer (no CPython wrapper)
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
+ # named structs
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 # nothing to do with opaque structs
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
+ # accept all integers, but complain on float or double
494
+ prnt(' (void)((p->%s) << 1);' % fname)
495
+ else:
496
+ # only accept exactly the type declared.
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)) # cannot verify it, ignore
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 # xxx ignore fbitsize for now
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 # nothing to do with opaque structs
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 # nothing to do with opaque structs
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
+ # use the function()'s sizes and offsets to guide the
545
+ # layout of the struct
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 # nothing to do with opaque structs
560
+ self.ffi._get_cached_btype(tp) # force 'fixedlayout' to be considered
561
+
562
+ if tp in self._struct_pending_verification:
563
+ # check that the layout sizes and offsets match the real ones
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 # xxx ignore fbitsize for now
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
+ # 'anonymous' declarations. These are produced for anonymous structs
589
+ # or unions; the 'name' is obtained by a typedef.
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
+ # constants, likely declared with '#define'
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
+ # enums
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
+ # "$enum_$1" => "___D_enum____D_1"
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
+ # macros: for now only for integers
742
+
743
+ def _generate_cpy_macro_decl(self, tp, name):
744
+ if tp == '...':
745
+ check_value = None
746
+ else:
747
+ check_value = tp # an integer
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
+ # global variables
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): # int a[5] is "constant" in the
780
+ # sense that "a=..." is forbidden
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
+ # 'value' is a <cdata 'type *'> which we have to replace with
792
+ # a <cdata 'type[N]'> if the N is actually known
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
+ # remove ptr=<cdata 'int *'> from the library instance, and replace
799
+ # it by a property on the class, which reads/writes into ptr[0].
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
+ '''