_cffi_include.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #define _CFFI_
  2. /* We try to define Py_LIMITED_API before including Python.h.
  3. Mess: we can only define it if Py_DEBUG, Py_TRACE_REFS and
  4. Py_REF_DEBUG are not defined. This is a best-effort approximation:
  5. we can learn about Py_DEBUG from pyconfig.h, but it is unclear if
  6. the same works for the other two macros. Py_DEBUG implies them,
  7. but not the other way around.
  8. The implementation is messy (issue #350): on Windows, with _MSC_VER,
  9. we have to define Py_LIMITED_API even before including pyconfig.h.
  10. In that case, we guess what pyconfig.h will do to the macros above,
  11. and check our guess after the #include.
  12. Note that on Windows, with CPython 3.x, you need >= 3.5 and virtualenv
  13. version >= 16.0.0. With older versions of either, you don't get a
  14. copy of PYTHON3.DLL in the virtualenv. We can't check the version of
  15. CPython *before* we even include pyconfig.h. ffi.set_source() puts
  16. a ``#define _CFFI_NO_LIMITED_API'' at the start of this file if it is
  17. running on Windows < 3.5, as an attempt at fixing it, but that's
  18. arguably wrong because it may not be the target version of Python.
  19. Still better than nothing I guess. As another workaround, you can
  20. remove the definition of Py_LIMITED_API here.
  21. See also 'py_limited_api' in cffi/setuptools_ext.py.
  22. */
  23. #if !defined(_CFFI_USE_EMBEDDING) && !defined(Py_LIMITED_API)
  24. # ifdef _MSC_VER
  25. # if !defined(_DEBUG) && !defined(Py_DEBUG) && !defined(Py_TRACE_REFS) && !defined(Py_REF_DEBUG) && !defined(_CFFI_NO_LIMITED_API)
  26. # define Py_LIMITED_API
  27. # endif
  28. # include <pyconfig.h>
  29. /* sanity-check: Py_LIMITED_API will cause crashes if any of these
  30. are also defined. Normally, the Python file PC/pyconfig.h does not
  31. cause any of these to be defined, with the exception that _DEBUG
  32. causes Py_DEBUG. Double-check that. */
  33. # ifdef Py_LIMITED_API
  34. # if defined(Py_DEBUG)
  35. # error "pyconfig.h unexpectedly defines Py_DEBUG, but Py_LIMITED_API is set"
  36. # endif
  37. # if defined(Py_TRACE_REFS)
  38. # error "pyconfig.h unexpectedly defines Py_TRACE_REFS, but Py_LIMITED_API is set"
  39. # endif
  40. # if defined(Py_REF_DEBUG)
  41. # error "pyconfig.h unexpectedly defines Py_REF_DEBUG, but Py_LIMITED_API is set"
  42. # endif
  43. # endif
  44. # else
  45. # include <pyconfig.h>
  46. # if !defined(Py_DEBUG) && !defined(Py_TRACE_REFS) && !defined(Py_REF_DEBUG) && !defined(_CFFI_NO_LIMITED_API)
  47. # define Py_LIMITED_API
  48. # endif
  49. # endif
  50. #endif
  51. #include <Python.h>
  52. #ifdef __cplusplus
  53. extern "C" {
  54. #endif
  55. #include <stddef.h>
  56. #include "parse_c_type.h"
  57. /* this block of #ifs should be kept exactly identical between
  58. c/_cffi_backend.c, cffi/vengine_cpy.py, cffi/vengine_gen.py
  59. and cffi/_cffi_include.h */
  60. #if defined(_MSC_VER)
  61. # include <malloc.h> /* for alloca() */
  62. # if _MSC_VER < 1600 /* MSVC < 2010 */
  63. typedef __int8 int8_t;
  64. typedef __int16 int16_t;
  65. typedef __int32 int32_t;
  66. typedef __int64 int64_t;
  67. typedef unsigned __int8 uint8_t;
  68. typedef unsigned __int16 uint16_t;
  69. typedef unsigned __int32 uint32_t;
  70. typedef unsigned __int64 uint64_t;
  71. typedef __int8 int_least8_t;
  72. typedef __int16 int_least16_t;
  73. typedef __int32 int_least32_t;
  74. typedef __int64 int_least64_t;
  75. typedef unsigned __int8 uint_least8_t;
  76. typedef unsigned __int16 uint_least16_t;
  77. typedef unsigned __int32 uint_least32_t;
  78. typedef unsigned __int64 uint_least64_t;
  79. typedef __int8 int_fast8_t;
  80. typedef __int16 int_fast16_t;
  81. typedef __int32 int_fast32_t;
  82. typedef __int64 int_fast64_t;
  83. typedef unsigned __int8 uint_fast8_t;
  84. typedef unsigned __int16 uint_fast16_t;
  85. typedef unsigned __int32 uint_fast32_t;
  86. typedef unsigned __int64 uint_fast64_t;
  87. typedef __int64 intmax_t;
  88. typedef unsigned __int64 uintmax_t;
  89. # else
  90. # include <stdint.h>
  91. # endif
  92. # if _MSC_VER < 1800 /* MSVC < 2013 */
  93. # ifndef __cplusplus
  94. typedef unsigned char _Bool;
  95. # endif
  96. # endif
  97. # define _cffi_float_complex_t _Fcomplex /* include <complex.h> for it */
  98. # define _cffi_double_complex_t _Dcomplex /* include <complex.h> for it */
  99. #else
  100. # include <stdint.h>
  101. # if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux)
  102. # include <alloca.h>
  103. # endif
  104. # define _cffi_float_complex_t float _Complex
  105. # define _cffi_double_complex_t double _Complex
  106. #endif
  107. #ifdef __GNUC__
  108. # define _CFFI_UNUSED_FN __attribute__((unused))
  109. #else
  110. # define _CFFI_UNUSED_FN /* nothing */
  111. #endif
  112. #ifdef __cplusplus
  113. # ifndef _Bool
  114. typedef bool _Bool; /* semi-hackish: C++ has no _Bool; bool is builtin */
  115. # endif
  116. #endif
  117. /********** CPython-specific section **********/
  118. #ifndef PYPY_VERSION
  119. #if PY_MAJOR_VERSION >= 3
  120. # define PyInt_FromLong PyLong_FromLong
  121. #endif
  122. #define _cffi_from_c_double PyFloat_FromDouble
  123. #define _cffi_from_c_float PyFloat_FromDouble
  124. #define _cffi_from_c_long PyInt_FromLong
  125. #define _cffi_from_c_ulong PyLong_FromUnsignedLong
  126. #define _cffi_from_c_longlong PyLong_FromLongLong
  127. #define _cffi_from_c_ulonglong PyLong_FromUnsignedLongLong
  128. #define _cffi_from_c__Bool PyBool_FromLong
  129. #define _cffi_to_c_double PyFloat_AsDouble
  130. #define _cffi_to_c_float PyFloat_AsDouble
  131. #define _cffi_from_c_int(x, type) \
  132. (((type)-1) > 0 ? /* unsigned */ \
  133. (sizeof(type) < sizeof(long) ? \
  134. PyInt_FromLong((long)x) : \
  135. sizeof(type) == sizeof(long) ? \
  136. PyLong_FromUnsignedLong((unsigned long)x) : \
  137. PyLong_FromUnsignedLongLong((unsigned long long)x)) : \
  138. (sizeof(type) <= sizeof(long) ? \
  139. PyInt_FromLong((long)x) : \
  140. PyLong_FromLongLong((long long)x)))
  141. #define _cffi_to_c_int(o, type) \
  142. ((type)( \
  143. sizeof(type) == 1 ? (((type)-1) > 0 ? (type)_cffi_to_c_u8(o) \
  144. : (type)_cffi_to_c_i8(o)) : \
  145. sizeof(type) == 2 ? (((type)-1) > 0 ? (type)_cffi_to_c_u16(o) \
  146. : (type)_cffi_to_c_i16(o)) : \
  147. sizeof(type) == 4 ? (((type)-1) > 0 ? (type)_cffi_to_c_u32(o) \
  148. : (type)_cffi_to_c_i32(o)) : \
  149. sizeof(type) == 8 ? (((type)-1) > 0 ? (type)_cffi_to_c_u64(o) \
  150. : (type)_cffi_to_c_i64(o)) : \
  151. (Py_FatalError("unsupported size for type " #type), (type)0)))
  152. #define _cffi_to_c_i8 \
  153. ((int(*)(PyObject *))_cffi_exports[1])
  154. #define _cffi_to_c_u8 \
  155. ((int(*)(PyObject *))_cffi_exports[2])
  156. #define _cffi_to_c_i16 \
  157. ((int(*)(PyObject *))_cffi_exports[3])
  158. #define _cffi_to_c_u16 \
  159. ((int(*)(PyObject *))_cffi_exports[4])
  160. #define _cffi_to_c_i32 \
  161. ((int(*)(PyObject *))_cffi_exports[5])
  162. #define _cffi_to_c_u32 \
  163. ((unsigned int(*)(PyObject *))_cffi_exports[6])
  164. #define _cffi_to_c_i64 \
  165. ((long long(*)(PyObject *))_cffi_exports[7])
  166. #define _cffi_to_c_u64 \
  167. ((unsigned long long(*)(PyObject *))_cffi_exports[8])
  168. #define _cffi_to_c_char \
  169. ((int(*)(PyObject *))_cffi_exports[9])
  170. #define _cffi_from_c_pointer \
  171. ((PyObject *(*)(char *, struct _cffi_ctypedescr *))_cffi_exports[10])
  172. #define _cffi_to_c_pointer \
  173. ((char *(*)(PyObject *, struct _cffi_ctypedescr *))_cffi_exports[11])
  174. #define _cffi_get_struct_layout \
  175. not used any more
  176. #define _cffi_restore_errno \
  177. ((void(*)(void))_cffi_exports[13])
  178. #define _cffi_save_errno \
  179. ((void(*)(void))_cffi_exports[14])
  180. #define _cffi_from_c_char \
  181. ((PyObject *(*)(char))_cffi_exports[15])
  182. #define _cffi_from_c_deref \
  183. ((PyObject *(*)(char *, struct _cffi_ctypedescr *))_cffi_exports[16])
  184. #define _cffi_to_c \
  185. ((int(*)(char *, struct _cffi_ctypedescr *, PyObject *))_cffi_exports[17])
  186. #define _cffi_from_c_struct \
  187. ((PyObject *(*)(char *, struct _cffi_ctypedescr *))_cffi_exports[18])
  188. #define _cffi_to_c_wchar_t \
  189. ((_cffi_wchar_t(*)(PyObject *))_cffi_exports[19])
  190. #define _cffi_from_c_wchar_t \
  191. ((PyObject *(*)(_cffi_wchar_t))_cffi_exports[20])
  192. #define _cffi_to_c_long_double \
  193. ((long double(*)(PyObject *))_cffi_exports[21])
  194. #define _cffi_to_c__Bool \
  195. ((_Bool(*)(PyObject *))_cffi_exports[22])
  196. #define _cffi_prepare_pointer_call_argument \
  197. ((Py_ssize_t(*)(struct _cffi_ctypedescr *, \
  198. PyObject *, char **))_cffi_exports[23])
  199. #define _cffi_convert_array_from_object \
  200. ((int(*)(char *, struct _cffi_ctypedescr *, PyObject *))_cffi_exports[24])
  201. #define _CFFI_CPIDX 25
  202. #define _cffi_call_python \
  203. ((void(*)(struct _cffi_externpy_s *, char *))_cffi_exports[_CFFI_CPIDX])
  204. #define _cffi_to_c_wchar3216_t \
  205. ((int(*)(PyObject *))_cffi_exports[26])
  206. #define _cffi_from_c_wchar3216_t \
  207. ((PyObject *(*)(int))_cffi_exports[27])
  208. #define _CFFI_NUM_EXPORTS 28
  209. struct _cffi_ctypedescr;
  210. static void *_cffi_exports[_CFFI_NUM_EXPORTS];
  211. #define _cffi_type(index) ( \
  212. assert((((uintptr_t)_cffi_types[index]) & 1) == 0), \
  213. (struct _cffi_ctypedescr *)_cffi_types[index])
  214. static PyObject *_cffi_init(const char *module_name, Py_ssize_t version,
  215. const struct _cffi_type_context_s *ctx)
  216. {
  217. PyObject *module, *o_arg, *new_module;
  218. void *raw[] = {
  219. (void *)module_name,
  220. (void *)version,
  221. (void *)_cffi_exports,
  222. (void *)ctx,
  223. };
  224. module = PyImport_ImportModule("_cffi_backend");
  225. if (module == NULL)
  226. goto failure;
  227. o_arg = PyLong_FromVoidPtr((void *)raw);
  228. if (o_arg == NULL)
  229. goto failure;
  230. new_module = PyObject_CallMethod(
  231. module, (char *)"_init_cffi_1_0_external_module", (char *)"O", o_arg);
  232. Py_DECREF(o_arg);
  233. Py_DECREF(module);
  234. return new_module;
  235. failure:
  236. Py_XDECREF(module);
  237. return NULL;
  238. }
  239. #ifdef HAVE_WCHAR_H
  240. typedef wchar_t _cffi_wchar_t;
  241. #else
  242. typedef uint16_t _cffi_wchar_t; /* same random pick as _cffi_backend.c */
  243. #endif
  244. _CFFI_UNUSED_FN static uint16_t _cffi_to_c_char16_t(PyObject *o)
  245. {
  246. if (sizeof(_cffi_wchar_t) == 2)
  247. return (uint16_t)_cffi_to_c_wchar_t(o);
  248. else
  249. return (uint16_t)_cffi_to_c_wchar3216_t(o);
  250. }
  251. _CFFI_UNUSED_FN static PyObject *_cffi_from_c_char16_t(uint16_t x)
  252. {
  253. if (sizeof(_cffi_wchar_t) == 2)
  254. return _cffi_from_c_wchar_t((_cffi_wchar_t)x);
  255. else
  256. return _cffi_from_c_wchar3216_t((int)x);
  257. }
  258. _CFFI_UNUSED_FN static int _cffi_to_c_char32_t(PyObject *o)
  259. {
  260. if (sizeof(_cffi_wchar_t) == 4)
  261. return (int)_cffi_to_c_wchar_t(o);
  262. else
  263. return (int)_cffi_to_c_wchar3216_t(o);
  264. }
  265. _CFFI_UNUSED_FN static PyObject *_cffi_from_c_char32_t(unsigned int x)
  266. {
  267. if (sizeof(_cffi_wchar_t) == 4)
  268. return _cffi_from_c_wchar_t((_cffi_wchar_t)x);
  269. else
  270. return _cffi_from_c_wchar3216_t((int)x);
  271. }
  272. union _cffi_union_alignment_u {
  273. unsigned char m_char;
  274. unsigned short m_short;
  275. unsigned int m_int;
  276. unsigned long m_long;
  277. unsigned long long m_longlong;
  278. float m_float;
  279. double m_double;
  280. long double m_longdouble;
  281. };
  282. struct _cffi_freeme_s {
  283. struct _cffi_freeme_s *next;
  284. union _cffi_union_alignment_u alignment;
  285. };
  286. _CFFI_UNUSED_FN static int
  287. _cffi_convert_array_argument(struct _cffi_ctypedescr *ctptr, PyObject *arg,
  288. char **output_data, Py_ssize_t datasize,
  289. struct _cffi_freeme_s **freeme)
  290. {
  291. char *p;
  292. if (datasize < 0)
  293. return -1;
  294. p = *output_data;
  295. if (p == NULL) {
  296. struct _cffi_freeme_s *fp = (struct _cffi_freeme_s *)PyObject_Malloc(
  297. offsetof(struct _cffi_freeme_s, alignment) + (size_t)datasize);
  298. if (fp == NULL)
  299. return -1;
  300. fp->next = *freeme;
  301. *freeme = fp;
  302. p = *output_data = (char *)&fp->alignment;
  303. }
  304. memset((void *)p, 0, (size_t)datasize);
  305. return _cffi_convert_array_from_object(p, ctptr, arg);
  306. }
  307. _CFFI_UNUSED_FN static void
  308. _cffi_free_array_arguments(struct _cffi_freeme_s *freeme)
  309. {
  310. do {
  311. void *p = (void *)freeme;
  312. freeme = freeme->next;
  313. PyObject_Free(p);
  314. } while (freeme != NULL);
  315. }
  316. /********** end CPython-specific section **********/
  317. #else
  318. _CFFI_UNUSED_FN
  319. static void (*_cffi_call_python_org)(struct _cffi_externpy_s *, char *);
  320. # define _cffi_call_python _cffi_call_python_org
  321. #endif
  322. #define _cffi_array_len(array) (sizeof(array) / sizeof((array)[0]))
  323. #define _cffi_prim_int(size, sign) \
  324. ((size) == 1 ? ((sign) ? _CFFI_PRIM_INT8 : _CFFI_PRIM_UINT8) : \
  325. (size) == 2 ? ((sign) ? _CFFI_PRIM_INT16 : _CFFI_PRIM_UINT16) : \
  326. (size) == 4 ? ((sign) ? _CFFI_PRIM_INT32 : _CFFI_PRIM_UINT32) : \
  327. (size) == 8 ? ((sign) ? _CFFI_PRIM_INT64 : _CFFI_PRIM_UINT64) : \
  328. _CFFI__UNKNOWN_PRIM)
  329. #define _cffi_prim_float(size) \
  330. ((size) == sizeof(float) ? _CFFI_PRIM_FLOAT : \
  331. (size) == sizeof(double) ? _CFFI_PRIM_DOUBLE : \
  332. (size) == sizeof(long double) ? _CFFI__UNKNOWN_LONG_DOUBLE : \
  333. _CFFI__UNKNOWN_FLOAT_PRIM)
  334. #define _cffi_check_int(got, got_nonpos, expected) \
  335. ((got_nonpos) == (expected <= 0) && \
  336. (got) == (unsigned long long)expected)
  337. #ifdef MS_WIN32
  338. # define _cffi_stdcall __stdcall
  339. #else
  340. # define _cffi_stdcall /* nothing */
  341. #endif
  342. #ifdef __cplusplus
  343. }
  344. #endif