codekingpro/portable-devtools
114k
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 and6 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 if8 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 virtualenv17 version >= 16.0.0. With older versions of either, you don't get a18 copy of PYTHON3.DLL in the virtualenv. We can't check the version of19 CPython *before* we even include pyconfig.h. ffi.set_source() puts20 a ``#define _CFFI_NO_LIMITED_API'' at the start of this file if it is21 running on Windows < 3.5, as an attempt at fixing it, but that's22 arguably wrong because it may not be the target version of Python.23 Still better than nothing I guess. As another workaround, you can24 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_VER30# if !defined(_DEBUG) && !defined(Py_DEBUG) && !defined(Py_TRACE_REFS) && !defined(Py_REF_DEBUG) && !defined(_CFFI_NO_LIMITED_API)31# define Py_LIMITED_API32# endif33# include <pyconfig.h>34 /* sanity-check: Py_LIMITED_API will cause crashes if any of these35 are also defined. Normally, the Python file PC/pyconfig.h does not36 cause any of these to be defined, with the exception that _DEBUG37 causes Py_DEBUG. Double-check that. */38# ifdef Py_LIMITED_API39# if defined(Py_DEBUG)40# error "pyconfig.h unexpectedly defines Py_DEBUG, but Py_LIMITED_API is set"41# endif42# if defined(Py_TRACE_REFS)43# error "pyconfig.h unexpectedly defines Py_TRACE_REFS, but Py_LIMITED_API is set"44# endif45# if defined(Py_REF_DEBUG)46# error "pyconfig.h unexpectedly defines Py_REF_DEBUG, but Py_LIMITED_API is set"47# endif48# endif49# else50# 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_API53# endif54# endif55#endif56 57#include <Python.h>58#ifdef __cplusplus59extern "C" {60#endif61#include <stddef.h>62#include "parse_c_type.h"63 64/* this block of #ifs should be kept exactly identical between65 c/_cffi_backend.c, cffi/vengine_cpy.py, cffi/vengine_gen.py66 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# else97# include <stdint.h>98# endif99# if _MSC_VER < 1800 /* MSVC < 2013 */100# ifndef __cplusplus101 typedef unsigned char _Bool;102# endif103# endif104# define _cffi_float_complex_t _Fcomplex /* include <complex.h> for it */105# define _cffi_double_complex_t _Dcomplex /* include <complex.h> for it */106#else107# include <stdint.h>108# if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux)109# include <alloca.h>110# endif111# define _cffi_float_complex_t float _Complex112# define _cffi_double_complex_t double _Complex113#endif114 115#ifdef __GNUC__116# define _CFFI_UNUSED_FN __attribute__((unused))117#else118# define _CFFI_UNUSED_FN /* nothing */119#endif120 121#ifdef __cplusplus122# ifndef _Bool123 typedef bool _Bool; /* semi-hackish: C++ has no _Bool; bool is builtin */124# endif125#endif126 127/********** CPython-specific section **********/128#ifndef PYPY_VERSION129 130 131#if PY_MAJOR_VERSION >= 3132# define PyInt_FromLong PyLong_FromLong133#endif134 135#define _cffi_from_c_double PyFloat_FromDouble136#define _cffi_from_c_float PyFloat_FromDouble137#define _cffi_from_c_long PyInt_FromLong138#define _cffi_from_c_ulong PyLong_FromUnsignedLong139#define _cffi_from_c_longlong PyLong_FromLongLong140#define _cffi_from_c_ulonglong PyLong_FromUnsignedLongLong141#define _cffi_from_c__Bool PyBool_FromLong142 143#define _cffi_to_c_double PyFloat_AsDouble144#define _cffi_to_c_float PyFloat_AsDouble145 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 more193#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 25219#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 28226 227struct _cffi_ctypedescr;228 229static 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 235static 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_H268typedef wchar_t _cffi_wchar_t;269#else270typedef uint16_t _cffi_wchar_t; /* same random pick as _cffi_backend.c */271#endif272 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 else278 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 else286 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 else294 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 else302 return _cffi_from_c_wchar3216_t((int)x);303}304 305union _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 316struct _cffi_freeme_s {317 struct _cffi_freeme_s *next;318 union _cffi_union_alignment_u alignment;319};320 321_CFFI_UNUSED_FN static int322_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 void345_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#else356_CFFI_UNUSED_FN357static void (*_cffi_call_python_org)(struct _cffi_externpy_s *, char *);358# define _cffi_call_python _cffi_call_python_org359#endif360 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_WIN32382# define _cffi_stdcall __stdcall383#else384# define _cffi_stdcall /* nothing */385#endif386 387#ifdef __cplusplus388}389#endif390 