Logo ROOT  
Reference Guide
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// initialize proxy type objects
46bool InitProxy(PyObject* module, PyTypeObject* pytype, const char* name);
47
48// retrieve the memory buffer from pyobject, return buflength, tc (optional) is python
49// array.array type code, size is type size, buf will point to buffer, and if check is
50// true, some heuristics will be applied to check buffer compatibility with the type
51Py_ssize_t GetBuffer(PyObject* pyobject, char tc, int size, void*& buf, bool check = true);
52
53// data/operator mappings
54std::string MapOperatorName(const std::string& name, bool bTakesParames);
55
57 PyOperators() : fEq(nullptr), fNe(nullptr), fLAdd(nullptr), fRAdd(nullptr),
58 fSub(nullptr), fLMul(nullptr), fRMul(nullptr), fDiv(nullptr), fHash(nullptr) {}
60
68};
69
70// meta information
71const std::string Compound(const std::string& name);
72Py_ssize_t ArraySize(const std::string& name);
73std::string ClassName(PyObject* pyobj);
74
75// for threading: save call to PyErr_Occurred()
77
78// helpers for collecting/maintaining python exception data
79struct PyError_t {
81
82 static void Clear(PyError_t& e)
83 {
84 // Remove exception information.
85 Py_XDECREF(e.fType); Py_XDECREF(e.fValue); Py_XDECREF(e.fTrace);
86 e.fType = e.fValue = e.fTrace = 0;
87 }
88
90};
91
92size_t FetchError(std::vector<PyError_t>&);
94 std::vector<PyError_t>& errors /* clears */, PyObject* topmsg /* steals ref */, PyObject* defexc);
95
96// setup Python API for callbacks
97bool IncludePython();
98
99} // namespace Utility
100
101} // namespace CPyCppyy
102
103#endif // !CPYCPPYY_UTILITY_H
PyDictEntry *(* dict_lookup_func)(PyDictObject *, PyObject *, long)
Definition: CPyCppyy.h:69
int Py_ssize_t
Definition: CPyCppyy.h:228
_object PyObject
Definition: PyMethodBase.h:41
#define e(i)
Definition: RSha256.hxx:103
unsigned long long ULong64_t
Definition: RtypesCore.h:72
char name[80]
Definition: TGX11.cxx:109
#define pyname
Definition: TMCParticle.cxx:19
PyCallable * FindBinaryOperator(PyObject *left, PyObject *right, const char *op, Cppyy::TCppScope_t scope=0)
Definition: Utility.cxx:280
size_t FetchError(std::vector< PyError_t > &)
Definition: Utility.cxx:879
void ConstructCallbackPreamble(const std::string &retType, const std::vector< std::string > &argtypes, std::ostringstream &code)
Definition: Utility.cxx:517
void ConstructCallbackReturn(const std::string &retType, int nArgs, std::ostringstream &code)
Definition: Utility.cxx:561
std::string MapOperatorName(const std::string &name, bool bTakesParames)
Definition: Utility.cxx:727
Py_ssize_t GetBuffer(PyObject *pyobject, char tc, int size, void *&buf, bool check=true)
Definition: Utility.cxx:614
std::string ConstructTemplateArgs(PyObject *pyname, PyObject *tpArgs, PyObject *args=nullptr, ArgPreference=kNone, int argoff=0, int *pcnt=nullptr)
Definition: Utility.cxx:473
PyCallable * FindUnaryOperator(PyObject *pyclass, const char *op)
Definition: Utility.cxx:266
void SetDetailedException(std::vector< PyError_t > &errors, PyObject *topmsg, PyObject *defexc)
Definition: Utility.cxx:891
bool InitProxy(PyObject *module, PyTypeObject *pytype, const char *name)
Definition: Utility.cxx:594
bool AddToClass(PyObject *pyclass, const char *label, PyCFunction cfunc, int flags=METH_VARARGS)
Definition: Utility.cxx:169
Py_ssize_t ArraySize(const std::string &name)
Definition: Utility.cxx:806
const std::string Compound(const std::string &name)
Definition: Utility.cxx:782
std::string ClassName(PyObject *pyobj)
Definition: Utility.cxx:825
bool IncludePython()
Definition: Utility.cxx:935
PyObject * PyErr_Occurred_WithGIL()
Definition: Utility.cxx:859
unsigned long PyLongOrInt_AsULong(PyObject *pyobject)
Definition: Utility.cxx:130
bool gDictLookupActive
Definition: Utility.cxx:27
dict_lookup_func gDictLookupOrg
Definition: Utility.cxx:26
ULong64_t PyLongOrInt_AsULong64(PyObject *pyobject)
Definition: Utility.cxx:150
size_t TCppScope_t
Definition: cpp_cppyy.h:18
static void Clear(PyError_t &e)
Definition: Utility.h:82