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