Salome HOME
[EDF28562] : Possibility of exclusion output port of squeezeMemory mecanism in Python...
[modules/yacs.git] / src / runtime / PythonNode.cxx
index 3334d97e2a41c0ef119f043a45cfc0eca16f349d..57a13a016448a21fbd9322d87bd9550823187f5d 100644 (file)
@@ -332,7 +332,24 @@ bool PythonEntry::IsProxy( PyObject *ob )
 {
   if(!_pyClsBigObject)
     return false;
-  return PyObject_IsInstance( ob, _pyClsBigObject) == 1;
+  if( PyObject_IsInstance( ob, _pyClsBigObject) == 1 )
+  {
+    return true;
+  }
+  else
+  {
+    if( PyList_Check( ob ) )
+    {
+      auto sz = PyList_Size( ob );
+      for( auto i = 0 ; i < sz ; ++i )
+      {
+        PyObject *elt = PyList_GetItem( ob, i );
+        if( PythonEntry::IsProxy(elt) )
+          return true;
+      }
+    }
+  }
+  return false;
 }
 
 bool PythonEntry::GetDestroyStatus( PyObject *ob )
@@ -349,6 +366,19 @@ bool PythonEntry::GetDestroyStatus( PyObject *ob )
     }
     return false;
   }
+  else
+  {
+    if( PyList_Check( ob ) )
+    {
+      auto sz = PyList_Size( ob );
+      for( auto i = 0 ; i < sz ; ++i )
+      {
+        PyObject *elt = PyList_GetItem( ob, i );
+        if( PythonEntry::GetDestroyStatus(elt) )
+          return true;
+      }
+    }
+  }
   return false;
 }
 
@@ -361,6 +391,18 @@ void PythonEntry::IfProxyDoSomething( PyObject *ob, const char *meth )
     AutoPyRef unlinkOnDestructor = PyObject_GetAttrString(ob,meth);
     AutoPyRef tmp = PyObject_CallFunctionObjArgs(unlinkOnDestructor,nullptr);
   }
+  else
+  {
+    if( PyList_Check( ob ) )
+    {
+      auto sz = PyList_Size( ob );
+      for( auto i = 0 ; i < sz ; ++i )
+      {
+        PyObject *elt = PyList_GetItem( ob, i );
+        PythonEntry::IfProxyDoSomething( elt, meth );
+      }
+    }
+  }
 }
 
 void PythonEntry::DoNotTouchFileIfProxy( PyObject *ob )
@@ -373,7 +415,8 @@ void PythonEntry::UnlinkOnDestructorIfProxy( PyObject *ob )
   IfProxyDoSomething(ob,"unlinkOnDestructor");
 }
 
-PythonNode::PythonNode(const PythonNode& other, ComposedNode *father):InlineNode(other,father),_autoSqueeze(other._autoSqueeze)
+PythonNode::PythonNode(const PythonNode& other, ComposedNode *father):
+InlineNode(other,father),_autoSqueeze(other._autoSqueeze),_nonSqueezableOutputNodes(other._nonSqueezableOutputNodes)
 {
   _pynode = Engines::PyScriptNode::_nil();
   _implementation=IMPL_NAME;
@@ -678,33 +721,34 @@ void PythonNode::executeRemote()
   }
   DEBTRACE( "++++++++++++++ ENDOF PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
 }
-void PythonNode::executeLocalInternal(const std::string& codeStr)
+
+void PythonNode::ExecuteLocalInternal(const std::string& codeStr, PyObject *context, std::string& errorDetails)
 {
   DEBTRACE(  code );
-  DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
+  DEBTRACE( "context refcnt: " << context->ob_refcnt );
   std::ostringstream stream;
   stream << "/tmp/PythonNode_";
   stream << getpid();
   AutoPyRef code=Py_CompileString(codeStr.c_str(), stream.str().c_str(), Py_file_input);
   if(code == NULL)
   {
-    _errorDetails=""; 
-    AutoPyRef new_stderr = newPyStdOut(_errorDetails);
+    errorDetails=""; 
+    AutoPyRef new_stderr = newPyStdOut(errorDetails);
     PySys_SetObject((char*)"stderr", new_stderr);
     PyErr_Print();
     PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
-    throw Exception("Error during execution");
+    throw YACS::Exception("Error during execution");
   }
   {
-    AutoPyRef res = PyEval_EvalCode(  code, _context, _context);
+    AutoPyRef res = PyEval_EvalCode(  code, context, context);
   }
-  DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
+  DEBTRACE( "context refcnt: " << context->ob_refcnt );
   fflush(stdout);
   fflush(stderr);
   if(PyErr_Occurred ())
   {
-    _errorDetails="";
-    AutoPyRef new_stderr = newPyStdOut(_errorDetails);
+    errorDetails="";
+    AutoPyRef new_stderr = newPyStdOut(errorDetails);
     PySys_SetObject((char*)"stderr", new_stderr);
     ofstream errorfile(stream.str().c_str());
     if (errorfile.is_open())
@@ -714,10 +758,15 @@ void PythonNode::executeLocalInternal(const std::string& codeStr)
       }
     PyErr_Print();
     PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
-    throw Exception("Error during execution");
+    throw YACS::Exception("Error during execution");
   }
 }
 
+void PythonNode::executeLocalInternal(const std::string& codeStr)
+{
+  ExecuteLocalInternal(codeStr,_context,_errorDetails);
+}
+
 void PythonNode::executeLocal()
 {
   DEBTRACE( "++++++++++++++ PyNode::executeLocal: " << getName() << " ++++++++++++++++++++" );
@@ -746,8 +795,9 @@ void PythonNode::executeLocal()
 
     //calculation
     DEBTRACE( "----------------PyNode::calculation---------------" );
-
-    executeLocalInternal( unpxy.str() );
+  
+    if( ! getSqueezeStatus() )
+      executeLocalInternal( unpxy.str() );
 
     executeLocalInternal( _script );
 
@@ -774,6 +824,8 @@ void PythonNode::executeLocal()
             cerr << endl;
 #endif
             p->put(ob);
+            if(!isUsingPythonCache())
+              PyDict_DelItemString(_context,p->getName().c_str());
           }
     }
     catch(ConversionException& ex)
@@ -784,10 +836,29 @@ void PythonNode::executeLocal()
     if(_autoSqueeze)
       squeezeMemory();
     DEBTRACE( "-----------------End PyNode::outputs-----------------" );
+    if(!isUsingPythonCache())
+    {
+      for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
+      {
+        AutoPyRef pStr = PyUnicode_FromString( (*iter2)->getName().c_str() );
+        if( PyDict_Contains(_context,pStr) == 1 )
+          { PyDict_DelItem(_context,pStr); }
+      }
+    }
   }
   DEBTRACE( "++++++++++++++ End PyNode::execute: " << getName() << " ++++++++++++++++++++" );
 }
 
+/*!
+ * [EDF28562]
+ * \param in squeezeExceptions list on output port name excluded from the squeeze mecanism
+ */
+void PythonNode::setSqueezeStatusWithExceptions(bool sqStatus, const std::vector<std::string>& squeezeExceptions)
+{
+  this->setSqueezeStatus(sqStatus);
+  this->_nonSqueezableOutputNodes = std::set<std::string>(squeezeExceptions.begin(), squeezeExceptions.end());
+}
+
 void PythonNode::squeezeMemorySafe()
 {
   AutoGIL agil;
@@ -808,6 +879,8 @@ void PythonNode::squeezeMemory()
     }
   for(auto p : _setOfOutputPort)
     {
+      if (!this->_nonSqueezableOutputNodes.empty() && this->_nonSqueezableOutputNodes.find(p->getName()) != this->_nonSqueezableOutputNodes.end())
+        continue;
       PyDict_DelItemString(_context,p->getName().c_str());
       OutputPyPort *p2(static_cast<OutputPyPort *>(p));
       p2->putWithoutForward(Py_None);
@@ -824,6 +897,8 @@ void PythonNode::squeezeMemoryRemote()
     }
   for(auto p : _setOfOutputPort)
     {
+      if (!this->_nonSqueezableOutputNodes.empty() && this->_nonSqueezableOutputNodes.find(p->getName()) != this->_nonSqueezableOutputNodes.end())
+        continue;
       OutputPyPort *p2(static_cast<OutputPyPort *>(p));
       p2->putWithoutForward(Py_None);
     }