Debian Codegraph @

src/cffi/_cffi_include.h

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