]> SALOME platform Git repositories - modules/yacs.git/blob - src/runtime/PythonNode.cxx
Salome HOME
[EDF27816] : Fix bug presence of proxy into a list
[modules/yacs.git] / src / runtime / PythonNode.cxx
1 // Copyright (C) 2006-2023  CEA, EDF
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "RuntimeSALOME.hxx"
21 #include "PythonNode.hxx"
22 #include "PythonPorts.hxx"
23 #include "TypeCode.hxx"
24 #include "PythonCppUtils.hxx"
25 #include "Container.hxx"
26 #include "SalomeContainer.hxx"
27 #include "SalomeHPContainer.hxx"
28 #include "SalomeContainerTmpForHP.hxx"
29 #include "ConversionException.hxx"
30 #include "ReceiverFactory.hxx"
31 #include "SenderByteImpl.hxx"
32
33 #include "PyStdout.hxx"
34 #include <iostream>
35 #include <memory>
36 #include <sstream>
37 #include <fstream>
38
39 #ifdef WIN32
40 #include <process.h>
41 #define getpid _getpid
42 #endif
43
44 #if PY_VERSION_HEX < 0x02050000 
45 typedef int Py_ssize_t;
46 #endif
47
48 //#define _DEVDEBUG_
49 #include "YacsTrace.hxx"
50
51 using namespace YACS::ENGINE;
52 using namespace std;
53
54 const char PythonEntry::SCRIPT_FOR_SIMPLE_SERIALIZATION[]="import pickle\n"
55     "def pickleForVarSimplePyth2009(val):\n"
56     "  return pickle.dumps(val,-1)\n"
57     "\n";
58
59 PyObject *PythonEntry::_pyClsBigObject = nullptr;
60
61 const char PythonNode::IMPL_NAME[]="Python";
62 const char PythonNode::KIND[]="Python";
63
64 const char PythonNode::SCRIPT_FOR_SERIALIZATION[]="import pickle\n"
65     "def pickleForDistPyth2009(kws):\n"
66     "  return pickle.dumps(((),kws),-1)\n"
67     "\n"
68     "def unPickleForDistPyth2009(st):\n"
69     "  args=pickle.loads(st)\n"
70     "  return args\n";
71
72 const char PythonNode::REMOTE_NAME[]="remote";
73
74 const char PythonNode::DPL_INFO_NAME[]="my_dpl_localization";
75
76 const char PyFuncNode::SCRIPT_FOR_SERIALIZATION[]="import pickle\n"
77     "def pickleForDistPyth2009(*args,**kws):\n"
78     "  return pickle.dumps((args,kws),-1)\n"
79     "\n"
80     "def unPickleForDistPyth2009(st):\n"
81     "  args=pickle.loads(st)\n"
82     "  return args\n";
83
84 static char SCRIPT_FOR_BIGOBJECT[]="import SALOME_PyNode\n"
85     "BigObjectOnDiskBase = SALOME_PyNode.BigObjectOnDiskBase\n";
86
87 // pickle.load concurrency issue : see https://bugs.python.org/issue12680
88 #if PY_VERSION_HEX < 0x03070000
89 #include <mutex>
90 static std::mutex data_mutex;
91 #endif
92
93 PythonEntry::PythonEntry():_context(0),_pyfuncSer(0),_pyfuncUnser(0),_pyfuncSimpleSer(0)
94 {
95 }
96
97 PythonEntry::~PythonEntry()
98 {
99   AutoGIL agil;
100   DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
101   // not Py_XDECREF of _pyfuncUnser because it is returned by PyDict_GetItem -> borrowed
102   // not Py_XDECREF of _pyfuncSer because it is returned by PyDict_GetItem -> borrowed
103   Py_XDECREF(_context);
104 }
105
106 void PythonEntry::loadRemoteContainer(InlineNode *reqNode)
107 {
108   DEBTRACE( "---------------PythonEntry::CommonRemoteLoad function---------------" );
109   Container *container(reqNode->getContainer());
110   bool isContAlreadyStarted(false);
111   if(container)
112     {
113       try
114       {
115         if(hasImposedResource())
116           container->start(reqNode, _imposedResource, _imposedContainer);
117         else
118         {
119           isContAlreadyStarted=container->isAlreadyStarted(reqNode);
120           if(!isContAlreadyStarted)
121             container->start(reqNode);
122         }
123       }
124       catch(Exception& e)
125       {
126           reqNode->setErrorDetails(e.what());
127           throw e;
128       }
129     }
130   else
131     {
132       std::string what("PythonEntry::CommonRemoteLoad : a load operation requested on \"");
133       what+=reqNode->getName(); what+="\" with no container specified.";
134       reqNode->setErrorDetails(what);
135       throw Exception(what);
136     }
137 }
138
139 Engines::Container_var GetContainerObj(InlineNode *reqNode, bool& isStandardCont)
140 {
141   isStandardCont = false;
142   Container *container(reqNode->getContainer());
143   Engines::Container_var objContainer(Engines::Container::_nil());
144   if(!container)
145     throw YACS::Exception("No container specified !");
146   SalomeContainer *containerCast0(dynamic_cast<SalomeContainer *>(container));
147   SalomeHPContainer *containerCast1(dynamic_cast<SalomeHPContainer *>(container));
148   if(containerCast0)
149     {
150       isStandardCont = true;
151       objContainer=containerCast0->getContainerPtr(reqNode);
152     }
153   else if(containerCast1)
154     {
155       YACS::BASES::AutoCppPtr<SalomeContainerTmpForHP> tmpCont(SalomeContainerTmpForHP::BuildFrom(containerCast1,reqNode));
156       objContainer=tmpCont->getContainerPtr(reqNode);
157     }
158   else
159     throw YACS::Exception("Unrecognized type of container ! Salome one is expected for PythonNode/PyFuncNode !");
160   if(CORBA::is_nil(objContainer))
161     throw YACS::Exception("Container corba pointer is NULL for PythonNode !");
162   return objContainer;
163 }
164
165 Engines::Container_var PythonEntry::loadPythonAdapter(InlineNode *reqNode, bool& isInitializeRequested)
166 {
167   bool isStandardCont(true);
168   Engines::Container_var objContainer(GetContainerObj(reqNode,isStandardCont));
169   isInitializeRequested=false;
170   try
171   {
172     Engines::PyNodeBase_var dftPyScript(retrieveDftRemotePyInterpretorIfAny(objContainer));
173     if(CORBA::is_nil(dftPyScript))
174     {
175       isInitializeRequested=!isStandardCont;
176       createRemoteAdaptedPyInterpretor(objContainer);
177     }
178     else
179       assignRemotePyInterpretor(dftPyScript);
180   }
181   catch( const SALOME::SALOME_Exception& ex )
182   {
183       std::string msg="Exception on remote python node creation ";
184       msg += '\n';
185       msg += ex.details.text.in();
186       reqNode->setErrorDetails(msg);
187       throw Exception(msg);
188   }
189   Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
190   if(CORBA::is_nil(pynode))
191     throw Exception("In PythonNode the ref in NULL ! ");
192   return objContainer;
193 }
194
195 void PythonEntry::loadRemoteContext(InlineNode *reqNode, Engines::Container_ptr objContainer, bool isInitializeRequested)
196 {
197   Container *container(reqNode->getContainer());
198   Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
199   ///
200   {
201 #if PY_VERSION_HEX < 0x03070000
202     std::unique_lock<std::mutex> lock(data_mutex);
203 #endif
204     AutoGIL agil;
205     const char *picklizeScript(getSerializationScript());
206     PyObject *res=PyRun_String(picklizeScript,Py_file_input,_context,_context);
207     PyObject *res2(PyRun_String(SCRIPT_FOR_SIMPLE_SERIALIZATION,Py_file_input,_context,_context));
208     if(res == NULL || res2==NULL)
209       {
210         std::string errorDetails;
211         PyObject* new_stderr = newPyStdOut(errorDetails);
212         reqNode->setErrorDetails(errorDetails);
213         PySys_SetObject((char*)"stderr", new_stderr);
214         PyErr_Print();
215         PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
216         Py_DECREF(new_stderr);
217         throw Exception("Error during load");
218       }
219     Py_DECREF(res); Py_DECREF(res2);
220     AutoPyRef res3(PyRun_String(SCRIPT_FOR_BIGOBJECT,Py_file_input,_context,_context));
221     _pyfuncSer=PyDict_GetItemString(_context,"pickleForDistPyth2009");
222     _pyfuncUnser=PyDict_GetItemString(_context,"unPickleForDistPyth2009");
223     _pyfuncSimpleSer=PyDict_GetItemString(_context,"pickleForVarSimplePyth2009");
224     if(! _pyClsBigObject )
225     {
226       _pyClsBigObject=PyDict_GetItemString(_context,"BigObjectOnDiskBase");
227       Py_INCREF(_pyClsBigObject);
228     }
229     if(_pyfuncSer == NULL)
230       {
231         std::string errorDetails;
232         PyObject *new_stderr(newPyStdOut(errorDetails));
233         reqNode->setErrorDetails(errorDetails);
234         PySys_SetObject((char*)"stderr", new_stderr);
235         PyErr_Print();
236         PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
237         Py_DECREF(new_stderr);
238         throw Exception("Error during load");
239       }
240     if(_pyfuncUnser == NULL)
241       {
242         std::string errorDetails;
243         PyObject *new_stderr(newPyStdOut(errorDetails));
244         reqNode->setErrorDetails(errorDetails);
245         PySys_SetObject((char*)"stderr", new_stderr);
246         PyErr_Print();
247         PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
248         Py_DECREF(new_stderr);
249         throw Exception("Error during load");
250       }
251     if(_pyfuncSimpleSer == NULL)
252       {
253         std::string errorDetails;
254         PyObject *new_stderr(newPyStdOut(errorDetails));
255         reqNode->setErrorDetails(errorDetails);
256         PySys_SetObject((char*)"stderr", new_stderr);
257         PyErr_Print();
258         PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
259         Py_DECREF(new_stderr);
260         throw Exception("Error during load");
261       }
262   }
263   if(isInitializeRequested)
264     {//This one is called only once at initialization in the container if an init-script is specified.
265       try
266       {
267           std::string zeInitScriptKey(container->getProperty(HomogeneousPoolContainer::INITIALIZE_SCRIPT_KEY));
268           if(!zeInitScriptKey.empty())
269             pynode->executeAnotherPieceOfCode(zeInitScriptKey.c_str());
270       }
271       catch( const SALOME::SALOME_Exception& ex )
272       {
273           std::string msg="Exception on PythonNode::loadRemote python invocation of initializisation py script !";
274           msg += '\n';
275           msg += ex.details.text.in();
276           reqNode->setErrorDetails(msg);
277           throw Exception(msg);
278       }
279       DEBTRACE( "---------------End PyNode::loadRemote function---------------" );
280     }
281 }
282
283 std::string PythonEntry::GetContainerLog(const std::string& mode, Container *container, const Task *askingTask)
284 {
285   if(mode=="local")
286     return "";
287
288   std::string msg;
289   try
290   {
291       SalomeContainer *containerCast(dynamic_cast<SalomeContainer *>(container));
292       SalomeHPContainer *objContainer2(dynamic_cast<SalomeHPContainer *>(container));
293       if(containerCast)
294         {
295           Engines::Container_var objContainer(containerCast->getContainerPtr(askingTask));
296           CORBA::String_var logname = objContainer->logfilename();
297           DEBTRACE(logname);
298           msg=logname;
299           std::string::size_type pos = msg.find(":");
300           msg=msg.substr(pos+1);
301         }
302       else if(objContainer2)
303         {
304           msg="Remote PythonNode is on HP Container : no log because no info of the location by definition of HP Container !";
305         }
306       else
307         {
308           msg="Not implemented yet for container log for that type of container !";
309         }
310   }
311   catch(...)
312   {
313       msg = "Container no longer reachable";
314   }
315   return msg;
316 }
317
318 void PythonEntry::commonRemoteLoad(InlineNode *reqNode)
319 {
320   loadRemoteContainer(reqNode);
321   bool isInitializeRequested;
322   Engines::Container_var objContainer(loadPythonAdapter(reqNode,isInitializeRequested));
323   loadRemoteContext(reqNode,objContainer,isInitializeRequested);
324 }
325
326 bool PythonEntry::hasImposedResource()const
327 {
328   return !_imposedResource.empty() && !_imposedContainer.empty();
329 }
330
331 bool PythonEntry::IsProxy( PyObject *ob )
332 {
333   if(!_pyClsBigObject)
334     return false;
335   if( PyObject_IsInstance( ob, _pyClsBigObject) == 1 )
336   {
337     return true;
338   }
339   else
340   {
341     if( PyList_Check( ob ) )
342     {
343       auto sz = PyList_Size( ob );
344       for( auto i = 0 ; i < sz ; ++i )
345       {
346         PyObject *elt = PyList_GetItem( ob, i );
347         if( PythonEntry::IsProxy(elt) )
348           return true;
349       }
350     }
351   }
352   return false;
353 }
354
355 bool PythonEntry::GetDestroyStatus( PyObject *ob )
356 {
357   if(!_pyClsBigObject)
358     return false;
359   if( PyObject_IsInstance( ob, _pyClsBigObject) == 1 )
360   {
361     AutoPyRef unlinkOnDestructor = PyObject_GetAttrString(ob,"getDestroyStatus");
362     AutoPyRef tmp = PyObject_CallFunctionObjArgs(unlinkOnDestructor,nullptr);
363     if( PyBool_Check(tmp.get()) )
364     {
365       return tmp.get() == Py_True;
366     }
367     return false;
368   }
369   else
370   {
371     if( PyList_Check( ob ) )
372     {
373       auto sz = PyList_Size( ob );
374       for( auto i = 0 ; i < sz ; ++i )
375       {
376         PyObject *elt = PyList_GetItem( ob, i );
377         if( PythonEntry::GetDestroyStatus(elt) )
378           return true;
379       }
380     }
381   }
382   return false;
383 }
384
385 void PythonEntry::IfProxyDoSomething( PyObject *ob, const char *meth )
386 {
387   if(!_pyClsBigObject)
388     return ;
389   if( PyObject_IsInstance( ob, _pyClsBigObject) == 1 )
390   {
391     AutoPyRef unlinkOnDestructor = PyObject_GetAttrString(ob,meth);
392     AutoPyRef tmp = PyObject_CallFunctionObjArgs(unlinkOnDestructor,nullptr);
393   }
394   else
395   {
396     if( PyList_Check( ob ) )
397     {
398       auto sz = PyList_Size( ob );
399       for( auto i = 0 ; i < sz ; ++i )
400       {
401         PyObject *elt = PyList_GetItem( ob, i );
402         PythonEntry::IfProxyDoSomething( elt, meth );
403       }
404     }
405   }
406 }
407
408 void PythonEntry::DoNotTouchFileIfProxy( PyObject *ob )
409 {
410   IfProxyDoSomething(ob,"doNotTouchFile");
411 }
412
413 void PythonEntry::UnlinkOnDestructorIfProxy( PyObject *ob )
414 {
415   IfProxyDoSomething(ob,"unlinkOnDestructor");
416 }
417
418 PythonNode::PythonNode(const PythonNode& other, ComposedNode *father):InlineNode(other,father),_autoSqueeze(other._autoSqueeze)
419 {
420   _pynode = Engines::PyScriptNode::_nil();
421   _implementation=IMPL_NAME;
422   {
423     AutoGIL agil;
424     _context=PyDict_New();
425     if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
426       {
427         stringstream msg;
428         msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
429         _errorDetails=msg.str();
430         throw Exception(msg.str());
431       }
432   }
433 }
434
435 PythonNode::PythonNode(const std::string& name):InlineNode(name)
436 {
437   _pynode = Engines::PyScriptNode::_nil();
438   _implementation=IMPL_NAME;
439   {
440     AutoGIL agil;
441     _context=PyDict_New();
442     if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
443       {
444         stringstream msg;
445         msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
446         _errorDetails=msg.str();
447         throw Exception(msg.str());
448       }
449   }
450 }
451
452 PythonNode::~PythonNode()
453 {
454   freeKernelPynode();
455 }
456
457 void PythonNode::checkBasicConsistency() const
458 {
459   DEBTRACE("checkBasicConsistency");
460   InlineNode::checkBasicConsistency();
461   {
462     AutoGIL agil;
463     PyObject* res;
464     res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
465     if(res == NULL)
466       {
467         std::string error="";
468         PyObject* new_stderr = newPyStdOut(error);
469         PySys_SetObject((char*)"stderr", new_stderr);
470         PyErr_Print();
471         PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
472         Py_DECREF(new_stderr);
473         throw Exception(error);
474       }
475     else
476       Py_XDECREF(res);
477   }
478 }
479
480 void PythonNode::load()
481 {
482   DEBTRACE( "---------------PyNode::load function---------------" );
483   if(_mode==PythonNode::REMOTE_NAME)
484     loadRemote();
485   else
486     loadLocal();
487 }
488
489 void PythonNode::loadLocal()
490 {
491   DEBTRACE( "---------------PyNode::loadLocal function---------------" );
492   // do nothing
493 }
494
495 void PythonNode::loadRemote()
496 {
497   commonRemoteLoad(this);
498 }
499
500 void PythonNode::execute()
501 {
502   if(_mode==PythonNode::REMOTE_NAME)
503     executeRemote();
504   else
505     executeLocal();
506 }
507
508 void PythonNode::executeRemote()
509 {
510   DEBTRACE( "++++++++++++++ PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
511   if(!_pyfuncSer)
512     throw Exception("PythonNode badly loaded");
513   //
514   if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
515     {
516       bool dummy;
517       loadPythonAdapter(this,dummy);
518       _pynode->assignNewCompiledCode(getScript().c_str());
519     }
520   // not managed by unique_ptr here because destructed by the order of client.
521   SenderByteImpl *serializationInputCorba = nullptr;
522   AutoPyRef serializationInput;
523   {
524 #if PY_VERSION_HEX < 0x03070000
525       std::unique_lock<std::mutex> lock(data_mutex);
526 #endif
527       AutoGIL agil;
528       PyObject *args(0),*ob(0);
529       //===========================================================================
530       // Get inputs in input ports, build a Python dict and pickle it
531       //===========================================================================
532       args = PyDict_New();
533       std::list<InputPort *>::iterator iter2;
534       int pos(0);
535       for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); ++iter2)
536         {
537           InputPyPort *p=(InputPyPort *)*iter2;
538           ob=p->getPyObj();
539           PyDict_SetItemString(args,p->getName().c_str(),ob);
540           pos++;
541         }
542 #ifdef _DEVDEBUG_
543       PyObject_Print(args,stderr,Py_PRINT_RAW);
544       std::cerr << endl;
545 #endif
546       serializationInput.set(PyObject_CallFunctionObjArgs(_pyfuncSer,args,nullptr));
547       Py_DECREF(args);
548       //The pickled string may contain NULL characters so use PyString_AsStringAndSize
549       char *serializationInputC(nullptr);
550       Py_ssize_t len;
551       if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
552         throw Exception("DistributedPythonNode problem in python pickle");
553       // no copy here. The C byte array of Python is taken  as this into CORBA sequence to avoid copy
554       serializationInputCorba = new SenderByteImpl(serializationInputC,len);
555   }
556
557   //get the list of output argument names
558   std::list<OutputPort *>::iterator iter;
559   Engines::listofstring myseq;
560   myseq.length(getNumberOfOutputPorts());
561   int pos=0;
562   for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
563     {
564       OutputPyPort *p=(OutputPyPort *)*iter;
565       myseq[pos]=p->getName().c_str();
566       DEBTRACE( "port name: " << p->getName() );
567       DEBTRACE( "port kind: " << p->edGetType()->kind() );
568       DEBTRACE( "port pos : " << pos );
569       pos++;
570     }
571   //===========================================================================
572   // Execute in remote Python node
573   //===========================================================================
574   DEBTRACE( "-----------------starting remote python invocation-----------------" );
575   std::unique_ptr<SALOME::SenderByteSeq> resultCorba;
576   try
577     {
578       //pass outargsname and dict serialized
579       SALOME::SenderByte_var serializationInputRef = serializationInputCorba->_this();
580       _pynode->executeFirst(serializationInputRef);
581       //serializationInput and serializationInputCorba are no more needed for server. Release it.
582       serializationInput.set(nullptr);
583       resultCorba.reset( _pynode->executeSecond(myseq) );
584     }
585   catch( const SALOME::SALOME_Exception& ex )
586     {
587       std::ostringstream msg; msg << "Exception on remote python invocation" << std::endl << ex.details.text.in() << std::endl;
588       msg << "PyScriptNode CORBA ref : ";
589       {
590         CORBA::ORB_ptr orb(getSALOMERuntime()->getOrb());
591         if(!CORBA::is_nil(orb))
592         {
593           CORBA::String_var IOR(orb->object_to_string(_pynode));
594           msg << IOR;
595         }
596       }
597       msg << std::endl;
598       _errorDetails=msg.str();
599       throw Exception(msg.str());
600     }
601   catch(CORBA::COMM_FAILURE& ex)
602     {
603       std::ostringstream msg;
604       msg << "Exception on remote python invocation." << std::endl ;
605       msg << "Caught system exception COMM_FAILURE -- unable to contact the "
606           << "object." << std::endl;
607       _errorDetails=msg.str();
608       throw Exception(msg.str());
609     }
610   catch(CORBA::SystemException& ex)
611     {
612       std::ostringstream msg;
613       msg << "Exception on remote python invocation." << std::endl ;
614       msg << "Caught a CORBA::SystemException." ;
615       CORBA::Any tmp;
616       tmp <<= ex;
617       CORBA::TypeCode_var tc = tmp.type();
618       const char *p = tc->name();
619       if ( *p != '\0' )
620         msg <<p;
621       else
622         msg  << tc->id();
623       msg << std::endl;
624       _errorDetails=msg.str();
625       throw Exception(msg.str());
626     }
627   catch(CORBA::Exception& ex)
628     {
629       std::ostringstream msg;
630       msg << "Exception on remote python invocation." << std::endl ;
631       msg << "Caught CORBA::Exception. " ;
632       CORBA::Any tmp;
633       tmp <<= ex;
634       CORBA::TypeCode_var tc = tmp.type();
635       const char *p = tc->name();
636       if ( *p != '\0' )
637         msg <<p;
638       else
639         msg  << tc->id();
640       msg << std::endl;
641       _errorDetails=msg.str();
642       throw Exception(msg.str());
643     }
644   catch(omniORB::fatalException& fe)
645     {
646       std::ostringstream msg;
647       msg << "Exception on remote python invocation." << std::endl ;
648       msg << "Caught omniORB::fatalException:" << std::endl;
649       msg << "  file: " << fe.file() << std::endl;
650       msg << "  line: " << fe.line() << std::endl;
651       msg << "  mesg: " << fe.errmsg() << std::endl;
652       _errorDetails=msg.str();
653       throw Exception(msg.str());
654     }
655   DEBTRACE( "-----------------end of remote python invocation-----------------" );
656   //===========================================================================
657   // Get results, unpickle and put them in output ports
658   //===========================================================================
659   {
660 #if PY_VERSION_HEX < 0x03070000
661       std::unique_lock<std::mutex> lock(data_mutex);
662 #endif
663       AutoGIL agil;
664       DEBTRACE( "-----------------PythonNode::outputs-----------------" );
665       int nres( resultCorba->length() );
666
667       if(getNumberOfOutputPorts() != nres)
668         {
669           std::string msg="Number of output arguments : Mismatch between definition and execution";
670           _errorDetails=msg;
671           throw Exception(msg);
672         }
673       pos=0;
674       try
675       {
676           for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
677             {
678               OutputPyPort *p=(OutputPyPort *)*iter;
679               DEBTRACE( "port name: " << p->getName() );
680               DEBTRACE( "port kind: " << p->edGetType()->kind() );
681               DEBTRACE( "port pos : " << pos );
682               SALOME::SenderByte_var elt = (*resultCorba)[pos];
683               SeqByteReceiver recv(elt);
684               unsigned long length = 0;
685               char *resultCorbaC = recv.data(length);
686               {
687                 AutoPyRef resultPython=PyMemoryView_FromMemory(resultCorbaC,length,PyBUF_READ);
688                 AutoPyRef args = PyTuple_New(1);
689                 PyTuple_SetItem(args,0,resultPython.retn());
690                 AutoPyRef ob = PyObject_CallObject(_pyfuncUnser,args);
691                 if (!ob)
692                 {
693                   std::stringstream msg;
694                   msg << "Conversion with pickle of output ports failed !";
695                   msg << " : " << __FILE__ << ":" << __LINE__;
696                   _errorDetails=msg.str();
697                   throw YACS::ENGINE::ConversionException(msg.str());
698                 }
699                 UnlinkOnDestructorIfProxy(ob);
700                 p->put( ob );
701               }
702               pos++;
703             }
704       }
705       catch(ConversionException& ex)
706       {
707           _errorDetails=ex.what();
708           throw;
709       }
710       if(_autoSqueeze)
711         squeezeMemoryRemote();
712   }
713   //
714   if(!isUsingPythonCache())
715   {
716     freeKernelPynode();
717     bool dummy;
718     Engines::Container_var cont(GetContainerObj(this,dummy));
719     cont->removePyScriptNode(getName().c_str());
720   }
721   DEBTRACE( "++++++++++++++ ENDOF PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
722 }
723
724 void PythonNode::ExecuteLocalInternal(const std::string& codeStr, PyObject *context, std::string& errorDetails)
725 {
726   DEBTRACE(  code );
727   DEBTRACE( "context refcnt: " << context->ob_refcnt );
728   std::ostringstream stream;
729   stream << "/tmp/PythonNode_";
730   stream << getpid();
731   AutoPyRef code=Py_CompileString(codeStr.c_str(), stream.str().c_str(), Py_file_input);
732   if(code == NULL)
733   {
734     errorDetails=""; 
735     AutoPyRef new_stderr = newPyStdOut(errorDetails);
736     PySys_SetObject((char*)"stderr", new_stderr);
737     PyErr_Print();
738     PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
739     throw YACS::Exception("Error during execution");
740   }
741   {
742     AutoPyRef res = PyEval_EvalCode(  code, context, context);
743   }
744   DEBTRACE( "context refcnt: " << context->ob_refcnt );
745   fflush(stdout);
746   fflush(stderr);
747   if(PyErr_Occurred ())
748   {
749     errorDetails="";
750     AutoPyRef new_stderr = newPyStdOut(errorDetails);
751     PySys_SetObject((char*)"stderr", new_stderr);
752     ofstream errorfile(stream.str().c_str());
753     if (errorfile.is_open())
754       {
755         errorfile << codeStr;
756         errorfile.close();
757       }
758     PyErr_Print();
759     PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
760     throw YACS::Exception("Error during execution");
761   }
762 }
763
764 void PythonNode::executeLocalInternal(const std::string& codeStr)
765 {
766   ExecuteLocalInternal(codeStr,_context,_errorDetails);
767 }
768
769 void PythonNode::executeLocal()
770 {
771   DEBTRACE( "++++++++++++++ PyNode::executeLocal: " << getName() << " ++++++++++++++++++++" );
772   {
773     AutoGIL agil;
774     std::ostringstream unpxy; unpxy << "from SALOME_PyNode import UnProxyObjectSimpleLocal" << std::endl;
775     DEBTRACE( "---------------PyNode::inputs---------------" );
776     list<InputPort *>::iterator iter2;
777     for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
778       {
779         InputPyPort *p=(InputPyPort *)*iter2;
780         DEBTRACE( "port name: " << p->getName() );
781         DEBTRACE( "port kind: " << p->edGetType()->kind() );
782         PyObject* ob=p->getPyObj();
783         DEBTRACE( "ob refcnt: " << ob->ob_refcnt ); 
784         unpxy << p->getName() << " = UnProxyObjectSimpleLocal( " << p->getName() << " )" << std::endl;
785 #ifdef _DEVDEBUG_
786         PyObject_Print(ob,stderr,Py_PRINT_RAW);
787         cerr << endl;
788 #endif
789         int ier=PyDict_SetItemString(_context,p->getName().c_str(),ob);
790         DEBTRACE( "after PyDict_SetItemString:ob refcnt: " << ob->ob_refcnt );
791       }
792
793     DEBTRACE( "---------------End PyNode::inputs---------------" );
794
795     //calculation
796     DEBTRACE( "----------------PyNode::calculation---------------" );
797   
798     if( ! getSqueezeStatus() )
799       executeLocalInternal( unpxy.str() );
800
801     executeLocalInternal( _script );
802
803     DEBTRACE( "-----------------PyNode::outputs-----------------" );
804     list<OutputPort *>::iterator iter;
805     try
806     {
807         for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
808           {
809             OutputPyPort *p=(OutputPyPort *)*iter;
810             DEBTRACE( "port name: " << p->getName() );
811             DEBTRACE( "port kind: " << p->edGetType()->kind() );
812             PyObject *ob=PyDict_GetItemString(_context,p->getName().c_str());
813             if(ob==NULL)
814               {
815                 std::string msg="Error during execution: there is no variable ";
816                 msg=msg+p->getName()+" in node context";
817                 _errorDetails=msg;
818                 throw Exception(msg);
819               }
820             DEBTRACE( "PyNode::outputs::ob refcnt: " << ob->ob_refcnt );
821 #ifdef _DEVDEBUG_
822             PyObject_Print(ob,stderr,Py_PRINT_RAW);
823             cerr << endl;
824 #endif
825             p->put(ob);
826             if(!isUsingPythonCache())
827               PyDict_DelItemString(_context,p->getName().c_str());
828           }
829     }
830     catch(ConversionException& ex)
831     {
832         _errorDetails=ex.what();
833         throw;
834     }
835     if(_autoSqueeze)
836       squeezeMemory();
837     DEBTRACE( "-----------------End PyNode::outputs-----------------" );
838     if(!isUsingPythonCache())
839     {
840       for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
841       {
842         AutoPyRef pStr = PyUnicode_FromString( (*iter2)->getName().c_str() );
843         if( PyDict_Contains(_context,pStr) == 1 )
844           { PyDict_DelItem(_context,pStr); }
845       }
846     }
847   }
848   DEBTRACE( "++++++++++++++ End PyNode::execute: " << getName() << " ++++++++++++++++++++" );
849 }
850
851 void PythonNode::squeezeMemorySafe()
852 {
853   AutoGIL agil;
854   if(_mode==PythonNode::REMOTE_NAME)
855     this->squeezeMemoryRemote();
856   else
857     this->squeezeMemory();
858 }
859   
860 void PythonNode::squeezeMemory()
861 {
862   for(auto p : _setOfInputPort)
863     {
864       PyDict_DelItemString(_context,p->getName().c_str());
865       InputPyPort *p2(static_cast<InputPyPort *>(p));
866       if(p2->canSafelySqueezeMemory())
867         p2->put(Py_None);
868     }
869   for(auto p : _setOfOutputPort)
870     {
871       PyDict_DelItemString(_context,p->getName().c_str());
872       OutputPyPort *p2(static_cast<OutputPyPort *>(p));
873       p2->putWithoutForward(Py_None);
874     }
875 }
876
877 void PythonNode::squeezeMemoryRemote()
878 {
879   for(auto p : _setOfInputPort)
880     {
881       InputPyPort *p2(static_cast<InputPyPort *>(p));
882       if(p2->canSafelySqueezeMemory())
883         p2->put(Py_None);
884     }
885   for(auto p : _setOfOutputPort)
886     {
887       OutputPyPort *p2(static_cast<OutputPyPort *>(p));
888       p2->putWithoutForward(Py_None);
889     }
890 }
891
892 std::string PythonNode::getContainerLog()
893 {
894   return PythonEntry::GetContainerLog(_mode,_container,this);
895 }
896
897 void PythonNode::shutdown(int level)
898 {
899   DEBTRACE("PythonNode::shutdown " << level);
900   if(_mode=="local")return;
901   if(_container)
902     {
903       freeKernelPynode();
904       _container->shutdown(level);
905     }
906 }
907
908 void PythonNode::imposeResource(const std::string& resource_name,
909                                 const std::string& container_name)
910 {
911   if(!resource_name.empty() && !container_name.empty())
912   {
913     _imposedResource = resource_name;
914     _imposedContainer = container_name;
915   }
916 }
917
918 bool PythonNode::canAcceptImposedResource()
919 {
920   return _container != nullptr && _container->canAcceptImposedResource();
921 }
922
923 bool PythonNode::hasImposedResource()const
924 {
925   return PythonEntry::hasImposedResource();
926 }
927
928 std::string PythonNode::pythonEntryName()const
929 {
930   if(isUsingPythonCache())
931     return "DEFAULT_NAME_FOR_UNIQUE_PYTHON_NODE_ENTRY";
932   else
933     return getName();
934 }
935
936 bool PythonNode::isUsingPythonCache()const
937 {
938   bool found = false;
939   if(_container)
940     found = _container->isUsingPythonCache();
941   return found;
942 }
943
944 void PythonNode::freeKernelPynode()
945 {
946   if(!CORBA::is_nil(_pynode))
947   {
948     try
949     {
950       _pynode->UnRegister();
951     }
952     catch(...)
953     {
954       DEBTRACE("Trouble when pynode->UnRegister!")
955     }
956     _pynode = Engines::PyScriptNode::_nil();
957   }
958 }
959
960 Node *PythonNode::simpleClone(ComposedNode *father, bool editionOnly) const
961 {
962   return new PythonNode(*this,father);
963 }
964
965 void PythonNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
966 {
967   freeKernelPynode();
968   _pynode=objContainer->createPyScriptNode(pythonEntryName().c_str(),getScript().c_str());
969   _pynode->Register();
970 }
971
972 Engines::PyNodeBase_var PythonNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
973 {
974   Engines::PyScriptNode_var ret(objContainer->getDefaultPyScriptNode(pythonEntryName().c_str()));
975   if(!CORBA::is_nil(ret))
976     {
977       ret->Register();
978     }
979   return Engines::PyNodeBase::_narrow(ret);
980 }
981
982 void PythonNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
983 {
984   if(CORBA::is_nil(_pynode))
985     _pynode=Engines::PyScriptNode::_narrow(remoteInterp);
986   else
987   {
988     Engines::PyScriptNode_var tmpp(Engines::PyScriptNode::_narrow(remoteInterp));
989     if(!_pynode->_is_equivalent(tmpp))
990     {
991       freeKernelPynode();
992       _pynode=Engines::PyScriptNode::_narrow(remoteInterp);
993     }
994   }
995   _pynode->assignNewCompiledCode(getScript().c_str());
996 }
997
998 Engines::PyNodeBase_var PythonNode::getRemoteInterpreterHandle()
999 {
1000   return Engines::PyNodeBase::_narrow(_pynode);
1001 }
1002
1003 //! Create a new node of same type with a given name
1004 PythonNode* PythonNode::cloneNode(const std::string& name)
1005 {
1006   PythonNode* n=new PythonNode(name);
1007   n->setScript(_script);
1008   list<InputPort *>::iterator iter;
1009   for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
1010     {
1011       InputPyPort *p=(InputPyPort *)*iter;
1012       DEBTRACE( "port name: " << p->getName() );
1013       DEBTRACE( "port kind: " << p->edGetType()->kind() );
1014       n->edAddInputPort(p->getName(),p->edGetType());
1015     }
1016   list<OutputPort *>::iterator iter2;
1017   for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
1018     {
1019       OutputPyPort *p=(OutputPyPort *)*iter2;
1020       DEBTRACE( "port name: " << p->getName() );
1021       DEBTRACE( "port kind: " << p->edGetType()->kind() );
1022       n->edAddOutputPort(p->getName(),p->edGetType());
1023     }
1024   return n;
1025 }
1026
1027 void PythonNode::applyDPLScope(ComposedNode *gfn)
1028 {
1029   std::vector< std::pair<std::string,int> > ret(getDPLScopeInfo(gfn));
1030   if(ret.empty())
1031     return ;
1032   //
1033   PyObject *ob(0);
1034   {
1035     AutoGIL agil;
1036     std::size_t sz(ret.size());
1037     ob=PyList_New(sz);
1038     for(std::size_t i=0;i<sz;i++)
1039       {
1040         const std::pair<std::string,int>& p(ret[i]);
1041         PyObject *elt(PyTuple_New(2));
1042         PyTuple_SetItem(elt,0,PyUnicode_FromString(p.first.c_str()));
1043         PyTuple_SetItem(elt,1,PyLong_FromLong(p.second));
1044         PyList_SetItem(ob,i,elt);
1045       }
1046   }
1047   if(_mode==REMOTE_NAME)
1048     {
1049       Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);
1050       {
1051         AutoGIL agil;
1052         PyObject *serializationInput(PyObject_CallFunctionObjArgs(_pyfuncSimpleSer,ob,NULL));
1053         Py_XDECREF(ob);
1054         char *serializationInputC(0);
1055         Py_ssize_t len;
1056         if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
1057           throw Exception("DistributedPythonNode problem in python pickle");
1058         serializationInputCorba->length(len);
1059         for(int i=0; i < len ; i++)
1060           serializationInputCorba[i]=serializationInputC[i];
1061         Py_XDECREF(serializationInput);
1062       }
1063       _pynode->defineNewCustomVar(DPL_INFO_NAME,serializationInputCorba);
1064     }
1065   else
1066     {
1067       AutoGIL agil;
1068       PyDict_SetItemString(_context,DPL_INFO_NAME,ob);
1069       Py_XDECREF(ob);
1070     }
1071 }
1072
1073 PyFuncNode::PyFuncNode(const PyFuncNode& other, ComposedNode *father):InlineFuncNode(other,father),_pyfunc(0)
1074 {
1075   _implementation = PythonNode::IMPL_NAME;
1076   {
1077     AutoGIL agil;
1078     _context=PyDict_New();
1079     DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
1080     if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
1081       {
1082         stringstream msg;
1083         msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
1084         _errorDetails=msg.str();
1085         throw Exception(msg.str());
1086       }
1087   }
1088 }
1089
1090 PyFuncNode::PyFuncNode(const std::string& name): InlineFuncNode(name),_pyfunc(0)
1091 {
1092
1093   _implementation = PythonNode::IMPL_NAME;
1094   DEBTRACE( "PyFuncNode::PyFuncNode " << name );
1095   {
1096     AutoGIL agil;
1097     _context=PyDict_New();
1098     DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
1099     if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
1100       {
1101         stringstream msg;
1102         msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
1103         _errorDetails=msg.str();
1104         throw Exception(msg.str());
1105       }
1106   }
1107 }
1108
1109 PyFuncNode::~PyFuncNode()
1110 {
1111   if(!CORBA::is_nil(_pynode))
1112     {
1113       _pynode->UnRegister();
1114     }
1115 }
1116
1117 void PyFuncNode::init(bool start)
1118 {
1119   initCommonPartWithoutStateManagement(start);
1120   if(_state == YACS::DISABLED)
1121     {
1122       exDisabledState(); // to refresh propagation of DISABLED state
1123       return ;
1124     }
1125   if(start) //complete initialization
1126     setState(YACS::READY);
1127   else if(_state > YACS::LOADED)// WARNING FuncNode has internal vars (CEA usecase) ! Partial initialization (inside a loop). Exclusivity of funcNode.
1128     setState(YACS::TORECONNECT);
1129 }
1130
1131 void PyFuncNode::checkBasicConsistency() const
1132 {
1133   DEBTRACE("checkBasicConsistency");
1134   InlineFuncNode::checkBasicConsistency();
1135   {
1136     AutoGIL agil;
1137     PyObject* res;
1138     res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
1139     if(res == NULL)
1140       {
1141         std::string error="";
1142         PyObject* new_stderr = newPyStdOut(error);
1143         PySys_SetObject((char*)"stderr", new_stderr);
1144         PyErr_Print();
1145         PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1146         Py_DECREF(new_stderr);
1147         throw Exception(error);
1148       }
1149     else
1150       Py_XDECREF(res);
1151   }
1152 }
1153
1154 void PyFuncNode::load()
1155 {
1156   DEBTRACE( "---------------PyfuncNode::load function---------------" );
1157   if(_mode==PythonNode::REMOTE_NAME)
1158     loadRemote();
1159   else
1160     loadLocal();
1161 }
1162
1163 void PyFuncNode::loadRemote()
1164 {
1165   commonRemoteLoad(this);
1166 }
1167
1168 void PyFuncNode::loadLocal()
1169 {
1170   DEBTRACE( "---------------PyFuncNode::load function " << getName() << " ---------------" );
1171   DEBTRACE(  _script );
1172
1173 #ifdef _DEVDEBUG_
1174   list<OutputPort *>::iterator iter;
1175   for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
1176     {
1177       OutputPyPort *p=(OutputPyPort *)*iter;
1178       DEBTRACE( "port name: " << p->getName() );
1179       DEBTRACE( "port kind: " << p->edGetType()->kind() );
1180     }
1181 #endif
1182
1183   {
1184     AutoGIL agil;
1185     DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
1186
1187     std::ostringstream stream;
1188     stream << "/tmp/PythonNode_";
1189     stream << getpid();
1190
1191     PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
1192     if(code == NULL)
1193       {
1194         _errorDetails="";
1195         PyObject* new_stderr = newPyStdOut(_errorDetails);
1196         PySys_SetObject((char*)"stderr", new_stderr);
1197         PyErr_Print();
1198         PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1199         Py_DECREF(new_stderr);
1200         throw Exception("Error during execution");
1201       }
1202     PyObject *res = PyEval_EvalCode( code, _context, _context);
1203     Py_DECREF(code);
1204     Py_XDECREF(res);
1205
1206     DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
1207     if(PyErr_Occurred ())
1208       {
1209         _errorDetails="";
1210         PyObject* new_stderr = newPyStdOut(_errorDetails);
1211         PySys_SetObject((char*)"stderr", new_stderr);
1212         ofstream errorfile(stream.str().c_str());
1213         if (errorfile.is_open())
1214           {
1215             errorfile << _script;
1216             errorfile.close();
1217           }
1218         PyErr_Print();
1219         PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1220         Py_DECREF(new_stderr);
1221         throw Exception("Error during execution");
1222         return;
1223       }
1224     _pyfunc=PyDict_GetItemString(_context,_fname.c_str());
1225     DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1226     if(_pyfunc == NULL)
1227       {
1228         _errorDetails="";
1229         PyObject* new_stderr = newPyStdOut(_errorDetails);
1230         PySys_SetObject((char*)"stderr", new_stderr);
1231         PyErr_Print();
1232         PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1233         Py_DECREF(new_stderr);
1234         throw Exception("Error during execution");
1235       }
1236     DEBTRACE( "---------------End PyFuncNode::load function---------------" );
1237   }
1238 }
1239
1240 void PyFuncNode::execute()
1241 {
1242   if(_mode==PythonNode::REMOTE_NAME)
1243     executeRemote();
1244   else
1245     executeLocal();
1246 }
1247
1248 void PyFuncNode::executeRemote()
1249 {
1250   DEBTRACE( "++++++++++++++ PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
1251   if(!_pyfuncSer)
1252     throw Exception("DistributedPythonNode badly loaded");
1253   //
1254   if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
1255     {
1256       bool dummy;
1257       loadPythonAdapter(this,dummy);
1258       _pynode->executeAnotherPieceOfCode(getScript().c_str());
1259     }
1260   //
1261   Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);;
1262   {
1263 #if PY_VERSION_HEX < 0x03070000
1264       std::unique_lock<std::mutex> lock(data_mutex);
1265 #endif
1266       AutoGIL agil;
1267       PyObject *ob(0);
1268       //===========================================================================
1269       // Get inputs in input ports, build a Python tuple and pickle it
1270       //===========================================================================
1271       PyObject *args(PyTuple_New(getNumberOfInputPorts()));
1272       int pos(0);
1273       for(std::list<InputPort *>::iterator iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++,pos++)
1274         {
1275           InputPyPort *p=(InputPyPort *)*iter2;
1276           ob=p->getPyObj();
1277           Py_INCREF(ob);
1278           PyTuple_SetItem(args,pos,ob);
1279         }
1280 #ifdef _DEVDEBUG_
1281       PyObject_Print(args,stderr,Py_PRINT_RAW);
1282       std::cerr << endl;
1283 #endif
1284       PyObject *serializationInput=PyObject_CallObject(_pyfuncSer,args);
1285       Py_DECREF(args);
1286       //The pickled string may contain NULL characters so use PyString_AsStringAndSize
1287       char *serializationInputC(0);
1288       Py_ssize_t len;
1289       if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
1290         throw Exception("DistributedPythonNode problem in python pickle");
1291
1292       serializationInputCorba->length(len);
1293       for(int i=0; i < len ; i++)
1294         serializationInputCorba[i]=serializationInputC[i];
1295       Py_DECREF(serializationInput);
1296   }
1297
1298   //===========================================================================
1299   // Execute in remote Python node
1300   //===========================================================================
1301   DEBTRACE( "-----------------starting remote python invocation-----------------" );
1302   Engines::pickledArgs_var resultCorba;
1303   try
1304     {
1305       resultCorba=_pynode->execute(getFname().c_str(),serializationInputCorba);
1306     }
1307   catch( const SALOME::SALOME_Exception& ex )
1308     {
1309       std::string msg="Exception on remote python invocation";
1310       msg += '\n';
1311       msg += ex.details.text.in();
1312       _errorDetails=msg;
1313       throw Exception(msg);
1314     }
1315   catch(CORBA::COMM_FAILURE& ex)
1316     {
1317       std::ostringstream msg;
1318       msg << "Exception on remote python invocation." << std::endl ;
1319       msg << "Caught system exception COMM_FAILURE -- unable to contact the "
1320           << "object." << std::endl;
1321       _errorDetails=msg.str();
1322       throw Exception(msg.str());
1323     }
1324   catch(CORBA::SystemException& ex)
1325     {
1326       std::ostringstream msg;
1327       msg << "Exception on remote python invocation." << std::endl ;
1328       msg << "Caught a CORBA::SystemException." ;
1329       CORBA::Any tmp;
1330       tmp <<= ex;
1331       CORBA::TypeCode_var tc = tmp.type();
1332       const char *p = tc->name();
1333       if ( *p != '\0' )
1334         msg <<p;
1335       else
1336         msg  << tc->id();
1337       msg << std::endl;
1338       _errorDetails=msg.str();
1339       throw Exception(msg.str());
1340     }
1341   catch(CORBA::Exception& ex)
1342     {
1343       std::ostringstream msg;
1344       msg << "Exception on remote python invocation." << std::endl ;
1345       msg << "Caught CORBA::Exception. " ;
1346       CORBA::Any tmp;
1347       tmp <<= ex;
1348       CORBA::TypeCode_var tc = tmp.type();
1349       const char *p = tc->name();
1350       if ( *p != '\0' )
1351         msg <<p;
1352       else
1353         msg  << tc->id();
1354       msg << std::endl;
1355       _errorDetails=msg.str();
1356       throw Exception(msg.str());
1357     }
1358   catch(omniORB::fatalException& fe)
1359     {
1360       std::ostringstream msg;
1361       msg << "Exception on remote python invocation." << std::endl ;
1362       msg << "Caught omniORB::fatalException:" << std::endl;
1363       msg << "  file: " << fe.file() << std::endl;
1364       msg << "  line: " << fe.line() << std::endl;
1365       msg << "  mesg: " << fe.errmsg() << std::endl;
1366       _errorDetails=msg.str();
1367       throw Exception(msg.str());
1368     }
1369   DEBTRACE( "-----------------end of remote python invocation-----------------" );
1370   //===========================================================================
1371   // Get results, unpickle and put them in output ports
1372   //===========================================================================
1373   char *resultCorbaC=new char[resultCorba->length()+1];
1374   resultCorbaC[resultCorba->length()]='\0';
1375   for(int i=0;i<resultCorba->length();i++)
1376     resultCorbaC[i]=resultCorba[i];
1377
1378   {
1379 #if PY_VERSION_HEX < 0x03070000
1380       std::unique_lock<std::mutex> lock(data_mutex);
1381 #endif
1382       AutoGIL agil;
1383
1384       PyObject *resultPython(PyBytes_FromStringAndSize(resultCorbaC,resultCorba->length()));
1385       delete [] resultCorbaC;
1386       PyObject *args(PyTuple_New(1)),*ob(0);
1387       PyTuple_SetItem(args,0,resultPython);
1388       PyObject *finalResult=PyObject_CallObject(_pyfuncUnser,args);
1389       Py_DECREF(args);
1390
1391       DEBTRACE( "-----------------PythonNode::outputs-----------------" );
1392       int nres=1;
1393       if(finalResult == Py_None)
1394         nres=0;
1395       else if(PyTuple_Check(finalResult))
1396         nres=PyTuple_Size(finalResult);
1397
1398       if(getNumberOfOutputPorts() != nres)
1399         {
1400           std::string msg="Number of output arguments : Mismatch between definition and execution";
1401           Py_DECREF(finalResult);
1402           _errorDetails=msg;
1403           throw Exception(msg);
1404         }
1405
1406       try
1407       {
1408           int pos(0);
1409           for(std::list<OutputPort *>::iterator iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++, pos++)
1410             {
1411               OutputPyPort *p=(OutputPyPort *)*iter;
1412               DEBTRACE( "port name: " << p->getName() );
1413               DEBTRACE( "port kind: " << p->edGetType()->kind() );
1414               DEBTRACE( "port pos : " << pos );
1415               if(PyTuple_Check(finalResult))
1416                 ob=PyTuple_GetItem(finalResult,pos) ;
1417               else
1418                 ob=finalResult;
1419               DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1420               p->put(ob);
1421             }
1422           Py_DECREF(finalResult);
1423       }
1424       catch(ConversionException& ex)
1425       {
1426           Py_DECREF(finalResult);
1427           _errorDetails=ex.what();
1428           throw;
1429       }
1430   }
1431
1432   DEBTRACE( "++++++++++++++ ENDOF PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
1433 }
1434
1435 void PyFuncNode::executeLocal()
1436 {
1437   DEBTRACE( "++++++++++++++ PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1438
1439   int pos=0;
1440   PyObject* ob;
1441   if(!_pyfunc)throw Exception("PyFuncNode badly loaded");
1442   {
1443       AutoGIL agil;
1444       DEBTRACE( "---------------PyFuncNode::inputs---------------" );
1445       PyObject* args = PyTuple_New(getNumberOfInputPorts()) ;
1446       list<InputPort *>::iterator iter2;
1447       for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
1448         {
1449           InputPyPort *p=(InputPyPort *)*iter2;
1450           DEBTRACE( "port name: " << p->getName() );
1451           DEBTRACE( "port kind: " << p->edGetType()->kind() );
1452           ob=p->getPyObj();
1453 #ifdef _DEVDEBUG_
1454           PyObject_Print(ob,stderr,Py_PRINT_RAW);
1455           cerr << endl;
1456 #endif
1457           DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1458           Py_INCREF(ob);
1459           PyTuple_SetItem(args,pos,ob);
1460           DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1461           pos++;
1462         }
1463       DEBTRACE( "---------------End PyFuncNode::inputs---------------" );
1464
1465       DEBTRACE( "----------------PyFuncNode::calculation---------------" );
1466 #ifdef _DEVDEBUG_
1467       PyObject_Print(_pyfunc,stderr,Py_PRINT_RAW);
1468       cerr << endl;
1469       PyObject_Print(args,stderr,Py_PRINT_RAW);
1470       cerr << endl;
1471 #endif
1472       DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1473       PyObject* result = PyObject_CallObject( _pyfunc , args ) ;
1474       DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1475       Py_DECREF(args);
1476       fflush(stdout);
1477       fflush(stderr);
1478       if(result == NULL)
1479         {
1480           _errorDetails="";
1481           PyObject* new_stderr = newPyStdOut(_errorDetails);
1482           PySys_SetObject((char*)"stderr", new_stderr);
1483           std::ostringstream stream;
1484           stream << "/tmp/PythonNode_";
1485           stream << getpid();
1486           ofstream errorfile(stream.str().c_str());
1487           if (errorfile.is_open())
1488             {
1489               errorfile << _script;
1490               errorfile.close();
1491             }
1492           PyErr_Print();
1493           PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1494           Py_DECREF(new_stderr);
1495           throw Exception("Error during execution");
1496         }
1497       DEBTRACE( "----------------End PyFuncNode::calculation---------------" );
1498
1499       DEBTRACE( "-----------------PyFuncNode::outputs-----------------" );
1500       int nres=1;
1501       if(result == Py_None)
1502         nres=0;
1503       else if(PyTuple_Check(result))
1504         nres=PyTuple_Size(result);
1505
1506       if(getNumberOfOutputPorts() != nres)
1507         {
1508           std::string msg="Number of output arguments : Mismatch between definition and execution";
1509           Py_DECREF(result);
1510           _errorDetails=msg;
1511           throw Exception(msg);
1512         }
1513
1514       pos=0;
1515 #ifdef _DEVDEBUG_
1516       PyObject_Print(result,stderr,Py_PRINT_RAW);
1517       cerr << endl;
1518 #endif
1519       list<OutputPort *>::iterator iter;
1520       try
1521       {
1522           for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
1523             {
1524               OutputPyPort *p=(OutputPyPort *)*iter;
1525               DEBTRACE( "port name: " << p->getName() );
1526               DEBTRACE( "port kind: " << p->edGetType()->kind() );
1527               DEBTRACE( "port pos : " << pos );
1528               if(PyTuple_Check(result))ob=PyTuple_GetItem(result,pos) ;
1529               else ob=result;
1530               DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1531 #ifdef _DEVDEBUG_
1532               PyObject_Print(ob,stderr,Py_PRINT_RAW);
1533               cerr << endl;
1534 #endif
1535               p->put(ob);
1536               pos++;
1537             }
1538       }
1539       catch(ConversionException& ex)
1540       {
1541           Py_DECREF(result);
1542           _errorDetails=ex.what();
1543           throw;
1544       }
1545       DEBTRACE( "-----------------End PyFuncNode::outputs-----------------" );
1546       Py_DECREF(result);
1547   }
1548   DEBTRACE( "++++++++++++++ End PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1549 }
1550
1551 Node *PyFuncNode::simpleClone(ComposedNode *father, bool editionOnly) const
1552 {
1553   return new PyFuncNode(*this,father);
1554 }
1555
1556 void PyFuncNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
1557 {
1558   if(!CORBA::is_nil(_pynode))
1559     _pynode->UnRegister();
1560   _pynode=objContainer->createPyNode(getName().c_str(),getScript().c_str());
1561 }
1562
1563 Engines::PyNodeBase_var PyFuncNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
1564 {
1565   Engines::PyNode_var ret(objContainer->getDefaultPyNode(getName().c_str()));
1566   if(!CORBA::is_nil(ret))
1567     {
1568       ret->Register();
1569     }
1570   return Engines::PyNodeBase::_narrow(ret);
1571 }
1572
1573 void PyFuncNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
1574 {
1575   if(!CORBA::is_nil(_pynode))
1576     {
1577       Engines::PyNode_var tmpp(Engines::PyNode::_narrow(remoteInterp));
1578       if(_pynode->_is_equivalent(tmpp))
1579         return ;
1580     }
1581   if(!CORBA::is_nil(_pynode))
1582     _pynode->UnRegister();
1583   _pynode=Engines::PyNode::_narrow(remoteInterp);
1584 }
1585
1586 Engines::PyNodeBase_var PyFuncNode::getRemoteInterpreterHandle()
1587 {
1588   return Engines::PyNodeBase::_narrow(_pynode);
1589 }
1590
1591 //! Create a new node of same type with a given name
1592 PyFuncNode* PyFuncNode::cloneNode(const std::string& name)
1593 {
1594   PyFuncNode* n=new PyFuncNode(name);
1595   n->setScript(_script);
1596   n->setFname(_fname);
1597   list<InputPort *>::iterator iter;
1598   for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
1599     {
1600       InputPyPort *p=(InputPyPort *)*iter;
1601       n->edAddInputPort(p->getName(),p->edGetType());
1602     }
1603   list<OutputPort *>::iterator iter2;
1604   for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
1605     {
1606       OutputPyPort *p=(OutputPyPort *)*iter2;
1607       n->edAddOutputPort(p->getName(),p->edGetType());
1608     }
1609   return n;
1610 }
1611
1612 std::string PyFuncNode::getContainerLog()
1613 {
1614   return PythonEntry::GetContainerLog(_mode,_container,this);
1615 }
1616
1617 void PyFuncNode::shutdown(int level)
1618 {
1619   DEBTRACE("PyFuncNode::shutdown " << level);
1620   if(_mode=="local")return;
1621   if(_container)
1622     {
1623       if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
1624       _pynode=Engines::PyNode::_nil();
1625       _container->shutdown(level);
1626     }
1627 }
1628
1629 void PyFuncNode::imposeResource(const std::string& resource_name,
1630                                 const std::string& container_name)
1631 {
1632   if(!resource_name.empty() && !container_name.empty())
1633   {
1634     _imposedResource = resource_name;
1635     _imposedContainer = container_name;
1636   }
1637 }
1638
1639 bool PyFuncNode::canAcceptImposedResource()
1640 {
1641   return _container != nullptr && _container->canAcceptImposedResource();
1642 }
1643
1644 bool PyFuncNode::hasImposedResource()const
1645 {
1646   return PythonEntry::hasImposedResource();
1647 }
1648