Classdesc 3.44
pythonCAPI.h
1/*
2 @copyright Russell Standish 2025
3 @author Russell Standish
4 This file is part of Classdesc
5
6 Open source licensed under the MIT license. See LICENSE for details.
7*/
8
9// Declares functions usded from the Python C API
10#ifndef CLASSDESC_PYTHON_CAPI_H
11#define CLASSDESC_PYTHON_CAPI_H
12#include <cstddef>
13#include <stdio.h>
14
15// TODO - is this field used on Windows?
16//#define _PyObject_HEAD_EXTRA
17
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
26#define Py_False ((PyObject *) &_Py_FalseStruct)
27#define Py_True ((PyObject *) &_Py_TrueStruct)
28#define Py_None ((PyObject *) &_Py_NoneStruct)
29#endif
30#define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True
31#define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False
32#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
33
34#define METH_VARARGS 0x0001
35#define METH_NOARGS 0x0004
36#define METH_O 0x0008
37
38#define PyType_FastSubclass(t,f) ((PyType_GetFlags(t) & (f)) != 0)
39
40#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
41#define PyObject_TypeCheck(ob, tp) \
42 (Py_TYPE(ob) == (tp) || PyType_IsSubtype(Py_TYPE(ob), (tp)))
43#define PyBool_Check(x) (Py_TYPE(x) == PyBool_Type)
44#define PyFloat_Check(op) PyObject_TypeCheck(op, PyFloat_Type)
45#define PyLong_Check(op) PyType_FastSubclass(Py_TYPE(op), 1UL << 24)
46#define PyList_Check(op) PyType_FastSubclass(Py_TYPE(op), 1UL << 25)
47#define PyUnicode_Check(op) PyType_FastSubclass(Py_TYPE(op), 1UL << 28)
48
49#define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt)
50#define Py_INCREF(op) (((PyObject *)(op))->ob_refcnt++)
51#define Py_DECREF(op) \
52 do { \
53 PyObject *_py_decref_tmp = (PyObject *)(op); \
54 if (--(_py_decref_tmp)->ob_refcnt == 0) \
55 _Py_Dealloc(_py_decref_tmp); \
56 } while (0)
57
58/* Macros to use in case the object pointer may be NULL: */
59#define Py_XINCREF(op) \
60 do { \
61 PyObject *_py_xincref_tmp = (PyObject *)(op); \
62 if (_py_xincref_tmp != NULL) \
63 Py_INCREF(_py_xincref_tmp); \
64 } while (0)
65
66#define Py_XDECREF(op) \
67 do { \
68 PyObject* _py_xdecref_tmp = (PyObject *)(op); \
69 if (_py_xdecref_tmp != NULL) \
70 Py_DECREF(_py_xdecref_tmp); \
71 } while (0)
72
73#ifdef _WIN32
74#define PyMODINIT_FUNC extern "C" __declspec(dllexport) PyObject*
75#else
76#define PyMODINIT_FUNC extern "C" PyObject*
77#endif
78
79#define PyObject_HEAD_INIT(type) {1, type },
80
81#define PyModuleDef_HEAD_INIT { \
82 PyObject_HEAD_INIT(NULL) \
83 NULL, /* m_init */ \
84 0, /* m_index */ \
85 NULL, /* m_copy */ \
86 }
87
88#define PyModule_Create(module) \
89 PyModule_Create2(module, PYTHON_API_VERSION)
90
91// Windows in particular doesn't define ssize_t
92using Py_ssize_t=std::ptrdiff_t;
93
94struct PyTypeObject;
95
97{
98 //_PyObject_HEAD_EXTRA
99 Py_ssize_t ob_refcnt;
100 PyTypeObject *ob_type;
101};
102
104{
105 PyObject ob_base;
106 Py_ssize_t ob_size; /* Number of items in variable part */
107};
108
109typedef PyObject * (*binaryfunc)(PyObject *, PyObject *);
110typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
111typedef Py_ssize_t (*lenfunc)(PyObject *);
112typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
113typedef void (*freefunc)(void *);
114typedef void (*destructor)(PyObject *);
115typedef int (*printfunc)(PyObject *, FILE *, int);
116typedef PyObject *(*getattrfunc)(PyObject *, char *);
117typedef PyObject *(*getattrofunc)(PyObject *, PyObject *);
118typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
119typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
120typedef PyObject *(*reprfunc)(PyObject *);
121typedef Py_ssize_t (*hashfunc)(PyObject *);
122typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
123typedef PyObject *(*getiterfunc) (PyObject *);
124typedef PyObject *(*iternextfunc) (PyObject *);
125typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *);
126typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *);
127typedef int (*initproc)(PyObject *, PyObject *, PyObject *);
128typedef PyObject *(*newfunc)(struct _typeobject *, PyObject *, PyObject *);
129typedef PyObject *(*allocfunc)(struct _typeobject *, Py_ssize_t);
130typedef int (*visitproc)(PyObject *, void *);
131typedef int (*traverseproc)(PyObject *, visitproc, void *);
132typedef int (*inquiry)(PyObject *);
133typedef PyObject *(*PyCFunction)(PyObject *, PyObject *);
134
135struct PyAsyncMethods;
136struct PyNumberMethods;
137struct PySequenceMethods;
138struct PyMappingMethods;
139struct PyBufferProcs;
140struct PyMethodDef;
141
143{
144 PyVarObject ob_base;
145 const char *tp_name; /* For printing, in format "<module>.<name>" */
146 Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */
147
148 /* Methods to implement standard operations */
149
150 destructor tp_dealloc;
151 printfunc tp_print;
152 getattrfunc tp_getattr;
153 setattrfunc tp_setattr;
154 PyAsyncMethods *tp_as_async; /* formerly known as tp_compare (Python 2)
155 or tp_reserved (Python 3) */
156 reprfunc tp_repr;
157
158 /* Method suites for standard classes */
159
160 PyNumberMethods *tp_as_number;
161 PySequenceMethods *tp_as_sequence;
162 PyMappingMethods *tp_as_mapping;
163
164 /* More standard operations (here for binary compatibility) */
165
166 hashfunc tp_hash;
167 ternaryfunc tp_call;
168 reprfunc tp_str;
169 getattrofunc tp_getattro;
170 setattrofunc tp_setattro;
171
172 /* Functions to access object as input/output buffer */
173 PyBufferProcs *tp_as_buffer;
174
175 /* Flags to define presence of optional/expanded features */
176 unsigned long tp_flags;
177
178 const char *tp_doc; /* Documentation string */
179
180 /* Assigned meaning in release 2.0 */
181 /* call function for all accessible objects */
182 traverseproc tp_traverse;
183
184 /* delete references to contained objects */
185 inquiry tp_clear;
186
187 /* Assigned meaning in release 2.1 */
188 /* rich comparisons */
189 richcmpfunc tp_richcompare;
190
191 /* weak reference enabler */
192 Py_ssize_t tp_weaklistoffset;
193
194 /* Iterators */
195 getiterfunc tp_iter;
196 iternextfunc tp_iternext;
197
198 /* Attribute descriptor and subclassing stuff */
199 struct PyMethodDef *tp_methods;
200 struct PyMemberDef *tp_members;
201 struct PyGetSetDef *tp_getset;
202 struct _typeobject *tp_base;
203 PyObject *tp_dict;
204 descrgetfunc tp_descr_get;
205 descrsetfunc tp_descr_set;
206 Py_ssize_t tp_dictoffset;
207 initproc tp_init;
208 allocfunc tp_alloc;
209 newfunc tp_new;
210 freefunc tp_free; /* Low-level free-memory routine */
211 inquiry tp_is_gc; /* For PyObject_IS_GC */
212 PyObject *tp_bases;
213 PyObject *tp_mro; /* method resolution order */
214 PyObject *tp_cache;
215 PyObject *tp_subclasses;
216 PyObject *tp_weaklist;
217 destructor tp_del;
218
219 /* Type attribute cache version tag. Added in version 2.6 */
220 unsigned int tp_version_tag;
221
222 destructor tp_finalize;
223};
224
226 const char *ml_name; /* The name of the built-in function/method */
227 PyCFunction ml_meth; /* The C function that implements it */
228 int ml_flags; /* Combination of METH_xxx flags, which mostly
229 describe the args expected by the C func */
230 const char *ml_doc; /* The __doc__ attribute, or NULL */
231};
232
234 lenfunc mp_length;
235 binaryfunc mp_subscript;
236 objobjargproc mp_ass_subscript;
237};
238
239
241 PyObject ob_base;
242 PyObject* (*m_init)(void);
243 Py_ssize_t m_index;
244 PyObject* m_copy;
245};
246
248 PyModuleDef_Base m_base;
249 const char* m_name;
250 const char* m_doc;
251 Py_ssize_t m_size;
252 PyMethodDef *m_methods;
253 struct PyModuleDef_Slot* m_slots;
254 traverseproc m_traverse;
255 inquiry m_clear;
256 freefunc m_free;
257};
258
259#ifndef _WIN32
260extern PyObject _Py_FalseStruct, _Py_TrueStruct, _Py_NoneStruct;
261#endif
262
263extern "C" {
264 void _Py_Dealloc(PyObject*);
265 PyObject* PyErr_Occurred();
266 void PyErr_Print();
267 void PyErr_SetString(PyObject*,const char*);
268
269
270 extern PyTypeObject* PyBool_Type;
271 extern PyTypeObject* PyFloat_Type;
272 extern PyObject* PyExc_RuntimeError;
273
274 int PyType_IsSubtype(PyTypeObject*, PyTypeObject*);
275 unsigned long PyType_GetFlags(PyTypeObject*);
276 PyObject* Py_GetConstantBorrowed(unsigned);
277 PyObject* PyLong_FromLong(long);
278 PyObject* PyFloat_FromDouble(double);
279 long long PyLong_AsLongLong(PyObject*);
280 double PyFloat_AsDouble(PyObject*);
281 int PyType_Ready(PyTypeObject*);
282
283 PyObject* PyObject_Str(PyObject*);
284 PyObject* PyObject_Dir(PyObject*);
285 PyObject* PyObject_GetAttr(PyObject*, PyObject*);
286 PyObject* PyObject_GenericGetAttr(PyObject*, PyObject*);
287 int PyObject_SetAttrString(PyObject*, const char*, PyObject*);
288
289 int PyModule_AddObject(PyObject*, const char*, PyObject*);
290 const char* PyModule_GetName(PyObject*);
291 PyObject* PyModule_Create2(PyModuleDef*,int);
292 int PySequence_Check(PyObject*);
293 ssize_t PySequence_Size(PyObject*);
294 PyObject* PySequence_GetItem(PyObject*, ssize_t i);
295
296 PyObject* PyUnicode_FromString(const char*);
297 char* PyUnicode_AsUTF8AndSize(PyObject*,Py_ssize_t*);
298
299 PyObject* PyDict_New();
300 int PyDict_SetItemString(PyObject* dp, const char* key, PyObject* item);
301
302 PyObject* PyList_New(ssize_t size);
303 int PyList_SetItem(PyObject*, ssize_t, PyObject*);
304 int PyList_Append(PyObject*, PyObject*);
305
306 int PyMapping_Check(PyObject*);
307 PyObject* PyMapping_Items(PyObject*);
308}
309
310#endif
Definition pythonCAPI.h:233
Definition pythonCAPI.h:225
Definition pythonCAPI.h:240
Definition pythonCAPI.h:247
Definition pythonCAPI.h:97
Definition pythonCAPI.h:143
Definition pythonCAPI.h:104