_cffi_include.h 15 KB

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