Salome HOME
Debug + makes the remote python nodes //
[modules/yacs.git] / src / runtime / PythonNode.cxx
index dcccba49a494d68787c9cb2f7816401c63049cfa..b82e20d70d4af39f0e1bfd64507fdd8718ff18d4 100644 (file)
@@ -1,11 +1,46 @@
+// Copyright (C) 2006-2014  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #include "RuntimeSALOME.hxx"
 #include "PythonNode.hxx"
 #include "PythonPorts.hxx"
+#include "TypeCode.hxx"
+#include "AutoGIL.hxx"
+#include "Container.hxx"
+#include "SalomeContainer.hxx"
+#include "SalomeHPContainer.hxx"
+#include "SalomeContainerTmpForHP.hxx"
+#include "ConversionException.hxx"
 
 #include "PyStdout.hxx"
 #include <iostream>
 #include <sstream>
+#include <fstream>
+
+#ifdef WIN32
+#include <process.h>
+#define getpid _getpid
+#endif
+
+#if PY_VERSION_HEX < 0x02050000 
+typedef int Py_ssize_t;
+#endif
 
 //#define _DEVDEBUG_
 #include "YacsTrace.hxx"
@@ -21,6 +56,14 @@ PythonNode::PythonNode(const PythonNode& other, ComposedNode *father):InlineNode
   _implementation=IMPL_NAME;
   PyGILState_STATE gstate=PyGILState_Ensure();
   _context=PyDict_New();
+  if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
+    {
+      stringstream msg;
+      msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
+      PyGILState_Release(gstate);
+      _errorDetails=msg.str();
+      throw Exception(msg.str());
+    }
   PyGILState_Release(gstate);
 }
 
@@ -29,6 +72,14 @@ PythonNode::PythonNode(const std::string& name):InlineNode(name)
   _implementation=IMPL_NAME;
   PyGILState_STATE gstate = PyGILState_Ensure();
   _context=PyDict_New();
+  if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
+    {
+      stringstream msg;
+      msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
+      PyGILState_Release(gstate);
+      _errorDetails=msg.str();
+      throw Exception(msg.str());
+    }
   PyGILState_Release(gstate);
 }
 
@@ -40,22 +91,317 @@ PythonNode::~PythonNode()
   PyGILState_Release(gstate);
 }
 
+void PythonNode::checkBasicConsistency() const throw(YACS::Exception)
+{
+  DEBTRACE("checkBasicConsistency");
+  InlineNode::checkBasicConsistency();
+
+  PyGILState_STATE gstate = PyGILState_Ensure();
+  PyObject* res;
+  res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
+  if(res == NULL)
+    {
+      std::string error="";
+      PyObject* new_stderr = newPyStdOut(error);
+      PySys_SetObject((char*)"stderr", new_stderr);
+      PyErr_Print();
+      PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+      Py_DECREF(new_stderr);
+      PyGILState_Release(gstate);
+      throw Exception(error);
+    }
+  else
+    Py_XDECREF(res);
+  PyGILState_Release(gstate);
+}
+
 void PythonNode::load()
 {
+  DEBTRACE( "---------------PyNode::load function---------------" );
+  if(_mode=="remote")
+    loadRemote();
+  else
+    loadLocal();
+}
+
+void PythonNode::loadLocal()
+{
+  DEBTRACE( "---------------PyNode::loadLocal function---------------" );
+  // do nothing
+}
+
+void PythonNode::loadRemote()
+{
+  DEBTRACE( "---------------PyNode::loadRemote function---------------" );
+  if(_container)
+    {
+      if(!_container->isAlreadyStarted(this))
+        {
+          try
+            {
+              _container->start(this);
+            }
+          catch(Exception& e)
+            {
+              _errorDetails=e.what();
+              throw e;
+            }
+        }
+    }
+  else
+    {
+      std::string what("PyNode::loadRemote : a load operation requested on \"");
+      what+=_name; what+="\" with no container specified.";
+      _errorDetails=what;
+      throw Exception(what);
+    }
+
+
+  Engines::Container_var objContainer=Engines::Container::_nil();
+  if(!_container)
+    throw Exception("No container specified !");
+  SalomeContainer *containerCast0(dynamic_cast<SalomeContainer *>(_container));
+  SalomeHPContainer *containerCast1(dynamic_cast<SalomeHPContainer *>(_container));
+  if(containerCast0)
+    objContainer=containerCast0->getContainerPtr(this);
+  else if(containerCast1)
+    {
+      YACS::BASES::AutoCppPtr<SalomeContainerTmpForHP> tmpCont(SalomeContainerTmpForHP::BuildFrom(containerCast1,this));
+      objContainer=tmpCont->getContainerPtr(this);
+    }
+  else
+    throw Exception("Unrecognized type of container ! Salome one is expected for PythonNode !");
+  if(CORBA::is_nil(objContainer))
+    throw Exception("Container corba pointer is NULL for PythonNode !");
+
+  try
+    {
+      _pynode = objContainer->createPyScriptNode(getName().c_str(),getScript().c_str());
+    }
+  catch( const SALOME::SALOME_Exception& ex )
+    {
+      std::string msg="Exception on remote python node creation ";
+      msg += '\n';
+      msg += ex.details.text.in();
+      _errorDetails=msg;
+      throw Exception(msg);
+    }
+
+  PyGILState_STATE gstate = PyGILState_Ensure();
+  const char picklizeScript[]="import cPickle\n"
+                              "def pickleForDistPyth2009(kws):\n"
+                              "  return cPickle.dumps(((),kws),-1)\n"
+                              "\n"
+                              "def unPickleForDistPyth2009(st):\n"
+                              "  args=cPickle.loads(st)\n"
+                              "  return args\n";
+  PyObject *res=PyRun_String(picklizeScript,Py_file_input,_context,_context);
+  if(res == NULL)
+    {
+      _errorDetails="";
+      PyObject* new_stderr = newPyStdOut(_errorDetails);
+      PySys_SetObject((char*)"stderr", new_stderr);
+      PyErr_Print();
+      PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+      Py_DECREF(new_stderr);
+
+      PyGILState_Release(gstate);
+      throw Exception("Error during load");
+    }
+  Py_DECREF(res);
+
+  _pyfuncSer=PyDict_GetItemString(_context,"pickleForDistPyth2009");
+  _pyfuncUnser=PyDict_GetItemString(_context,"unPickleForDistPyth2009");
+  if(_pyfuncSer == NULL)
+    {
+      _errorDetails="";
+      PyObject* new_stderr = newPyStdOut(_errorDetails);
+      PySys_SetObject((char*)"stderr", new_stderr);
+      PyErr_Print();
+      PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+      Py_DECREF(new_stderr);
+
+      PyGILState_Release(gstate);
+      throw Exception("Error during load");
+    }
+  if(_pyfuncUnser == NULL)
+    {
+      _errorDetails="";
+      PyObject* new_stderr = newPyStdOut(_errorDetails);
+      PySys_SetObject((char*)"stderr", new_stderr);
+      PyErr_Print();
+      PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+      Py_DECREF(new_stderr);
+
+      PyGILState_Release(gstate);
+      throw Exception("Error during load");
+    }
+  DEBTRACE( "---------------End PyNode::loadRemote function---------------" );
+  PyGILState_Release(gstate);
+
 }
 
 void PythonNode::execute()
 {
-  DEBTRACE( "++++++++++++++ PyNode::execute: " << getName() << " ++++++++++++++++++++" );
+  if(_mode=="remote")
+    executeRemote();
+  else
+    executeLocal();
+}
+
+void PythonNode::executeRemote()
+{
+  DEBTRACE( "++++++++++++++ PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
+  if(!_pyfuncSer)
+    throw Exception("DistributedPythonNode badly loaded");
   PyGILState_STATE gstate = PyGILState_Ensure();
-  if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
+
+  //===========================================================================
+  // Get inputs in input ports, build a Python dict and pickle it
+  //===========================================================================
+  PyObject* ob;
+  PyObject* args = PyDict_New();
+  std::list<InputPort *>::iterator iter2;
+  int pos=0;
+  for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); ++iter2)
+    {
+      InputPyPort *p=(InputPyPort *)*iter2;
+      ob=p->getPyObj();
+      PyDict_SetItemString(args,p->getName().c_str(),ob);
+      pos++;
+    }
+#ifdef _DEVDEBUG_
+  PyObject_Print(args,stderr,Py_PRINT_RAW);
+  std::cerr << endl;
+#endif
+  PyObject *serializationInput=PyObject_CallFunctionObjArgs(_pyfuncSer,args,NULL);
+  //The pickled string may contain NULL characters so use PyString_AsStringAndSize
+  char* serializationInputC;
+  Py_ssize_t len;
+  if (PyString_AsStringAndSize(serializationInput, &serializationInputC, &len))
     {
-      stringstream msg;
-      msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
       PyGILState_Release(gstate);
-      _errorDetails=msg.str();
-      throw Exception(msg.str());
+      throw Exception("DistributedPythonNode problem in python pickle");
+    }
+  PyGILState_Release(gstate);
+
+  Engines::pickledArgs_var serializationInputCorba=new Engines::pickledArgs;
+  serializationInputCorba->length(len);
+  for(int i=0; i < len ; i++)
+    serializationInputCorba[i]=serializationInputC[i];
+
+  //get the list of output argument names
+  std::list<OutputPort *>::iterator iter;
+  Engines::listofstring myseq;
+  myseq.length(getNumberOfOutputPorts());
+  pos=0;
+  for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
+    {
+      OutputPyPort *p=(OutputPyPort *)*iter;
+      myseq[pos]=p->getName().c_str();
+      DEBTRACE( "port name: " << p->getName() );
+      DEBTRACE( "port kind: " << p->edGetType()->kind() );
+      DEBTRACE( "port pos : " << pos );
+      pos++;
+    }
+  //===========================================================================
+  // Execute in remote Python node
+  //===========================================================================
+  DEBTRACE( "-----------------starting remote python invocation-----------------" );
+  Engines::pickledArgs_var resultCorba;
+  try
+    {
+      //pass outargsname and dict serialized
+      resultCorba=_pynode->execute(myseq,serializationInputCorba);
     }
+  catch( const SALOME::SALOME_Exception& ex )
+    {
+      std::string msg="Exception on remote python invocation";
+      msg += '\n';
+      msg += ex.details.text.in();
+      _errorDetails=msg;
+      throw Exception(msg);
+    }
+  DEBTRACE( "-----------------end of remote python invocation-----------------" );
+  //===========================================================================
+  // Get results, unpickle and put them in output ports
+  //===========================================================================
+  char *resultCorbaC=new char[resultCorba->length()+1];
+  resultCorbaC[resultCorba->length()]='\0';
+  for(int i=0;i<resultCorba->length();i++)
+    resultCorbaC[i]=resultCorba[i];
+
+  gstate = PyGILState_Ensure();
+
+  PyObject* resultPython=PyString_FromStringAndSize(resultCorbaC,resultCorba->length());
+  delete [] resultCorbaC;
+  args = PyTuple_New(1);
+  PyTuple_SetItem(args,0,resultPython);
+  PyObject *finalResult=PyObject_CallObject(_pyfuncUnser,args);
+  Py_DECREF(args);
+
+  if (finalResult == NULL)
+  {
+    std::stringstream msg;
+    msg << "Conversion with pickle of output ports failed !";
+    msg << " : " << __FILE__ << ":" << __LINE__;
+    PyGILState_Release(gstate);
+    _errorDetails=msg.str();
+    throw YACS::ENGINE::ConversionException(msg.str());
+  }
+
+  DEBTRACE( "-----------------PythonNode::outputs-----------------" );
+  int nres=1;
+  if(finalResult == Py_None)
+    nres=0;
+  else if(PyTuple_Check(finalResult))
+    nres=PyTuple_Size(finalResult);
+
+  if(getNumberOfOutputPorts() != nres)
+    {
+      std::string msg="Number of output arguments : Mismatch between definition and execution";
+      Py_DECREF(finalResult);
+      PyGILState_Release(gstate);
+      _errorDetails=msg;
+      throw Exception(msg);
+    }
+
+  pos=0;
+  try
+    {
+      for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
+        {
+          OutputPyPort *p=(OutputPyPort *)*iter;
+          DEBTRACE( "port name: " << p->getName() );
+          DEBTRACE( "port kind: " << p->edGetType()->kind() );
+          DEBTRACE( "port pos : " << pos );
+          if(PyTuple_Check(finalResult))
+            ob=PyTuple_GetItem(finalResult,pos) ;
+          else
+            ob=finalResult;
+          DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
+          p->put(ob);
+          pos++;
+        }
+      Py_DECREF(finalResult);
+    }
+  catch(ConversionException& ex)
+    {
+      Py_DECREF(finalResult);
+      PyGILState_Release(gstate);
+      _errorDetails=ex.what();
+      throw;
+    }
+
+  PyGILState_Release(gstate);
+
+  DEBTRACE( "++++++++++++++ ENDOF PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
+}
+
+void PythonNode::executeLocal()
+{
+  DEBTRACE( "++++++++++++++ PyNode::executeLocal: " << getName() << " ++++++++++++++++++++" );
+  PyGILState_STATE gstate = PyGILState_Ensure();
 
   DEBTRACE( "---------------PyNode::inputs---------------" );
   list<InputPort *>::iterator iter2;
@@ -80,23 +426,48 @@ void PythonNode::execute()
   DEBTRACE( "----------------PyNode::calculation---------------" );
   DEBTRACE(  _script );
   DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
-  PyObject *res=PyRun_String(_script.c_str(),Py_file_input,_context,_context);
+
+  std::ostringstream stream;
+  stream << "/tmp/PythonNode_";
+  stream << getpid();
+
+  PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
+  if(code == NULL)
+    {
+      _errorDetails="";
+      PyObject* new_stderr = newPyStdOut(_errorDetails);
+      PySys_SetObject((char*)"stderr", new_stderr);
+      PyErr_Print();
+      PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+      Py_DECREF(new_stderr);
+      PyGILState_Release(gstate);
+      throw Exception("Error during execution");
+    }
+  PyObject *res = PyEval_EvalCode((PyCodeObject *)code, _context, _context);
+
+  Py_DECREF(code);
+  Py_XDECREF(res);
   DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
   fflush(stdout);
   fflush(stderr);
-  if(res == NULL)
+  if(PyErr_Occurred ())
     {
       _errorDetails="";
       PyObject* new_stderr = newPyStdOut(_errorDetails);
-      PySys_SetObject("stderr", new_stderr);
+      PySys_SetObject((char*)"stderr", new_stderr);
+      ofstream errorfile(stream.str().c_str());
+      if (errorfile.is_open())
+        {
+          errorfile << _script;
+          errorfile.close();
+        }
       PyErr_Print();
-      PySys_SetObject("stderr", PySys_GetObject("__stderr__"));
+      PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
       Py_DECREF(new_stderr);
 
       PyGILState_Release(gstate);
       throw Exception("Error during execution");
     }
-  Py_DECREF(res);
   
   DEBTRACE( "-----------------PyNode::outputs-----------------" );
   list<OutputPort *>::iterator iter;
@@ -135,6 +506,39 @@ void PythonNode::execute()
   DEBTRACE( "++++++++++++++ End PyNode::execute: " << getName() << " ++++++++++++++++++++" );
 }
 
+std::string PythonNode::getContainerLog()
+{
+  if(_mode=="local")return "";
+
+  std::string msg;
+  try
+    {
+      Engines::Container_var objContainer=((SalomeContainer*)_container)->getContainerPtr(this);
+      CORBA::String_var logname = objContainer->logfilename();
+      DEBTRACE(logname);
+      msg=logname;
+      std::string::size_type pos = msg.find(":");
+      msg=msg.substr(pos+1);
+    }
+  catch(...)
+    {
+      msg = "Container no longer reachable";
+    }
+  return msg;
+}
+
+void PythonNode::shutdown(int level)
+{
+  DEBTRACE("PythonNode::shutdown " << level);
+  if(_mode=="local")return;
+  if(_container)
+    {
+      if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
+      _pynode=Engines::PyScriptNode::_nil();
+      _container->shutdown(level);
+    }
+}
+
 Node *PythonNode::simpleClone(ComposedNode *father, bool editionOnly) const
 {
   return new PythonNode(*this,father);
@@ -208,12 +612,154 @@ PyFuncNode::~PyFuncNode()
   if(_pyfunc)DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
   Py_DECREF(_context);
   PyGILState_Release(gstate);
+  if(!CORBA::is_nil(_pynode))
+    {
+      _pynode->UnRegister();
+    }
+}
+
+void PyFuncNode::checkBasicConsistency() const throw(YACS::Exception)
+{
+  DEBTRACE("checkBasicConsistency");
+  InlineFuncNode::checkBasicConsistency();
+
+  PyGILState_STATE gstate = PyGILState_Ensure();
+  PyObject* res;
+  res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
+  if(res == NULL)
+    {
+      std::string error="";
+      PyObject* new_stderr = newPyStdOut(error);
+      PySys_SetObject((char*)"stderr", new_stderr);
+      PyErr_Print();
+      PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+      Py_DECREF(new_stderr);
+      PyGILState_Release(gstate);
+      throw Exception(error);
+    }
+  else
+    Py_XDECREF(res);
+  PyGILState_Release(gstate);
 }
 
 void PyFuncNode::load()
+{
+  DEBTRACE( "---------------PyfuncNode::load function---------------" );
+  if(_mode=="remote")
+    loadRemote();
+  else
+    loadLocal();
+}
+
+void PyFuncNode::loadRemote()
+{
+  DEBTRACE( "---------------PyfuncNode::loadRemote function---------------" );
+  if(_container)
+    {
+      if(!_container->isAlreadyStarted(this))
+        {
+          try
+            {
+              _container->start(this);
+            }
+          catch(Exception& e)
+            {
+              _errorDetails=e.what();
+              throw e;
+            }
+        }
+    }
+  else
+    {
+      std::string what("PyFuncNode::loadRemote : a load operation requested on \"");
+      what+=_name; what+="\" with no container specified.";
+      _errorDetails=what;
+      throw Exception(what);
+    }
+
+  Engines::Container_var objContainer=Engines::Container::_nil();
+  if(!_container)
+    throw Exception("No container specified !");
+  SalomeContainer *containerCast0(dynamic_cast<SalomeContainer *>(_container));
+  SalomeHPContainer *containerCast1(dynamic_cast<SalomeHPContainer *>(_container));
+  if(containerCast0)
+    objContainer=containerCast0->getContainerPtr(this);
+  else if(containerCast1)
+    {
+      YACS::BASES::AutoCppPtr<SalomeContainerTmpForHP> tmpCont(SalomeContainerTmpForHP::BuildFrom(containerCast1,this));
+      objContainer=tmpCont->getContainerPtr(this);
+    }
+  else
+    throw Exception("Unrecognized type of container ! Salome one is expected ! In PythonNode !");
+  if(CORBA::is_nil(objContainer))
+    throw Exception("Container corba pointer is NULL ! In PythonNode !");
+
+  try
+    {
+      _pynode = objContainer->createPyNode(getName().c_str(),getScript().c_str());
+    }
+  catch( const SALOME::SALOME_Exception& ex )
+    {
+      std::string msg="Exception on remote python node creation ";
+      msg += '\n';
+      msg += ex.details.text.in();
+      _errorDetails=msg;
+      throw Exception(msg);
+    }
+
+  {
+    AutoGIL agil;
+    const char picklizeScript[]="import cPickle\n"
+        "def pickleForDistPyth2009(*args,**kws):\n"
+        "  return cPickle.dumps((args,kws),-1)\n"
+        "\n"
+        "def unPickleForDistPyth2009(st):\n"
+        "  args=cPickle.loads(st)\n"
+        "  return args\n";
+    PyObject *res=PyRun_String(picklizeScript,Py_file_input,_context,_context);
+    if(res == NULL)
+      {
+        _errorDetails="";
+        PyObject* new_stderr = newPyStdOut(_errorDetails);
+        PySys_SetObject((char*)"stderr", new_stderr);
+        PyErr_Print();
+        PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+        Py_DECREF(new_stderr);
+        throw Exception("Error during load");
+      }
+    Py_DECREF(res);
+
+    _pyfuncSer=PyDict_GetItemString(_context,"pickleForDistPyth2009");
+    _pyfuncUnser=PyDict_GetItemString(_context,"unPickleForDistPyth2009");
+    if(_pyfuncSer == NULL)
+      {
+        _errorDetails="";
+        PyObject* new_stderr = newPyStdOut(_errorDetails);
+        PySys_SetObject((char*)"stderr", new_stderr);
+        PyErr_Print();
+        PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+        Py_DECREF(new_stderr);
+        throw Exception("Error during load");
+      }
+    if(_pyfuncUnser == NULL)
+      {
+        _errorDetails="";
+        PyObject* new_stderr = newPyStdOut(_errorDetails);
+        PySys_SetObject((char*)"stderr", new_stderr);
+        PyErr_Print();
+        PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+        Py_DECREF(new_stderr);
+        throw Exception("Error during load");
+      }
+    DEBTRACE( "---------------End PyfuncNode::loadRemote function---------------" );
+  }
+}
+
+void PyFuncNode::loadLocal()
 {
   DEBTRACE( "---------------PyFuncNode::load function " << getName() << " ---------------" );
   DEBTRACE(  _script );
+
 #ifdef _DEVDEBUG_
   list<OutputPort *>::iterator iter;
   for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
@@ -223,33 +769,59 @@ void PyFuncNode::load()
       DEBTRACE( "port kind: " << p->edGetType()->kind() );
     }
 #endif
+
   PyGILState_STATE gstate = PyGILState_Ensure();
   DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
-  PyObject *res=PyRun_String(_script.c_str(),Py_file_input,_context,_context);
+
+  std::ostringstream stream;
+  stream << "/tmp/PythonNode_";
+  stream << getpid();
+
+  PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
+  if(code == NULL)
+    {
+      _errorDetails="";
+      PyObject* new_stderr = newPyStdOut(_errorDetails);
+      PySys_SetObject((char*)"stderr", new_stderr);
+      PyErr_Print();
+      PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+      Py_DECREF(new_stderr);
+      PyGILState_Release(gstate);
+      throw Exception("Error during execution");
+    }
+  PyObject *res = PyEval_EvalCode((PyCodeObject *)code, _context, _context);
+  Py_DECREF(code);
+  Py_XDECREF(res);
+
   DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
-  if(res == NULL)
+  if(PyErr_Occurred ())
     {
       _errorDetails="";
       PyObject* new_stderr = newPyStdOut(_errorDetails);
-      PySys_SetObject("stderr", new_stderr);
+      PySys_SetObject((char*)"stderr", new_stderr);
+      ofstream errorfile(stream.str().c_str());
+      if (errorfile.is_open())
+        {
+          errorfile << _script;
+          errorfile.close();
+        }
       PyErr_Print();
-      PySys_SetObject("stderr", PySys_GetObject("__stderr__"));
+      PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
       Py_DECREF(new_stderr);
 
       PyGILState_Release(gstate);
       throw Exception("Error during execution");
       return;
     }
-  Py_DECREF(res);
   _pyfunc=PyDict_GetItemString(_context,_fname.c_str());
   DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
   if(_pyfunc == NULL)
     {
       _errorDetails="";
       PyObject* new_stderr = newPyStdOut(_errorDetails);
-      PySys_SetObject("stderr", new_stderr);
+      PySys_SetObject((char*)"stderr", new_stderr);
       PyErr_Print();
-      PySys_SetObject("stderr", PySys_GetObject("__stderr__"));
+      PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
       Py_DECREF(new_stderr);
 
       PyGILState_Release(gstate);
@@ -260,8 +832,139 @@ void PyFuncNode::load()
 }
 
 void PyFuncNode::execute()
+{
+  if(_mode=="remote")
+    executeRemote();
+  else
+    executeLocal();
+}
+
+void PyFuncNode::executeRemote()
+{
+  DEBTRACE( "++++++++++++++ PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
+  if(!_pyfuncSer)
+    throw Exception("DistributedPythonNode badly loaded");
+  Py_ssize_t len;
+  char *serializationInputC(0);
+  PyObject *args(0),*ob(0);
+  int pos(0);
+  {
+      AutoGIL agil;
+
+      //===========================================================================
+      // Get inputs in input ports, build a Python tuple and pickle it
+      //===========================================================================
+      args = PyTuple_New(getNumberOfInputPorts());
+      std::list<InputPort *>::iterator iter2;
+      for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); ++iter2)
+        {
+          InputPyPort *p=(InputPyPort *)*iter2;
+          ob=p->getPyObj();
+          Py_INCREF(ob);
+          PyTuple_SetItem(args,pos,ob);
+          pos++;
+        }
+#ifdef _DEVDEBUG_
+      PyObject_Print(args,stderr,Py_PRINT_RAW);
+      std::cerr << endl;
+#endif
+      PyObject *serializationInput=PyObject_CallObject(_pyfuncSer,args);
+      //The pickled string may contain NULL characters so use PyString_AsStringAndSize
+      if (PyString_AsStringAndSize(serializationInput, &serializationInputC, &len))
+        throw Exception("DistributedPythonNode problem in python pickle");
+  }
+
+  Engines::pickledArgs_var serializationInputCorba=new Engines::pickledArgs;
+  serializationInputCorba->length(len);
+  for(int i=0; i < len ; i++)
+    serializationInputCorba[i]=serializationInputC[i];
+
+  //===========================================================================
+  // Execute in remote Python node
+  //===========================================================================
+  DEBTRACE( "-----------------starting remote python invocation-----------------" );
+  Engines::pickledArgs_var resultCorba;
+  try
+    {
+      resultCorba=_pynode->execute(getFname().c_str(),serializationInputCorba);
+    }
+  catch( const SALOME::SALOME_Exception& ex )
+    {
+      std::string msg="Exception on remote python invocation";
+      msg += '\n';
+      msg += ex.details.text.in();
+      _errorDetails=msg;
+      throw Exception(msg);
+    }
+  DEBTRACE( "-----------------end of remote python invocation-----------------" );
+  //===========================================================================
+  // Get results, unpickle and put them in output ports
+  //===========================================================================
+  char *resultCorbaC=new char[resultCorba->length()+1];
+  resultCorbaC[resultCorba->length()]='\0';
+  for(int i=0;i<resultCorba->length();i++)
+    resultCorbaC[i]=resultCorba[i];
+
+  {
+      AutoGIL agil;
+
+      PyObject* resultPython=PyString_FromStringAndSize(resultCorbaC,resultCorba->length());
+      delete [] resultCorbaC;
+      args = PyTuple_New(1);
+      PyTuple_SetItem(args,0,resultPython);
+      PyObject *finalResult=PyObject_CallObject(_pyfuncUnser,args);
+      Py_DECREF(args);
+
+      DEBTRACE( "-----------------PythonNode::outputs-----------------" );
+      int nres=1;
+      if(finalResult == Py_None)
+        nres=0;
+      else if(PyTuple_Check(finalResult))
+        nres=PyTuple_Size(finalResult);
+
+      if(getNumberOfOutputPorts() != nres)
+        {
+          std::string msg="Number of output arguments : Mismatch between definition and execution";
+          Py_DECREF(finalResult);
+          _errorDetails=msg;
+          throw Exception(msg);
+        }
+
+      pos=0;
+      std::list<OutputPort *>::iterator iter;
+      try
+      {
+          for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
+            {
+              OutputPyPort *p=(OutputPyPort *)*iter;
+              DEBTRACE( "port name: " << p->getName() );
+              DEBTRACE( "port kind: " << p->edGetType()->kind() );
+              DEBTRACE( "port pos : " << pos );
+              if(PyTuple_Check(finalResult))
+                ob=PyTuple_GetItem(finalResult,pos) ;
+              else
+                ob=finalResult;
+              DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
+              p->put(ob);
+              pos++;
+            }
+          Py_DECREF(finalResult);
+      }
+      catch(ConversionException& ex)
+      {
+          Py_DECREF(finalResult);
+          _errorDetails=ex.what();
+          throw;
+      }
+  }
+
+  DEBTRACE( "++++++++++++++ ENDOF PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
+}
+
+void PyFuncNode::executeLocal()
 {
   DEBTRACE( "++++++++++++++ PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
+
   int pos=0;
   PyObject* ob;
   if(!_pyfunc)throw Exception("PyFuncNode badly loaded");
@@ -305,9 +1008,18 @@ void PyFuncNode::execute()
     {
       _errorDetails="";
       PyObject* new_stderr = newPyStdOut(_errorDetails);
-      PySys_SetObject("stderr", new_stderr);
+      PySys_SetObject((char*)"stderr", new_stderr);
+      std::ostringstream stream;
+      stream << "/tmp/PythonNode_";
+      stream << getpid();
+      ofstream errorfile(stream.str().c_str());
+      if (errorfile.is_open())
+        {
+          errorfile << _script;
+          errorfile.close();
+        }
       PyErr_Print();
-      PySys_SetObject("stderr", PySys_GetObject("__stderr__"));
+      PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
       Py_DECREF(new_stderr);
 
       PyGILState_Release(gstate);
@@ -395,3 +1107,37 @@ PyFuncNode* PyFuncNode::cloneNode(const std::string& name)
     }
   return n;
 }
+
+std::string PyFuncNode::getContainerLog()
+{
+  if(_mode=="local")return "";
+
+  std::string msg;
+  try
+    {
+      Engines::Container_var objContainer=((SalomeContainer*)_container)->getContainerPtr(this);
+      CORBA::String_var logname = objContainer->logfilename();
+      DEBTRACE(logname);
+      msg=logname;
+      std::string::size_type pos = msg.find(":");
+      msg=msg.substr(pos+1);
+    }
+  catch(...)
+    {
+      msg = "Container no longer reachable";
+    }
+  return msg;
+}
+
+void PyFuncNode::shutdown(int level)
+{
+  DEBTRACE("PyFuncNode::shutdown " << level);
+  if(_mode=="local")return;
+  if(_container)
+    {
+      if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
+      _pynode=Engines::PyNode::_nil();
+      _container->shutdown(level);
+    }
+}
+