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 <string>
7#include <vector>
8
9
10namespace CPyCppyy {
11
12class PyCallable;
13
14#if PY_VERSION_HEX < 0x030b0000
16extern bool gDictLookupActive;
17#endif
18
19// additional converter functions
20unsigned long PyLongOrInt_AsULong(PyObject* pyobject);
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);
42
43// helper for generating callbacks
44void ConstructCallbackPreamble(const std::string& retType,
45 const std::vector<std::string>& argtypes, std::ostringstream& code);
46void ConstructCallbackReturn(const std::string& retType, int nArgs, std::ostringstream& code);
47
48// helper for function pointer conversions
49PyObject* FuncPtr2StdFunction(const std::string& retType, const std::string& signature, void* address);
50
51// initialize proxy type objects
52bool InitProxy(PyObject* module, PyTypeObject* pytype, const char* name);
53
54// retrieve the memory buffer from pyobject, return buflength, tc (optional) is python
55// array.array type code, size is type size, buf will point to buffer, and if check is
56// true, some heuristics will be applied to check buffer compatibility with the type
57Py_ssize_t GetBuffer(PyObject* pyobject, char tc, int size, void*& buf, bool check = true);
58
59// data/operator mappings
60std::string MapOperatorName(const std::string& name, bool bTakesParames, bool* stubbed = nullptr);
61
63 PyOperators() : fEq(nullptr), fNe(nullptr), fLt(nullptr), fLe(nullptr), fGt(nullptr), fGe(nullptr),
64 fLAdd(nullptr), fRAdd(nullptr), fSub(nullptr), fLMul(nullptr), fRMul(nullptr), fDiv(nullptr),
65 fHash(nullptr) {}
67
77};
78
79// meta information
80std::string ClassName(PyObject* pyobj);
81bool IsSTLIterator(const std::string& classname);
82
83// for threading: save call to PyErr_Occurred()
85
86// helpers for collecting/maintaining python exception data
87struct PyError_t {
88 PyError_t(bool is_cpp = false) : fIsCpp(is_cpp) { fType = fValue = fTrace = 0; }
89
90 static void Clear(PyError_t& e)
91 {
92 // Remove exception information.
93 Py_XDECREF(e.fType); Py_XDECREF(e.fValue); Py_XDECREF(e.fTrace);
94 e.fType = e.fValue = e.fTrace = 0;
95 }
96
98 bool fIsCpp;
99};
100
101size_t FetchError(std::vector<PyError_t>&, bool is_cpp = false);
103 std::vector<PyError_t>& errors /* clears */, PyObject* topmsg /* steals ref */, PyObject* defexc);
104
105// setup Python API for callbacks
106bool IncludePython();
107
108} // namespace Utility
109
110} // namespace CPyCppyy
111
112#endif // !CPYCPPYY_UTILITY_H
PyDictEntry *(* dict_lookup_func)(PyDictObject *, PyObject *, long)
Definition CPyCppyy.h:44
int Py_ssize_t
Definition CPyCppyy.h:215
unsigned long long PY_ULONG_LONG
Definition Cppyy.h:31
_object PyObject
#define e(i)
Definition RSha256.hxx:103
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
char name[80]
Definition TGX11.cxx:110
PyCallable * FindBinaryOperator(PyObject *left, PyObject *right, const char *op, Cppyy::TCppScope_t scope=0)
Definition Utility.cxx:294
void ConstructCallbackPreamble(const std::string &retType, const std::vector< std::string > &argtypes, std::ostringstream &code)
Definition Utility.cxx:629
void ConstructCallbackReturn(const std::string &retType, int nArgs, std::ostringstream &code)
Definition Utility.cxx:695
Py_ssize_t GetBuffer(PyObject *pyobject, char tc, int size, void *&buf, bool check=true)
Definition Utility.cxx:808
std::string ConstructTemplateArgs(PyObject *pyname, PyObject *tpArgs, PyObject *args=nullptr, ArgPreference=kNone, int argoff=0, int *pcnt=nullptr)
Definition Utility.cxx:580
PyObject * FuncPtr2StdFunction(const std::string &retType, const std::string &signature, void *address)
Definition Utility.cxx:732
PyCallable * FindUnaryOperator(PyObject *pyclass, const char *op)
Definition Utility.cxx:279
size_t FetchError(std::vector< PyError_t > &, bool is_cpp=false)
Definition Utility.cxx:1074
std::string MapOperatorName(const std::string &name, bool bTakesParames, bool *stubbed=nullptr)
Definition Utility.cxx:932
void SetDetailedException(std::vector< PyError_t > &errors, PyObject *topmsg, PyObject *defexc)
Definition Utility.cxx:1086
bool InitProxy(PyObject *module, PyTypeObject *pytype, const char *name)
Definition Utility.cxx:788
bool AddToClass(PyObject *pyclass, const char *label, PyCFunction cfunc, int flags=METH_VARARGS)
Definition Utility.cxx:182
bool IsSTLIterator(const std::string &classname)
Definition Utility.cxx:1018
std::string ClassName(PyObject *pyobj)
Definition Utility.cxx:997
PyObject * PyErr_Occurred_WithGIL()
Definition Utility.cxx:1054
unsigned long PyLongOrInt_AsULong(PyObject *pyobject)
Definition Utility.cxx:129
bool gDictLookupActive
Definition Utility.cxx:28
dict_lookup_func gDictLookupOrg
Definition Utility.cxx:27
PY_ULONG_LONG PyLongOrInt_AsULong64(PyObject *pyobject)
Definition Utility.cxx:156
size_t TCppScope_t
Definition cpp_cppyy.h:18
PyError_t(bool is_cpp=false)
Definition Utility.h:88
static void Clear(PyError_t &e)
Definition Utility.h:90