Logo ROOT  
Reference Guide
Utility.h
Go to the documentation of this file.
1// @(#)root/pyroot:$Id$
2// Author: Wim Lavrijsen, Apr 2004
3
4#ifndef PYROOT_UTILITY_H
5#define PYROOT_UTILITY_H
6
7// Standard
8#include <map>
9#include <string>
10#include <vector>
11
12
13namespace PyROOT {
14
15 class PyCallable;
16
19
20// additional converter functions
23
24 namespace Utility {
25
26 // convenience functions for adding methods to classes
27 Bool_t AddToClass( PyObject* pyclass, const char* label, PyCFunction cfunc,
28 int flags = METH_VARARGS );
29 Bool_t AddToClass( PyObject* pyclass, const char* label, const char* func );
30 Bool_t AddToClass( PyObject* pyclass, const char* label, PyCallable* pyfunc );
31
32 Bool_t AddUsingToClass( PyObject* pyclass, const char* method );
33
34 // helpers for dynamically constructing binary operators
36 const char* op, const char* label, const char* alt_label = NULL, bool lazy = false );
38 const char* op, const char* label, const char* alt_label = NULL, bool lazy = false );
39 Bool_t AddBinaryOperator( PyObject* pyclass, const std::string& lcname, const std::string& rcname,
40 const char* op, const char* label, const char* alt_label = NULL, bool lazy = false );
41
42 // helper for template classes and methods
44 PyObject* BuildTemplateName( PyObject* pyname, PyObject* tpArgs, int argoff,
45 PyObject* args = nullptr, ArgPreference = kNone, int* pcnt = nullptr, bool inferredTypes = false );
46
47 // initialize proxy type objects
48 Bool_t InitProxy( PyObject* module, PyTypeObject* pytype, const char* name );
49
50 // retrieve the memory buffer from pyobject, return buflength, tc (optional) is python
51 // array.array type code, size is type size, buf will point to buffer, and if check is
52 // true, some heuristics will be applied to check buffer compatibility with the type
53 int GetBuffer( PyObject* pyobject, char tc, int size, void*& buf, Bool_t check = kTRUE );
54
55 // data/operator mappings
56 std::string MapOperatorName( const std::string& name, Bool_t bTakesParames );
57
58 // meta information
59 const std::string Compound( const std::string& name );
60 Py_ssize_t ArraySize( const std::string& name );
61 const std::string ClassName( PyObject* pyobj );
62
63 // CINT integration
64 void ErrMsgCallback( char* msg );
65 void ErrMsgHandler( int level, Bool_t abort, const char* location, const char* msg );
66
67 // create a wrapper for the python function with Cling; returns function pointer
68 void* CreateWrapperMethod( PyObject* pyfunc, Long_t user,
69 const char* retType, const std::vector<std::string>& signature, const char* callback );
70
71 // for threading: save call to PyErr_Occurred()
73
74 // for GUI driving; install/remove event callback
77
78 } // namespace Utility
79
80 class PyGILRAII {
81 PyGILState_STATE m_GILState;
82 public:
83 PyGILRAII():m_GILState(PyGILState_Ensure()){}
84 ~PyGILRAII(){PyGILState_Release(m_GILState);}
85 };
86
87} // namespace PyROOT
88
89#endif // !PYROOT_UTILITY_H
#define R__EXTERN
Definition: DllImport.h:27
int Py_ssize_t
Definition: PyROOT.h:171
PyDictEntry *(* dict_lookup_func)(PyDictObject *, PyObject *, Long_t)
Definition: PyROOT.h:43
unsigned long ULong_t
Definition: RtypesCore.h:51
long Long_t
Definition: RtypesCore.h:50
bool Bool_t
Definition: RtypesCore.h:59
unsigned long long ULong64_t
Definition: RtypesCore.h:70
const Bool_t kTRUE
Definition: RtypesCore.h:87
char name[80]
Definition: TGX11.cxx:109
#define pyname
Definition: TMCParticle.cxx:19
_object PyObject
Definition: TPyArg.h:20
PyGILState_STATE m_GILState
Definition: Utility.h:81
PyObject * RemoveGUIEventInputHook()
Definition: Utility.cxx:973
Bool_t InitProxy(PyObject *module, PyTypeObject *pytype, const char *name)
Initialize a proxy class for use by python, and add it to the ROOT module.
Definition: Utility.cxx:542
void ErrMsgHandler(int level, Bool_t abort, const char *location, const char *msg)
Translate ROOT error/warning to python.
Definition: Utility.cxx:833
PyObject * PyErr_Occurred_WithGIL()
Re-acquire the GIL before calling PyErr_Occurred() in case it has been released; note that the p2....
Definition: Utility.cxx:925
Bool_t AddUsingToClass(PyObject *pyclass, const char *method)
Helper to add base class methods to the derived class one (this covers the 'using' cases,...
Definition: Utility.cxx:261
Bool_t AddBinaryOperator(PyObject *left, PyObject *right, const char *op, const char *label, const char *alt_label=NULL, bool lazy=false)
Install the named operator (op) into the left object's class if such a function exists as a global ov...
Definition: Utility.cxx:315
std::string MapOperatorName(const std::string &name, Bool_t bTakesParames)
Map the given C++ operator name on the python equivalent.
Definition: Utility.cxx:635
PyObject * InstallGUIEventInputHook()
Definition: Utility.cxx:960
const std::string Compound(const std::string &name)
Break down the compound of a fully qualified type name.
Definition: Utility.cxx:681
int GetBuffer(PyObject *pyobject, char tc, int size, void *&buf, Bool_t check=kTRUE)
Retrieve a linear buffer pointer from the given pyobject.
Definition: Utility.cxx:562
PyObject * BuildTemplateName(PyObject *pyname, PyObject *tpArgs, int argoff, PyObject *args=nullptr, ArgPreference=kNone, int *pcnt=nullptr, bool inferredTypes=false)
Helper to construct the "< type, type, ... >" part of a templated name (either for a class as in Make...
Definition: Utility.cxx:463
Bool_t AddToClass(PyObject *pyclass, const char *label, PyCFunction cfunc, int flags=METH_VARARGS)
Add the given function to the class under name 'label'.
Definition: Utility.cxx:186
void ErrMsgCallback(char *msg)
Translate CINT error/warning into python equivalent.
Definition: Utility.cxx:756
Py_ssize_t ArraySize(const std::string &name)
Extract size from an array type, if available.
Definition: Utility.cxx:705
void * CreateWrapperMethod(PyObject *pyfunc, Long_t user, const char *retType, const std::vector< std::string > &signature, const char *callback)
Compile a function on the fly and return a function pointer for use on C-APIs.
Definition: Utility.cxx:870
const std::string ClassName(PyObject *pyobj)
Retrieve the class name from the given python object (which may be just an instance of the class).
Definition: Utility.cxx:725
R__EXTERN dict_lookup_func gDictLookupOrg
Definition: Utility.h:15
ULong64_t PyLongOrInt_AsULong64(PyObject *pyobject)
Convert <pyobject> to C++ unsigned long long, with bounds checking.
Definition: Utility.cxx:166
R__EXTERN Bool_t gDictLookupActive
Definition: Utility.h:18
ULong_t PyLongOrInt_AsULong(PyObject *pyobject)
Definition: Utility.cxx:145