Debian Codegraph @

lib_pypy/cffi/_cffi_include.h

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