From 3cb1cdbc4f96c6858f1cb2105b99b3a82f281dab Mon Sep 17 00:00:00 2001 From: scott snyder <snyder@bnl.gov> Date: Sun, 3 Sep 2017 16:15:12 +0200 Subject: [PATCH] AthenaPython: EventContext fixes for PyAthenaAlg. Fix PyAthenaAlg to take into account EventContext-related interface changes in the Algorithm base classes. --- Control/AthenaPython/AthenaPython/PyAthenaUtils.h | 4 +++- Control/AthenaPython/python/PyAthenaComps.py | 14 ++++++++++++-- Control/AthenaPython/src/PyAthenaAlg.cxx | 5 ++++- Control/AthenaPython/src/PyAthenaUtils.cxx | 10 ++++++++-- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/Control/AthenaPython/AthenaPython/PyAthenaUtils.h b/Control/AthenaPython/AthenaPython/PyAthenaUtils.h index 9ff746dca071..043239c3bce7 100644 --- a/Control/AthenaPython/AthenaPython/PyAthenaUtils.h +++ b/Control/AthenaPython/AthenaPython/PyAthenaUtils.h @@ -36,7 +36,9 @@ namespace PyAthena { std::string str (PyObject* o); /// call the python method - StatusCode callPyMethod( PyObject* self , const char* method ); + StatusCode callPyMethod( PyObject* self, + const char* method, + PyObject* arg = nullptr ); /// query interface binding StatusCode queryInterface( PyObject* self, diff --git a/Control/AthenaPython/python/PyAthenaComps.py b/Control/AthenaPython/python/PyAthenaComps.py index 0bf3638e7cf7..c0424db16d19 100644 --- a/Control/AthenaPython/python/PyAthenaComps.py +++ b/Control/AthenaPython/python/PyAthenaComps.py @@ -77,6 +77,7 @@ class Alg( CfgPyAlgorithm ): super(Alg, self).__init__(name, **kw) self._pyath_evtstore = None # handle to the evt store self._pyath_detstore = None # handle to the det store + self._ctx = None return @property @@ -107,8 +108,14 @@ class Alg( CfgPyAlgorithm ): def reinitialize(self): return StatusCode.Success - def sysExecute(self): - return self.execute() + def sysExecute(self, cppcontext): + import cppyy + self._ctx = cppyy.bind_object(cppcontext, "EventContext") + try: + ret = self.execute() + finally: + self._ctx = None + return ret def execute(self): return StatusCode.Success @@ -148,6 +155,9 @@ class Alg( CfgPyAlgorithm ): def isExecuted(self): return self._cppHandle.isExecuted() + + def getContext(self): + return self._ctx pass # PyAthena.Alg diff --git a/Control/AthenaPython/src/PyAthenaAlg.cxx b/Control/AthenaPython/src/PyAthenaAlg.cxx index c32e505fa858..b5c8a6e2dcb6 100644 --- a/Control/AthenaPython/src/PyAthenaAlg.cxx +++ b/Control/AthenaPython/src/PyAthenaAlg.cxx @@ -93,7 +93,10 @@ StatusCode Alg::execute() { // ATH_MSG_DEBUG("Executing " << name() << "..."); - return PyAthena::callPyMethod( m_self, "sysExecute" ); + PyObject* pycontext = PyCObject_FromVoidPtr ( const_cast<EventContext*>(&getContext()), nullptr); + StatusCode sc = PyAthena::callPyMethod( m_self, "sysExecute", pycontext ); + Py_DECREF (pycontext); + return sc; } void diff --git a/Control/AthenaPython/src/PyAthenaUtils.cxx b/Control/AthenaPython/src/PyAthenaUtils.cxx index b0a981514351..58ac3e935dde 100644 --- a/Control/AthenaPython/src/PyAthenaUtils.cxx +++ b/Control/AthenaPython/src/PyAthenaUtils.cxx @@ -152,7 +152,9 @@ void PyAthena::throw_py_exception (bool display) } StatusCode -PyAthena::callPyMethod( PyObject* self, const char* methodName ) +PyAthena::callPyMethod( PyObject* self, + const char* methodName, + PyObject* arg /*= nullptr*/) { // that's a bit ugly... char* method = const_cast<char*>(methodName); @@ -162,7 +164,11 @@ PyAthena::callPyMethod( PyObject* self, const char* methodName ) // call Python PyGILStateEnsure ensure; - PyObject* r = PyObject_CallMethod( self, method, const_cast<char*>("") ); + PyObject* r; + if (arg) + r = PyObject_CallMethod( self, method, const_cast<char*>("O"), arg ); + else + r = PyObject_CallMethod( self, method, const_cast<char*>("") ); if ( 0 == r ) { throw_py_exception(); -- GitLab