ATLAS Offline Software
Loading...
Searching...
No Matches
PyAthena::PyComponentMgr Class Reference

#include <PyComponentMgr.h>

Inheritance diagram for PyAthena::PyComponentMgr:
Collaboration diagram for PyAthena::PyComponentMgr:

Public Member Functions

 PyComponentMgr (const std::string &name, ISvcLocator *pSvcLocator)
 Constructor with parameters:
virtual ~PyComponentMgr ()
 Destructor:
virtual StatusCode initialize () override
 Gaudi Service Implementation.
virtual StatusCode finalize () override
virtual PyObject * pyObject (IPyComponent *component) override
 Retrieve a python object from the python world.

Private Types

typedef std::unordered_map< std::string, PyObject * > PyComponents_t

Private Member Functions

 PyComponentMgr ()=delete

Private Attributes

PyObject * m_dict
 The dictionary of python components' description It should be of the form: { 'name' : { 'package' : "MyAnaPkg", 'class' : "MyAlg", 'pyprops' : { 'OutputLevel' : 1, ..., } }, }.
PyComponents_t m_components
 A fast look-up hash-map for python components { 'name' : PyObject* } PyObject* is NULL if not yet instantiated Note that we own the PyObjects.

Friends

class SvcFactory< PyAthena::PyComponentMgr >

Detailed Description

Definition at line 35 of file PyComponentMgr.h.

Member Typedef Documentation

◆ PyComponents_t

typedef std::unordered_map<std::string, PyObject*> PyAthena::PyComponentMgr::PyComponents_t
private

Definition at line 84 of file PyComponentMgr.h.

Constructor & Destructor Documentation

◆ PyComponentMgr() [1/2]

PyComponentMgr::PyComponentMgr ( const std::string & name,
ISvcLocator * pSvcLocator )

Constructor with parameters:

Definition at line 75 of file PyComponentMgr.cxx.

76 :
77 base_class ( name, pSvcLocator ),
78 m_dict ( nullptr ),
80{
81 //
82 // Property declaration
83 //
84 //declareProperty( "Property", m_nProperty, "descr" );
85
86}
PyComponents_t m_components
A fast look-up hash-map for python components { 'name' : PyObject* } PyObject* is NULL if not yet ins...
PyObject * m_dict
The dictionary of python components' description It should be of the form: { 'name' : { 'package' : "...

◆ ~PyComponentMgr()

PyComponentMgr::~PyComponentMgr ( )
virtual

Destructor:

Definition at line 90 of file PyComponentMgr.cxx.

91{
92 ATH_MSG_DEBUG("Calling destructor");
93
94 // we own the repository of instances' description
95 if ( m_dict ) {
96 RootUtils::PyGILStateEnsure ensure;
97 Py_DECREF( m_dict );
98 m_dict = nullptr;
99 }
100
101 // as well as the one of corresponding instances
102 if ( m_components.size() ) {
103 RootUtils::PyGILStateEnsure ensure;
104 for ( PyComponents_t::iterator
105 i = m_components.begin(),
106 iEnd = m_components.end();
107 i != iEnd;
108 ++i ) {
109 ATH_MSG_VERBOSE("__del__(" << i->first << ")...");
110 Py_XDECREF( i->second );
111 }
112 }
113
114}
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_VERBOSE(x,...)

◆ PyComponentMgr() [2/2]

PyAthena::PyComponentMgr::PyComponentMgr ( )
privatedelete

Member Function Documentation

◆ finalize()

StatusCode PyComponentMgr::finalize ( )
overridevirtual

Definition at line 205 of file PyComponentMgr.cxx.

206{
207 ATH_MSG_INFO("Finalizing " << name() << "...");
208 return StatusCode::SUCCESS;
209}
#define ATH_MSG_INFO(x,...)

◆ initialize()

StatusCode PyComponentMgr::initialize ( )
overridevirtual

Gaudi Service Implementation.

Definition at line 119 of file PyComponentMgr.cxx.

120{
121 ATH_MSG_INFO("Initializing " << name() << "...");
122
123 const std::string pyModuleName = "AthenaPython.Configurables";
124
125 // import the module holding the dictionary of component instances
126 RootUtils::PyGILStateEnsure ensure;
127 ATH_MSG_DEBUG("Importing module [" << pyModuleName << "]...");
128 PyObject* module = PyImport_ImportModule( const_cast<char*>(pyModuleName.c_str()) );
129 if ( !module || !PyModule_Check( module ) ) {
130 ATH_MSG_ERROR("Could not import [" << pyModuleName << "] !!");
131 Py_XDECREF (module);
132#ifdef ATLAS_HAVE_CPYCPPYY
133 throw CPyCppyy::PyException();
134#else
135 throw std::runtime_error ("Python exception");
136#endif // ATLAS_HAVE_CPYCPPYY
137 }
138
139 const std::string pyClassName = "PyComponents";
140 PyObject* pyClass = 0;
141 pyClass = PyDict_GetItemString( PyModule_GetDict( module ),
142 const_cast<char*>( pyClassName.c_str() ) );
143
144 // borrowed ref. so ->increment
145 Py_XINCREF( pyClass );
146
147 if ( !pyClass ) {
148 ATH_MSG_ERROR("Could not retrieve class [" << pyClassName
149 << "] from module [" << pyModuleName << "] !!");
150 Py_XDECREF(pyClass);
151 Py_DECREF (module);
152 return StatusCode::FAILURE;
153 }
154
155 m_dict = PyObject_GetAttrString( pyClass,
156 const_cast<char*>( "instances" ) );
157 if ( !m_dict || !PyDict_Check( m_dict ) ) {
158 ATH_MSG_ERROR("Could not retrieve attribute [instances] from class ["
159 << pyClassName << "] !!");
160 Py_DECREF (pyClass);
161 Py_DECREF (module);
162 return StatusCode::FAILURE;
163 }
164 Py_DECREF(pyClass);
165
166 // install the fancy attribute getter for PyComponents to automatically
167 // retrieve an initialized component.
168 // see bug #46668
169 {
170 PyObject* fancy_attr = 0;
171 fancy_attr = PyDict_GetItemString(PyModule_GetDict(module),
172 (char*)"_install_fancy_attrs");
173 // borrowed ref => increment
174 Py_XINCREF(fancy_attr);
175 if (!fancy_attr) {
176 PyErr_Clear();
178 ("could not retrieve function [_install_fancy_attrs] from module ["
179 << pyModuleName << "]");
180 } else {
181 PyObject* ret = PyObject_CallFunction(fancy_attr, NULL);
182 Py_XDECREF(ret);
183 }
184 Py_XDECREF(fancy_attr);
185 }
186 Py_DECREF (module);
187
188 // make sure the PyROOT fixes are installed
189 const std::string pyRootFixes = "RootUtils.PyROOTFixes";
190 ATH_MSG_DEBUG("Importing module [" << pyRootFixes << "]...");
191 PyObject *rootFixes = 0;
192 rootFixes = PyImport_ImportModule(const_cast<char*>(pyRootFixes.c_str()));
193 if ( !rootFixes || !PyModule_Check( rootFixes ) ) {
194 PyErr_Print();
195 PyErr_Clear();
196 ATH_MSG_WARNING("Could not import [" << pyRootFixes << "] !!"
197 << endmsg
198 << "Some problem may appear with some C++->python binded classes (YMMV)");
199 }
200 Py_XDECREF(rootFixes);
201 return StatusCode::SUCCESS;
202}
#define endmsg
#define ATH_MSG_ERROR(x,...)
#define ATH_MSG_WARNING(x,...)
_object PyObject

◆ pyObject()

PyObject * PyComponentMgr::pyObject ( IPyComponent * component)
overridevirtual

Retrieve a python object from the python world.

this returns a NEW reference to that python object.

Hum... remove ? or not ? leaving the objects on the python side may allow easier retrieval from py-components...

Definition at line 213 of file PyComponentMgr.cxx.

214{
215 const std::string& name = cppComp->name();
216
217 // Check if we already have instantiated that component
218 RootUtils::PyGILStateEnsure ensure;
219 PyComponents_t::iterator comp = m_components.find( name );
220 if ( comp != m_components.end() && comp->second ) {
221 Py_INCREF (comp->second);
222 return comp->second;
223 }
224 m_components[name] = static_cast<PyObject*>( NULL );
225
226 // Check we have a valid dict.
227 if ( !m_dict || !PyDict_Check(m_dict) ) {
228 ATH_MSG_ERROR("Invalid repository of Python components !!");
229 Py_INCREF( Py_None );
230 return Py_None;
231 }
232
233 PyObject* o = PyDict_GetItemString( m_dict,
234 const_cast<char*>(name.c_str()) );
235 // borrowed ref -> incr
236 Py_XINCREF( o );
237
238 // Check we have a valid dict.
239 if ( !o ) {
240 ATH_MSG_ERROR("No such python component [" << name
241 << "] or invalid item !!");
242 Py_XDECREF( o );
243#ifdef ATLAS_HAVE_CPYCPPYY
244 throw CPyCppyy::PyException();
245#else
246 throw std::runtime_error ("Python exception");
247#endif // ATLAS_HAVE_CPYCPPYY
248 }
249 m_components[name] = o;
250
254// // remove the object from the python dict
255// if ( PyDict_DelItemString( m_dict, const_cast<char*>(name.c_str()) ) ) {
256// ATH_MSG_ERROR("Could not remove [" << name << "] from PyComponents.instances !!");
257// throw PyROOT::TPyException();
258// }
259
260 // now we tell the PyObject which C++ object it is the cousin of.
261 if ( !cppComp->setPyAttr(o) ) {
262 ATH_MSG_WARNING("Could not connect the C++ object ["
263 << cppComp->typeName() << "/" << cppComp->name()
264 << "] with its python cousin !");
265 }
266
267 removePyGen();
268
269 Py_INCREF(o);
270 return o;
271}

◆ SvcFactory< PyAthena::PyComponentMgr >

friend class SvcFactory< PyAthena::PyComponentMgr >
friend

Definition at line 35 of file PyComponentMgr.h.

Member Data Documentation

◆ m_components

PyComponents_t PyAthena::PyComponentMgr::m_components
private

A fast look-up hash-map for python components { 'name' : PyObject* } PyObject* is NULL if not yet instantiated Note that we own the PyObjects.

Definition at line 90 of file PyComponentMgr.h.

◆ m_dict

PyObject* PyAthena::PyComponentMgr::m_dict
private

The dictionary of python components' description It should be of the form: { 'name' : { 'package' : "MyAnaPkg", 'class' : "MyAlg", 'pyprops' : { 'OutputLevel' : 1, ..., } }, }.

Definition at line 82 of file PyComponentMgr.h.


The documentation for this class was generated from the following files: