ATLAS Offline Software
Loading...
Searching...
No Matches
PyComponentMgr.cxx
Go to the documentation of this file.
1
2
3/*
4 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
5*/
6
7// PyComponentMgr.cxx
8// Implementation file for class PyComponentMgr
9// Author: S.Binet<binet@cern.ch>
10// Modified: Wim Lavrijsen <WLavrijsen@lbl.gov>
12
13// Python includes
14#include "Python.h"
15
17CLASS_DEF (PyObject, 72785480, 1)
18
19// AthenaPython includes
20#include "PyComponentMgr.h"
22
23// FrameWork includes
24#include "Gaudi/Property.h"
26
27// PyROOT includes
28#ifdef ATLAS_HAVE_CPYCPPYY
29# include "CPyCppyy/PyException.h"
30#endif // ATLAS_HAVE_CPYCPPYY
31#include "TPython.h"
32#include "TROOT.h"
33#include "TClassGenerator.h"
34
35// STL includes
36#include <stdexcept>
37
38using namespace PyAthena;
39
40namespace {
41
42// Remove the TPyClassGenerator from the list of class generators.
43// TPyClassGenerator makes classes defined in Python accessible to ROOT.
44// We don't rely on this at all in Athena. Further, in MT jobs,
45// we can get deadlocks. TClass::GetClass holds the ROOT internal
46// mutexes, and if it ends up calling TPyClassGenerator, then the
47// generator will end up acquiring the Python GIL.
48// On the other hand, Python-based algorithms will hold the GIL.
49// If they then use PyROOT (or do anything which could trigger I/O),
50// then they can try to acquire the ROOT internal locks.
51// Since we have the same locks being acquired in different orders
52// in different threads, we can deadlock.
53//
54// Ideally, TClass::GetClass should probably drop the ROOT locks
55// before calling a class generator. But since we don't actually
56// use this functionality on Athena, just suppress it.
57void removePyGen ()
58{
59 TCollection* gens = gROOT->GetListOfClassGenerators();
60 TIter next(gens);
61 TClassGenerator *gen;
62 while( (gen = (TClassGenerator*) next()) ) {
63 if (System::typeinfoName (typeid (*gen)) == "TPyClassGenerator") {
64 gens->Remove (gen);
65 break;
66 }
67 }
68}
69} // anonymous namespace
70
71
72
73// Constructors
75PyComponentMgr::PyComponentMgr( const std::string& name,
76 ISvcLocator* pSvcLocator ) :
77 base_class ( name, pSvcLocator ),
78 m_dict ( nullptr ),
80{
81 //
82 // Property declaration
83 //
84 //declareProperty( "Property", m_nProperty, "descr" );
85
86}
87
88// Destructor
91{
92 ATH_MSG_DEBUG("Calling destructor");
93
94 // we own the repository of instances' description
95 if ( m_dict ) {
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() ) {
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}
115
116// Athena Algorithm's Hooks
118StatusCode
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
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}
203
204StatusCode
206{
207 ATH_MSG_INFO("Finalizing " << name() << "...");
208 return StatusCode::SUCCESS;
209}
210
211
214{
215 const std::string& name = cppComp->name();
216
217 // Check if we already have instantiated that component
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}
#define endmsg
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_ERROR(x,...)
#define ATH_MSG_WARNING(x,...)
#define ATH_MSG_VERBOSE(x,...)
#define ATH_MSG_INFO(x,...)
macros to associate a CLID to a type
#define CLASS_DEF(NAME, CID, VERSION)
associate a clid and a version to a type eg
_object PyObject
virtual const char * typeName() const =0
return the std::type_info name of the underlying py-component This is used by concrete implementation...
virtual bool setPyAttr(PyObject *pyobj)=0
attach the C++ component to its python cousin
PyComponents_t m_components
A fast look-up hash-map for python components { 'name' : PyObject* } PyObject* is NULL if not yet ins...
virtual StatusCode finalize() override
virtual StatusCode initialize() override
Gaudi Service Implementation.
virtual PyObject * pyObject(IPyComponent *component) override
Retrieve a python object from the python world.
virtual ~PyComponentMgr()
Destructor:
PyObject * m_dict
The dictionary of python components' description It should be of the form: { 'name' : { 'package' : "...