Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
CPyCppyy.h
Go to the documentation of this file.
1#ifndef CPYCPPYY_CPYCPPYY_H
2#define CPYCPPYY_CPYCPPYY_H
3
4#ifdef _WIN32
5// Disable warning C4275: non dll-interface class
6#pragma warning (disable : 4275)
7// Disable warning C4251: needs to have dll-interface to be used by clients
8#pragma warning (disable : 4251)
9// Disable warning C4800: 'int' : forcing value to bool
10#pragma warning (disable : 4800)
11// Avoid that pyconfig.h decides using a #pragma what library python library to use
12//#define MS_NO_COREDLL 1
13#endif
14
15// to prevent problems with fpos_t and redefinition warnings
16#if defined(linux)
17
18#include <stdio.h>
19
20#ifdef _POSIX_C_SOURCE
21#undef _POSIX_C_SOURCE
22#endif
23
24#ifdef _FILE_OFFSET_BITS
25#undef _FILE_OFFSET_BITS
26#endif
27
28#ifdef _XOPEN_SOURCE
29#undef _XOPEN_SOURCE
30#endif
31
32#endif // linux
33
34#define PY_SSIZE_T_CLEAN
35#include "Python.h"
36#include <sys/types.h>
37
38namespace CPyCppyy {
39 typedef Py_ssize_t dim_t;
40} // namespace CPyCppyy
41
42// for 3.3 support
43#if PY_VERSION_HEX < 0x03030000
44 typedef PyDictEntry* (*dict_lookup_func)(PyDictObject*, PyObject*, long);
45#else
46#if PY_VERSION_HEX >= 0x030b0000
48 PyDictObject*, PyObject*, Py_hash_t, PyObject**);
49#elif PY_VERSION_HEX >= 0x03060000
51 PyDictObject*, PyObject*, Py_hash_t, PyObject***, Py_ssize_t*);
52#else
53 struct PyDictKeyEntry;
54 typedef PyDictKeyEntry* (*dict_lookup_func)(PyDictObject*, PyObject*, Py_hash_t, PyObject***);
55#define PyDictEntry PyDictKeyEntry
56#endif
57#endif
58
59// for 3.0 support (backwards compatibility, really)
60#if PY_VERSION_HEX < 0x03000000
61#define PyBytes_Check PyString_Check
62#define PyBytes_CheckExact PyString_CheckExact
63#define PyBytes_AS_STRING PyString_AS_STRING
64#define PyBytes_AsString PyString_AsString
65#define PyBytes_AsStringAndSize PyString_AsStringAndSize
66#define PyBytes_GET_SIZE PyString_GET_SIZE
67#define PyBytes_Size PyString_Size
68#define PyBytes_FromFormat PyString_FromFormat
69#define PyBytes_FromString PyString_FromString
70#define PyBytes_FromStringAndSize PyString_FromStringAndSize
71
72#define PyBytes_Type PyString_Type
73
74#define CPyCppyy_PyText_Check PyString_Check
75#define CPyCppyy_PyText_CheckExact PyString_CheckExact
76#define CPyCppyy_PyText_AsString PyString_AS_STRING
77#define CPyCppyy_PyText_AsStringChecked PyString_AsString
78#define CPyCppyy_PyText_GET_SIZE PyString_GET_SIZE
79#define CPyCppyy_PyText_GetSize PyString_Size
80#define CPyCppyy_PyText_FromFormat PyString_FromFormat
81#define CPyCppyy_PyText_FromString PyString_FromString
82#define CPyCppyy_PyText_InternFromString PyString_InternFromString
83#define CPyCppyy_PyText_Append PyString_Concat
84#define CPyCppyy_PyText_AppendAndDel PyString_ConcatAndDel
85#define CPyCppyy_PyText_FromStringAndSize PyString_FromStringAndSize
86
87static inline const char* CPyCppyy_PyText_AsStringAndSize(PyObject* pystr, Py_ssize_t* size)
88{
89 const char* cstr = CPyCppyy_PyText_AsStringChecked(pystr);
90 if (cstr) *size = CPyCppyy_PyText_GetSize(pystr);
91 return cstr;
92}
93
94#define CPyCppyy_PyText_Type PyString_Type
95
96#define CPyCppyy_PyUnicode_GET_SIZE PyUnicode_GET_SIZE
97
99 void* cobj, const char* /* name */, void (*destr)(void*))
100{
101 return PyCObject_FromVoidPtr(cobj, destr);
102}
103#define CPyCppyy_PyCapsule_CheckExact PyCObject_Check
104static inline void* CPyCppyy_PyCapsule_GetPointer(PyObject* capsule, const char* /* name */)
105{
106 return (void*)PyCObject_AsVoidPtr(capsule);
107}
108
109#define CPPYY__long__ "__long__"
110#define CPPYY__idiv__ "__idiv__"
111#define CPPYY__div__ "__div__"
112#define CPPYY__next__ "next"
113
114typedef long Py_hash_t;
115
116#endif // ! 3.0
117
118// for 3.0 support (backwards compatibility, really)
119#if PY_VERSION_HEX >= 0x03000000
120#define CPyCppyy_PyText_Check PyUnicode_Check
121#define CPyCppyy_PyText_CheckExact PyUnicode_CheckExact
122#define CPyCppyy_PyText_AsString PyUnicode_AsUTF8
123#define CPyCppyy_PyText_AsStringChecked PyUnicode_AsUTF8
124#define CPyCppyy_PyText_GetSize PyUnicode_GetSize
125#if PY_VERSION_HEX < 0x03090000
126#define CPyCppyy_PyText_GET_SIZE PyUnicode_GET_SIZE
127#define CPyCppyy_PyUnicode_GET_SIZE PyUnicode_GET_LENGTH
128#else
129#define CPyCppyy_PyText_GET_SIZE PyUnicode_GET_LENGTH
130#define CPyCppyy_PyUnicode_GET_SIZE PyUnicode_GET_LENGTH
131#endif
132#define CPyCppyy_PyText_FromFormat PyUnicode_FromFormat
133#define CPyCppyy_PyText_FromString PyUnicode_FromString
134#define CPyCppyy_PyText_InternFromString PyUnicode_InternFromString
135#define CPyCppyy_PyText_Append PyUnicode_Append
136#define CPyCppyy_PyText_AppendAndDel PyUnicode_AppendAndDel
137#define CPyCppyy_PyText_FromStringAndSize PyUnicode_FromStringAndSize
138
139#if PY_VERSION_HEX >= 0x03030000
140#define _CPyCppyy_PyText_AsStringAndSize PyUnicode_AsUTF8AndSize
141#else
142#define _CPyCppyy_PyText_AsStringAndSize PyUnicode_AsStringAndSize
143#endif // >= 3.3
144
145static inline const char* CPyCppyy_PyText_AsStringAndSize(PyObject* pystr, Py_ssize_t* size)
146{
147 const char* cstr = _CPyCppyy_PyText_AsStringAndSize(pystr, size);
148 if (!cstr && PyBytes_CheckExact(pystr)) {
149 PyErr_Clear();
150 PyBytes_AsStringAndSize(pystr, (char**)&cstr, size);
151 }
152 return cstr;
153}
154
155#define CPyCppyy_PyText_Type PyUnicode_Type
156
157#define PyIntObject PyLongObject
158#define PyInt_Check PyLong_Check
159#define PyInt_AsLong PyLong_AsLong
160#define PyInt_AS_LONG PyLong_AsLong
161#define PyInt_AsSsize_t PyLong_AsSsize_t
162#define PyInt_CheckExact PyLong_CheckExact
163#define PyInt_FromLong PyLong_FromLong
164#define PyInt_FromSsize_t PyLong_FromSsize_t
165
166#define PyInt_Type PyLong_Type
167
168#define CPyCppyy_PyCapsule_New PyCapsule_New
169#define CPyCppyy_PyCapsule_CheckExact PyCapsule_CheckExact
170#define CPyCppyy_PyCapsule_GetPointer PyCapsule_GetPointer
171
172#define CPPYY__long__ "__int__"
173#define CPPYY__idiv__ "__itruediv__"
174#define CPPYY__div__ "__truediv__"
175#define CPPYY__next__ "__next__"
176
177#define Py_TPFLAGS_HAVE_RICHCOMPARE 0
178#define Py_TPFLAGS_CHECKTYPES 0
179
180#define PyClass_Check PyType_Check
181
182#define PyBuffer_Type PyMemoryView_Type
183#endif // ! 3.0
184
185#if PY_VERSION_HEX >= 0x03020000
186#define CPyCppyy_PySliceCast PyObject*
187#define PyUnicode_GetSize PyUnicode_GetLength
188#else
189#define CPyCppyy_PySliceCast PySliceObject*
190#endif // >= 3.2
191
192// feature of 3.0 not in 2.5 and earlier
193#if PY_VERSION_HEX < 0x02060000
194#define PyVarObject_HEAD_INIT(type, size) \
195 PyObject_HEAD_INIT(type) size,
196#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
197#endif
198
199// API changes in 2.5 (int -> Py_ssize_t) and 3.2 (PyUnicodeObject -> PyObject)
200#if PY_VERSION_HEX < 0x03020000
202{
203#if PY_VERSION_HEX < 0x02050000
204 return (Py_ssize_t)PyUnicode_AsWideChar((PyUnicodeObject*)pyobj, w, (int)size);
205#else
206 return PyUnicode_AsWideChar((PyUnicodeObject*)pyobj, w, size);
207#endif
208}
209#else
210#define CPyCppyy_PyUnicode_AsWideChar PyUnicode_AsWideChar
211#endif
212
213// backwards compatibility, pre python 2.5
214#if PY_VERSION_HEX < 0x02050000
215typedef int Py_ssize_t;
216#define PyInt_AsSsize_t PyInt_AsLong
217#define PyInt_FromSsize_t PyInt_FromLong
218# define PY_SSIZE_T_FORMAT "%d"
219# if !defined(PY_SSIZE_T_MIN)
220# define PY_SSIZE_T_MAX INT_MAX
221# define PY_SSIZE_T_MIN INT_MIN
222# endif
223#define ssizeobjargproc intobjargproc
224#define lenfunc inquiry
225#define ssizeargfunc intargfunc
226
227#define PyIndex_Check(obj) \
228 (PyInt_Check(obj) || PyLong_Check(obj))
229
231 return (Py_ssize_t)PyLong_AsLong(obj);
232}
233
234#else
235# ifdef R__MACOSX
236# if SIZEOF_SIZE_T == SIZEOF_INT
237# if defined(MAC_OS_X_VERSION_10_4)
238# define PY_SSIZE_T_FORMAT "%ld"
239# else
240# define PY_SSIZE_T_FORMAT "%d"
241# endif
242# elif SIZEOF_SIZE_T == SIZEOF_LONG
243# define PY_SSIZE_T_FORMAT "%ld"
244# endif
245# else
246# define PY_SSIZE_T_FORMAT "%zd"
247# endif
248#endif
249
250#if PY_VERSION_HEX < 0x02020000
251#define PyBool_FromLong PyInt_FromLong
252#endif
253
254#if PY_VERSION_HEX < 0x03000000
255// the following should quiet Solaris
256#ifdef Py_False
257#undef Py_False
258#define Py_False ((PyObject*)(void*)&_Py_ZeroStruct)
259#endif
260
261#ifdef Py_True
262#undef Py_True
263#define Py_True ((PyObject*)(void*)&_Py_TrueStruct)
264#endif
265#endif
266
267#ifndef Py_RETURN_NONE
268#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
269#endif
270
271#ifndef Py_RETURN_TRUE
272#define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True
273#endif
274
275#ifndef Py_RETURN_FALSE
276#define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False
277#endif
278
279#if PY_VERSION_HEX >= 0x03000000 && PY_VERSION_HEX < 0x03010000
280#define CPyCppyy_PyBuffer PyBuffer_Release
281#else
282inline void CPyCppyy_PyBuffer_Release(PyObject* /* unused */, Py_buffer* view) {
283 PyBuffer_Release(view);
284}
285#endif
286
287// vector call support
288#if PY_VERSION_HEX >= 0x03090000
289#define CPyCppyy_PyCFunction_Call PyObject_Call
290#else
291#define CPyCppyy_PyCFunction_Call PyCFunction_Call
292#endif
293
294// vector call support
295#if PY_VERSION_HEX >= 0x03080000
296typedef PyObject* const* CPyCppyy_PyArgs_t;
298 return args[i];
299}
301 return ((PyObject**)args)[i] = item;
302}
303static inline Py_ssize_t CPyCppyy_PyArgs_GET_SIZE(CPyCppyy_PyArgs_t, size_t nargsf) {
304 return PyVectorcall_NARGS(nargsf);
305}
307 return (CPyCppyy_PyArgs_t)PyMem_Malloc(N*sizeof(PyObject*));
308}
309static inline void CPyCppyy_PyArgs_DEL(CPyCppyy_PyArgs_t args) {
310 PyMem_Free((void*)args);
311}
312#if PY_VERSION_HEX >= 0x03090000
313#define CPyCppyy_PyObject_Call PyObject_Vectorcall
314#else
315#define CPyCppyy_PyObject_Call _PyObject_Vectorcall
316#endif
318 PyObject* cb, CPyCppyy_PyArgs_t args, size_t nargsf, PyObject* kwds) {
319 Py_ssize_t offset = Py_TYPE(cb)->tp_vectorcall_offset;
320 vectorcallfunc func = *(vectorcallfunc*)(((char*)cb) + offset);
321 return func(cb, args, nargsf, kwds);
322}
323
324#ifndef Py_TPFLAGS_HAVE_VECTORCALL
325#define Py_TPFLAGS_HAVE_VECTORCALL _Py_TPFLAGS_HAVE_VECTORCALL
326#endif
327
328#else
329
332 return PyTuple_GET_ITEM(args, i);
333}
335 return PyTuple_SET_ITEM(args, i, item);
336}
338 return PyTuple_GET_SIZE(args);
339}
341 return PyTuple_New(N);
342}
343static inline void CPyCppyy_PyArgs_DEL(CPyCppyy_PyArgs_t args) {
344 Py_DECREF(args);
345}
346inline PyObject* CPyCppyy_PyObject_Call(PyObject* cb, PyObject* args, size_t, PyObject* kwds) {
347 return PyObject_Call(cb, args, kwds);
348}
349inline PyObject* CPyCppyy_tp_call(PyObject* cb, PyObject* args, size_t, PyObject* kwds) {
350 return Py_TYPE(cb)->tp_call(cb, args, kwds);
351}
352#endif
353
354// weakref forced strong reference
355#if PY_VERSION_HEX < 0x30d0000
357 PyObject* pyobject = PyWeakref_GetObject(ref);
358 if (!pyobject || pyobject == Py_None)
359 return nullptr;
360 Py_INCREF(pyobject);
361 return pyobject;
362}
363#else
364static inline PyObject* CPyCppyy_GetWeakRef(PyObject* ref) {
365 PyObject* pyobject = nullptr;
366 if (PyWeakref_GetRef(ref, &pyobject) != -1)
367 return pyobject;
368 return nullptr;
369}
370#endif
371
372// Py_TYPE as inline function
373#if PY_VERSION_HEX < 0x03090000 && !defined(Py_SET_TYPE)
374static inline
375void _Py_SET_TYPE(PyObject *ob, PyTypeObject *type) { ob->ob_type = type; }
376#define Py_SET_TYPE(ob, type) _Py_SET_TYPE((PyObject*)(ob), type)
377#endif
378
379// py39 gained several faster (through vector call) equivalent method call API
380#if PY_VERSION_HEX < 0x03090000
382 return PyObject_CallMethodObjArgs(obj, name, nullptr);
383}
384
386 return PyObject_CallMethodObjArgs(obj, name, arg, nullptr);
387}
388#endif
389
390// C++ version of the cppyy API
391#include "Cppyy.h"
392
393// export macros for our own API
394#include "CPyCppyy/CommonDefs.h"
395
396#endif // !CPYCPPYY_CPYCPPYY_H
static PyObject * CPyCppyy_GetWeakRef(PyObject *ref)
Definition CPyCppyy.h:356
#define Py_TYPE(ob)
Definition CPyCppyy.h:196
static Py_ssize_t CPyCppyy_PyUnicode_AsWideChar(PyObject *pyobj, wchar_t *w, Py_ssize_t size)
Definition CPyCppyy.h:201
static void CPyCppyy_PyArgs_DEL(CPyCppyy_PyArgs_t args)
Definition CPyCppyy.h:343
PyDictEntry *(* dict_lookup_func)(PyDictObject *, PyObject *, long)
Definition CPyCppyy.h:44
#define PyBytes_AsStringAndSize
Definition CPyCppyy.h:65
Py_ssize_t PyNumber_AsSsize_t(PyObject *obj, PyObject *)
Definition CPyCppyy.h:230
int Py_ssize_t
Definition CPyCppyy.h:215
static void * CPyCppyy_PyCapsule_GetPointer(PyObject *capsule, const char *)
Definition CPyCppyy.h:104
static PyObject * CPyCppyy_PyArgs_SET_ITEM(CPyCppyy_PyArgs_t args, Py_ssize_t i, PyObject *item)
Definition CPyCppyy.h:334
static Py_ssize_t CPyCppyy_PyArgs_GET_SIZE(CPyCppyy_PyArgs_t args, size_t)
Definition CPyCppyy.h:337
PyObject * CPyCppyy_PyArgs_t
Definition CPyCppyy.h:330
static PyObject * CPyCppyy_PyCapsule_New(void *cobj, const char *, void(*destr)(void *))
Definition CPyCppyy.h:98
#define CPyCppyy_PyText_GetSize
Definition CPyCppyy.h:79
long Py_hash_t
Definition CPyCppyy.h:114
void CPyCppyy_PyBuffer_Release(PyObject *, Py_buffer *view)
Definition CPyCppyy.h:282
static PyObject * PyObject_CallMethodOneArg(PyObject *obj, PyObject *name, PyObject *arg)
Definition CPyCppyy.h:385
PyObject * CPyCppyy_PyObject_Call(PyObject *cb, PyObject *args, size_t, PyObject *kwds)
Definition CPyCppyy.h:346
#define PyBytes_CheckExact
Definition CPyCppyy.h:62
static const char * CPyCppyy_PyText_AsStringAndSize(PyObject *pystr, Py_ssize_t *size)
Definition CPyCppyy.h:87
static void _Py_SET_TYPE(PyObject *ob, PyTypeObject *type)
Definition CPyCppyy.h:375
#define CPyCppyy_PyText_AsStringChecked
Definition CPyCppyy.h:77
PyObject * CPyCppyy_tp_call(PyObject *cb, PyObject *args, size_t, PyObject *kwds)
Definition CPyCppyy.h:349
static PyObject * PyObject_CallMethodNoArgs(PyObject *obj, PyObject *name)
Definition CPyCppyy.h:381
static CPyCppyy_PyArgs_t CPyCppyy_PyArgs_New(Py_ssize_t N)
Definition CPyCppyy.h:340
static PyObject * CPyCppyy_PyArgs_GET_ITEM(CPyCppyy_PyArgs_t args, Py_ssize_t i)
Definition CPyCppyy.h:331
_object PyObject
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define N
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
Definition TGX11.cxx:110
Py_ssize_t dim_t
Definition API.h:90