Logo ROOT  
Reference Guide
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
118 void* cobj, const char* /* name */, void (*destr)(void*))
119{
120 return PyCObject_FromVoidPtr(cobj, destr);
121}
122#define CPyCppyy_PyCapsule_CheckExact PyCObject_Check
123static inline void* CPyCppyy_PyCapsule_GetPointer(PyObject* capsule, const char* /* name */)
124{
125 return (void*)PyCObject_AsVoidPtr(capsule);
126}
127
128#define CPPYY__long__ "__long__"
129#define CPPYY__idiv__ "__idiv__"
130#define CPPYY__div__ "__div__"
131#define CPPYY__next__ "next"
132
133typedef long Py_hash_t;
134
135#endif // ! 3.0
136
137// for 3.0 support (backwards compatibility, really)
138#if PY_VERSION_HEX >= 0x03000000
139#define CPyCppyy_PyText_Check PyUnicode_Check
140#define CPyCppyy_PyText_CheckExact PyUnicode_CheckExact
141#define CPyCppyy_PyText_AsString PyUnicode_AsUTF8
142#define CPyCppyy_PyText_AsStringChecked PyUnicode_AsUTF8
143#define CPyCppyy_PyText_GetSize PyUnicode_GetSize
144#define CPyCppyy_PyText_GET_SIZE PyUnicode_GET_SIZE
145#define CPyCppyy_PyText_FromFormat PyUnicode_FromFormat
146#define CPyCppyy_PyText_FromString PyUnicode_FromString
147#define CPyCppyy_PyText_InternFromString PyUnicode_InternFromString
148#define CPyCppyy_PyText_Append PyUnicode_Append
149#define CPyCppyy_PyText_AppendAndDel PyUnicode_AppendAndDel
150#define CPyCppyy_PyText_FromStringAndSize PyUnicode_FromStringAndSize
151
152#if PY_VERSION_HEX >= 0x03030000
153#define _CPyCppyy_PyText_AsStringAndSize PyUnicode_AsUTF8AndSize
154#else
155#define _CPyCppyy_PyText_AsStringAndSize PyUnicode_AsStringAndSize
156#endif // >= 3.3
157
158static inline const char* CPyCppyy_PyText_AsStringAndSize(PyObject* pystr, Py_ssize_t* size)
159{
160 const char* cstr = _CPyCppyy_PyText_AsStringAndSize(pystr, size);
161 if (!cstr && PyBytes_CheckExact(pystr)) {
162 PyErr_Clear();
163 PyBytes_AsStringAndSize(pystr, (char**)&cstr, size);
164 }
165 return cstr;
166}
167
168#define CPyCppyy_PyText_Type PyUnicode_Type
169
170#define PyIntObject PyLongObject
171#define PyInt_Check PyLong_Check
172#define PyInt_AsLong PyLong_AsLong
173#define PyInt_AS_LONG PyLong_AsLong
174#define PyInt_AsSsize_t PyLong_AsSsize_t
175#define PyInt_CheckExact PyLong_CheckExact
176#define PyInt_FromLong PyLong_FromLong
177#define PyInt_FromSsize_t PyLong_FromSsize_t
178
179#define PyInt_Type PyLong_Type
180
181#define CPyCppyy_PyCapsule_New PyCapsule_New
182#define CPyCppyy_PyCapsule_CheckExact PyCapsule_CheckExact
183#define CPyCppyy_PyCapsule_GetPointer PyCapsule_GetPointer
184
185#define CPPYY__long__ "__int__"
186#define CPPYY__idiv__ "__itruediv__"
187#define CPPYY__div__ "__truediv__"
188#define CPPYY__next__ "__next__"
189
190#define Py_TPFLAGS_HAVE_RICHCOMPARE 0
191#define Py_TPFLAGS_CHECKTYPES 0
192
193#define PyClass_Check PyType_Check
194
195#define PyBuffer_Type PyMemoryView_Type
196#endif // ! 3.0
197
198#if PY_VERSION_HEX >= 0x03020000
199#define CPyCppyy_PySliceCast PyObject*
200#define PyUnicode_GetSize PyUnicode_GetLength
201#else
202#define CPyCppyy_PySliceCast PySliceObject*
203#endif // >= 3.2
204
205// feature of 3.0 not in 2.5 and earlier
206#if PY_VERSION_HEX < 0x02060000
207#define PyVarObject_HEAD_INIT(type, size) \
208 PyObject_HEAD_INIT(type) size,
209#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
210#endif
211
212// API changes in 2.5 (int -> Py_ssize_t) and 3.2 (PyUnicodeObject -> PyObject)
213#if PY_VERSION_HEX < 0x03020000
214static inline Py_ssize_t CPyCppyy_PyUnicode_AsWideChar(PyObject* pyobj, wchar_t* w, Py_ssize_t size)
215{
216#if PY_VERSION_HEX < 0x02050000
217 return (Py_ssize_t)PyUnicode_AsWideChar((PyUnicodeObject*)pyobj, w, (int)size);
218#else
219 return PyUnicode_AsWideChar((PyUnicodeObject*)pyobj, w, size);
220#endif
221}
222#else
223#define CPyCppyy_PyUnicode_AsWideChar PyUnicode_AsWideChar
224#endif
225
226// backwards compatibility, pre python 2.5
227#if PY_VERSION_HEX < 0x02050000
228typedef int Py_ssize_t;
229#define PyInt_AsSsize_t PyInt_AsLong
230#define PyInt_FromSsize_t PyInt_FromLong
231# define PY_SSIZE_T_FORMAT "%d"
232# if !defined(PY_SSIZE_T_MIN)
233# define PY_SSIZE_T_MAX INT_MAX
234# define PY_SSIZE_T_MIN INT_MIN
235# endif
236#define ssizeobjargproc intobjargproc
237#define lenfunc inquiry
238#define ssizeargfunc intargfunc
239
240#define PyIndex_Check(obj) \
241 (PyInt_Check(obj) || PyLong_Check(obj))
242
244 return (Py_ssize_t)PyLong_AsLong(obj);
245}
246
247#else
248# ifdef R__MACOSX
249# if SIZEOF_SIZE_T == SIZEOF_INT
250# if defined(MAC_OS_X_VERSION_10_4)
251# define PY_SSIZE_T_FORMAT "%ld"
252# else
253# define PY_SSIZE_T_FORMAT "%d"
254# endif
255# elif SIZEOF_SIZE_T == SIZEOF_LONG
256# define PY_SSIZE_T_FORMAT "%ld"
257# endif
258# else
259# define PY_SSIZE_T_FORMAT "%zd"
260# endif
261#endif
262
263#if PY_VERSION_HEX < 0x02020000
264#define PyBool_FromLong PyInt_FromLong
265#endif
266
267#if PY_VERSION_HEX < 0x03000000
268// the following should quiet Solaris
269#ifdef Py_False
270#undef Py_False
271#define Py_False ((PyObject*)(void*)&_Py_ZeroStruct)
272#endif
273
274#ifdef Py_True
275#undef Py_True
276#define Py_True ((PyObject*)(void*)&_Py_TrueStruct)
277#endif
278#endif
279
280#ifndef Py_RETURN_NONE
281#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
282#endif
283
284#ifndef Py_RETURN_TRUE
285#define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True
286#endif
287
288#ifndef Py_RETURN_FALSE
289#define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False
290#endif
291
292// C++ version of the cppyy API
293#include "Cppyy.h"
294
295// export macros for our own API
296#include "CPyCppyy/CommonDefs.h"
297
298#endif // !CPYCPPYY_CPYCPPYY_H
static Py_ssize_t CPyCppyy_PyUnicode_AsWideChar(PyObject *pyobj, wchar_t *w, Py_ssize_t size)
Definition: CPyCppyy.h:214
float Float16_t
Definition: CPyCppyy.h:53
dim_t * dims_t
Definition: CPyCppyy.h:65
static const char * CPyCppyy_PyText_AsStringAndSize(PyObject *pystr, Py_ssize_t *size)
Definition: CPyCppyy.h:108
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:243
int Py_ssize_t
Definition: CPyCppyy.h:228
static PyObject * CPyCppyy_PyCapsule_New(void *cobj, const char *, void(*destr)(void *))
Definition: CPyCppyy.h:117
#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:133
long Long_t
Definition: CPyCppyy.h:50
static void * CPyCppyy_PyCapsule_GetPointer(PyObject *capsule, const char *)
Definition: CPyCppyy.h:123
PyDictEntry *(* dict_lookup_func)(PyDictObject *, PyObject *, long)
Definition: CPyCppyy.h:69
Py_ssize_t dim_t
Definition: CPyCppyy.h:64
#define PyBytes_CheckExact
Definition: CPyCppyy.h:84
long double LongDouble_t
Definition: CPyCppyy.h:55
#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
Definition: Converters.cxx:858
_object PyObject
Definition: PyResult.h:13