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);
43std::string CT2CppNameS(PyObject* pytc, bool allow_voidp);
44inline PyObject* CT2CppName(PyObject* pytc, const char* cpd, bool allow_voidp)
45{
46 const std::string& name = CT2CppNameS(pytc, allow_voidp);
47 if (!name.empty()) {
48 if (name == "const char*") cpd = "";
49 return CPyCppyy_PyText_FromString((std::string{name}+cpd).c_str());
50 }
51 return nullptr;
52}
53
54// helper for generating callbacks
55void ConstructCallbackPreamble(const std::string& retType,
56 const std::vector<std::string>& argtypes, std::ostringstream& code);
57void ConstructCallbackReturn(const std::string& retType, int nArgs, std::ostringstream& code);
58
59// helper for function pointer conversions
60PyObject* FuncPtr2StdFunction(const std::string& retType, const std::string& signature, void* address);
61
62// initialize proxy type objects
63bool InitProxy(PyObject* module, PyTypeObject* pytype, const char* name);
64
65std::map<std::string, char> const &TypecodeMap();
66
67// retrieve the memory buffer from pyobject, return buflength, tc (optional) is python
68// array.array type code, size is type size, buf will point to buffer, and if check is
69// true, some heuristics will be applied to check buffer compatibility with the type
70Py_ssize_t GetBuffer(PyObject* pyobject, char tc, int size, void*& buf, bool check = true);
71
72// data/operator mappings
73std::string MapOperatorName(const std::string& name, bool bTakesParames, bool* stubbed = nullptr);
74
76 PyOperators() : fEq(nullptr), fNe(nullptr), fLt(nullptr), fLe(nullptr), fGt(nullptr), fGe(nullptr),
77 fLAdd(nullptr), fRAdd(nullptr), fSub(nullptr), fLMul(nullptr), fRMul(nullptr), fDiv(nullptr),
78 fHash(nullptr) {}
80
90};
91
92// meta information
93std::string ClassName(PyObject* pyobj);
94bool IsSTLIterator(const std::string& classname);
95
96// for threading: save call to PyErr_Occurred()
98
99// helpers for collecting/maintaining python exception data
100struct PyError_t {
102 void operator()(PyObject *obj) { Py_XDECREF(obj); }
103 };
104#if PY_VERSION_HEX < 0x030c0000
105 std::unique_ptr<PyObject, PyObjectDeleter> fType;
106 std::unique_ptr<PyObject, PyObjectDeleter> fTrace;
107#endif
108 std::unique_ptr<PyObject, PyObjectDeleter> fValue;
109 bool fIsCpp = false;
110};
111
113void RestorePyError(PyError_t &error);
114
115size_t FetchError(std::vector<PyError_t>&, bool is_cpp = false);
117 std::vector<PyError_t>&& errors /* clears */, PyObject* topmsg /* steals ref */, PyObject* defexc);
118
119// setup Python API for callbacks
120bool IncludePython();
121
122} // namespace Utility
123
124} // namespace CPyCppyy
125
126#endif // !CPYCPPYY_UTILITY_H
PyDictEntry *(* dict_lookup_func)(PyDictObject *, PyObject *, long)
Definition CPyCppyy.h:44
int Py_ssize_t
Definition CPyCppyy.h:215
#define CPyCppyy_PyText_FromString
Definition CPyCppyy.h:81
_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:157
PyCallable * FindBinaryOperator(PyObject *left, PyObject *right, const char *op, Cppyy::TCppScope_t scope=0)
Definition Utility.cxx:298
std::string CT2CppNameS(PyObject *pytc, bool allow_voidp)
Definition Utility.cxx:678
void ConstructCallbackPreamble(const std::string &retType, const std::vector< std::string > &argtypes, std::ostringstream &code)
Definition Utility.cxx:714
void ConstructCallbackReturn(const std::string &retType, int nArgs, std::ostringstream &code)
Definition Utility.cxx:780
PyObject * CT2CppName(PyObject *pytc, const char *cpd, bool allow_voidp)
Definition Utility.h:44
void RestorePyError(PyError_t &error)
Definition Utility.cxx:1209
std::map< std::string, char > const & TypecodeMap()
Definition Utility.cxx:893
void SetDetailedException(std::vector< PyError_t > &&errors, PyObject *topmsg, PyObject *defexc)
Definition Utility.cxx:1231
Py_ssize_t GetBuffer(PyObject *pyobject, char tc, int size, void *&buf, bool check=true)
Definition Utility.cxx:920
std::string ConstructTemplateArgs(PyObject *pyname, PyObject *tpArgs, PyObject *args=nullptr, ArgPreference=kNone, int argoff=0, int *pcnt=nullptr)
Definition Utility.cxx:636
PyObject * FuncPtr2StdFunction(const std::string &retType, const std::string &signature, void *address)
Definition Utility.cxx:817
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:1220
std::string MapOperatorName(const std::string &name, bool bTakesParames, bool *stubbed=nullptr)
Definition Utility.cxx:1043
bool InitProxy(PyObject *module, PyTypeObject *pytype, const char *name)
Definition Utility.cxx:873
bool AddToClass(PyObject *pyclass, const char *label, PyCFunction cfunc, int flags=METH_VARARGS)
Definition Utility.cxx:186
PyError_t FetchPyError()
Definition Utility.cxx:1189
bool IsSTLIterator(const std::string &classname)
Definition Utility.cxx:1129
std::string ClassName(PyObject *pyobj)
Definition Utility.cxx:1108
PyObject * PyErr_Occurred_WithGIL()
Definition Utility.cxx:1169
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:34
std::unique_ptr< PyObject, PyObjectDeleter > fTrace
Definition Utility.h:106
std::unique_ptr< PyObject, PyObjectDeleter > fValue
Definition Utility.h:108
std::unique_ptr< PyObject, PyObjectDeleter > fType
Definition Utility.h:105