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
35#include "Python.h"
36#include <sys/types.h>
37
38// selected ROOT types from RtypesCore.h
39#ifdef R__INT16
40typedef long Int_t; //Signed integer 4 bytes
41typedef unsigned long UInt_t; //Unsigned integer 4 bytes
42#else
43typedef int Int_t; //Signed integer 4 bytes (int)
44typedef unsigned int UInt_t; //Unsigned integer 4 bytes (unsigned int)
45#endif
46#ifdef R__B64 // Note: Long_t and ULong_t are currently not portable types
47typedef long Long_t; //Signed long integer 8 bytes (long)
48typedef unsigned long ULong_t; //Unsigned long integer 8 bytes (unsigned long)
49#else
50typedef long Long_t; //Signed long integer 4 bytes (long)
51typedef unsigned long ULong_t; //Unsigned long integer 4 bytes (unsigned long)
52#endif
53typedef float Float16_t; //Float 4 bytes written with a truncated mantissa
54typedef double Double32_t; //Double 8 bytes in memory, written as a 4 bytes float
55typedef long double LongDouble_t;//Long Double
56#ifdef _WIN32
57typedef __int64 Long64_t; //Portable signed long integer 8 bytes
58typedef unsigned __int64 ULong64_t; //Portable unsigned long integer 8 bytes
59#else
60typedef long long Long64_t; //Portable signed long integer 8 bytes
61typedef unsigned long long ULong64_t;//Portable unsigned long integer 8 bytes
62#endif
63
65typedef dim_t* dims_t;
66
67// for 3.3 support
68#if PY_VERSION_HEX < 0x03030000
69 typedef PyDictEntry* (*dict_lookup_func)(PyDictObject*, PyObject*, long);
70#else
71#if PY_VERSION_HEX >= 0x03060000
73 PyDictObject*, PyObject*, Py_hash_t, PyObject***, Py_ssize_t*);
74#else
75 struct PyDictKeyEntry;
76 typedef PyDictKeyEntry* (*dict_lookup_func)(PyDictObject*, PyObject*, Py_hash_t, PyObject***);
77#define PyDictEntry PyDictKeyEntry
78#endif
79#endif
80
81// for 3.0 support (backwards compatibility, really)
82#if PY_VERSION_HEX < 0x03000000
83#define PyBytes_Check PyString_Check
84#define PyBytes_CheckExact PyString_CheckExact
85#define PyBytes_AS_STRING PyString_AS_STRING
86#define PyBytes_AsString PyString_AsString
87#define PyBytes_GET_SIZE PyString_GET_SIZE
88#define PyBytes_Size PyString_Size
89#define PyBytes_FromFormat PyString_FromFormat
90#define PyBytes_FromString PyString_FromString
91#define PyBytes_FromStringAndSize PyString_FromStringAndSize
92
93#define PyBytes_Type PyString_Type
94
95#define CPyCppyy_PyText_Check PyString_Check
96#define CPyCppyy_PyText_CheckExact PyString_CheckExact
97#define CPyCppyy_PyText_AsString PyString_AS_STRING
98#define CPyCppyy_PyText_AsStringChecked PyString_AsString
99#define CPyCppyy_PyText_GET_SIZE PyString_GET_SIZE
100#define CPyCppyy_PyText_GetSize PyString_Size
101#define CPyCppyy_PyText_FromFormat PyString_FromFormat
102#define CPyCppyy_PyText_FromString PyString_FromString
103#define CPyCppyy_PyText_InternFromString PyString_InternFromString
104#define CPyCppyy_PyText_Append PyString_Concat
105#define CPyCppyy_PyText_AppendAndDel PyString_ConcatAndDel
106#define CPyCppyy_PyText_FromStringAndSize PyString_FromStringAndSize
107
108static inline const char* CPyCppyy_PyText_AsStringAndSize(PyObject* pystr, Py_ssize_t* size)
109{
110 const char* cstr = CPyCppyy_PyText_AsStringChecked(pystr);
111 if (cstr) *size = CPyCppyy_PyText_GetSize(pystr);
112 return cstr;
113}
114
115#define CPyCppyy_PyText_Type PyString_Type
116
117#define CPyCppyy_PyUnicode_GET_SIZE PyUnicode_GET_SIZE
118
120 void* cobj, const char* /* name */, void (*destr)(void*))
121{
122 return PyCObject_FromVoidPtr(cobj, destr);
123}
124#define CPyCppyy_PyCapsule_CheckExact PyCObject_Check
125static inline void* CPyCppyy_PyCapsule_GetPointer(PyObject* capsule, const char* /* name */)
126{
127 return (void*)PyCObject_AsVoidPtr(capsule);
128}
129
130#define CPPYY__long__ "__long__"
131#define CPPYY__idiv__ "__idiv__"
132#define CPPYY__div__ "__div__"
133#define CPPYY__next__ "next"
134
135typedef long Py_hash_t;
136
137#endif // ! 3.0
138
139// for 3.0 support (backwards compatibility, really)
140#if PY_VERSION_HEX >= 0x03000000
141#define CPyCppyy_PyText_Check PyUnicode_Check
142#define CPyCppyy_PyText_CheckExact PyUnicode_CheckExact
143#define CPyCppyy_PyText_AsString PyUnicode_AsUTF8
144#define CPyCppyy_PyText_AsStringChecked PyUnicode_AsUTF8
145#define CPyCppyy_PyText_GetSize PyUnicode_GetSize
146#if PY_VERSION_HEX < 0x03090000
147#define CPyCppyy_PyText_GET_SIZE PyUnicode_GET_SIZE
148#define CPyCppyy_PyUnicode_GET_SIZE PyUnicode_GET_LENGTH
149#else
150#define CPyCppyy_PyText_GET_SIZE PyUnicode_GET_LENGTH
151#define CPyCppyy_PyUnicode_GET_SIZE PyUnicode_GET_LENGTH
152#endif
153#define CPyCppyy_PyText_FromFormat PyUnicode_FromFormat
154#define CPyCppyy_PyText_FromString PyUnicode_FromString
155#define CPyCppyy_PyText_InternFromString PyUnicode_InternFromString
156#define CPyCppyy_PyText_Append PyUnicode_Append
157#define CPyCppyy_PyText_AppendAndDel PyUnicode_AppendAndDel
158#define CPyCppyy_PyText_FromStringAndSize PyUnicode_FromStringAndSize
159
160#if PY_VERSION_HEX >= 0x03030000
161#define _CPyCppyy_PyText_AsStringAndSize PyUnicode_AsUTF8AndSize
162#else
163#define _CPyCppyy_PyText_AsStringAndSize PyUnicode_AsStringAndSize
164#endif // >= 3.3
165
166static inline const char* CPyCppyy_PyText_AsStringAndSize(PyObject* pystr, Py_ssize_t* size)
167{
168 const char* cstr = _CPyCppyy_PyText_AsStringAndSize(pystr, size);
169 if (!cstr && PyBytes_CheckExact(pystr)) {
170 PyErr_Clear();
171 PyBytes_AsStringAndSize(pystr, (char**)&cstr, size);
172 }
173 return cstr;
174}
175
176#define CPyCppyy_PyText_Type PyUnicode_Type
177
178#define PyIntObject PyLongObject
179#define PyInt_Check PyLong_Check
180#define PyInt_AsLong PyLong_AsLong
181#define PyInt_AS_LONG PyLong_AsLong
182#define PyInt_AsSsize_t PyLong_AsSsize_t
183#define PyInt_CheckExact PyLong_CheckExact
184#define PyInt_FromLong PyLong_FromLong
185#define PyInt_FromSsize_t PyLong_FromSsize_t
186
187#define PyInt_Type PyLong_Type
188
189#define CPyCppyy_PyCapsule_New PyCapsule_New
190#define CPyCppyy_PyCapsule_CheckExact PyCapsule_CheckExact
191#define CPyCppyy_PyCapsule_GetPointer PyCapsule_GetPointer
192
193#define CPPYY__long__ "__int__"
194#define CPPYY__idiv__ "__itruediv__"
195#define CPPYY__div__ "__truediv__"
196#define CPPYY__next__ "__next__"
197
198#define Py_TPFLAGS_HAVE_RICHCOMPARE 0
199#define Py_TPFLAGS_CHECKTYPES 0
200
201#define PyClass_Check PyType_Check
202
203#define PyBuffer_Type PyMemoryView_Type
204#endif // ! 3.0
205
206#if PY_VERSION_HEX >= 0x03020000
207#define CPyCppyy_PySliceCast PyObject*
208#define PyUnicode_GetSize PyUnicode_GetLength
209#else
210#define CPyCppyy_PySliceCast PySliceObject*
211#endif // >= 3.2
212
213// feature of 3.0 not in 2.5 and earlier
214#if PY_VERSION_HEX < 0x02060000
215#define PyVarObject_HEAD_INIT(type, size) \
216 PyObject_HEAD_INIT(type) size,
217#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
218#endif
219
220// API changes in 2.5 (int -> Py_ssize_t) and 3.2 (PyUnicodeObject -> PyObject)
221#if PY_VERSION_HEX < 0x03020000
223{
224#if PY_VERSION_HEX < 0x02050000
225 return (Py_ssize_t)PyUnicode_AsWideChar((PyUnicodeObject*)pyobj, w, (int)size);
226#else
227 return PyUnicode_AsWideChar((PyUnicodeObject*)pyobj, w, size);
228#endif
229}
230#else
231#define CPyCppyy_PyUnicode_AsWideChar PyUnicode_AsWideChar
232#endif
233
234// backwards compatibility, pre python 2.5
235#if PY_VERSION_HEX < 0x02050000
236typedef int Py_ssize_t;
237#define PyInt_AsSsize_t PyInt_AsLong
238#define PyInt_FromSsize_t PyInt_FromLong
239# define PY_SSIZE_T_FORMAT "%d"
240# if !defined(PY_SSIZE_T_MIN)
241# define PY_SSIZE_T_MAX INT_MAX
242# define PY_SSIZE_T_MIN INT_MIN
243# endif
244#define ssizeobjargproc intobjargproc
245#define lenfunc inquiry
246#define ssizeargfunc intargfunc
247
248#define PyIndex_Check(obj) \
249 (PyInt_Check(obj) || PyLong_Check(obj))
250
252 return (Py_ssize_t)PyLong_AsLong(obj);
253}
254
255#else
256# ifdef R__MACOSX
257# if SIZEOF_SIZE_T == SIZEOF_INT
258# if defined(MAC_OS_X_VERSION_10_4)
259# define PY_SSIZE_T_FORMAT "%ld"
260# else
261# define PY_SSIZE_T_FORMAT "%d"
262# endif
263# elif SIZEOF_SIZE_T == SIZEOF_LONG
264# define PY_SSIZE_T_FORMAT "%ld"
265# endif
266# else
267# define PY_SSIZE_T_FORMAT "%zd"
268# endif
269#endif
270
271#if PY_VERSION_HEX < 0x02020000
272#define PyBool_FromLong PyInt_FromLong
273#endif
274
275#if PY_VERSION_HEX < 0x03000000
276// the following should quiet Solaris
277#ifdef Py_False
278#undef Py_False
279#define Py_False ((PyObject*)(void*)&_Py_ZeroStruct)
280#endif
281
282#ifdef Py_True
283#undef Py_True
284#define Py_True ((PyObject*)(void*)&_Py_TrueStruct)
285#endif
286#endif
287
288#ifndef Py_RETURN_NONE
289#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
290#endif
291
292#ifndef Py_RETURN_TRUE
293#define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True
294#endif
295
296#ifndef Py_RETURN_FALSE
297#define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False
298#endif
299
300// vector call support
301#if PY_VERSION_HEX >= 0x03090000
302#define CPyCppyy_PyCFunction_Call PyObject_Call
303#else
304#define CPyCppyy_PyCFunction_Call PyCFunction_Call
305#endif
306
307// Py_TYPE is changed to an inline static function in 3.11
308#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_TYPE)
309static inline
310void _Py_SET_TYPE(PyObject *ob, PyTypeObject *type) { ob->ob_type = type; }
311#define Py_SET_TYPE(ob, type) _Py_SET_TYPE((PyObject*)(ob), type)
312#endif
313
314// C++ version of the cppyy API
315#include "Cppyy.h"
316
317// export macros for our own API
318#include "CPyCppyy/CommonDefs.h"
319
320#endif // !CPYCPPYY_CPYCPPYY_H
static Py_ssize_t CPyCppyy_PyUnicode_AsWideChar(PyObject *pyobj, wchar_t *w, Py_ssize_t size)
Definition CPyCppyy.h:222
float Float16_t
Definition CPyCppyy.h:53
PyDictEntry *(* dict_lookup_func)(PyDictObject *, PyObject *, long)
Definition CPyCppyy.h:69
dim_t * dims_t
Definition CPyCppyy.h:65
double Double32_t
Definition CPyCppyy.h:54
int Int_t
Definition CPyCppyy.h:43
Py_ssize_t PyNumber_AsSsize_t(PyObject *obj, PyObject *)
Definition CPyCppyy.h:251
int Py_ssize_t
Definition CPyCppyy.h:236
static void * CPyCppyy_PyCapsule_GetPointer(PyObject *capsule, const char *)
Definition CPyCppyy.h:125
static PyObject * CPyCppyy_PyCapsule_New(void *cobj, const char *, void(*destr)(void *))
Definition CPyCppyy.h:119
#define CPyCppyy_PyText_GetSize
Definition CPyCppyy.h:100
unsigned int UInt_t
Definition CPyCppyy.h:44
unsigned long ULong_t
Definition CPyCppyy.h:51
long Py_hash_t
Definition CPyCppyy.h:135
long Long_t
Definition CPyCppyy.h:50
Py_ssize_t dim_t
Definition CPyCppyy.h:64
#define PyBytes_CheckExact
Definition CPyCppyy.h:84
static const char * CPyCppyy_PyText_AsStringAndSize(PyObject *pystr, Py_ssize_t *size)
Definition CPyCppyy.h:108
long double LongDouble_t
Definition CPyCppyy.h:55
static void _Py_SET_TYPE(PyObject *ob, PyTypeObject *type)
Definition CPyCppyy.h:310
#define CPyCppyy_PyText_AsStringChecked
Definition CPyCppyy.h:98
long long Long64_t
Definition CPyCppyy.h:60
unsigned long long ULong64_t
Definition CPyCppyy.h:61
long
_object PyObject
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
int type
Definition TGX11.cxx:121