ATLAS Offline Software
PyAthenaAlg.cxx
Go to the documentation of this file.
1 
3 /*
4  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 // PyAthenaAlg.cxx
8 // Implementation file for class PyAthena::Alg
9 // Author: S.Binet<binet@cern.ch>
10 // Modified: Wim Lavrijsen <WLavrijsen@lbl.gov>
12 
13 // Python includes
14 #include "Python.h"
15 
16 // PyROOT includes
17 #include "TPython.h"
18 
19 // AthenaPython includes
23 
24 // STL includes
25 
26 // FrameWork includes
27 #include "GaudiKernel/System.h"
28 #include "GaudiKernel/MsgStream.h"
29 #include "GaudiKernel/ServiceHandle.h"
31 
32 namespace PyAthena {
33 
35 // Public methods:
37 
38 // Constructors
40 Alg::Alg( const std::string& name, ISvcLocator* svcLocator ) :
41  AlgBase_t( name, svcLocator ),
42  m_self ( nullptr )
43 {}
44 
45 // Destructor
48 {
49  ATH_MSG_DEBUG("Calling destructor");
50  if ( m_self ) {
52  Py_DECREF( m_self );
53  m_self = nullptr;
54  }
55 }
56 
57 // Framework's Hooks
61 {
62  ATH_MSG_INFO("Initializing " << name() << "...");
63  return PyAthena::callPyMethod( m_self, "sysInitialize" );
64 }
65 
68 {
69  ATH_MSG_INFO("Re-Initializing " << name() << "...");
70  return PyAthena::callPyMethod( m_self, "sysReinitialize" );
71 }
72 
75 {
76  return PyAthena::callPyMethod( m_self, "sysStart" );
77 }
78 
81 {
82  return PyAthena::callPyMethod( m_self, "sysStop" );
83 }
84 
87 {
88  ATH_MSG_INFO("Finalizing " << name() << "...");
89  return PyAthena::callPyMethod( m_self, "sysFinalize" );
90 }
91 
94 {
95 // ATH_MSG_DEBUG("Executing " << name() << "...");
97  PyObject* pycontext = PyCapsule_New ( const_cast<EventContext*>(&getContext()), nullptr, nullptr);
98 
99  StatusCode sc = PyAthena::callPyMethod( m_self, "sysExecute", pycontext );
100  Py_DECREF (pycontext);
101  return sc;
102 }
103 
106 {
108  ( "PyAthena::PyComponentMgr/PyComponentMgr", name() );
109  if ( !pyMgr.retrieve().isSuccess() ) {
110  ATH_MSG_ERROR("Could not retrieve service [" << pyMgr.typeAndName()
111  << "] !!");
112  return StatusCode::FAILURE;
113  }
114 
115  // first retrieve our python object cousin...
116  m_self = pyMgr->pyObject( this );
117 
118  if ( m_self == Py_None ) {
119  ATH_MSG_ERROR("Wrapped PyObject is NONE !");
120  return StatusCode::FAILURE;
121  }
122 
123  // re-route to usual sysInit...
124  return AlgBase_t::sysInitialize();
125 }
126 
128 // Const methods:
130 
131 const char*
133 {
134  static const std::string tname = System::typeinfoName(typeid(*this));
135  return tname.c_str();
136 }
137 
139 // Non-const methods:
141 
142 bool
144 {
145  // now we tell the PyObject which C++ object it is the cousin of.
147  PyObject* pyobj = TPython::CPPInstance_FromVoidPtr
148  ( (void*)this, this->typeName() );
149  if ( !pyobj ) {
150  PyErr_Clear();
151  // try PyAthena::Alg
152  pyobj = TPython::CPPInstance_FromVoidPtr ((void*)this, "PyAthena::Alg");
154  ("could not dyncast component [" << name() << "] to a python "
155  << "object of type [" << this->typeName() << "] (probably a missing "
156  << "dictionary)" << endmsg
157  << "fallback to [PyAthena::Alg]...");
158  }
159  if ( !pyobj ) {
160  PyErr_Clear();
161  ATH_MSG_WARNING("Could not dyncast component ["
162  << name() << "] to a pyobject of type ["
163  << this->typeName() << "]");
164  } else {
165  if ( -1 == PyObject_SetAttrString(o, "_cppHandle", pyobj) ) {
166  PyErr_Clear();
168  ("Could not attach C++ handle [" << name() << "] to its python "
169  << "cousin !");
170  if ( -1 == PyObject_SetAttrString(o, "_cppHandle", Py_None) ) {
171  PyErr_Clear();
173  ("could not attach a dummy C++ handle [" << name() << "] to its "
174  "python cousin !");
175  }
176  } else {
177  return true;
178  }
179  }
180  return false;
181 }
182 
183 
184 } //> end namespace PyAthena
PyAthena::Alg::m_self
PyObject * m_self
Pointer to self (from the python world)
Definition: PyAthenaAlg.h:96
PyAthena::Alg::typeName
virtual const char * typeName() const override
return the std::type_info name of the underlying py-component This is used by concrete implementation...
Definition: PyAthenaAlg.cxx:132
PyAthena::Alg::~Alg
virtual ~Alg()
Destructor:
Definition: PyAthenaAlg.cxx:47
PyAthenaAlg.h
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
PyAthena::Alg::setPyAttr
virtual bool setPyAttr(PyObject *pyobj) override
attach the C++ component to its python cousin
Definition: PyAthenaAlg.cxx:143
PyAthena::Alg::start
virtual StatusCode start() override
Definition: PyAthenaAlg.cxx:74
PyAthena::Alg::reinitialize
virtual StatusCode reinitialize() override
Definition: PyAthenaAlg.cxx:67
PyAthena::Alg::initialize
virtual StatusCode initialize() override
Definition: PyAthenaAlg.cxx:60
PyAthena::Alg::execute
virtual StatusCode execute() override
Definition: PyAthenaAlg.cxx:93
PyAthena::Alg::sysInitialize
virtual StatusCode sysInitialize() override
Override sysInitialize.
Definition: PyAthenaAlg.cxx:105
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
PyAthena::Alg::stop
virtual StatusCode stop() override
Definition: PyAthenaAlg.cxx:80
RootUtils::PyGILStateEnsure
Definition: PyAthenaGILStateEnsure.h:20
PyAthena::Alg::finalize
virtual StatusCode finalize() override
Definition: PyAthenaAlg.cxx:86
AthAlgorithm::sysInitialize
virtual StatusCode sysInitialize() override
Override sysInitialize.
Definition: AthAlgorithm.cxx:66
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
Athena::typeinfoName
std::string typeinfoName(const std::type_info &ti)
Convert a type_info to a demangled string.
Definition: AthenaKernel/src/ClassName.cxx:23
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
PyAthena::Alg::Alg
Alg()
Default constructor:
AthAlgorithm
Definition: AthAlgorithm.h:47
PyAthenaUtils.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
IPyComponentMgr.h
PyAthena
Definition: IPyComponent.h:28
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
PyAthenaGILStateEnsure.h
PyObject
_object PyObject
Definition: IPyComponent.h:26
ServiceHandle
Definition: ClusterMakerTool.h:37