Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Utility.h
Go to the documentation of this file.
1#ifndef CPYCPPYY_UTILITY_H
2#define CPYCPPYY_UTILITY_H
3
4// Standard
5#include <map>
6#include <memory>
7#include <string>
8#include <vector>
9
10
11namespace CPyCppyy {
12
13class PyCallable;
14
15#if PY_VERSION_HEX < 0x030b0000
17extern bool gDictLookupActive;
18#endif
19
20// additional converter functions
23
24namespace Utility {
25
26// convenience functions for adding methods to classes
27bool AddToClass(PyObject* pyclass, const char* label, PyCFunction cfunc,
28 int flags = METH_VARARGS);
29bool AddToClass(PyObject* pyclass, const char* label, const char* func);
30bool AddToClass(PyObject* pyclass, const char* label, PyCallable* pyfunc);
31
32// helpers for dynamically constructing operators
33PyCallable* FindUnaryOperator(PyObject* pyclass, const char* op);
34PyCallable* FindBinaryOperator(PyObject* left, PyObject* right,
35 const char* op, Cppyy::TCppScope_t scope = 0);
36PyCallable* FindBinaryOperator(const std::string& lcname, const std::string& rcname,
37 const char* op, Cppyy::TCppScope_t scope = 0, bool reverse = false);
38
39// helper for template classes and methods
41std::string ConstructTemplateArgs(
42 PyObject* pyname, PyObject* tpArgs, PyObject* args = nullptr, ArgPreference = kNone, int argoff = 0, int* pcnt = nullptr);
43
44// helper for generating callbacks
45void ConstructCallbackPreamble(const std::string& retType,
46 const std::vector<std::string>& argtypes, std::ostringstream& code);
47void ConstructCallbackReturn(const std::string& retType, int nArgs, std::ostringstream& code);
48
49// helper for function pointer conversions
50PyObject* FuncPtr2StdFunction(const std::string& retType, const std::string& signature, void* address);
51
52// initialize proxy type objects
53bool InitProxy(PyObject* module, PyTypeObject* pytype, const char* name);
54
55// retrieve the memory buffer from pyobject, return buflength, tc (optional) is python
56// array.array type code, size is type size, buf will point to buffer, and if check is
57// true, some heuristics will be applied to check buffer compatibility with the type
58Py_ssize_t GetBuffer(PyObject* pyobject, char tc, int size, void*& buf, bool check = true);
59
60// data/operator mappings
61std::string MapOperatorName(const std::string& name, bool bTakesParames, bool* stubbed = nullptr);
62
64 PyOperators() : fEq(nullptr), fNe(nullptr), fLt(nullptr), fLe(nullptr), fGt(nullptr), fGe(nullptr),
65 fLAdd(nullptr), fRAdd(nullptr), fSub(nullptr), fLMul(nullptr), fRMul(nullptr), fDiv(nullptr),
66 fHash(nullptr) {}
68
78};
79
80// meta information
81std::string ClassName(PyObject* pyobj);
82bool IsSTLIterator(const std::string& classname);
83
84// for threading: save call to PyErr_Occurred()
86
87// helpers for collecting/maintaining python exception data
88struct PyError_t {
90 void operator()(PyObject *obj) { Py_XDECREF(obj); }
91 };
92#if PY_VERSION_HEX < 0x030c0000
93 std::unique_ptr<PyObject, PyObjectDeleter> fType;
94 std::unique_ptr<PyObject, PyObjectDeleter> fTrace;
95#endif
96 std::unique_ptr<PyObject, PyObjectDeleter> fValue;
97 bool fIsCpp = false;
98};
99
101void RestorePyError(PyError_t &error);
102
103size_t FetchError(std::vector<PyError_t>&, bool is_cpp = false);
105 std::vector<PyError_t>&& errors /* clears */, PyObject* topmsg /* steals ref */, PyObject* defexc);
106
107// setup Python API for callbacks
108bool IncludePython();
109
110} // namespace Utility
111
112} // namespace CPyCppyy
113
114#endif // !CPYCPPYY_UTILITY_H
PyDictEntry *(* dict_lookup_func)(PyDictObject *, PyObject *, long)
Definition CPyCppyy.h:44
int Py_ssize_t
Definition CPyCppyy.h:215
_object PyObject
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
char name[80]
Definition TGX11.cxx:110
PyCallable * FindBinaryOperator(PyObject *left, PyObject *right, const char *op, Cppyy::TCppScope_t scope=0)
Definition Utility.cxx:298
void ConstructCallbackPreamble(const std::string &retType, const std::vector< std::string > &argtypes, std::ostringstream &code)
Definition Utility.cxx:634
void ConstructCallbackReturn(const std::string &retType, int nArgs, std::ostringstream &code)
Definition Utility.cxx:700
void RestorePyError(PyError_t &error)
Definition Utility.cxx:1102
void SetDetailedException(std::vector< PyError_t > &&errors, PyObject *topmsg, PyObject *defexc)
Definition Utility.cxx:1124
Py_ssize_t GetBuffer(PyObject *pyobject, char tc, int size, void *&buf, bool check=true)
Definition Utility.cxx:813
std::string ConstructTemplateArgs(PyObject *pyname, PyObject *tpArgs, PyObject *args=nullptr, ArgPreference=kNone, int argoff=0, int *pcnt=nullptr)
Definition Utility.cxx:585
PyObject * FuncPtr2StdFunction(const std::string &retType, const std::string &signature, void *address)
Definition Utility.cxx:737
PyCallable * FindUnaryOperator(PyObject *pyclass, const char *op)
Definition Utility.cxx:283
size_t FetchError(std::vector< PyError_t > &, bool is_cpp=false)
Definition Utility.cxx:1113
std::string MapOperatorName(const std::string &name, bool bTakesParames, bool *stubbed=nullptr)
Definition Utility.cxx:936
bool InitProxy(PyObject *module, PyTypeObject *pytype, const char *name)
Definition Utility.cxx:793
bool AddToClass(PyObject *pyclass, const char *label, PyCFunction cfunc, int flags=METH_VARARGS)
Definition Utility.cxx:186
PyError_t FetchPyError()
Definition Utility.cxx:1082
bool IsSTLIterator(const std::string &classname)
Definition Utility.cxx:1022
std::string ClassName(PyObject *pyobj)
Definition Utility.cxx:1001
PyObject * PyErr_Occurred_WithGIL()
Definition Utility.cxx:1062
unsigned long PyLongOrInt_AsULong(PyObject *pyobject)
Definition Utility.cxx:133
bool gDictLookupActive
Definition Utility.cxx:28
dict_lookup_func gDictLookupOrg
Definition Utility.cxx:27
PY_ULONG_LONG PyLongOrInt_AsULong64(PyObject *pyobject)
Definition Utility.cxx:160
size_t TCppScope_t
Definition cpp_cppyy.h:18
std::unique_ptr< PyObject, PyObjectDeleter > fTrace
Definition Utility.h:94
std::unique_ptr< PyObject, PyObjectDeleter > fValue
Definition Utility.h:96
std::unique_ptr< PyObject, PyObjectDeleter > fType
Definition Utility.h:93