ATLAS Offline Software
PyAthenaTool.cxx
Go to the documentation of this file.
1 
3 /*
4  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 // Tool.cxx
8 // Implementation file for class Tool
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/MsgStream.h"
28 #include "GaudiKernel/ServiceHandle.h"
30 
31 namespace PyAthena {
32 
33 
34 // Constructors
36 Tool::Tool( const std::string& type,
37  const std::string& name,
38  const IInterface* parent ) :
39  base_class( type, name, parent ),
40  m_self ( nullptr )
41 {
42  //
43  // Property declaration
44  //
45  //declareProperty( "Property", m_nProperty );
46 
47 }
48 
49 // Destructor
52 {
53  ATH_MSG_DEBUG("Calling destructor");
54  if ( m_self ) {
56  Py_DECREF( m_self );
57  m_self = nullptr;
58  }
59 }
60 
61 // Athena AlgTool's Hooks
64 {
65  ATH_MSG_INFO("Initializing " << name() << "...");
66  return PyAthena::callPyMethod( m_self, "sysInitialize" );
67 }
68 
70 {
71  ATH_MSG_INFO("Finalizing " << name() << "...");
72  return PyAthena::callPyMethod( m_self, "sysFinalize" );
73 }
74 
77 {
79  ( "PyAthena::PyComponentMgr/PyComponentMgr",
80  name() );
81  if ( !pyMgr.retrieve().isSuccess() ) {
82  ATH_MSG_ERROR("Could not retrieve service [" << pyMgr.typeAndName() << "] !!");
83  return StatusCode::FAILURE;
84  }
85 
86  // first retrieve our python object cousin...
87  m_self = pyMgr->pyObject( this );
88 
89  if ( m_self == Py_None ) {
90  ATH_MSG_ERROR("Wrapped PyObject is NONE !");
91  return StatusCode::FAILURE;
92  }
93 
94  // re-route to usual sysInit...
96 }
97 
98 
99 const char*
101 {
102  static const std::string tname = System::typeinfoName(typeid(*this));
103  return tname.c_str();
104 }
105 
106 
107 bool
109 {
110  // now we tell the PyObject which C++ object it is the cousin of.
112  PyObject* pyobj = TPython::CPPInstance_FromVoidPtr
113  ( (void*)this, this->typeName() );
114  if ( !pyobj ) {
115  PyErr_Clear();
116  // try PyAthena::Tool
117  pyobj = TPython::CPPInstance_FromVoidPtr ((void*)this, "PyAthena::Tool");
119  ("could not dyncast component [" << name() << "] to a python "
120  << "object of type [" << this->typeName() << "] (probably a missing "
121  << "dictionary)" << endmsg
122  << "fallback to [PyAthena::Tool]...");
123  }
124  if ( !pyobj ) {
125  PyErr_Clear();
126  ATH_MSG_WARNING("Could not dyncast component ["
127  << name() << "] to a pyobject of type ["
128  << this->typeName() << "] (missing reflex dict ?)");
129  } else {
130  if ( -1 == PyObject_SetAttrString(o, "_cppHandle", pyobj) ) {
131  PyErr_Clear();
133  ("Could not attach C++ handle [" << name() << "] to its python "
134  << "cousin !");
135  if ( -1 == PyObject_SetAttrString(o, "_cppHandle", Py_None) ) {
136  PyErr_Clear();
138  ("could not attach a dummy C++ handle [" << name() << "] to its "
139  << "python cousin !");
140  }
141  } else {
142  return true;
143  }
144  }
145  return false;
146 }
147 
148 } //> end namespace PyAthena
PyAthena::Tool::sysInitialize
virtual StatusCode sysInitialize() override
Definition: PyAthenaTool.cxx:76
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
PyAthena::Tool::m_self
PyObject * m_self
Pointer to self (from the python world)
Definition: PyAthenaTool.h:80
PyAthena::Tool::Tool
Tool()=delete
Default constructor:
RootUtils::PyGILStateEnsure
Definition: PyAthenaGILStateEnsure.h:20
PyAthena::Tool::initialize
virtual StatusCode initialize() override
Definition: PyAthenaTool.cxx:63
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::Tool::setPyAttr
virtual bool setPyAttr(PyObject *pyobj) override
attach the C++ component to its python cousin
Definition: PyAthenaTool.cxx:108
PyAthena::Tool::finalize
virtual StatusCode finalize() override
Definition: PyAthenaTool.cxx:69
test_pyathena.parent
parent
Definition: test_pyathena.py:15
PyAthena::Tool::~Tool
virtual ~Tool()
Destructor:
Definition: PyAthenaTool.cxx:51
PyAthenaUtils.h
PyAthenaTool.h
PyAthena::Tool::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: PyAthenaTool.cxx:100
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
AthCommonDataStore< AthCommonMsg< AlgTool > >::sysInitialize
virtual StatusCode sysInitialize() override
Perform system initialization for an algorithm.
IPyComponentMgr.h
PyAthena
Definition: IPyComponent.h:28
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
PyAthenaGILStateEnsure.h
PyObject
_object PyObject
Definition: IPyComponent.h:26
ServiceHandle
Definition: ClusterMakerTool.h:37