From 3faa9bfe96ae74fdca18143c639dcfb9b41cd43e Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Mon, 12 Jun 2023 14:15:52 +0200 Subject: [PATCH] [EDF27816] Management of double foreach and management of proxyfile lifecycle --- src/runtime/PythonNode.cxx | 13 +++++++--- src/runtime/PythonNode.hxx | 3 ++- src/runtime/PythonPorts.cxx | 42 +++++++-------------------------- src/runtime/TypeConversions.cxx | 15 ++++++++++++ 4 files changed, 35 insertions(+), 38 deletions(-) diff --git a/src/runtime/PythonNode.cxx b/src/runtime/PythonNode.cxx index d124da6a3..ec172ff4b 100644 --- a/src/runtime/PythonNode.cxx +++ b/src/runtime/PythonNode.cxx @@ -328,6 +328,13 @@ bool PythonEntry::hasImposedResource()const return !_imposedResource.empty() && !_imposedContainer.empty(); } +bool PythonEntry::IsProxy( PyObject *ob ) +{ + if(!_pyClsBigObject) + return false; + return PyObject_IsInstance( ob, _pyClsBigObject) == 1; +} + bool PythonEntry::GetDestroyStatus( PyObject *ob ) { if(!_pyClsBigObject) @@ -718,7 +725,7 @@ void PythonNode::executeLocal() DEBTRACE( "++++++++++++++ PyNode::executeLocal: " << getName() << " ++++++++++++++++++++" ); { AutoGIL agil; - std::ostringstream unpxy; unpxy << "from SALOME_PyNode import UnProxyObjectSimple" << std::endl; + std::ostringstream unpxy; unpxy << "from SALOME_PyNode import UnProxyObjectSimpleLocal" << std::endl; DEBTRACE( "---------------PyNode::inputs---------------" ); list::iterator iter2; for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++) @@ -727,8 +734,8 @@ void PythonNode::executeLocal() DEBTRACE( "port name: " << p->getName() ); DEBTRACE( "port kind: " << p->edGetType()->kind() ); PyObject* ob=p->getPyObj(); - DEBTRACE( "ob refcnt: " << ob->ob_refcnt ); - unpxy << p->getName() << " = UnProxyObjectSimple( " << p->getName() << " )" << std::endl; + DEBTRACE( "ob refcnt: " << ob->ob_refcnt ); + unpxy << p->getName() << " = UnProxyObjectSimpleLocal( " << p->getName() << " )" << std::endl; #ifdef _DEVDEBUG_ PyObject_Print(ob,stderr,Py_PRINT_RAW); cerr << endl; diff --git a/src/runtime/PythonNode.hxx b/src/runtime/PythonNode.hxx index 7749f6549..d83997cde 100644 --- a/src/runtime/PythonNode.hxx +++ b/src/runtime/PythonNode.hxx @@ -35,10 +35,12 @@ namespace YACS class YACSRUNTIMESALOME_EXPORT PythonEntry { public: + static bool IsProxy( PyObject *ob ); /*! return true only if ob is a proxy and destroy flag set to true*/ static bool GetDestroyStatus( PyObject *ob ); static void DoNotTouchFileIfProxy( PyObject *ob ); static void UnlinkOnDestructorIfProxy( PyObject *ob ); + static void IfProxyDoSomething( PyObject *ob, const char *meth ); protected: PythonEntry(); ~PythonEntry(); @@ -56,7 +58,6 @@ namespace YACS void loadRemoteContext(InlineNode *reqNode, Engines::Container_ptr objContainer, bool isInitializeRequested); static std::string GetContainerLog(const std::string& mode, Container *container, const Task *askingTask); virtual bool hasImposedResource()const; - static void IfProxyDoSomething( PyObject *ob, const char *meth ); protected: PyObject *_context; PyObject *_pyfuncSer; diff --git a/src/runtime/PythonPorts.cxx b/src/runtime/PythonPorts.cxx index e4af89859..f1e8a1232 100644 --- a/src/runtime/PythonPorts.cxx +++ b/src/runtime/PythonPorts.cxx @@ -32,7 +32,7 @@ using namespace YACS::ENGINE; using namespace std; -void releasePyObj(PyObject* data) +static void RegisterReleasePyObj(PyObject* data, const char *method1) { if(!data) return ; @@ -44,7 +44,7 @@ void releasePyObj(PyObject* data) { if(PyLong_AS_LONG(result)) { - PyObject* o=PyObject_CallMethod(data, (char*)"Destroy", (char*)""); + PyObject* o=PyObject_CallMethod(data, (char*)method1, (char*)""); if(o) Py_XDECREF( o); else @@ -71,40 +71,14 @@ void releasePyObj(PyObject* data) } } +void releasePyObj(PyObject* data) +{ + RegisterReleasePyObj(data,"Destroy"); +} + void registerPyObj(PyObject* data) { - if (PyObject_HasAttrString(data, (char*)"_is_a")) - { - PyObject *result = PyObject_CallMethod(data, (char*)"_is_a", (char*)"s",(char*)"IDL:SALOME/GenericObj:1.0"); - if(result && PyLong_Check(result)) - { - if(PyLong_AS_LONG(result)) - { - PyObject* o= PyObject_CallMethod(data, (char*)"Register", (char*)"") ; - if(o) - Py_XDECREF( o); - else - { -#ifdef _DEVDEBUG_ - PyErr_Print(); -#else - PyErr_Clear(); -#endif - throw ConversionException("Corba object does not exist: you have perhaps forgotten to call Register on a GenericObj"); - } - } - Py_XDECREF(result); - } - if(!result) - { -#ifdef _DEVDEBUG_ - PyErr_Print(); -#else - PyErr_Clear(); -#endif - throw ConversionException("Corba object does not exist: you have perhaps forgotten to call Register on a GenericObj"); - } - } + RegisterReleasePyObj(data,"Register"); } InputPyPort::InputPyPort(const std::string& name, Node *node, TypeCode * type) diff --git a/src/runtime/TypeConversions.cxx b/src/runtime/TypeConversions.cxx index 87524ed74..1d194dfdc 100644 --- a/src/runtime/TypeConversions.cxx +++ b/src/runtime/TypeConversions.cxx @@ -791,7 +791,10 @@ namespace YACS PyObject* mod=PyImport_ImportModule("pickle"); PyObject *pickled=PyObject_CallMethod(mod,(char *)"dumps",(char *)"Oi",o,protocol); if( somthingToDo ) + { YACS::ENGINE::PythonEntry::UnlinkOnDestructorIfProxy(o); + YACS::ENGINE::PythonEntry::IfProxyDoSomething(o,"incrRef"); + } DEBTRACE(PyObject_Repr(pickled) ); Py_DECREF(mod); if(pickled==NULL) @@ -863,6 +866,12 @@ namespace YACS for(int i=0;i