Logo ROOT   6.16/01
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 );
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
#define R__EXTERN
Definition: DllImport.h:27
int Py_ssize_t
Definition: PyROOT.h:166
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
#define pyname
Definition: TMCParticle.cxx:19
_object PyObject
Definition: TPyArg.h:20
PyGILState_STATE m_GILState
Definition: Utility.h:79
PyObject * RemoveGUIEventInputHook()
Definition: Utility.cxx:950
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
void ErrMsgHandler(int level, Bool_t abort, const char *location, const char *msg)
Translate ROOT error/warning to python.
Definition: Utility.cxx:810
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's class if such a function exists as a global ov...
Definition: Utility.cxx:315
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:902
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
std::string MapOperatorName(const std::string &name, Bool_t bTakesParames)
Map the given C++ operator name on the python equivalent.
Definition: Utility.cxx:612
PyObject * InstallGUIEventInputHook()
Definition: Utility.cxx:937
const std::string Compound(const std::string &name)
Break down the compound of a fully qualified type name.
Definition: Utility.cxx:658
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
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
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
void ErrMsgCallback(char *msg)
Translate CINT error/warning into python equivalent.
Definition: Utility.cxx:733
Py_ssize_t ArraySize(const std::string &name)
Extract size from an array type, if available.
Definition: Utility.cxx:682
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:847
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
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