Logo ROOT   master
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 <string>
9 #include <vector>
10 
11 
12 namespace PyROOT {
13 
14  class PyCallable;
15 
18 
19 // additional converter functions
22 
23  namespace Utility {
24 
25  // convenience functions for adding methods to classes
26  Bool_t AddToClass( PyObject* pyclass, const char* label, PyCFunction cfunc,
27  int flags = METH_VARARGS );
28  Bool_t AddToClass( PyObject* pyclass, const char* label, const char* func );
29  Bool_t AddToClass( PyObject* pyclass, const char* label, PyCallable* pyfunc );
30 
31  Bool_t AddUsingToClass( PyObject* pyclass, const char* method );
32 
33  // helpers for dynamically constructing binary operators
35  const char* op, const char* label, const char* alt_label = NULL, bool lazy = false );
37  const char* op, const char* label, const char* alt_label = NULL, bool lazy = false );
38  Bool_t AddBinaryOperator( PyObject* pyclass, const std::string& lcname, const std::string& rcname,
39  const char* op, const char* label, const char* alt_label = NULL, bool lazy = false );
40 
41  // helper for template classes and methods
43  PyObject* BuildTemplateName( PyObject* pyname, PyObject* tpArgs, int argoff,
44  PyObject* args = nullptr, ArgPreference = kNone, int* pcnt = nullptr, bool inferredTypes = false );
45 
46  // initialize proxy type objects
47  Bool_t InitProxy( PyObject* module, PyTypeObject* pytype, const char* name );
48 
49  // retrieve the memory buffer from pyobject, return buflength, tc (optional) is python
50  // array.array type code, size is type size, buf will point to buffer, and if check is
51  // true, some heuristics will be applied to check buffer compatibility with the type
52  int GetBuffer( PyObject* pyobject, char tc, int size, void*& buf, Bool_t check = kTRUE );
53 
54  // data/operator mappings
55  std::string MapOperatorName( const std::string& name, Bool_t bTakesParames );
56 
57  // meta information
58  const std::string Compound( const std::string& name );
59  Py_ssize_t ArraySize( const std::string& name );
60  const std::string ClassName( PyObject* pyobj );
61 
62  // CINT integration
63  void ErrMsgCallback( char* msg );
64  void ErrMsgHandler( int level, Bool_t abort, const char* location, const char* msg );
65 
66  // create a wrapper for the python function with Cling; returns function pointer
67  void* CreateWrapperMethod( PyObject* pyfunc, Long_t user,
68  const char* retType, const std::vector<std::string>& signature, const char* callback );
69 
70  // for threading: save call to PyErr_Occurred()
72 
73  // for GUI driving; install/remove event callback
76 
77  } // namespace Utility
78 
79  class PyGILRAII {
80  PyGILState_STATE m_GILState;
81  public:
82  PyGILRAII():m_GILState(PyGILState_Ensure()){}
83  ~PyGILRAII(){PyGILState_Release(m_GILState);}
84  };
85 
86 } // namespace PyROOT
87 
88 #endif // !PYROOT_UTILITY_H
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
PyDictEntry *(* dict_lookup_func)(PyDictObject *, PyObject *, Long_t)
Definition: PyROOT.h:43
#define pyname
Definition: TMCParticle.cxx:19
PyObject * RemoveGUIEventInputHook()
Definition: Utility.cxx:973
R__EXTERN dict_lookup_func gDictLookupOrg
Definition: Utility.h:14
R__EXTERN Bool_t gDictLookupActive
Definition: Utility.h:17
std::string MapOperatorName(const std::string &name, Bool_t bTakesParames)
Map the given C++ operator name on the python equivalent.
Definition: Utility.cxx:635
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&#39;s class if such a function exists as a global ov...
Definition: Utility.cxx:315
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
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
PyGILState_STATE m_GILState
Definition: Utility.h:80
ULong_t PyLongOrInt_AsULong(PyObject *pyobject)
Definition: Utility.cxx:145
void ErrMsgHandler(int level, Bool_t abort, const char *location, const char *msg)
Translate ROOT error/warning to python.
Definition: Utility.cxx:833
ULong64_t PyLongOrInt_AsULong64(PyObject *pyobject)
Convert <pyobject> to C++ unsigned long long, with bounds checking.
Definition: Utility.cxx:166
long Long_t
Definition: RtypesCore.h:52
PyObject * InstallGUIEventInputHook()
Definition: Utility.cxx:960
unsigned long long ULong64_t
Definition: RtypesCore.h:72
unsigned long ULong_t
Definition: RtypesCore.h:53
Bool_t AddToClass(PyObject *pyclass, const char *label, PyCFunction cfunc, int flags=METH_VARARGS)
Add the given function to the class under name &#39;label&#39;.
Definition: Utility.cxx:186
Bool_t AddUsingToClass(PyObject *pyclass, const char *method)
Helper to add base class methods to the derived class one (this covers the &#39;using&#39; cases...
Definition: Utility.cxx:261
#define R__EXTERN
Definition: DllImport.h:27
int Py_ssize_t
Definition: PyROOT.h:171
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
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
Py_ssize_t ArraySize(const std::string &name)
Extract size from an array type, if available.
Definition: Utility.cxx:705
void ErrMsgCallback(char *msg)
Translate CINT error/warning into python equivalent.
Definition: Utility.cxx:756
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
const Bool_t kTRUE
Definition: RtypesCore.h:89
char name[80]
Definition: TGX11.cxx:109
_object PyObject
Definition: TPyArg.h:20
const std::string Compound(const std::string &name)
Break down the compound of a fully qualified type name.
Definition: Utility.cxx:681