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 <string>
6#include <vector>
7
8
9namespace CPyCppyy {
10
11class PyCallable;
12
14extern bool gDictLookupActive;
15
16// additional converter functions
17unsigned long PyLongOrInt_AsULong(PyObject* pyobject);
19
20namespace Utility {
21
22// convenience functions for adding methods to classes
23bool AddToClass(PyObject* pyclass, const char* label, PyCFunction cfunc,
24 int flags = METH_VARARGS);
25bool AddToClass(PyObject* pyclass, const char* label, const char* func);
26bool AddToClass(PyObject* pyclass, const char* label, PyCallable* pyfunc);
27
28// helpers for dynamically constructing operators
29PyCallable* FindUnaryOperator(PyObject* pyclass, const char* op);
30PyCallable* FindBinaryOperator(PyObject* left, PyObject* right,
31 const char* op, Cppyy::TCppScope_t scope = 0);
32PyCallable* FindBinaryOperator(const std::string& lcname, const std::string& rcname,
33 const char* op, Cppyy::TCppScope_t scope = 0, bool reverse = false);
34
35// helper for template classes and methods
37std::string ConstructTemplateArgs(
38 PyObject* pyname, PyObject* tpArgs, PyObject* args = nullptr, ArgPreference = kNone, int argoff = 0, int* pcnt = nullptr);
39
40// helper for generating callbacks
41void ConstructCallbackPreamble(const std::string& retType,
42 const std::vector<std::string>& argtypes, std::ostringstream& code);
43void ConstructCallbackReturn(const std::string& retType, int nArgs, std::ostringstream& code);
44
45// helper for function pointer conversions
46PyObject* FuncPtr2StdFunction(const std::string& retType, const std::string& signature, void* address);
47
48// initialize proxy type objects
49bool InitProxy(PyObject* module, PyTypeObject* pytype, const char* name);
50
51// retrieve the memory buffer from pyobject, return buflength, tc (optional) is python
52// array.array type code, size is type size, buf will point to buffer, and if check is
53// true, some heuristics will be applied to check buffer compatibility with the type
54Py_ssize_t GetBuffer(PyObject* pyobject, char tc, int size, void*& buf, bool check = true);
55
56// data/operator mappings
57std::string MapOperatorName(const std::string& name, bool bTakesParames);
58
60 PyOperators() : fEq(nullptr), fNe(nullptr), fLAdd(nullptr), fRAdd(nullptr),
61 fSub(nullptr), fLMul(nullptr), fRMul(nullptr), fDiv(nullptr), fHash(nullptr) {}
63
71};
72
73// meta information
74const std::string Compound(const std::string& name);
75Py_ssize_t ArraySize(const std::string& name);
76std::string ClassName(PyObject* pyobj);
77
78// for threading: save call to PyErr_Occurred()
80
81// helpers for collecting/maintaining python exception data
82struct PyError_t {
84
85 static void Clear(PyError_t& e)
86 {
87 // Remove exception information.
88 Py_XDECREF(e.fType); Py_XDECREF(e.fValue); Py_XDECREF(e.fTrace);
89 e.fType = e.fValue = e.fTrace = 0;
90 }
91
93};
94
95size_t FetchError(std::vector<PyError_t>&);
97 std::vector<PyError_t>& errors /* clears */, PyObject* topmsg /* steals ref */, PyObject* defexc);
98
99// setup Python API for callbacks
100bool IncludePython();
101
102} // namespace Utility
103
104} // namespace CPyCppyy
105
106#endif // !CPYCPPYY_UTILITY_H
PyDictEntry *(* dict_lookup_func)(PyDictObject *, PyObject *, long)
Definition CPyCppyy.h:69
int Py_ssize_t
Definition CPyCppyy.h:236
_object PyObject
#define e(i)
Definition RSha256.hxx:103
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
unsigned long long ULong64_t
Definition RtypesCore.h:81
char name[80]
Definition TGX11.cxx:110
#define pyname
PyCallable * FindBinaryOperator(PyObject *left, PyObject *right, const char *op, Cppyy::TCppScope_t scope=0)
Definition Utility.cxx:281
size_t FetchError(std::vector< PyError_t > &)
Definition Utility.cxx:945
void ConstructCallbackPreamble(const std::string &retType, const std::vector< std::string > &argtypes, std::ostringstream &code)
Definition Utility.cxx:518
void ConstructCallbackReturn(const std::string &retType, int nArgs, std::ostringstream &code)
Definition Utility.cxx:562
std::string MapOperatorName(const std::string &name, bool bTakesParames)
Definition Utility.cxx:793
Py_ssize_t GetBuffer(PyObject *pyobject, char tc, int size, void *&buf, bool check=true)
Definition Utility.cxx:679
std::string ConstructTemplateArgs(PyObject *pyname, PyObject *tpArgs, PyObject *args=nullptr, ArgPreference=kNone, int argoff=0, int *pcnt=nullptr)
Definition Utility.cxx:474
PyObject * FuncPtr2StdFunction(const std::string &retType, const std::string &signature, void *address)
Definition Utility.cxx:598
PyCallable * FindUnaryOperator(PyObject *pyclass, const char *op)
Definition Utility.cxx:267
void SetDetailedException(std::vector< PyError_t > &errors, PyObject *topmsg, PyObject *defexc)
Definition Utility.cxx:957
bool InitProxy(PyObject *module, PyTypeObject *pytype, const char *name)
Definition Utility.cxx:659
bool AddToClass(PyObject *pyclass, const char *label, PyCFunction cfunc, int flags=METH_VARARGS)
Definition Utility.cxx:170
Py_ssize_t ArraySize(const std::string &name)
Definition Utility.cxx:872
const std::string Compound(const std::string &name)
Definition Utility.cxx:848
std::string ClassName(PyObject *pyobj)
Definition Utility.cxx:891
PyObject * PyErr_Occurred_WithGIL()
Definition Utility.cxx:925
unsigned long PyLongOrInt_AsULong(PyObject *pyobject)
Definition Utility.cxx:131
bool gDictLookupActive
Definition Utility.cxx:28
dict_lookup_func gDictLookupOrg
Definition Utility.cxx:27
ULong64_t PyLongOrInt_AsULong64(PyObject *pyobject)
Definition Utility.cxx:151
size_t TCppScope_t
Definition cpp_cppyy.h:18
static void Clear(PyError_t &e)
Definition Utility.h:85