Algorithm.cpp
Go to the documentation of this file.
1 // ============================================================================
2 // Include files
3 // ============================================================================
4 #include "Python.h"
5 // ============================================================================
6 // GaudiKernel
7 // ============================================================================
8 #include "GaudiKernel/Bootstrap.h"
9 // ============================================================================
10 // GaudiPython
11 // ============================================================================
12 #include "GaudiPython/Algorithm.h"
13 // ============================================================================
14 
15 namespace {
22  inline char *chr(const char*c){ return const_cast<char*>(c); }
23 }
24 
25 // ============================================================================
27 // ============================================================================
29 ( PyObject* self, const char* method )
30 {
32  // check arguments
33  if ( !self || !method ) { return StatusCode::FAILURE ; }
34 
35  // call Python
36  PyObject* r = PyObject_CallMethod(self, chr(method), nullptr );
37 
38  if ( !r ) { PyErr_Print() ; return sc ; } // RETURN
39 
40  if ( PyInt_Check ( r ) )
41  { sc = PyInt_AS_LONG( r ) ; Py_DECREF( r ) ; return sc ; } // RETURN
42 
43  // look for the method getCode with the signature:
44  // ' int getCode() '
45  PyObject* c = PyObject_CallMethod(r, chr("getCode"), nullptr);
46 
47  if ( !c ) { PyErr_Print() ; }
48  else if ( PyLong_Check( c )) { sc = PyLong_AsLong( c ); }
49  else
50  {
51  std::string msg( " call_python_method unexpected type from '" );
52  msg += method ;
53  msg += "().getCode()' " ;
54  PyErr_SetString( PyExc_TypeError , msg.c_str() ) ;
55  PyErr_Print() ;
56  }
57  // release used objects
58  Py_XDECREF( c ) ;
59  Py_XDECREF( r ) ;
60  //
61  return sc;
62 }
63 // ============================================================================
64 
65 // ============================================================================
70 // ============================================================================
72 ( PyObject* self ,
73  const std::string& name )
74  : Algorithm ( name , Gaudi::svcLocator() )
75  , m_self ( self )
76 {
77  // The owner of the Algorithm is Python (as creator) therefore
78  // it should not be deleted by Gaudi (added an extra addRef()).
79  addRef() ;
80  addRef() ;
81 }
82 // ============================================================================
83 
84 // ============================================================================
86 { return GaudiPython::call_python_method ( m_self , "initialize" ) ; }
87 // ============================================================================
89 { return GaudiPython::call_python_method ( m_self , "start" ) ; }
90 // ============================================================================
92 { return GaudiPython::call_python_method ( m_self , "execute" ) ; }
93 // ============================================================================
95 { return GaudiPython::call_python_method ( m_self , "stop" ) ; }
96 // ============================================================================
98 { return GaudiPython::call_python_method ( m_self , "finalize" ) ; }
99 
101  return GaudiPython::call_python_method ( m_self , "beginRun" ) ; }
103  return GaudiPython::call_python_method ( m_self , "endRun" ) ; }
104 
tuple c
Definition: gaudirun.py:392
PyAlgorithm(PyObject *self, const std::string &name)
constructor
Definition: Algorithm.cpp:72
GAUDI_API ISvcLocator * svcLocator()
StatusCode initialize()
Definition: Algorithm.cpp:85
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
GAUDI_API StatusCode call_python_method(PyObject *self, const char *method)
call the python method
Definition: Algorithm.cpp:29
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:61