Skip to content

Commit d6ed79d

Browse files
pythonCAPI.h created and tested on Linux.
1 parent 2b78033 commit d6ed79d

File tree

1 file changed

+271
-9
lines changed

1 file changed

+271
-9
lines changed

pythonCAPI.h

+271-9
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,282 @@
1111
#define CLASSDESC_PYTHON_CAPI_H
1212

1313
// TODO - is this field used on Windows?
14-
#define _PyObject_HEAD_EXTRA
14+
//#define _PyObject_HEAD_EXTRA
15+
16+
#define PYTHON_API_VERSION 1013
17+
#define Py_False ((PyObject *) &_Py_FalseStruct)
18+
#define Py_True ((PyObject *) &_Py_TrueStruct)
19+
#define Py_None ((PyObject *) &_Py_NoneStruct)
1520
#define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True
1621
#define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False
22+
#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
23+
#define METH_VARARGS 0x0001
24+
#define METH_NOARGS 0x0004
25+
#define METH_O 0x0008
26+
27+
#define PyType_FastSubclass(t,f) ((PyType_GetFlags(t) & (f)) != 0)
28+
29+
#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
30+
#define PyObject_TypeCheck(ob, tp) \
31+
(Py_TYPE(ob) == (tp) || PyType_IsSubtype(Py_TYPE(ob), (tp)))
32+
#define PyBool_Check(x) (Py_TYPE(x) == &PyBool_Type)
33+
#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+
#define PyList_Check(op) PyType_FastSubclass(Py_TYPE(op), 1UL << 25)
37+
38+
#define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt)
39+
#define Py_INCREF(op) (((PyObject *)(op))->ob_refcnt++)
40+
#define Py_DECREF(op) \
41+
do { \
42+
PyObject *_py_decref_tmp = (PyObject *)(op); \
43+
if (--(_py_decref_tmp)->ob_refcnt == 0) \
44+
_Py_Dealloc(_py_decref_tmp); \
45+
} while (0)
46+
47+
/* Macros to use in case the object pointer may be NULL: */
48+
#define Py_XINCREF(op) \
49+
do { \
50+
PyObject *_py_xincref_tmp = (PyObject *)(op); \
51+
if (_py_xincref_tmp != NULL) \
52+
Py_INCREF(_py_xincref_tmp); \
53+
} while (0)
54+
55+
#define Py_XDECREF(op) \
56+
do { \
57+
PyObject* _py_xdecref_tmp = (PyObject *)(op); \
58+
if (_py_xdecref_tmp != NULL) \
59+
Py_DECREF(_py_xdecref_tmp); \
60+
} while (0)
61+
62+
#ifdef _WIN32
63+
#define PyMODINIT_FUNC extern "C" __declspec(dllexport) PyObject*
64+
#else
65+
#define PyMODINIT_FUNC extern "C" PyObject*
66+
#endif
67+
68+
#define PyObject_HEAD_INIT(type) {1, type },
69+
70+
#define PyModuleDef_HEAD_INIT { \
71+
PyObject_HEAD_INIT(NULL) \
72+
NULL, /* m_init */ \
73+
0, /* m_index */ \
74+
NULL, /* m_copy */ \
75+
}
76+
77+
#define PyModule_Create(module) \
78+
PyModule_Create2(module, PYTHON_API_VERSION)
79+
80+
struct PyTypeObject;
81+
82+
struct PyObject
83+
{
84+
//_PyObject_HEAD_EXTRA
85+
ssize_t ob_refcnt;
86+
PyTypeObject *ob_type;
87+
};
88+
89+
struct PyVarObject
90+
{
91+
PyObject ob_base;
92+
ssize_t ob_size; /* Number of items in variable part */
93+
};
94+
95+
typedef PyObject * (*binaryfunc)(PyObject *, PyObject *);
96+
typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
97+
typedef ssize_t (*lenfunc)(PyObject *);
98+
typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
99+
typedef void (*freefunc)(void *);
100+
typedef void (*destructor)(PyObject *);
101+
typedef int (*printfunc)(PyObject *, FILE *, int);
102+
typedef PyObject *(*getattrfunc)(PyObject *, char *);
103+
typedef PyObject *(*getattrofunc)(PyObject *, PyObject *);
104+
typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
105+
typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
106+
typedef PyObject *(*reprfunc)(PyObject *);
107+
typedef ssize_t (*hashfunc)(PyObject *);
108+
typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
109+
typedef PyObject *(*getiterfunc) (PyObject *);
110+
typedef PyObject *(*iternextfunc) (PyObject *);
111+
typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *);
112+
typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *);
113+
typedef int (*initproc)(PyObject *, PyObject *, PyObject *);
114+
typedef PyObject *(*newfunc)(struct _typeobject *, PyObject *, PyObject *);
115+
typedef PyObject *(*allocfunc)(struct _typeobject *, ssize_t);
116+
typedef int (*visitproc)(PyObject *, void *);
117+
typedef int (*traverseproc)(PyObject *, visitproc, void *);
118+
typedef int (*inquiry)(PyObject *);
119+
typedef PyObject *(*PyCFunction)(PyObject *, PyObject *);
120+
121+
struct PyAsyncMethods;
122+
struct PyNumberMethods;
123+
struct PySequenceMethods;
124+
struct PyMappingMethods;
125+
struct PyBufferProcs;
126+
struct PyMethodDef;
127+
128+
struct PyTypeObject
129+
{
130+
PyVarObject ob_base;
131+
const char *tp_name; /* For printing, in format "<module>.<name>" */
132+
ssize_t tp_basicsize, tp_itemsize; /* For allocation */
133+
134+
/* Methods to implement standard operations */
135+
136+
destructor tp_dealloc;
137+
printfunc tp_print;
138+
getattrfunc tp_getattr;
139+
setattrfunc tp_setattr;
140+
PyAsyncMethods *tp_as_async; /* formerly known as tp_compare (Python 2)
141+
or tp_reserved (Python 3) */
142+
reprfunc tp_repr;
143+
144+
/* Method suites for standard classes */
145+
146+
PyNumberMethods *tp_as_number;
147+
PySequenceMethods *tp_as_sequence;
148+
PyMappingMethods *tp_as_mapping;
149+
150+
/* More standard operations (here for binary compatibility) */
151+
152+
hashfunc tp_hash;
153+
ternaryfunc tp_call;
154+
reprfunc tp_str;
155+
getattrofunc tp_getattro;
156+
setattrofunc tp_setattro;
157+
158+
/* Functions to access object as input/output buffer */
159+
PyBufferProcs *tp_as_buffer;
160+
161+
/* Flags to define presence of optional/expanded features */
162+
unsigned long tp_flags;
163+
164+
const char *tp_doc; /* Documentation string */
165+
166+
/* Assigned meaning in release 2.0 */
167+
/* call function for all accessible objects */
168+
traverseproc tp_traverse;
169+
170+
/* delete references to contained objects */
171+
inquiry tp_clear;
172+
173+
/* Assigned meaning in release 2.1 */
174+
/* rich comparisons */
175+
richcmpfunc tp_richcompare;
176+
177+
/* weak reference enabler */
178+
ssize_t tp_weaklistoffset;
179+
180+
/* Iterators */
181+
getiterfunc tp_iter;
182+
iternextfunc tp_iternext;
183+
184+
/* Attribute descriptor and subclassing stuff */
185+
struct PyMethodDef *tp_methods;
186+
struct PyMemberDef *tp_members;
187+
struct PyGetSetDef *tp_getset;
188+
struct _typeobject *tp_base;
189+
PyObject *tp_dict;
190+
descrgetfunc tp_descr_get;
191+
descrsetfunc tp_descr_set;
192+
ssize_t tp_dictoffset;
193+
initproc tp_init;
194+
allocfunc tp_alloc;
195+
newfunc tp_new;
196+
freefunc tp_free; /* Low-level free-memory routine */
197+
inquiry tp_is_gc; /* For PyObject_IS_GC */
198+
PyObject *tp_bases;
199+
PyObject *tp_mro; /* method resolution order */
200+
PyObject *tp_cache;
201+
PyObject *tp_subclasses;
202+
PyObject *tp_weaklist;
203+
destructor tp_del;
204+
205+
/* Type attribute cache version tag. Added in version 2.6 */
206+
unsigned int tp_version_tag;
207+
208+
destructor tp_finalize;
209+
};
210+
211+
struct PyMethodDef {
212+
const char *ml_name; /* The name of the built-in function/method */
213+
PyCFunction ml_meth; /* The C function that implements it */
214+
int ml_flags; /* Combination of METH_xxx flags, which mostly
215+
describe the args expected by the C func */
216+
const char *ml_doc; /* The __doc__ attribute, or NULL */
217+
};
218+
219+
struct PyMappingMethods {
220+
lenfunc mp_length;
221+
binaryfunc mp_subscript;
222+
objobjargproc mp_ass_subscript;
223+
};
224+
225+
226+
struct PyModuleDef_Base {
227+
PyObject ob_base;
228+
PyObject* (*m_init)(void);
229+
ssize_t m_index;
230+
PyObject* m_copy;
231+
};
232+
233+
struct PyModuleDef {
234+
PyModuleDef_Base m_base;
235+
const char* m_name;
236+
const char* m_doc;
237+
ssize_t m_size;
238+
PyMethodDef *m_methods;
239+
struct PyModuleDef_Slot* m_slots;
240+
traverseproc m_traverse;
241+
inquiry m_clear;
242+
freefunc m_free;
243+
};
244+
245+
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;
17250

18251
extern "C" {
19-
typedef ssize_t Py_ssize_t;
20-
typedef struct _object {
21-
_PyObject_HEAD_EXTRA
22-
Py_ssize_t ob_refcnt;
23-
struct _typeobject *ob_type;
24-
} PyObject;
25-
26-
PyObject* PyUnicode_FromString(const char *);
252+
void _Py_Dealloc(PyObject*);
253+
PyObject* PyErr_Occurred();
254+
void PyErr_Print();
255+
void PyErr_SetString(PyObject*,const char*);
256+
257+
int PyType_IsSubtype(PyTypeObject*, PyTypeObject*);
258+
unsigned long PyType_GetFlags(PyTypeObject*);
259+
PyObject* PyLong_FromLong(long);
260+
PyObject* PyFloat_FromDouble(double);
261+
long long PyLong_AsLongLong(PyObject*);
262+
double PyFloat_AsDouble(PyObject*);
263+
int PyType_Ready(PyTypeObject*);
264+
265+
PyObject* PyObject_Str(PyObject*);
266+
PyObject* PyObject_Dir(PyObject*);
267+
PyObject* PyObject_GetAttr(PyObject*, PyObject*);
268+
PyObject* PyObject_GenericGetAttr(PyObject*, PyObject*);
269+
int PyObject_SetAttrString(PyObject*, const char*, PyObject*);
270+
271+
int PyModule_AddObject(PyObject*, const char*, PyObject*);
272+
const char* PyModule_GetName(PyObject*);
273+
PyObject* PyModule_Create2(PyModuleDef*,int);
274+
int PySequence_Check(PyObject*);
275+
ssize_t PySequence_Size(PyObject*);
276+
PyObject* PySequence_GetItem(PyObject*, ssize_t i);
277+
278+
PyObject* PyUnicode_FromString(const char*);
279+
char* PyUnicode_AsUTF8(PyObject*);
280+
27281
PyObject* PyDict_New();
282+
int PyDict_SetItemString(PyObject* dp, const char* key, PyObject* item);
283+
284+
PyObject* PyList_New(ssize_t size);
285+
int PyList_SetItem(PyObject*, ssize_t, PyObject*);
286+
int PyList_Append(PyObject*, PyObject*);
28287

288+
int PyMapping_Check(PyObject*);
289+
PyObject* PyMapping_Items(PyObject*);
29290
}
291+
30292
#endif

0 commit comments

Comments
 (0)