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// check whether a PEP 3118 buffer format string describes an element type
77// compatible with the requested type code: same kind (signed/unsigned
78// integer, floating point, complex, ...) and same native size, so e.g.
79// 'long' and 'long long' buffers are interchangeable where they coincide.
80// The type code may be a PEP 3118 character or a cppjit-internal code:
81// 'z' (std::complex<float>) or 'Z' (std::complex<double>).
82bool BufferFormatCompatible(char tc, const char* fmt);
83
84// retrieve the memory buffer from pyobject, return buflength, tc (optional) is
85// python array.array type code, size is type size, buf will point to buffer,
86// and if check is true, some heuristics will be applied to check buffer
87// compatibility with the type
88Py_ssize_t GetBuffer(PyObject* pyobject, char tc, int size, void*& buf,
89 bool check = true);
90
91// data/operator mappings
92std::string MapOperatorName(const std::string& name, bool bTakesParames,
93 bool* stubbed = nullptr);
94
97 : fEq(nullptr), fNe(nullptr), fLt(nullptr), fLe(nullptr), fGt(nullptr),
98 fGe(nullptr), fLAdd(nullptr), fRAdd(nullptr), fSub(nullptr),
99 fLMul(nullptr), fRMul(nullptr), fDiv(nullptr), fHash(nullptr) {}
100 ~PyOperators();
101
111};
112
113// meta information
114std::string ClassName(PyObject* pyobj);
115bool IsSTLIterator(const std::string& classname);
116
117// for threading: save call to PyErr_Occurred()
119
120// helpers for collecting/maintaining python exception data
121struct PyError_t {
123 void operator()(PyObject* obj) { Py_XDECREF(obj); }
124 };
125#if PY_VERSION_HEX < 0x030c0000
126 std::unique_ptr<PyObject, PyObjectDeleter> fType;
127 std::unique_ptr<PyObject, PyObjectDeleter> fTrace;
128#endif
129 std::unique_ptr<PyObject, PyObjectDeleter> fValue;
130 bool fIsCpp = false;
131};
132
134void RestorePyError(PyError_t& error);
135
136size_t FetchError(std::vector<PyError_t>&, bool is_cpp = false);
137void SetDetailedException(std::vector<PyError_t>&& errors /* clears */,
138 PyObject* topmsg /* steals ref */, PyObject* defexc);
139
140// setup Python API for callbacks
141bool IncludePython();
142
143} // namespace Utility
144
145} // namespace cppjit::cpyrt
146
147#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
bool BufferFormatCompatible(char tc, const char *fmt)
Definition Utility.cxx:1312
std::string ClassName(PyObject *pyobj)
Definition Utility.cxx:1527
void ConstructCallbackPreamble(const std::string &retType, const std::vector< std::string > &argtypes, std::ostringstream &code)
Definition Utility.cxx:1004
std::string CT2CppNameS(PyObject *pytc, bool allow_voidp)
Definition Utility.cxx:935
void RestorePyError(PyError_t &error)
Definition Utility.cxx:1617
void ConstructCallbackReturn(const std::string &retType, int nArgs, std::ostringstream &code)
Definition Utility.cxx:1073
size_t FetchError(std::vector< PyError_t > &, bool is_cpp=false)
Definition Utility.cxx:1627
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:1547
std::vector< Cpp::TemplateArgInfo > GetTemplateArgsTypes(PyObject *scope, PyObject *tpArgs, PyObject *args=nullptr, ArgPreference=kNone, int argoff=0, int *pcnt=nullptr)
Definition Utility.cxx:892
bool AddToClass(PyObject *pyclass, const char *label, PyCFunction cfunc, int flags=METH_VARARGS)
Definition Utility.cxx:178
PyObject * PyErr_Occurred_WithGIL()
Definition Utility.cxx:1587
bool InitProxy(PyObject *module, PyTypeObject *pytype, const char *name)
Definition Utility.cxx:1168
void SetDetailedException(std::vector< PyError_t > &&errors, PyObject *topmsg, PyObject *defexc)
Definition Utility.cxx:1637
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:1341
std::unordered_map< std::string, char > const & TypecodeMap()
Definition Utility.cxx:1188
std::string MapOperatorName(const std::string &name, bool bTakesParames, bool *stubbed=nullptr)
Definition Utility.cxx:1459
PyObject * FuncPtr2StdFunction(const std::string &retType, const std::string &signature, void *address)
Definition Utility.cxx:1112
PyCallable * FindUnaryOperator(PyObject *pyclass, const char *op)
Definition Utility.cxx:276
PyError_t FetchPyError()
Definition Utility.cxx:1598
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:126
std::unique_ptr< PyObject, PyObjectDeleter > fValue
Definition Utility.h:129
std::unique_ptr< PyObject, PyObjectDeleter > fTrace
Definition Utility.h:127