9
9
// Declares functions usded from the Python C API
10
10
#ifndef CLASSDESC_PYTHON_CAPI_H
11
11
#define CLASSDESC_PYTHON_CAPI_H
12
+ #include < cstddef>
13
+ #include < stdio.h>
12
14
13
15
// TODO - is this field used on Windows?
14
16
// #define _PyObject_HEAD_EXTRA
15
17
16
18
#define PYTHON_API_VERSION 1013
19
+
20
+ // Windows uses stable API, so the *Struct symbols are not available
21
+ #ifdef _WIN32
22
+ #define Py_False Py_GetConstantBorrowed (1 )
23
+ #define Py_True Py_GetConstantBorrowed (2 )
24
+ #define Py_None Py_GetConstantBorrowed (0 )
25
+ #else
17
26
#define Py_False ((PyObject *) &_Py_FalseStruct)
18
27
#define Py_True ((PyObject *) &_Py_TrueStruct)
19
28
#define Py_None ((PyObject *) &_Py_NoneStruct)
29
+ #endif
20
30
#define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True
21
31
#define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False
22
32
#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
33
+
23
34
#define METH_VARARGS 0x0001
24
35
#define METH_NOARGS 0x0004
25
36
#define METH_O 0x0008
29
40
#define Py_TYPE (ob ) (((PyObject*)(ob))->ob_type)
30
41
#define PyObject_TypeCheck (ob, tp ) \
31
42
(Py_TYPE(ob) == (tp) || PyType_IsSubtype(Py_TYPE(ob), (tp)))
32
- #define PyBool_Check (x ) (Py_TYPE(x) == &PyBool_Type)
43
+ #define PyBool_Check (x ) (Py_TYPE(x) == PyBool_Type)
44
+ #define PyFloat_Check (op ) PyObject_TypeCheck(op, PyFloat_Type)
33
45
#define PyLong_Check (op ) PyType_FastSubclass(Py_TYPE(op), 1UL << 24 )
34
- #define PyFloat_Check (op ) PyObject_TypeCheck(op, &PyFloat_Type)
35
- #define PyUnicode_Check (op ) PyType_FastSubclass(Py_TYPE(op), 1UL << 28)
36
46
#define PyList_Check (op ) PyType_FastSubclass(Py_TYPE(op), 1UL << 25 )
47
+ #define PyUnicode_Check (op ) PyType_FastSubclass(Py_TYPE(op), 1UL << 28 )
37
48
38
49
#define Py_REFCNT (ob ) (((PyObject*)(ob))->ob_refcnt)
39
50
#define Py_INCREF (op ) (((PyObject *)(op))->ob_refcnt++)
77
88
#define PyModule_Create (module ) \
78
89
PyModule_Create2 (module , PYTHON_API_VERSION)
79
90
91
+ // Windows in particular doesn't define ssize_t
92
+ using Py_ssize_t=std::ptrdiff_t;
93
+
80
94
struct PyTypeObject ;
81
95
82
96
struct PyObject
83
97
{
84
98
// _PyObject_HEAD_EXTRA
85
- ssize_t ob_refcnt ;
99
+ Py_ssize_t ob_refcnt;
86
100
PyTypeObject *ob_type;
87
101
};
88
102
89
103
struct PyVarObject
90
104
{
91
105
PyObject ob_base;
92
- ssize_t ob_size ; /* Number of items in variable part */
106
+ Py_ssize_t ob_size; /* Number of items in variable part */
93
107
};
94
108
95
109
typedef PyObject * (*binaryfunc)(PyObject *, PyObject *);
96
110
typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
97
- typedef ssize_t (* lenfunc )(PyObject * );
111
+ typedef Py_ssize_t (*lenfunc)(PyObject *);
98
112
typedef int (*objobjargproc)(PyObject *, PyObject *, PyObject *);
99
113
typedef void (*freefunc)(void *);
100
114
typedef void (*destructor)(PyObject *);
@@ -104,15 +118,15 @@ typedef PyObject *(*getattrofunc)(PyObject *, PyObject *);
104
118
typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
105
119
typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
106
120
typedef PyObject *(*reprfunc)(PyObject *);
107
- typedef ssize_t (* hashfunc )(PyObject * );
121
+ typedef Py_ssize_t (*hashfunc)(PyObject *);
108
122
typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int );
109
123
typedef PyObject *(*getiterfunc) (PyObject *);
110
124
typedef PyObject *(*iternextfunc) (PyObject *);
111
125
typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *);
112
126
typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *);
113
127
typedef int (*initproc)(PyObject *, PyObject *, PyObject *);
114
128
typedef PyObject *(*newfunc)(struct _typeobject *, PyObject *, PyObject *);
115
- typedef PyObject * (* allocfunc )(struct _typeobject * , ssize_t );
129
+ typedef PyObject *(*allocfunc)(struct _typeobject *, Py_ssize_t );
116
130
typedef int (*visitproc)(PyObject *, void *);
117
131
typedef int (*traverseproc)(PyObject *, visitproc, void *);
118
132
typedef int (*inquiry)(PyObject *);
@@ -129,7 +143,7 @@ struct PyTypeObject
129
143
{
130
144
PyVarObject ob_base;
131
145
const char *tp_name; /* For printing, in format "<module>.<name>" */
132
- ssize_t tp_basicsize , tp_itemsize ; /* For allocation */
146
+ Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */
133
147
134
148
/* Methods to implement standard operations */
135
149
@@ -175,7 +189,7 @@ struct PyTypeObject
175
189
richcmpfunc tp_richcompare;
176
190
177
191
/* weak reference enabler */
178
- ssize_t tp_weaklistoffset ;
192
+ Py_ssize_t tp_weaklistoffset;
179
193
180
194
/* Iterators */
181
195
getiterfunc tp_iter;
@@ -189,7 +203,7 @@ struct PyTypeObject
189
203
PyObject *tp_dict;
190
204
descrgetfunc tp_descr_get;
191
205
descrsetfunc tp_descr_set;
192
- ssize_t tp_dictoffset ;
206
+ Py_ssize_t tp_dictoffset;
193
207
initproc tp_init;
194
208
allocfunc tp_alloc;
195
209
newfunc tp_new;
@@ -226,36 +240,40 @@ struct PyMappingMethods {
226
240
struct PyModuleDef_Base {
227
241
PyObject ob_base;
228
242
PyObject* (*m_init)(void );
229
- ssize_t m_index ;
243
+ Py_ssize_t m_index;
230
244
PyObject* m_copy;
231
245
};
232
246
233
247
struct PyModuleDef {
234
248
PyModuleDef_Base m_base;
235
249
const char * m_name;
236
250
const char * m_doc;
237
- ssize_t m_size ;
251
+ Py_ssize_t m_size;
238
252
PyMethodDef *m_methods;
239
253
struct PyModuleDef_Slot * m_slots;
240
254
traverseproc m_traverse;
241
255
inquiry m_clear;
242
256
freefunc m_free;
243
257
};
244
258
259
+ #ifndef _WIN32
245
260
extern PyObject _Py_FalseStruct, _Py_TrueStruct, _Py_NoneStruct;
246
- extern PyTypeObject PyBool_Type , PyFloat_Type ;
247
- extern PyObject * PyExc_RuntimeError ;
248
-
249
- using Py_ssize_t = ssize_t ;
261
+ #endif
250
262
251
263
extern " C" {
252
264
void _Py_Dealloc (PyObject*);
253
265
PyObject* PyErr_Occurred ();
254
266
void PyErr_Print ();
255
267
void PyErr_SetString (PyObject*,const char *);
268
+
269
+
270
+ extern PyTypeObject* PyBool_Type;
271
+ extern PyTypeObject* PyFloat_Type;
272
+ extern PyObject* PyExc_RuntimeError;
256
273
257
274
int PyType_IsSubtype (PyTypeObject*, PyTypeObject*);
258
275
unsigned long PyType_GetFlags (PyTypeObject*);
276
+ PyObject* Py_GetConstantBorrowed (unsigned );
259
277
PyObject* PyLong_FromLong (long );
260
278
PyObject* PyFloat_FromDouble (double );
261
279
long long PyLong_AsLongLong (PyObject*);
@@ -276,7 +294,7 @@ extern "C" {
276
294
PyObject* PySequence_GetItem (PyObject*, ssize_t i);
277
295
278
296
PyObject* PyUnicode_FromString (const char *);
279
- char * PyUnicode_AsUTF8 (PyObject * );
297
+ char * PyUnicode_AsUTF8AndSize (PyObject*,Py_ssize_t *);
280
298
281
299
PyObject* PyDict_New ();
282
300
int PyDict_SetItemString (PyObject* dp, const char * key, PyObject* item);
0 commit comments