Team Ai
Datasetpublic

codekingpro/portable-devtools

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