11#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+
228#include <Python.h>
329#ifdef __cplusplus
430extern "C" {
@@ -42,7 +68,9 @@ extern "C" {
4268# include <stdint.h>
4369# endif
4470# if _MSC_VER < 1800 /* MSVC < 2013 */
45- typedef unsigned char _Bool ;
71+ # ifndef __cplusplus
72+ typedef unsigned char _Bool ;
73+ # endif
4674# endif
4775#else
4876# include <stdint.h>
@@ -59,7 +87,7 @@ extern "C" {
5987
6088#ifdef __cplusplus
6189# ifndef _Bool
62- # define _Bool bool /* semi-hackish: C++ has no _Bool; bool is builtin */
90+ typedef bool _Bool ; /* semi-hackish: C++ has no _Bool; bool is builtin */
6391# endif
6492#endif
6593
@@ -77,6 +105,7 @@ extern "C" {
77105#define _cffi_from_c_ulong PyLong_FromUnsignedLong
78106#define _cffi_from_c_longlong PyLong_FromLongLong
79107#define _cffi_from_c_ulonglong PyLong_FromUnsignedLongLong
108+ #define _cffi_from_c__Bool PyBool_FromLong
80109
81110#define _cffi_to_c_double PyFloat_AsDouble
82111#define _cffi_to_c_float PyFloat_AsDouble
@@ -123,9 +152,9 @@ extern "C" {
123152#define _cffi_to_c_char \
124153 ((int(*)(PyObject *))_cffi_exports[9])
125154#define _cffi_from_c_pointer \
126- ((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[10])
155+ ((PyObject *(*)(char *, struct _cffi_ctypedescr *))_cffi_exports[10])
127156#define _cffi_to_c_pointer \
128- ((char *(*)(PyObject *, CTypeDescrObject *))_cffi_exports[11])
157+ ((char *(*)(PyObject *, struct _cffi_ctypedescr *))_cffi_exports[11])
129158#define _cffi_get_struct_layout \
130159 not used any more
131160#define _cffi_restore_errno \
@@ -135,35 +164,40 @@ extern "C" {
135164#define _cffi_from_c_char \
136165 ((PyObject *(*)(char))_cffi_exports[15])
137166#define _cffi_from_c_deref \
138- ((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[16])
167+ ((PyObject *(*)(char *, struct _cffi_ctypedescr *))_cffi_exports[16])
139168#define _cffi_to_c \
140- ((int(*)(char *, CTypeDescrObject *, PyObject *))_cffi_exports[17])
169+ ((int(*)(char *, struct _cffi_ctypedescr *, PyObject *))_cffi_exports[17])
141170#define _cffi_from_c_struct \
142- ((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[18])
171+ ((PyObject *(*)(char *, struct _cffi_ctypedescr *))_cffi_exports[18])
143172#define _cffi_to_c_wchar_t \
144- ((wchar_t (*)(PyObject *))_cffi_exports[19])
173+ ((_cffi_wchar_t (*)(PyObject *))_cffi_exports[19])
145174#define _cffi_from_c_wchar_t \
146- ((PyObject *(*)(wchar_t ))_cffi_exports[20])
175+ ((PyObject *(*)(_cffi_wchar_t ))_cffi_exports[20])
147176#define _cffi_to_c_long_double \
148177 ((long double(*)(PyObject *))_cffi_exports[21])
149178#define _cffi_to_c__Bool \
150179 ((_Bool(*)(PyObject *))_cffi_exports[22])
151180#define _cffi_prepare_pointer_call_argument \
152- ((Py_ssize_t(*)(CTypeDescrObject *, PyObject *, char **))_cffi_exports[23])
181+ ((Py_ssize_t(*)(struct _cffi_ctypedescr *, \
182+ PyObject *, char **))_cffi_exports[23])
153183#define _cffi_convert_array_from_object \
154- ((int(*)(char *, CTypeDescrObject *, PyObject *))_cffi_exports[24])
184+ ((int(*)(char *, struct _cffi_ctypedescr *, PyObject *))_cffi_exports[24])
155185#define _CFFI_CPIDX 25
156186#define _cffi_call_python \
157187 ((void(*)(struct _cffi_externpy_s *, char *))_cffi_exports[_CFFI_CPIDX])
158- #define _CFFI_NUM_EXPORTS 26
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
159193
160- typedef struct _ctypedescr CTypeDescrObject ;
194+ struct _cffi_ctypedescr ;
161195
162196static void * _cffi_exports [_CFFI_NUM_EXPORTS ];
163197
164198#define _cffi_type (index ) ( \
165199 assert((((uintptr_t)_cffi_types[index]) & 1) == 0), \
166- (CTypeDescrObject *)_cffi_types[index])
200+ (struct _cffi_ctypedescr *)_cffi_types[index])
167201
168202static PyObject * _cffi_init (const char * module_name , Py_ssize_t version ,
169203 const struct _cffi_type_context_s * ctx )
@@ -196,20 +230,46 @@ static PyObject *_cffi_init(const char *module_name, Py_ssize_t version,
196230 return NULL ;
197231}
198232
199- _CFFI_UNUSED_FN
200- static PyObject * * _cffi_unpack_args (PyObject * args_tuple , Py_ssize_t expected ,
201- const char * fnname )
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+
240+ _CFFI_UNUSED_FN static uint16_t _cffi_to_c_char16_t (PyObject * o )
202241{
203- if (PyTuple_GET_SIZE (args_tuple ) != expected ) {
204- PyErr_Format (PyExc_TypeError ,
205- "%.150s() takes exactly %zd arguments (%zd given)" ,
206- fnname , expected , PyTuple_GET_SIZE (args_tuple ));
207- return NULL ;
208- }
209- return & PyTuple_GET_ITEM (args_tuple , 0 ); /* pointer to the first item,
210- the others follow */
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 );
211246}
212247
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+
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+
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+
213273/********** end CPython-specific section **********/
214274#else
215275_CFFI_UNUSED_FN
0 commit comments