Logo ROOT  
Reference Guide
Utility.h
Go to the documentation of this file.
1 #ifndef CPYCPPYY_UTILITY_H
2 #define CPYCPPYY_UTILITY_H
3 
4 // Standard
5 #include <string>
6 #include <vector>
7 
8 
9 namespace CPyCppyy {
10 
11 class PyCallable;
12 
14 extern bool gDictLookupActive;
15 
16 // additional converter functions
17 unsigned long PyLongOrInt_AsULong(PyObject* pyobject);
19 
20 namespace Utility {
21 
22 // convenience functions for adding methods to classes
23 bool AddToClass(PyObject* pyclass, const char* label, PyCFunction cfunc,
24  int flags = METH_VARARGS);
25 bool AddToClass(PyObject* pyclass, const char* label, const char* func);
26 bool AddToClass(PyObject* pyclass, const char* label, PyCallable* pyfunc);
27 
28 // helpers for dynamically constructing operators
29 PyCallable* FindUnaryOperator(PyObject* pyclass, const char* op);
30 PyCallable* FindBinaryOperator(PyObject* left, PyObject* right,
31  const char* op, Cppyy::TCppScope_t scope = 0);
32 PyCallable* FindBinaryOperator(const std::string& lcname, const std::string& rcname,
33  const char* op, Cppyy::TCppScope_t scope = 0, bool reverse = false);
34 
35 // helper for template classes and methods
37 std::string ConstructTemplateArgs(
38  PyObject* pyname, PyObject* tpArgs, PyObject* args = nullptr, ArgPreference = kNone, int argoff = 0, int* pcnt = nullptr);
39 
40 // helper for generating callbacks
41 void ConstructCallbackPreamble(const std::string& retType,
42  const std::vector<std::string>& argtypes, std::ostringstream& code);
43 void ConstructCallbackReturn(const std::string& retType, int nArgs, std::ostringstream& code);
44 
45 // initialize proxy type objects
46 bool InitProxy(PyObject* module, PyTypeObject* pytype, const char* name);
47 
48 // retrieve the memory buffer from pyobject, return buflength, tc (optional) is python
49 // array.array type code, size is type size, buf will point to buffer, and if check is
50 // true, some heuristics will be applied to check buffer compatibility with the type
51 Py_ssize_t GetBuffer(PyObject* pyobject, char tc, int size, void*& buf, bool check = true);
52 
53 // data/operator mappings
54 std::string MapOperatorName(const std::string& name, bool bTakesParames);
55 
56 struct PyOperators {
57  PyOperators() : fEq(nullptr), fNe(nullptr), fLAdd(nullptr), fRAdd(nullptr),
58  fSub(nullptr), fLMul(nullptr), fRMul(nullptr), fDiv(nullptr), fHash(nullptr) {}
59  ~PyOperators();
60 
68 };
69 
70 // meta information
71 const std::string Compound(const std::string& name);
72 Py_ssize_t ArraySize(const std::string& name);
73 std::string ClassName(PyObject* pyobj);
74 
75 // for threading: save call to PyErr_Occurred()
77 
78 // helpers for collecting/maintaining python exception data
79 struct PyError_t {
80  PyError_t() { fType = fValue = fTrace = 0; }
81 
82  static void Clear(PyError_t& e)
83  {
84  // Remove exception information.
85  Py_XDECREF(e.fType); Py_XDECREF(e.fValue); Py_XDECREF(e.fTrace);
86  e.fType = e.fValue = e.fTrace = 0;
87  }
88 
90 };
91 
92 size_t FetchError(std::vector<PyError_t>&);
94  std::vector<PyError_t>& errors /* clears */, PyObject* topmsg /* steals ref */, PyObject* defexc);
95 
96 // setup Python API for callbacks
97 bool IncludePython();
98 
99 } // namespace Utility
100 
101 } // namespace CPyCppyy
102 
103 #endif // !CPYCPPYY_UTILITY_H
#define pyname
Definition: TMCParticle.cxx:19
bool gDictLookupActive
Definition: Utility.cxx:27
ULong64_t PyLongOrInt_AsULong64(PyObject *pyobject)
Definition: Utility.cxx:150
std::string ClassName(PyObject *pyobj)
Definition: Utility.cxx:825
size_t TCppScope_t
Definition: cpp_cppyy.h:18
bool InitProxy(PyObject *module, PyTypeObject *pytype, const char *name)
Definition: Utility.cxx:594
void SetDetailedException(std::vector< PyError_t > &errors, PyObject *topmsg, PyObject *defexc)
Definition: Utility.cxx:891
Py_ssize_t GetBuffer(PyObject *pyobject, char tc, int size, void *&buf, bool check=true)
Definition: Utility.cxx:614
Py_ssize_t ArraySize(const std::string &name)
Definition: Utility.cxx:806
std::string ConstructTemplateArgs(PyObject *pyname, PyObject *tpArgs, PyObject *args=nullptr, ArgPreference=kNone, int argoff=0, int *pcnt=nullptr)
Definition: Utility.cxx:473
std::string MapOperatorName(const std::string &name, bool bTakesParames)
Definition: Utility.cxx:727
PyObject * PyErr_Occurred_WithGIL()
Definition: Utility.cxx:859
dict_lookup_func gDictLookupOrg
Definition: Utility.cxx:26
void ConstructCallbackReturn(const std::string &retType, int nArgs, std::ostringstream &code)
Definition: Utility.cxx:561
_object PyObject
Definition: PyMethodBase.h:41
bool IncludePython()
Definition: Utility.cxx:935
PyCallable * FindUnaryOperator(PyObject *pyclass, const char *op)
Definition: Utility.cxx:266
int Py_ssize_t
Definition: CPyCppyy.h:228
static void Clear(PyError_t &e)
Definition: Utility.h:82
unsigned long long ULong64_t
Definition: RtypesCore.h:72
void ConstructCallbackPreamble(const std::string &retType, const std::vector< std::string > &argtypes, std::ostringstream &code)
Definition: Utility.cxx:517
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
unsigned long PyLongOrInt_AsULong(PyObject *pyobject)
Definition: Utility.cxx:130
const std::string Compound(const std::string &name)
Definition: Utility.cxx:782
size_t FetchError(std::vector< PyError_t > &)
Definition: Utility.cxx:879
PyDictEntry *(* dict_lookup_func)(PyDictObject *, PyObject *, long)
Definition: CPyCppyy.h:69
bool AddToClass(PyObject *pyclass, const char *label, PyCFunction cfunc, int flags=METH_VARARGS)
Definition: Utility.cxx:169
PyCallable * FindBinaryOperator(PyObject *left, PyObject *right, const char *op, Cppyy::TCppScope_t scope=0)
Definition: Utility.cxx:280
char name[80]
Definition: TGX11.cxx:109