Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Utility.h
Go to the documentation of this file.
1#ifndef CPYRT_UTILITY_H
2#define CPYRT_UTILITY_H
3
4// Standard
5#include <memory>
6#include <string>
7#include <unordered_map>
8#include <vector>
9
10#include "Python.h"
11#include "cppjit_interop.h"
12
13namespace cppjit::cpyrt {
14
15class PyCallable;
16
17// additional converter functions
18unsigned long PyLongOrInt_AsULong(PyObject* pyobject);
20
21namespace Utility {
22
23// convenience functions for adding methods to classes
24bool AddToClass(PyObject* pyclass, const char* label, PyCFunction cfunc,
25 int flags = METH_VARARGS);
26bool AddToClass(PyObject* pyclass, const char* label, const char* func);
27bool AddToClass(PyObject* pyclass, const char* label, PyCallable* pyfunc);
28
29// helpers for dynamically constructing operators
30PyCallable* FindUnaryOperator(PyObject* pyclass, const char* op);
31PyCallable*
32FindBinaryOperator(PyObject* left, PyObject* right, const char* op,
34PyCallable* FindBinaryOperator(
35 const std::string& lcname, const std::string& rcname, const char* op,
36 interop::TCppScope_t scope = interop::TCppScope_t{}, bool reverse = false);
37
38// helper for template classes and methods
40std::string ConstructTemplateArgs(PyObject* pyname, PyObject* tpArgs,
41 PyObject* args = nullptr,
42 ArgPreference = kNone, int argoff = 0,
43 int* pcnt = nullptr);
44std::vector<Cpp::TemplateArgInfo>
46 PyObject* args = nullptr, ArgPreference = kNone,
47 int argoff = 0, int* pcnt = nullptr);
48
49std::string CT2CppNameS(PyObject* pytc, bool allow_voidp);
50inline PyObject* CT2CppName(PyObject* pytc, const char* cpd, bool allow_voidp) {
51 const std::string& name = CT2CppNameS(pytc, allow_voidp);
52 if (!name.empty()) {
53 if (name == "const char*")
54 cpd = "";
55 return PyUnicode_FromString((std::string{name} + cpd).c_str());
56 }
57 return nullptr;
58}
59
60// helper for generating callbacks
61void ConstructCallbackPreamble(const std::string& retType,
62 const std::vector<std::string>& argtypes,
63 std::ostringstream& code);
64void ConstructCallbackReturn(const std::string& retType, int nArgs,
65 std::ostringstream& code);
66
67// helper for function pointer conversions
68PyObject* FuncPtr2StdFunction(const std::string& retType,
69 const std::string& signature, void* address);
70
71// initialize proxy type objects
72bool InitProxy(PyObject* module, PyTypeObject* pytype, const char* name);
73
74std::unordered_map<std::string, char> const& TypecodeMap();
75
76// retrieve the memory buffer from pyobject, return buflength, tc (optional) is
77// python array.array type code, size is type size, buf will point to buffer,
78// and if check is true, some heuristics will be applied to check buffer
79// compatibility with the type
80Py_ssize_t GetBuffer(PyObject* pyobject, char tc, int size, void*& buf,
81 bool check = true);
82
83// data/operator mappings
84std::string MapOperatorName(const std::string& name, bool bTakesParames,
85 bool* stubbed = nullptr);
86
89 : fEq(nullptr), fNe(nullptr), fLt(nullptr), fLe(nullptr), fGt(nullptr),
90 fGe(nullptr), fLAdd(nullptr), fRAdd(nullptr), fSub(nullptr),
91 fLMul(nullptr), fRMul(nullptr), fDiv(nullptr), fHash(nullptr) {}
93
103};
104
105// meta information
106std::string ClassName(PyObject* pyobj);
107bool IsSTLIterator(const std::string& classname);
108
109// for threading: save call to PyErr_Occurred()
111
112// helpers for collecting/maintaining python exception data
113struct PyError_t {
115 void operator()(PyObject* obj) { Py_XDECREF(obj); }
116 };
117#if PY_VERSION_HEX < 0x030c0000
118 std::unique_ptr<PyObject, PyObjectDeleter> fType;
119 std::unique_ptr<PyObject, PyObjectDeleter> fTrace;
120#endif
121 std::unique_ptr<PyObject, PyObjectDeleter> fValue;
122 bool fIsCpp = false;
123};
124
126void RestorePyError(PyError_t& error);
127
128size_t FetchError(std::vector<PyError_t>&, bool is_cpp = false);
129void SetDetailedException(std::vector<PyError_t>&& errors /* clears */,
130 PyObject* topmsg /* steals ref */, PyObject* defexc);
131
132// setup Python API for callbacks
133bool IncludePython();
134
135} // namespace Utility
136
137} // namespace cppjit::cpyrt
138
139#endif // !CPYRT_UTILITY_H
_object PyObject
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
char name[80]
Definition TGX11.cxx:142
unsigned long long PY_ULONG_LONG
std::string ConstructTemplateArgs(PyObject *pyname, PyObject *tpArgs, PyObject *args=nullptr, ArgPreference=kNone, int argoff=0, int *pcnt=nullptr)
Definition Utility.cxx:640
std::string ClassName(PyObject *pyobj)
Definition Utility.cxx:1403
void ConstructCallbackPreamble(const std::string &retType, const std::vector< std::string > &argtypes, std::ostringstream &code)
Definition Utility.cxx:1005
std::string CT2CppNameS(PyObject *pytc, bool allow_voidp)
Definition Utility.cxx:936
void RestorePyError(PyError_t &error)
Definition Utility.cxx:1493
void ConstructCallbackReturn(const std::string &retType, int nArgs, std::ostringstream &code)
Definition Utility.cxx:1074
size_t FetchError(std::vector< PyError_t > &, bool is_cpp=false)
Definition Utility.cxx:1503
PyCallable * FindBinaryOperator(PyObject *left, PyObject *right, const char *op, interop::TCppScope_t scope=interop::TCppScope_t{})
Definition Utility.cxx:293
bool IsSTLIterator(const std::string &classname)
Definition Utility.cxx:1423
std::vector< Cpp::TemplateArgInfo > GetTemplateArgsTypes(PyObject *scope, PyObject *tpArgs, PyObject *args=nullptr, ArgPreference=kNone, int argoff=0, int *pcnt=nullptr)
Definition Utility.cxx:893
bool AddToClass(PyObject *pyclass, const char *label, PyCFunction cfunc, int flags=METH_VARARGS)
Definition Utility.cxx:178
PyObject * PyErr_Occurred_WithGIL()
Definition Utility.cxx:1463
bool InitProxy(PyObject *module, PyTypeObject *pytype, const char *name)
Definition Utility.cxx:1169
void SetDetailedException(std::vector< PyError_t > &&errors, PyObject *topmsg, PyObject *defexc)
Definition Utility.cxx:1513
PyObject * CT2CppName(PyObject *pytc, const char *cpd, bool allow_voidp)
Definition Utility.h:50
Py_ssize_t GetBuffer(PyObject *pyobject, char tc, int size, void *&buf, bool check=true)
Definition Utility.cxx:1205
std::unordered_map< std::string, char > const & TypecodeMap()
Definition Utility.cxx:1189
std::string MapOperatorName(const std::string &name, bool bTakesParames, bool *stubbed=nullptr)
Definition Utility.cxx:1335
PyObject * FuncPtr2StdFunction(const std::string &retType, const std::string &signature, void *address)
Definition Utility.cxx:1113
PyCallable * FindUnaryOperator(PyObject *pyclass, const char *op)
Definition Utility.cxx:276
PyError_t FetchPyError()
Definition Utility.cxx:1474
unsigned long PyLongOrInt_AsULong(PyObject *pyobject)
Definition Utility.cxx:125
PY_ULONG_LONG PyLongOrInt_AsULong64(PyObject *pyobject)
Definition Utility.cxx:152
Cpp::DeclRef TCppScope_t
Definition API.h:41
std::unique_ptr< PyObject, PyObjectDeleter > fType
Definition Utility.h:118
std::unique_ptr< PyObject, PyObjectDeleter > fValue
Definition Utility.h:121
std::unique_ptr< PyObject, PyObjectDeleter > fTrace
Definition Utility.h:119