_cffi_include.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. #else
  98. # include <stdint.h>
  99. # if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux)
  100. # include <alloca.h>
  101. # endif
  102. #endif
  103. #ifdef __GNUC__
  104. # define _CFFI_UNUSED_FN __attribute__((unused))
  105. #else
  106. # define _CFFI_UNUSED_FN /* nothing */
  107. #endif
  108. #ifdef __cplusplus
  109. # ifndef _Bool
  110. typedef bool _Bool; /* semi-hackish: C++ has no _Bool; bool is builtin */
  111. # endif
  112. #endif
  113. /********** CPython-specific section **********/
  114. #ifndef PYPY_VERSION
  115. #if PY_MAJOR_VERSION >= 3
  116. # define PyInt_FromLong PyLong_FromLong
  117. #endif
  118. #define _cffi_from_c_double PyFloat_FromDouble
  119. #define _cffi_from_c_float PyFloat_FromDouble
  120. #define _cffi_from_c_long PyInt_FromLong
  121. #define _cffi_from_c_ulong PyLong_FromUnsignedLong
  122. #define _cffi_from_c_longlong PyLong_FromLongLong
  123. #define _cffi_from_c_ulonglong PyLong_FromUnsignedLongLong
  124. #define _cffi_from_c__Bool PyBool_FromLong
  125. #define _cffi_to_c_double PyFloat_AsDouble
  126. #define _cffi_to_c_float PyFloat_AsDouble
  127. #define _cffi_from_c_int(x, type) \
  128. (((type)-1) > 0 ? /* unsigned */ \
  129. (sizeof(type) < sizeof(long) ? \
  130. PyInt_FromLong((long)x) : \
  131. sizeof(type) == sizeof(long) ? \
  132. PyLong_FromUnsignedLong((unsigned long)x) : \
  133. PyLong_FromUnsignedLongLong((unsigned long long)x)) : \
  134. (sizeof(type) <= sizeof(long) ? \
  135. PyInt_FromLong((long)x) : \
  136. PyLong_FromLongLong((long long)x)))
  137. #define _cffi_to_c_int(o, type) \
  138. ((type)( \
  139. sizeof(type) == 1 ? (((type)-1) > 0 ? (type)_cffi_to_c_u8(o) \
  140. : (type)_cffi_to_c_i8(o)) : \
  141. sizeof(type) == 2 ? (((type)-1) > 0 ? (type)_cffi_to_c_u16(o) \
  142. : (type)_cffi_to_c_i16(o)) : \
  143. sizeof(type) == 4 ? (((type)-1) > 0 ? (type)_cffi_to_c_u32(o) \
  144. : (type)_cffi_to_c_i32(o)) : \
  145. sizeof(type) == 8 ? (((type)-1) > 0 ? (type)_cffi_to_c_u64(o) \
  146. : (type)_cffi_to_c_i64(o)) : \
  147. (Py_FatalError("unsupported size for type " #type), (type)0)))
  148. #define _cffi_to_c_i8 \
  149. ((int(*)(PyObject *))_cffi_exports[1])
  150. #define _cffi_to_c_u8 \
  151. ((int(*)(PyObject *))_cffi_exports[2])
  152. #define _cffi_to_c_i16 \
  153. ((int(*)(PyObject *))_cffi_exports[3])
  154. #define _cffi_to_c_u16 \
  155. ((int(*)(PyObject *))_cffi_exports[4])
  156. #define _cffi_to_c_i32 \
  157. ((int(*)(PyObject *))_cffi_exports[5])
  158. #define _cffi_to_c_u32 \
  159. ((unsigned int(*)(PyObject *))_cffi_exports[6])
  160. #define _cffi_to_c_i64 \
  161. ((long long(*)(PyObject *))_cffi_exports[7])
  162. #define _cffi_to_c_u64 \
  163. ((unsigned long long(*)(PyObject *))_cffi_exports[8])
  164. #define _cffi_to_c_char \
  165. ((int(*)(PyObject *))_cffi_exports[9])
  166. #define _cffi_from_c_pointer \
  167. ((PyObject *(*)(char *, struct _cffi_ctypedescr *))_cffi_exports[10])
  168. #define _cffi_to_c_pointer \
  169. ((char *(*)(PyObject *, struct _cffi_ctypedescr *))_cffi_exports[11])
  170. #define _cffi_get_struct_layout \
  171. not used any more
  172. #define _cffi_restore_errno \
  173. ((void(*)(void))_cffi_exports[13])
  174. #define _cffi_save_errno \
  175. ((void(*)(void))_cffi_exports[14])
  176. #define _cffi_from_c_char \
  177. ((PyObject *(*)(char))_cffi_exports[15])
  178. #define _cffi_from_c_deref \
  179. ((PyObject *(*)(char *, struct _cffi_ctypedescr *))_cffi_exports[16])
  180. #define _cffi_to_c \
  181. ((int(*)(char *, struct _cffi_ctypedescr *, PyObject *))_cffi_exports[17])
  182. #define _cffi_from_c_struct \
  183. ((PyObject *(*)(char *, struct _cffi_ctypedescr *))_cffi_exports[18])
  184. #define _cffi_to_c_wchar_t \
  185. ((_cffi_wchar_t(*)(PyObject *))_cffi_exports[19])
  186. #define _cffi_from_c_wchar_t \
  187. ((PyObject *(*)(_cffi_wchar_t))_cffi_exports[20])
  188. #define _cffi_to_c_long_double \
  189. ((long double(*)(PyObject *))_cffi_exports[21])
  190. #define _cffi_to_c__Bool \
  191. ((_Bool(*)(PyObject *))_cffi_exports[22])
  192. #define _cffi_prepare_pointer_call_argument \
  193. ((Py_ssize_t(*)(struct _cffi_ctypedescr *, \
  194. PyObject *, char **))_cffi_exports[23])
  195. #define _cffi_convert_array_from_object \
  196. ((int(*)(char *, struct _cffi_ctypedescr *, PyObject *))_cffi_exports[24])
  197. #define _CFFI_CPIDX 25
  198. #define _cffi_call_python \
  199. ((void(*)(struct _cffi_externpy_s *, char *))_cffi_exports[_CFFI_CPIDX])
  200. #define _cffi_to_c_wchar3216_t \
  201. ((int(*)(PyObject *))_cffi_exports[26])
  202. #define _cffi_from_c_wchar3216_t \
  203. ((PyObject *(*)(int))_cffi_exports[27])
  204. #define _CFFI_NUM_EXPORTS 28
  205. struct _cffi_ctypedescr;
  206. static void *_cffi_exports[_CFFI_NUM_EXPORTS];
  207. #define _cffi_type(index) ( \
  208. assert((((uintptr_t)_cffi_types[index]) & 1) == 0), \
  209. (struct _cffi_ctypedescr *)_cffi_types[index])
  210. static PyObject *_cffi_init(const char *module_name, Py_ssize_t version,
  211. const struct _cffi_type_context_s *ctx)
  212. {
  213. PyObject *module, *o_arg, *new_module;
  214. void *raw[] = {
  215. (void *)module_name,
  216. (void *)version,
  217. (void *)_cffi_exports,
  218. (void *)ctx,
  219. };
  220. module = PyImport_ImportModule("_cffi_backend");
  221. if (module == NULL)
  222. goto failure;
  223. o_arg = PyLong_FromVoidPtr((void *)raw);
  224. if (o_arg == NULL)
  225. goto failure;
  226. new_module = PyObject_CallMethod(
  227. module, (char *)"_init_cffi_1_0_external_module", (char *)"O", o_arg);
  228. Py_DECREF(o_arg);
  229. Py_DECREF(module);
  230. return new_module;
  231. failure:
  232. Py_XDECREF(module);
  233. return NULL;
  234. }
  235. #ifdef HAVE_WCHAR_H
  236. typedef wchar_t _cffi_wchar_t;
  237. #else
  238. typedef uint16_t _cffi_wchar_t; /* same random pick as _cffi_backend.c */
  239. #endif
  240. _CFFI_UNUSED_FN static uint16_t _cffi_to_c_char16_t(PyObject *o)
  241. {
  242. if (sizeof(_cffi_wchar_t) == 2)
  243. return (uint16_t)_cffi_to_c_wchar_t(o);
  244. else
  245. return (uint16_t)_cffi_to_c_wchar3216_t(o);
  246. }
  247. _CFFI_UNUSED_FN static PyObject *_cffi_from_c_char16_t(uint16_t x)
  248. {
  249. if (sizeof(_cffi_wchar_t) == 2)
  250. return _cffi_from_c_wchar_t((_cffi_wchar_t)x);
  251. else
  252. return _cffi_from_c_wchar3216_t((int)x);
  253. }
  254. _CFFI_UNUSED_FN static int _cffi_to_c_char32_t(PyObject *o)
  255. {
  256. if (sizeof(_cffi_wchar_t) == 4)
  257. return (int)_cffi_to_c_wchar_t(o);
  258. else
  259. return (int)_cffi_to_c_wchar3216_t(o);
  260. }
  261. _CFFI_UNUSED_FN static PyObject *_cffi_from_c_char32_t(unsigned int x)
  262. {
  263. if (sizeof(_cffi_wchar_t) == 4)
  264. return _cffi_from_c_wchar_t((_cffi_wchar_t)x);
  265. else
  266. return _cffi_from_c_wchar3216_t((int)x);
  267. }
  268. union _cffi_union_alignment_u {
  269. unsigned char m_char;
  270. unsigned short m_short;
  271. unsigned int m_int;
  272. unsigned long m_long;
  273. unsigned long long m_longlong;
  274. float m_float;
  275. double m_double;
  276. long double m_longdouble;
  277. };
  278. struct _cffi_freeme_s {
  279. struct _cffi_freeme_s *next;
  280. union _cffi_union_alignment_u alignment;
  281. };
  282. _CFFI_UNUSED_FN static int
  283. _cffi_convert_array_argument(struct _cffi_ctypedescr *ctptr, PyObject *arg,
  284. char **output_data, Py_ssize_t datasize,
  285. struct _cffi_freeme_s **freeme)
  286. {
  287. char *p;
  288. if (datasize < 0)
  289. return -1;
  290. p = *output_data;
  291. if (p == NULL) {
  292. struct _cffi_freeme_s *fp = (struct _cffi_freeme_s *)PyObject_Malloc(
  293. offsetof(struct _cffi_freeme_s, alignment) + (size_t)datasize);
  294. if (fp == NULL)
  295. return -1;
  296. fp->next = *freeme;
  297. *freeme = fp;
  298. p = *output_data = (char *)&fp->alignment;
  299. }
  300. memset((void *)p, 0, (size_t)datasize);
  301. return _cffi_convert_array_from_object(p, ctptr, arg);
  302. }
  303. _CFFI_UNUSED_FN static void
  304. _cffi_free_array_arguments(struct _cffi_freeme_s *freeme)
  305. {
  306. do {
  307. void *p = (void *)freeme;
  308. freeme = freeme->next;
  309. PyObject_Free(p);
  310. } while (freeme != NULL);
  311. }
  312. /********** end CPython-specific section **********/
  313. #else
  314. _CFFI_UNUSED_FN
  315. static void (*_cffi_call_python_org)(struct _cffi_externpy_s *, char *);
  316. # define _cffi_call_python _cffi_call_python_org
  317. #endif
  318. #define _cffi_array_len(array) (sizeof(array) / sizeof((array)[0]))
  319. #define _cffi_prim_int(size, sign) \
  320. ((size) == 1 ? ((sign) ? _CFFI_PRIM_INT8 : _CFFI_PRIM_UINT8) : \
  321. (size) == 2 ? ((sign) ? _CFFI_PRIM_INT16 : _CFFI_PRIM_UINT16) : \
  322. (size) == 4 ? ((sign) ? _CFFI_PRIM_INT32 : _CFFI_PRIM_UINT32) : \
  323. (size) == 8 ? ((sign) ? _CFFI_PRIM_INT64 : _CFFI_PRIM_UINT64) : \
  324. _CFFI__UNKNOWN_PRIM)
  325. #define _cffi_prim_float(size) \
  326. ((size) == sizeof(float) ? _CFFI_PRIM_FLOAT : \
  327. (size) == sizeof(double) ? _CFFI_PRIM_DOUBLE : \
  328. (size) == sizeof(long double) ? _CFFI__UNKNOWN_LONG_DOUBLE : \
  329. _CFFI__UNKNOWN_FLOAT_PRIM)
  330. #define _cffi_check_int(got, got_nonpos, expected) \
  331. ((got_nonpos) == (expected <= 0) && \
  332. (got) == (unsigned long long)expected)
  333. #ifdef MS_WIN32
  334. # define _cffi_stdcall __stdcall
  335. #else
  336. # define _cffi_stdcall /* nothing */
  337. #endif
  338. #ifdef __cplusplus
  339. }
  340. #endif