25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

309 lines
12 KiB

  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. Issue #350 is still open: on Windows, the code here causes it to link
  9. with PYTHON36.DLL (for example) instead of PYTHON3.DLL. A fix was
  10. attempted in 164e526a5515 and 14ce6985e1c3, but reverted: virtualenv
  11. does not make PYTHON3.DLL available, and so the "correctly" compiled
  12. version would not run inside a virtualenv. We will re-apply the fix
  13. after virtualenv has been fixed for some time. For explanation, see
  14. issue #355. For a workaround if you want PYTHON3.DLL and don't worry
  15. about virtualenv, see issue #350. See also 'py_limited_api' in
  16. setuptools_ext.py.
  17. */
  18. #if !defined(_CFFI_USE_EMBEDDING) && !defined(Py_LIMITED_API)
  19. # include <pyconfig.h>
  20. # if !defined(Py_DEBUG) && !defined(Py_TRACE_REFS) && !defined(Py_REF_DEBUG)
  21. # define Py_LIMITED_API
  22. # endif
  23. #endif
  24. #include <Python.h>
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. #include <stddef.h>
  29. #include "parse_c_type.h"
  30. /* this block of #ifs should be kept exactly identical between
  31. c/_cffi_backend.c, cffi/vengine_cpy.py, cffi/vengine_gen.py
  32. and cffi/_cffi_include.h */
  33. #if defined(_MSC_VER)
  34. # include <malloc.h> /* for alloca() */
  35. # if _MSC_VER < 1600 /* MSVC < 2010 */
  36. typedef __int8 int8_t;
  37. typedef __int16 int16_t;
  38. typedef __int32 int32_t;
  39. typedef __int64 int64_t;
  40. typedef unsigned __int8 uint8_t;
  41. typedef unsigned __int16 uint16_t;
  42. typedef unsigned __int32 uint32_t;
  43. typedef unsigned __int64 uint64_t;
  44. typedef __int8 int_least8_t;
  45. typedef __int16 int_least16_t;
  46. typedef __int32 int_least32_t;
  47. typedef __int64 int_least64_t;
  48. typedef unsigned __int8 uint_least8_t;
  49. typedef unsigned __int16 uint_least16_t;
  50. typedef unsigned __int32 uint_least32_t;
  51. typedef unsigned __int64 uint_least64_t;
  52. typedef __int8 int_fast8_t;
  53. typedef __int16 int_fast16_t;
  54. typedef __int32 int_fast32_t;
  55. typedef __int64 int_fast64_t;
  56. typedef unsigned __int8 uint_fast8_t;
  57. typedef unsigned __int16 uint_fast16_t;
  58. typedef unsigned __int32 uint_fast32_t;
  59. typedef unsigned __int64 uint_fast64_t;
  60. typedef __int64 intmax_t;
  61. typedef unsigned __int64 uintmax_t;
  62. # else
  63. # include <stdint.h>
  64. # endif
  65. # if _MSC_VER < 1800 /* MSVC < 2013 */
  66. # ifndef __cplusplus
  67. typedef unsigned char _Bool;
  68. # endif
  69. # endif
  70. #else
  71. # include <stdint.h>
  72. # if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux)
  73. # include <alloca.h>
  74. # endif
  75. #endif
  76. #ifdef __GNUC__
  77. # define _CFFI_UNUSED_FN __attribute__((unused))
  78. #else
  79. # define _CFFI_UNUSED_FN /* nothing */
  80. #endif
  81. #ifdef __cplusplus
  82. # ifndef _Bool
  83. typedef bool _Bool; /* semi-hackish: C++ has no _Bool; bool is builtin */
  84. # endif
  85. #endif
  86. /********** CPython-specific section **********/
  87. #ifndef PYPY_VERSION
  88. #if PY_MAJOR_VERSION >= 3
  89. # define PyInt_FromLong PyLong_FromLong
  90. #endif
  91. #define _cffi_from_c_double PyFloat_FromDouble
  92. #define _cffi_from_c_float PyFloat_FromDouble
  93. #define _cffi_from_c_long PyInt_FromLong
  94. #define _cffi_from_c_ulong PyLong_FromUnsignedLong
  95. #define _cffi_from_c_longlong PyLong_FromLongLong
  96. #define _cffi_from_c_ulonglong PyLong_FromUnsignedLongLong
  97. #define _cffi_from_c__Bool PyBool_FromLong
  98. #define _cffi_to_c_double PyFloat_AsDouble
  99. #define _cffi_to_c_float PyFloat_AsDouble
  100. #define _cffi_from_c_int(x, type) \
  101. (((type)-1) > 0 ? /* unsigned */ \
  102. (sizeof(type) < sizeof(long) ? \
  103. PyInt_FromLong((long)x) : \
  104. sizeof(type) == sizeof(long) ? \
  105. PyLong_FromUnsignedLong((unsigned long)x) : \
  106. PyLong_FromUnsignedLongLong((unsigned long long)x)) : \
  107. (sizeof(type) <= sizeof(long) ? \
  108. PyInt_FromLong((long)x) : \
  109. PyLong_FromLongLong((long long)x)))
  110. #define _cffi_to_c_int(o, type) \
  111. ((type)( \
  112. sizeof(type) == 1 ? (((type)-1) > 0 ? (type)_cffi_to_c_u8(o) \
  113. : (type)_cffi_to_c_i8(o)) : \
  114. sizeof(type) == 2 ? (((type)-1) > 0 ? (type)_cffi_to_c_u16(o) \
  115. : (type)_cffi_to_c_i16(o)) : \
  116. sizeof(type) == 4 ? (((type)-1) > 0 ? (type)_cffi_to_c_u32(o) \
  117. : (type)_cffi_to_c_i32(o)) : \
  118. sizeof(type) == 8 ? (((type)-1) > 0 ? (type)_cffi_to_c_u64(o) \
  119. : (type)_cffi_to_c_i64(o)) : \
  120. (Py_FatalError("unsupported size for type " #type), (type)0)))
  121. #define _cffi_to_c_i8 \
  122. ((int(*)(PyObject *))_cffi_exports[1])
  123. #define _cffi_to_c_u8 \
  124. ((int(*)(PyObject *))_cffi_exports[2])
  125. #define _cffi_to_c_i16 \
  126. ((int(*)(PyObject *))_cffi_exports[3])
  127. #define _cffi_to_c_u16 \
  128. ((int(*)(PyObject *))_cffi_exports[4])
  129. #define _cffi_to_c_i32 \
  130. ((int(*)(PyObject *))_cffi_exports[5])
  131. #define _cffi_to_c_u32 \
  132. ((unsigned int(*)(PyObject *))_cffi_exports[6])
  133. #define _cffi_to_c_i64 \
  134. ((long long(*)(PyObject *))_cffi_exports[7])
  135. #define _cffi_to_c_u64 \
  136. ((unsigned long long(*)(PyObject *))_cffi_exports[8])
  137. #define _cffi_to_c_char \
  138. ((int(*)(PyObject *))_cffi_exports[9])
  139. #define _cffi_from_c_pointer \
  140. ((PyObject *(*)(char *, struct _cffi_ctypedescr *))_cffi_exports[10])
  141. #define _cffi_to_c_pointer \
  142. ((char *(*)(PyObject *, struct _cffi_ctypedescr *))_cffi_exports[11])
  143. #define _cffi_get_struct_layout \
  144. not used any more
  145. #define _cffi_restore_errno \
  146. ((void(*)(void))_cffi_exports[13])
  147. #define _cffi_save_errno \
  148. ((void(*)(void))_cffi_exports[14])
  149. #define _cffi_from_c_char \
  150. ((PyObject *(*)(char))_cffi_exports[15])
  151. #define _cffi_from_c_deref \
  152. ((PyObject *(*)(char *, struct _cffi_ctypedescr *))_cffi_exports[16])
  153. #define _cffi_to_c \
  154. ((int(*)(char *, struct _cffi_ctypedescr *, PyObject *))_cffi_exports[17])
  155. #define _cffi_from_c_struct \
  156. ((PyObject *(*)(char *, struct _cffi_ctypedescr *))_cffi_exports[18])
  157. #define _cffi_to_c_wchar_t \
  158. ((_cffi_wchar_t(*)(PyObject *))_cffi_exports[19])
  159. #define _cffi_from_c_wchar_t \
  160. ((PyObject *(*)(_cffi_wchar_t))_cffi_exports[20])
  161. #define _cffi_to_c_long_double \
  162. ((long double(*)(PyObject *))_cffi_exports[21])
  163. #define _cffi_to_c__Bool \
  164. ((_Bool(*)(PyObject *))_cffi_exports[22])
  165. #define _cffi_prepare_pointer_call_argument \
  166. ((Py_ssize_t(*)(struct _cffi_ctypedescr *, \
  167. PyObject *, char **))_cffi_exports[23])
  168. #define _cffi_convert_array_from_object \
  169. ((int(*)(char *, struct _cffi_ctypedescr *, PyObject *))_cffi_exports[24])
  170. #define _CFFI_CPIDX 25
  171. #define _cffi_call_python \
  172. ((void(*)(struct _cffi_externpy_s *, char *))_cffi_exports[_CFFI_CPIDX])
  173. #define _cffi_to_c_wchar3216_t \
  174. ((int(*)(PyObject *))_cffi_exports[26])
  175. #define _cffi_from_c_wchar3216_t \
  176. ((PyObject *(*)(int))_cffi_exports[27])
  177. #define _CFFI_NUM_EXPORTS 28
  178. struct _cffi_ctypedescr;
  179. static void *_cffi_exports[_CFFI_NUM_EXPORTS];
  180. #define _cffi_type(index) ( \
  181. assert((((uintptr_t)_cffi_types[index]) & 1) == 0), \
  182. (struct _cffi_ctypedescr *)_cffi_types[index])
  183. static PyObject *_cffi_init(const char *module_name, Py_ssize_t version,
  184. const struct _cffi_type_context_s *ctx)
  185. {
  186. PyObject *module, *o_arg, *new_module;
  187. void *raw[] = {
  188. (void *)module_name,
  189. (void *)version,
  190. (void *)_cffi_exports,
  191. (void *)ctx,
  192. };
  193. module = PyImport_ImportModule("_cffi_backend");
  194. if (module == NULL)
  195. goto failure;
  196. o_arg = PyLong_FromVoidPtr((void *)raw);
  197. if (o_arg == NULL)
  198. goto failure;
  199. new_module = PyObject_CallMethod(
  200. module, (char *)"_init_cffi_1_0_external_module", (char *)"O", o_arg);
  201. Py_DECREF(o_arg);
  202. Py_DECREF(module);
  203. return new_module;
  204. failure:
  205. Py_XDECREF(module);
  206. return NULL;
  207. }
  208. #ifdef HAVE_WCHAR_H
  209. typedef wchar_t _cffi_wchar_t;
  210. #else
  211. typedef uint16_t _cffi_wchar_t; /* same random pick as _cffi_backend.c */
  212. #endif
  213. _CFFI_UNUSED_FN static uint16_t _cffi_to_c_char16_t(PyObject *o)
  214. {
  215. if (sizeof(_cffi_wchar_t) == 2)
  216. return (uint16_t)_cffi_to_c_wchar_t(o);
  217. else
  218. return (uint16_t)_cffi_to_c_wchar3216_t(o);
  219. }
  220. _CFFI_UNUSED_FN static PyObject *_cffi_from_c_char16_t(uint16_t x)
  221. {
  222. if (sizeof(_cffi_wchar_t) == 2)
  223. return _cffi_from_c_wchar_t((_cffi_wchar_t)x);
  224. else
  225. return _cffi_from_c_wchar3216_t((int)x);
  226. }
  227. _CFFI_UNUSED_FN static int _cffi_to_c_char32_t(PyObject *o)
  228. {
  229. if (sizeof(_cffi_wchar_t) == 4)
  230. return (int)_cffi_to_c_wchar_t(o);
  231. else
  232. return (int)_cffi_to_c_wchar3216_t(o);
  233. }
  234. _CFFI_UNUSED_FN static PyObject *_cffi_from_c_char32_t(int x)
  235. {
  236. if (sizeof(_cffi_wchar_t) == 4)
  237. return _cffi_from_c_wchar_t((_cffi_wchar_t)x);
  238. else
  239. return _cffi_from_c_wchar3216_t(x);
  240. }
  241. /********** end CPython-specific section **********/
  242. #else
  243. _CFFI_UNUSED_FN
  244. static void (*_cffi_call_python_org)(struct _cffi_externpy_s *, char *);
  245. # define _cffi_call_python _cffi_call_python_org
  246. #endif
  247. #define _cffi_array_len(array) (sizeof(array) / sizeof((array)[0]))
  248. #define _cffi_prim_int(size, sign) \
  249. ((size) == 1 ? ((sign) ? _CFFI_PRIM_INT8 : _CFFI_PRIM_UINT8) : \
  250. (size) == 2 ? ((sign) ? _CFFI_PRIM_INT16 : _CFFI_PRIM_UINT16) : \
  251. (size) == 4 ? ((sign) ? _CFFI_PRIM_INT32 : _CFFI_PRIM_UINT32) : \
  252. (size) == 8 ? ((sign) ? _CFFI_PRIM_INT64 : _CFFI_PRIM_UINT64) : \
  253. _CFFI__UNKNOWN_PRIM)
  254. #define _cffi_prim_float(size) \
  255. ((size) == sizeof(float) ? _CFFI_PRIM_FLOAT : \
  256. (size) == sizeof(double) ? _CFFI_PRIM_DOUBLE : \
  257. (size) == sizeof(long double) ? _CFFI__UNKNOWN_LONG_DOUBLE : \
  258. _CFFI__UNKNOWN_FLOAT_PRIM)
  259. #define _cffi_check_int(got, got_nonpos, expected) \
  260. ((got_nonpos) == (expected <= 0) && \
  261. (got) == (unsigned long long)expected)
  262. #ifdef MS_WIN32
  263. # define _cffi_stdcall __stdcall
  264. #else
  265. # define _cffi_stdcall /* nothing */
  266. #endif
  267. #ifdef __cplusplus
  268. }
  269. #endif