![]() |
|
|
Generated: 18 Jul 2008 |
00001 // $Id: Algorithm.cpp,v 1.6 2008/07/15 16:05:53 marcocle Exp $ 00002 // ============================================================================ 00003 // Incldue files 00004 // ============================================================================ 00005 #ifdef _POSIX_C_SOURCE 00006 #undef _POSIX_C_SOURCE 00007 #endif 00008 #include "Python.h" 00009 // ============================================================================ 00010 // GaudiKernel 00011 // ============================================================================ 00012 #include "GaudiKernel/Bootstrap.h" 00013 // ============================================================================ 00014 // GaudiPython 00015 // ============================================================================ 00016 #include "GaudiPython/Algorithm.h" 00017 // ============================================================================ 00018 00019 00020 // ============================================================================ 00022 // ============================================================================ 00023 StatusCode GaudiPython::call_python_method 00024 ( PyObject* self, char* method ) 00025 { 00026 StatusCode sc = StatusCode::FAILURE; 00027 // check arguments 00028 if ( 0 == self || 0 == method ) { return StatusCode::FAILURE ; } 00029 00030 // call Python 00031 PyObject* r = PyObject_CallMethod ( self , method , "" ) ; 00032 00033 if ( 0 == r ) { PyErr_Print() ; return sc ; } // RETURN 00034 00035 if ( PyInt_Check ( r ) ) 00036 { sc = PyInt_AS_LONG( r ) ; Py_DECREF( r ) ; return sc ; } // RETURN 00037 00038 // look for the method getCode with the signature: 00039 // ' int getCode() ' 00040 PyObject* c = PyObject_CallMethod( r , "getCode" , "" ) ; 00041 00042 if ( 0 == c ) { PyErr_Print() ; } 00043 else if ( PyLong_Check( c )) { sc = PyLong_AsLong( c ); } 00044 else 00045 { 00046 std::string msg( " call_python_method unexpected type from '" ); 00047 msg += method ; 00048 msg += "().getCode()' " ; 00049 PyErr_SetString( PyExc_TypeError , msg.c_str() ) ; 00050 PyErr_Print() ; 00051 } 00052 // release used objects 00053 Py_XDECREF( c ) ; 00054 Py_XDECREF( r ) ; 00055 // 00056 return sc; 00057 } ; 00058 // ============================================================================ 00059 00060 // ============================================================================ 00065 // ============================================================================ 00066 GaudiPython::PyAlgorithm::PyAlgorithm 00067 ( PyObject* self , 00068 const std::string& name ) 00069 : Algorithm ( name , Gaudi::svcLocator() ) 00070 , m_self ( self ) 00071 { 00072 // The owner of the Algorithm is Python (as creator) therfore 00073 // it should not be deleted by Gaudi (added an extra addRef()). 00074 addRef() ; 00075 addRef() ; 00076 }; 00077 // ============================================================================ 00078 00079 // ============================================================================ 00080 StatusCode GaudiPython::PyAlgorithm::initialize () 00081 { return GaudiPython::call_python_method ( m_self , "initialize" ) ; } 00082 // ============================================================================ 00083 StatusCode GaudiPython::PyAlgorithm::start () 00084 { return GaudiPython::call_python_method ( m_self , "start" ) ; } 00085 // ============================================================================ 00086 StatusCode GaudiPython::PyAlgorithm::execute () 00087 { return GaudiPython::call_python_method ( m_self , "execute" ) ; } 00088 // ============================================================================ 00089 StatusCode GaudiPython::PyAlgorithm::stop () 00090 { return GaudiPython::call_python_method ( m_self , "stop" ) ; } 00091 // ============================================================================ 00092 StatusCode GaudiPython::PyAlgorithm::finalize () 00093 { return GaudiPython::call_python_method ( m_self , "finalize" ) ; } 00094 00095 StatusCode GaudiPython::PyAlgorithm::beginRun() { 00096 return GaudiPython::call_python_method ( m_self , "beginRun" ) ; } 00097 StatusCode GaudiPython::PyAlgorithm::endRun() { 00098 return GaudiPython::call_python_method ( m_self , "endRun" ) ; } 00099