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
16extern bool gDictLookupActive;
17#endif
18
19// additional converter functions
22
23namespace Utility {
24
25// convenience functions for adding methods to classes
26bool AddToClass(PyObject* pyclass, const char* label, PyCFunction cfunc,
27 int flags = METH_VARARGS);
28bool AddToClass(PyObject* pyclass, const char* label, const char* func);
29bool AddToClass(PyObject* pyclass, const char* label, PyCallable* pyfunc);
30
31// helpers for dynamically constructing operators
32PyCallable* FindUnaryOperator(PyObject* pyclass, const char* op);
33PyCallable* FindBinaryOperator(PyObject* left, PyObject* right,
34 const char* op, Cppyy::TCppScope_t scope = 0);
35PyCallable* FindBinaryOperator(const std::string& lcname, const std::string& rcname,
36 const char* op, Cppyy::TCppScope_t scope = 0, bool reverse = false);
37
38// helper for template classes and methods
40std::string ConstructTemplateArgs(
41 PyObject* pyname, PyObject* tpArgs, PyObject* args = nullptr, ArgPreference = kNone, int argoff = 0, int* pcnt = nullptr);
42std::string CT2CppNameS(PyObject* pytc, bool allow_voidp);
43inline PyObject* CT2CppName(PyObject* pytc, const char* cpd, bool allow_voidp)
44{
45 const std::string& name = CT2CppNameS(pytc, allow_voidp);
46 if (!name.empty()) {
47 if (name == "const char*") cpd = "";
48#if PY_VERSION_HEX < 0x03000000
49 return PyString_FromString((std::string{name}+cpd).c_str());
50#else
51 return PyUnicode_FromString((std::string{name}+cpd).c_str());
52#endif
53 }
54 return nullptr;
55}
56
57// helper for generating callbacks
58void ConstructCallbackPreamble(const std::string& retType,
59 const std::vector<std::string>& argtypes, std::ostringstream& code);
60void ConstructCallbackReturn(const std::string& retType, int nArgs, std::ostringstream& code);
61
62// helper for function pointer conversions
63PyObject* FuncPtr2StdFunction(const std::string& retType, const std::string& signature, void* address);
64
65// initialize proxy type objects
66bool InitProxy(PyObject* module, PyTypeObject* pytype, const char* name);
67
68std::map<std::string, char> const &TypecodeMap();
69
70// retrieve the memory buffer from pyobject, return buflength, tc (optional) is python
71// array.array type code, size is type size, buf will point to buffer, and if check is
72// true, some heuristics will be applied to check buffer compatibility with the type
73Py_ssize_t GetBuffer(PyObject* pyobject, char tc, int size, void*& buf, bool check = true);
74
75// data/operator mappings
76std::string MapOperatorName(const std::string& name, bool bTakesParames, bool* stubbed = nullptr);
77
79 PyOperators() : fEq(nullptr), fNe(nullptr), fLt(nullptr), fLe(nullptr), fGt(nullptr), fGe(nullptr),
80 fLAdd(nullptr), fRAdd(nullptr), fSub(nullptr), fLMul(nullptr), fRMul(nullptr), fDiv(nullptr),
81 fHash(nullptr) {}
83
93};
94
95// meta information
96std::string ClassName(PyObject* pyobj);
97bool IsSTLIterator(const std::string& classname);
98
99// for threading: save call to PyErr_Occurred()
101
102// helpers for collecting/maintaining python exception data
103struct PyError_t {
105 void operator()(PyObject *obj) { Py_XDECREF(obj); }
106 };
107#if PY_VERSION_HEX < 0x030c0000
108 std::unique_ptr<PyObject, PyObjectDeleter> fType;
109 std::unique_ptr<PyObject, PyObjectDeleter> fTrace;
110#endif
111 std::unique_ptr<PyObject, PyObjectDeleter> fValue;
112 bool fIsCpp = false;
113};
114
116void RestorePyError(PyError_t &error);
117
118size_t FetchError(std::vector<PyError_t>&, bool is_cpp = false);
120 std::vector<PyError_t>&& errors /* clears */, PyObject* topmsg /* steals ref */, PyObject* defexc);
121
122// setup Python API for callbacks
123bool IncludePython();
124
125} // namespace Utility
126
127} // namespace CPyCppyy
128
129#endif // !CPYCPPYY_UTILITY_H
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:146
PyCallable * FindBinaryOperator(PyObject *left, PyObject *right, const char *op, Cppyy::TCppScope_t scope=0)
Definition Utility.cxx:297
std::string CT2CppNameS(PyObject *pytc, bool allow_voidp)
Definition Utility.cxx:677
void ConstructCallbackPreamble(const std::string &retType, const std::vector< std::string > &argtypes, std::ostringstream &code)
Definition Utility.cxx:713
void ConstructCallbackReturn(const std::string &retType, int nArgs, std::ostringstream &code)
Definition Utility.cxx:779
PyObject * CT2CppName(PyObject *pytc, const char *cpd, bool allow_voidp)
Definition Utility.h:43
void RestorePyError(PyError_t &error)
Definition Utility.cxx:1208
std::map< std::string, char > const & TypecodeMap()
Definition Utility.cxx:892
void SetDetailedException(std::vector< PyError_t > &&errors, PyObject *topmsg, PyObject *defexc)
Definition Utility.cxx:1230
Py_ssize_t GetBuffer(PyObject *pyobject, char tc, int size, void *&buf, bool check=true)
Definition Utility.cxx:919
std::string ConstructTemplateArgs(PyObject *pyname, PyObject *tpArgs, PyObject *args=nullptr, ArgPreference=kNone, int argoff=0, int *pcnt=nullptr)
Definition Utility.cxx:635
PyObject * FuncPtr2StdFunction(const std::string &retType, const std::string &signature, void *address)
Definition Utility.cxx:816
PyCallable * FindUnaryOperator(PyObject *pyclass, const char *op)
Definition Utility.cxx:282
size_t FetchError(std::vector< PyError_t > &, bool is_cpp=false)
Definition Utility.cxx:1219
std::string MapOperatorName(const std::string &name, bool bTakesParames, bool *stubbed=nullptr)
Definition Utility.cxx:1042
bool InitProxy(PyObject *module, PyTypeObject *pytype, const char *name)
Definition Utility.cxx:872
bool AddToClass(PyObject *pyclass, const char *label, PyCFunction cfunc, int flags=METH_VARARGS)
Definition Utility.cxx:185
PyError_t FetchPyError()
Definition Utility.cxx:1188
bool IsSTLIterator(const std::string &classname)
Definition Utility.cxx:1128
std::string ClassName(PyObject *pyobj)
Definition Utility.cxx:1107
PyObject * PyErr_Occurred_WithGIL()
Definition Utility.cxx:1168
unsigned long PyLongOrInt_AsULong(PyObject *pyobject)
Definition Utility.cxx:132
bool gDictLookupActive
Definition Utility.cxx:27
PY_ULONG_LONG PyLongOrInt_AsULong64(PyObject *pyobject)
Definition Utility.cxx:159
size_t TCppScope_t
Definition cpp_cppyy.h:34
std::unique_ptr< PyObject, PyObjectDeleter > fTrace
Definition Utility.h:109
std::unique_ptr< PyObject, PyObjectDeleter > fValue
Definition Utility.h:111
std::unique_ptr< PyObject, PyObjectDeleter > fType
Definition Utility.h:108