Logo ROOT   6.14/05
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 
13 namespace 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 );
38  const char* op, const char* label, const char* alt_label = NULL );
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 );
41 
42  // helper for template classes and methods
43  PyObject* BuildTemplateName( PyObject* pyname, PyObject* args, int argoff );
44 
45  // initialize proxy type objects
46  Bool_t 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
51  int GetBuffer( PyObject* pyobject, char tc, int size, void*& buf, Bool_t check = kTRUE );
52 
53  // data/operator mappings
54  std::string MapOperatorName( const std::string& name, Bool_t bTakesParames );
55 
56  // meta information
57  const std::string Compound( const std::string& name );
58  Py_ssize_t ArraySize( const std::string& name );
59  const std::string ClassName( PyObject* pyobj );
60 
61  // CINT integration
62  void ErrMsgCallback( char* msg );
63  void ErrMsgHandler( int level, Bool_t abort, const char* location, const char* msg );
64 
65  // create a wrapper for the python function with Cling; returns function pointer
66  void* CreateWrapperMethod( PyObject* pyfunc, Long_t user,
67  const char* retType, const std::vector<std::string>& signature, const char* callback );
68 
69  // for threading: save call to PyErr_Occurred()
71 
72  // for GUI driving; install/remove event callback
75 
76  } // namespace Utility
77 
78  class PyGILRAII {
79  PyGILState_STATE m_GILState;
80  public:
81  PyGILRAII():m_GILState(PyGILState_Ensure()){}
82  ~PyGILRAII(){PyGILState_Release(m_GILState);}
83  };
84 
85 } // namespace PyROOT
86 
87 #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:519
Bool_t AddBinaryOperator(PyObject *left, PyObject *right, const char *op, const char *label, const char *alt_label=NULL)
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
PyDictEntry *(* dict_lookup_func)(PyDictObject *, PyObject *, Long_t)
Definition: PyROOT.h:43
#define pyname
Definition: TMCParticle.cxx:19
PyObject * RemoveGUIEventInputHook()
Definition: Utility.cxx:949
R__EXTERN dict_lookup_func gDictLookupOrg
Definition: Utility.h:15
PyObject * BuildTemplateName(PyObject *pyname, PyObject *args, int argoff)
Helper to construct the "< type, type, ... >" part of a templated name (either for a class as in Make...
Definition: Utility.cxx:462
bool Bool_t
Definition: RtypesCore.h:59
R__EXTERN Bool_t gDictLookupActive
Definition: Utility.h:18
std::string MapOperatorName(const std::string &name, Bool_t bTakesParames)
Map the given C++ operator name on the python equivalent.
Definition: Utility.cxx:612
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:702
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:901
PyGILState_STATE m_GILState
Definition: Utility.h:79
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:809
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:50
PyObject * InstallGUIEventInputHook()
Definition: Utility.cxx:936
unsigned long long ULong64_t
Definition: RtypesCore.h:70
unsigned long ULong_t
Definition: RtypesCore.h:51
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:156
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:539
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:846
Py_ssize_t ArraySize(const std::string &name)
Extract size from an array type, if available.
Definition: Utility.cxx:682
void ErrMsgCallback(char *msg)
Translate CINT error/warning into python equivalent.
Definition: Utility.cxx:732
const Bool_t kTRUE
Definition: RtypesCore.h:87
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:658