ATLAS Offline Software
Loading...
Searching...
No Matches
PyAthenaAlg.cxx
Go to the documentation of this file.
1
2
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
32namespace PyAthena {
33
35// Public methods:
37
38// Constructors
40Alg::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
59StatusCode
61{
62 ATH_MSG_INFO("Initializing " << name() << "...");
63 return PyAthena::callPyMethod( m_self, "sysInitialize" );
64}
65
66StatusCode
68{
69 ATH_MSG_INFO("Re-Initializing " << name() << "...");
70 return PyAthena::callPyMethod( m_self, "sysReinitialize" );
71}
72
73StatusCode
75{
76 return PyAthena::callPyMethod( m_self, "sysStart" );
77}
78
79StatusCode
81{
82 return PyAthena::callPyMethod( m_self, "sysStop" );
83}
84
85StatusCode
87{
88 ATH_MSG_INFO("Finalizing " << name() << "...");
89 return PyAthena::callPyMethod( m_self, "sysFinalize" );
90}
91
92StatusCode
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
104StatusCode
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...
125}
126
128// Const methods:
130
131const char*
133{
134 static const std::string tname = System::typeinfoName(typeid(*this));
135 return tname.c_str();
136}
137
139// Non-const methods:
141
142bool
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
#define endmsg
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
_object PyObject
static Double_t sc
virtual StatusCode sysInitialize() override
Override sysInitialize.
virtual StatusCode reinitialize() override
PyObject * m_self
Pointer to self (from the python world)
Definition PyAthenaAlg.h:96
virtual StatusCode stop() override
virtual StatusCode sysInitialize() override
Override sysInitialize.
virtual bool setPyAttr(PyObject *pyobj) override
attach the C++ component to its python cousin
virtual const char * typeName() const override
return the std::type_info name of the underlying py-component This is used by concrete implementation...
virtual StatusCode start() override
virtual StatusCode execute() override
Alg()
Default constructor:
virtual StatusCode finalize() override
virtual ~Alg()
Destructor:
virtual StatusCode initialize() override
::AthAlgorithm AlgBase_t
Definition PyAthenaAlg.h:29