Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
17  R__EXTERN dict_lookup_func gDictLookupOrg;
18  R__EXTERN Bool_t gDictLookupActive;
19 
20 // additional converter functions
21  ULong_t PyLongOrInt_AsULong( PyObject* pyobject );
22  ULong64_t PyLongOrInt_AsULong64( PyObject* pyobject );
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
35  Bool_t AddBinaryOperator( PyObject* left, PyObject* right,
36  const char* op, const char* label, const char* alt_label = NULL, bool lazy = false );
37  Bool_t AddBinaryOperator( PyObject* pyclass,
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
43  enum ArgPreference { kNone, kPointer, kReference, kValue };
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()
72  PyObject* PyErr_Occurred_WithGIL();
73 
74  // for GUI driving; install/remove event callback
75  PyObject* InstallGUIEventInputHook();
76  PyObject* RemoveGUIEventInputHook();
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