]> SALOME platform Git repositories - modules/yacs.git/blob - src/runtime/PythonNode.cxx
Salome HOME
ddf37d0cb39ebaaebc18708616e2b9b6a1f77da9
[modules/yacs.git] / src / runtime / PythonNode.cxx
1 // Copyright (C) 2006-2015  CEA/DEN, EDF R&D
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 "AutoGIL.hxx"
25 #include "Container.hxx"
26 #include "SalomeContainer.hxx"
27 #include "SalomeHPContainer.hxx"
28 #include "SalomeContainerTmpForHP.hxx"
29 #include "ConversionException.hxx"
30
31 #include "PyStdout.hxx"
32 #include <iostream>
33 #include <sstream>
34 #include <fstream>
35
36 #ifdef WIN32
37 #include <process.h>
38 #define getpid _getpid
39 #endif
40
41 #if PY_VERSION_HEX < 0x02050000 
42 typedef int Py_ssize_t;
43 #endif
44
45 //#define _DEVDEBUG_
46 #include "YacsTrace.hxx"
47
48 using namespace YACS::ENGINE;
49 using namespace std;
50
51 const char PythonEntry::SCRIPT_FOR_SIMPLE_SERIALIZATION[]="import cPickle\n"
52     "def pickleForVarSimplePyth2009(val):\n"
53     "  return cPickle.dumps(val,-1)\n"
54     "\n";
55
56 const char PythonNode::IMPL_NAME[]="Python";
57 const char PythonNode::KIND[]="Python";
58
59 const char PythonNode::SCRIPT_FOR_SERIALIZATION[]="import cPickle\n"
60     "def pickleForDistPyth2009(kws):\n"
61     "  return cPickle.dumps(((),kws),-1)\n"
62     "\n"
63     "def unPickleForDistPyth2009(st):\n"
64     "  args=cPickle.loads(st)\n"
65     "  return args\n";
66
67 const char PythonNode::REMOTE_NAME[]="remote";
68
69 const char PythonNode::DPL_INFO_NAME[]="my_dpl_localization";
70
71 const char PyFuncNode::SCRIPT_FOR_SERIALIZATION[]="import cPickle\n"
72     "def pickleForDistPyth2009(*args,**kws):\n"
73     "  return cPickle.dumps((args,kws),-1)\n"
74     "\n"
75     "def unPickleForDistPyth2009(st):\n"
76     "  args=cPickle.loads(st)\n"
77     "  return args\n";
78
79
80 PythonEntry::PythonEntry():_context(0),_pyfuncSer(0),_pyfuncUnser(0),_pyfuncSimpleSer(0)
81 {
82 }
83
84 PythonEntry::~PythonEntry()
85 {
86   AutoGIL agil;
87   DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
88   // not Py_XDECREF of _pyfuncUnser because it is returned by PyDict_GetItem -> borrowed
89   // not Py_XDECREF of _pyfuncSer because it is returned by PyDict_GetItem -> borrowed
90   Py_XDECREF(_context);
91 }
92
93 void PythonEntry::commonRemoteLoadPart1(InlineNode *reqNode)
94 {
95   DEBTRACE( "---------------PythonEntry::CommonRemoteLoad function---------------" );
96   Container *container(reqNode->getContainer());
97   bool isContAlreadyStarted(false);
98   if(container)
99     {
100       isContAlreadyStarted=container->isAlreadyStarted(reqNode);
101       if(!isContAlreadyStarted)
102         {
103           try
104           {
105               container->start(reqNode);
106           }
107           catch(Exception& e)
108           {
109               reqNode->setErrorDetails(e.what());
110               throw e;
111           }
112         }
113     }
114   else
115     {
116       std::string what("PythonEntry::CommonRemoteLoad : a load operation requested on \"");
117       what+=reqNode->getName(); what+="\" with no container specified.";
118       reqNode->setErrorDetails(what);
119       throw Exception(what);
120     }
121 }
122
123 Engines::Container_var PythonEntry::commonRemoteLoadPart2(InlineNode *reqNode, bool& isInitializeRequested)
124 {
125   Container *container(reqNode->getContainer());
126   Engines::Container_var objContainer=Engines::Container::_nil();
127   if(!container)
128     throw Exception("No container specified !");
129   SalomeContainer *containerCast0(dynamic_cast<SalomeContainer *>(container));
130   SalomeHPContainer *containerCast1(dynamic_cast<SalomeHPContainer *>(container));
131   if(containerCast0)
132     objContainer=containerCast0->getContainerPtr(reqNode);
133   else if(containerCast1)
134     {
135       YACS::BASES::AutoCppPtr<SalomeContainerTmpForHP> tmpCont(SalomeContainerTmpForHP::BuildFrom(containerCast1,reqNode));
136       objContainer=tmpCont->getContainerPtr(reqNode);
137     }
138   else
139     throw Exception("Unrecognized type of container ! Salome one is expected for PythonNode/PyFuncNode !");
140   if(CORBA::is_nil(objContainer))
141     throw Exception("Container corba pointer is NULL for PythonNode !");
142   isInitializeRequested=false;
143   try
144   {
145       if(containerCast0)
146         {
147           createRemoteAdaptedPyInterpretor(objContainer);
148         }
149       else
150         {
151           Engines::PyNodeBase_var dftPyScript(retrieveDftRemotePyInterpretorIfAny(objContainer));
152           if(CORBA::is_nil(dftPyScript))
153             {
154               isInitializeRequested=true;
155               createRemoteAdaptedPyInterpretor(objContainer);
156             }
157           else
158             assignRemotePyInterpretor(dftPyScript);
159         }
160   }
161   catch( const SALOME::SALOME_Exception& ex )
162   {
163       std::string msg="Exception on remote python node creation ";
164       msg += '\n';
165       msg += ex.details.text.in();
166       reqNode->setErrorDetails(msg);
167       throw Exception(msg);
168   }
169   Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
170   if(CORBA::is_nil(pynode))
171     throw Exception("In PythonNode the ref in NULL ! ");
172   return objContainer;
173 }
174
175 void PythonEntry::commonRemoteLoadPart3(InlineNode *reqNode, Engines::Container_ptr objContainer, bool isInitializeRequested)
176 {
177   Container *container(reqNode->getContainer());
178   Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
179   ///
180   {
181     AutoGIL agil;
182     const char *picklizeScript(getSerializationScript());
183     PyObject *res=PyRun_String(picklizeScript,Py_file_input,_context,_context);
184     PyObject *res2(PyRun_String(SCRIPT_FOR_SIMPLE_SERIALIZATION,Py_file_input,_context,_context));
185     if(res == NULL || res2==NULL)
186       {
187         std::string errorDetails;
188         PyObject* new_stderr = newPyStdOut(errorDetails);
189         reqNode->setErrorDetails(errorDetails);
190         PySys_SetObject((char*)"stderr", new_stderr);
191         PyErr_Print();
192         PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
193         Py_DECREF(new_stderr);
194         throw Exception("Error during load");
195       }
196     Py_DECREF(res); Py_DECREF(res2);
197     _pyfuncSer=PyDict_GetItemString(_context,"pickleForDistPyth2009");
198     _pyfuncUnser=PyDict_GetItemString(_context,"unPickleForDistPyth2009");
199     _pyfuncSimpleSer=PyDict_GetItemString(_context,"pickleForVarSimplePyth2009");
200     if(_pyfuncSer == NULL)
201       {
202         std::string errorDetails;
203         PyObject *new_stderr(newPyStdOut(errorDetails));
204         reqNode->setErrorDetails(errorDetails);
205         PySys_SetObject((char*)"stderr", new_stderr);
206         PyErr_Print();
207         PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
208         Py_DECREF(new_stderr);
209         throw Exception("Error during load");
210       }
211     if(_pyfuncUnser == NULL)
212       {
213         std::string errorDetails;
214         PyObject *new_stderr(newPyStdOut(errorDetails));
215         reqNode->setErrorDetails(errorDetails);
216         PySys_SetObject((char*)"stderr", new_stderr);
217         PyErr_Print();
218         PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
219         Py_DECREF(new_stderr);
220         throw Exception("Error during load");
221       }
222     if(_pyfuncSimpleSer == NULL)
223       {
224         std::string errorDetails;
225         PyObject *new_stderr(newPyStdOut(errorDetails));
226         reqNode->setErrorDetails(errorDetails);
227         PySys_SetObject((char*)"stderr", new_stderr);
228         PyErr_Print();
229         PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
230         Py_DECREF(new_stderr);
231         throw Exception("Error during load");
232       }
233   }
234   if(isInitializeRequested)
235     {//This one is called only once at initialization in the container if an init-script is specified.
236       try
237       {
238           std::string zeInitScriptKey(container->getProperty(HomogeneousPoolContainer::INITIALIZE_SCRIPT_KEY));
239           if(!zeInitScriptKey.empty())
240             pynode->executeAnotherPieceOfCode(zeInitScriptKey.c_str());
241       }
242       catch( const SALOME::SALOME_Exception& ex )
243       {
244           std::string msg="Exception on PythonNode::loadRemote python invocation of initializisation py script !";
245           msg += '\n';
246           msg += ex.details.text.in();
247           reqNode->setErrorDetails(msg);
248           throw Exception(msg);
249       }
250       DEBTRACE( "---------------End PyNode::loadRemote function---------------" );
251     }
252 }
253
254 std::string PythonEntry::GetContainerLog(const std::string& mode, Container *container, const Task *askingTask)
255 {
256   if(mode=="local")
257     return "";
258
259   std::string msg;
260   try
261   {
262       SalomeContainer *containerCast(dynamic_cast<SalomeContainer *>(container));
263       SalomeHPContainer *objContainer2(dynamic_cast<SalomeHPContainer *>(container));
264       if(containerCast)
265         {
266           Engines::Container_var objContainer(containerCast->getContainerPtr(askingTask));
267           CORBA::String_var logname = objContainer->logfilename();
268           DEBTRACE(logname);
269           msg=logname;
270           std::string::size_type pos = msg.find(":");
271           msg=msg.substr(pos+1);
272         }
273       else if(objContainer2)
274         {
275           msg="Remote PythonNode is on HP Container : no log because no info of the location by definition of HP Container !";
276         }
277       else
278         {
279           msg="Not implemented yet for container log for that type of container !";
280         }
281   }
282   catch(...)
283   {
284       msg = "Container no longer reachable";
285   }
286   return msg;
287 }
288
289 void PythonEntry::commonRemoteLoad(InlineNode *reqNode)
290 {
291   commonRemoteLoadPart1(reqNode);
292   bool isInitializeRequested;
293   Engines::Container_var objContainer(commonRemoteLoadPart2(reqNode,isInitializeRequested));
294   commonRemoteLoadPart3(reqNode,objContainer,isInitializeRequested);
295 }
296
297 PythonNode::PythonNode(const PythonNode& other, ComposedNode *father):InlineNode(other,father)
298 {
299   _implementation=IMPL_NAME;
300   {
301     AutoGIL agil;
302     _context=PyDict_New();
303     if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
304       {
305         stringstream msg;
306         msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
307         _errorDetails=msg.str();
308         throw Exception(msg.str());
309       }
310   }
311 }
312
313 PythonNode::PythonNode(const std::string& name):InlineNode(name)
314 {
315   _implementation=IMPL_NAME;
316   {
317     AutoGIL agil;
318     _context=PyDict_New();
319     if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
320       {
321         stringstream msg;
322         msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
323         _errorDetails=msg.str();
324         throw Exception(msg.str());
325       }
326   }
327 }
328
329 PythonNode::~PythonNode()
330 {
331   if(!CORBA::is_nil(_pynode))
332     {
333       _pynode->UnRegister();
334     }
335 }
336
337 void PythonNode::checkBasicConsistency() const throw(YACS::Exception)
338 {
339   DEBTRACE("checkBasicConsistency");
340   InlineNode::checkBasicConsistency();
341   {
342     AutoGIL agil;
343     PyObject* res;
344     res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
345     if(res == NULL)
346       {
347         std::string error="";
348         PyObject* new_stderr = newPyStdOut(error);
349         PySys_SetObject((char*)"stderr", new_stderr);
350         PyErr_Print();
351         PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
352         Py_DECREF(new_stderr);
353         throw Exception(error);
354       }
355     else
356       Py_XDECREF(res);
357   }
358 }
359
360 void PythonNode::load()
361 {
362   DEBTRACE( "---------------PyNode::load function---------------" );
363   if(_mode==PythonNode::REMOTE_NAME)
364     loadRemote();
365   else
366     loadLocal();
367 }
368
369 void PythonNode::loadLocal()
370 {
371   DEBTRACE( "---------------PyNode::loadLocal function---------------" );
372   // do nothing
373 }
374
375 void PythonNode::loadRemote()
376 {
377   commonRemoteLoad(this);
378 }
379
380 void PythonNode::execute()
381 {
382   if(_mode==PythonNode::REMOTE_NAME)
383     executeRemote();
384   else
385     executeLocal();
386 }
387
388 void PythonNode::executeRemote()
389 {
390   DEBTRACE( "++++++++++++++ PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
391   if(!_pyfuncSer)
392     throw Exception("DistributedPythonNode badly loaded");
393   //
394   if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
395     {
396       bool dummy;
397       commonRemoteLoadPart2(this,dummy);
398       _pynode->assignNewCompiledCode(getScript().c_str());
399     }
400   //
401   Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);
402   {
403       AutoGIL agil;
404       PyObject *args(0),*ob(0);
405       //===========================================================================
406       // Get inputs in input ports, build a Python dict and pickle it
407       //===========================================================================
408       args = PyDict_New();
409       std::list<InputPort *>::iterator iter2;
410       int pos(0);
411       for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); ++iter2)
412         {
413           InputPyPort *p=(InputPyPort *)*iter2;
414           ob=p->getPyObj();
415           PyDict_SetItemString(args,p->getName().c_str(),ob);
416           pos++;
417         }
418 #ifdef _DEVDEBUG_
419       PyObject_Print(args,stderr,Py_PRINT_RAW);
420       std::cerr << endl;
421 #endif
422       PyObject *serializationInput(PyObject_CallFunctionObjArgs(_pyfuncSer,args,NULL));
423       Py_DECREF(args);
424       //The pickled string may contain NULL characters so use PyString_AsStringAndSize
425       char *serializationInputC(0);
426       Py_ssize_t len;
427       if (PyString_AsStringAndSize(serializationInput, &serializationInputC, &len))
428         throw Exception("DistributedPythonNode problem in python pickle");
429       serializationInputCorba->length(len);
430       for(int i=0; i < len ; i++)
431         serializationInputCorba[i]=serializationInputC[i];
432       Py_DECREF(serializationInput);
433   }
434
435   //get the list of output argument names
436   std::list<OutputPort *>::iterator iter;
437   Engines::listofstring myseq;
438   myseq.length(getNumberOfOutputPorts());
439   int pos=0;
440   for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
441     {
442       OutputPyPort *p=(OutputPyPort *)*iter;
443       myseq[pos]=p->getName().c_str();
444       DEBTRACE( "port name: " << p->getName() );
445       DEBTRACE( "port kind: " << p->edGetType()->kind() );
446       DEBTRACE( "port pos : " << pos );
447       pos++;
448     }
449   //===========================================================================
450   // Execute in remote Python node
451   //===========================================================================
452   DEBTRACE( "-----------------starting remote python invocation-----------------" );
453   Engines::pickledArgs_var resultCorba;
454   try
455     {
456       //pass outargsname and dict serialized
457       resultCorba=_pynode->execute(myseq,serializationInputCorba);
458     }
459   catch( const SALOME::SALOME_Exception& ex )
460     {
461       std::string msg="Exception on remote python invocation";
462       msg += '\n';
463       msg += ex.details.text.in();
464       _errorDetails=msg;
465       throw Exception(msg);
466     }
467   DEBTRACE( "-----------------end of remote python invocation-----------------" );
468   //===========================================================================
469   // Get results, unpickle and put them in output ports
470   //===========================================================================
471   char *resultCorbaC=new char[resultCorba->length()+1];
472   resultCorbaC[resultCorba->length()]='\0';
473   for(int i=0;i<resultCorba->length();i++)
474     resultCorbaC[i]=resultCorba[i];
475
476   {
477       AutoGIL agil;
478       PyObject *args(0),*ob(0);
479       PyObject* resultPython=PyString_FromStringAndSize(resultCorbaC,resultCorba->length());
480       delete [] resultCorbaC;
481       args = PyTuple_New(1);
482       PyTuple_SetItem(args,0,resultPython);
483       PyObject *finalResult=PyObject_CallObject(_pyfuncUnser,args);
484       Py_DECREF(args);
485
486       if (finalResult == NULL)
487         {
488           std::stringstream msg;
489           msg << "Conversion with pickle of output ports failed !";
490           msg << " : " << __FILE__ << ":" << __LINE__;
491           _errorDetails=msg.str();
492           throw YACS::ENGINE::ConversionException(msg.str());
493         }
494
495       DEBTRACE( "-----------------PythonNode::outputs-----------------" );
496       int nres=1;
497       if(finalResult == Py_None)
498         nres=0;
499       else if(PyTuple_Check(finalResult))
500         nres=PyTuple_Size(finalResult);
501
502       if(getNumberOfOutputPorts() != nres)
503         {
504           std::string msg="Number of output arguments : Mismatch between definition and execution";
505           Py_DECREF(finalResult);
506           _errorDetails=msg;
507           throw Exception(msg);
508         }
509
510       pos=0;
511       try
512       {
513           for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
514             {
515               OutputPyPort *p=(OutputPyPort *)*iter;
516               DEBTRACE( "port name: " << p->getName() );
517               DEBTRACE( "port kind: " << p->edGetType()->kind() );
518               DEBTRACE( "port pos : " << pos );
519               if(PyTuple_Check(finalResult))
520                 ob=PyTuple_GetItem(finalResult,pos) ;
521               else
522                 ob=finalResult;
523               DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
524               p->put(ob);
525               pos++;
526             }
527           Py_DECREF(finalResult);
528       }
529       catch(ConversionException& ex)
530       {
531           Py_DECREF(finalResult);
532           _errorDetails=ex.what();
533           throw;
534       }
535   }
536   DEBTRACE( "++++++++++++++ ENDOF PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
537 }
538
539 void PythonNode::executeLocal()
540 {
541   DEBTRACE( "++++++++++++++ PyNode::executeLocal: " << getName() << " ++++++++++++++++++++" );
542   {
543     AutoGIL agil;
544
545     DEBTRACE( "---------------PyNode::inputs---------------" );
546     list<InputPort *>::iterator iter2;
547     for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
548       {
549         InputPyPort *p=(InputPyPort *)*iter2;
550         DEBTRACE( "port name: " << p->getName() );
551         DEBTRACE( "port kind: " << p->edGetType()->kind() );
552         PyObject* ob=p->getPyObj();
553         DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
554 #ifdef _DEVDEBUG_
555         PyObject_Print(ob,stderr,Py_PRINT_RAW);
556         cerr << endl;
557 #endif
558         int ier=PyDict_SetItemString(_context,p->getName().c_str(),ob);
559         DEBTRACE( "after PyDict_SetItemString:ob refcnt: " << ob->ob_refcnt );
560       }
561
562     DEBTRACE( "---------------End PyNode::inputs---------------" );
563
564     //calculation
565     DEBTRACE( "----------------PyNode::calculation---------------" );
566     DEBTRACE(  _script );
567     DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
568
569     std::ostringstream stream;
570     stream << "/tmp/PythonNode_";
571     stream << getpid();
572
573     PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
574     if(code == NULL)
575       {
576         _errorDetails="";
577         PyObject* new_stderr = newPyStdOut(_errorDetails);
578         PySys_SetObject((char*)"stderr", new_stderr);
579         PyErr_Print();
580         PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
581         Py_DECREF(new_stderr);
582         throw Exception("Error during execution");
583       }
584     PyObject *res = PyEval_EvalCode((PyCodeObject *)code, _context, _context);
585
586     Py_DECREF(code);
587     Py_XDECREF(res);
588     DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
589     fflush(stdout);
590     fflush(stderr);
591     if(PyErr_Occurred ())
592       {
593         _errorDetails="";
594         PyObject* new_stderr = newPyStdOut(_errorDetails);
595         PySys_SetObject((char*)"stderr", new_stderr);
596         ofstream errorfile(stream.str().c_str());
597         if (errorfile.is_open())
598           {
599             errorfile << _script;
600             errorfile.close();
601           }
602         PyErr_Print();
603         PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
604         Py_DECREF(new_stderr);
605         throw Exception("Error during execution");
606       }
607
608     DEBTRACE( "-----------------PyNode::outputs-----------------" );
609     list<OutputPort *>::iterator iter;
610     try
611     {
612         for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
613           {
614             OutputPyPort *p=(OutputPyPort *)*iter;
615             DEBTRACE( "port name: " << p->getName() );
616             DEBTRACE( "port kind: " << p->edGetType()->kind() );
617             PyObject *ob=PyDict_GetItemString(_context,p->getName().c_str());
618             if(ob==NULL)
619               {
620                 std::string msg="Error during execution: there is no variable ";
621                 msg=msg+p->getName()+" in node context";
622                 _errorDetails=msg;
623                 throw Exception(msg);
624               }
625             DEBTRACE( "PyNode::outputs::ob refcnt: " << ob->ob_refcnt );
626 #ifdef _DEVDEBUG_
627             PyObject_Print(ob,stderr,Py_PRINT_RAW);
628             cerr << endl;
629 #endif
630             p->put(ob);
631           }
632     }
633     catch(ConversionException& ex)
634     {
635         _errorDetails=ex.what();
636         throw;
637     }
638
639     DEBTRACE( "-----------------End PyNode::outputs-----------------" );
640   }
641   DEBTRACE( "++++++++++++++ End PyNode::execute: " << getName() << " ++++++++++++++++++++" );
642 }
643
644 std::string PythonNode::getContainerLog()
645 {
646   return PythonEntry::GetContainerLog(_mode,_container,this);
647 }
648
649 void PythonNode::shutdown(int level)
650 {
651   DEBTRACE("PythonNode::shutdown " << level);
652   if(_mode=="local")return;
653   if(_container)
654     {
655       if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
656       _pynode=Engines::PyScriptNode::_nil();
657       _container->shutdown(level);
658     }
659 }
660
661 Node *PythonNode::simpleClone(ComposedNode *father, bool editionOnly) const
662 {
663   return new PythonNode(*this,father);
664 }
665
666 void PythonNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
667 {
668   if(!CORBA::is_nil(_pynode))
669     _pynode->UnRegister();
670   _pynode=objContainer->createPyScriptNode(getName().c_str(),getScript().c_str());
671 }
672
673 Engines::PyNodeBase_var PythonNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
674 {
675   Engines::PyScriptNode_var ret(objContainer->getDefaultPyScriptNode(getName().c_str()));
676   if(!CORBA::is_nil(ret))
677     {
678       ret->Register();
679     }
680   return Engines::PyNodeBase::_narrow(ret);
681 }
682
683 void PythonNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
684 {
685   if(!CORBA::is_nil(_pynode))
686     {
687       Engines::PyScriptNode_var tmpp(Engines::PyScriptNode::_narrow(remoteInterp));
688       if(_pynode->_is_equivalent(tmpp))
689         return ;
690     }
691   if(!CORBA::is_nil(_pynode))
692     _pynode->UnRegister();
693   _pynode=Engines::PyScriptNode::_narrow(remoteInterp);
694 }
695
696 Engines::PyNodeBase_var PythonNode::getRemoteInterpreterHandle()
697 {
698   return Engines::PyNodeBase::_narrow(_pynode);
699 }
700
701 //! Create a new node of same type with a given name
702 PythonNode* PythonNode::cloneNode(const std::string& name)
703 {
704   PythonNode* n=new PythonNode(name);
705   n->setScript(_script);
706   list<InputPort *>::iterator iter;
707   for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
708     {
709       InputPyPort *p=(InputPyPort *)*iter;
710       DEBTRACE( "port name: " << p->getName() );
711       DEBTRACE( "port kind: " << p->edGetType()->kind() );
712       n->edAddInputPort(p->getName(),p->edGetType());
713     }
714   list<OutputPort *>::iterator iter2;
715   for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
716     {
717       OutputPyPort *p=(OutputPyPort *)*iter2;
718       DEBTRACE( "port name: " << p->getName() );
719       DEBTRACE( "port kind: " << p->edGetType()->kind() );
720       n->edAddOutputPort(p->getName(),p->edGetType());
721     }
722   return n;
723 }
724
725 void PythonNode::applyDPLScope(ComposedNode *gfn)
726 {
727   std::vector< std::pair<std::string,int> > ret(getDPLScopeInfo(gfn));
728   if(ret.empty())
729     return ;
730   //
731   PyObject *ob(0);
732   {
733     AutoGIL agil;
734     std::size_t sz(ret.size());
735     ob=PyList_New(sz);
736     for(std::size_t i=0;i<sz;i++)
737       {
738         const std::pair<std::string,int>& p(ret[i]);
739         PyObject *elt(PyTuple_New(2));
740         PyTuple_SetItem(elt,0,PyString_FromString(p.first.c_str()));
741         PyTuple_SetItem(elt,1,PyLong_FromLong(p.second));
742         PyList_SetItem(ob,i,elt);
743       }
744   }
745   if(_mode==REMOTE_NAME)
746     {
747       Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);
748       {
749         AutoGIL agil;
750         PyObject *serializationInput(PyObject_CallFunctionObjArgs(_pyfuncSimpleSer,ob,NULL));
751         Py_XDECREF(ob);
752         char *serializationInputC(0);
753         Py_ssize_t len;
754         if (PyString_AsStringAndSize(serializationInput, &serializationInputC, &len))
755           throw Exception("DistributedPythonNode problem in python pickle");
756         serializationInputCorba->length(len);
757         for(int i=0; i < len ; i++)
758           serializationInputCorba[i]=serializationInputC[i];
759         Py_XDECREF(serializationInput);
760       }
761       _pynode->defineNewCustomVar(DPL_INFO_NAME,serializationInputCorba);
762     }
763   else
764     {
765       AutoGIL agil;
766       PyDict_SetItemString(_context,DPL_INFO_NAME,ob);
767       Py_XDECREF(ob);
768     }
769 }
770
771 PyFuncNode::PyFuncNode(const PyFuncNode& other, ComposedNode *father):InlineFuncNode(other,father),_pyfunc(0)
772 {
773   _implementation = PythonNode::IMPL_NAME;
774   {
775     AutoGIL agil;
776     _context=PyDict_New();
777     DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
778     if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
779       {
780         stringstream msg;
781         msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
782         _errorDetails=msg.str();
783         throw Exception(msg.str());
784       }
785   }
786 }
787
788 PyFuncNode::PyFuncNode(const std::string& name): InlineFuncNode(name),_pyfunc(0)
789 {
790
791   _implementation = PythonNode::IMPL_NAME;
792   DEBTRACE( "PyFuncNode::PyFuncNode " << name );
793   {
794     AutoGIL agil;
795     _context=PyDict_New();
796     DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
797     if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
798       {
799         stringstream msg;
800         msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
801         _errorDetails=msg.str();
802         throw Exception(msg.str());
803       }
804   }
805 }
806
807 PyFuncNode::~PyFuncNode()
808 {
809   if(!CORBA::is_nil(_pynode))
810     {
811       _pynode->UnRegister();
812     }
813 }
814
815 void PyFuncNode::init(bool start)
816 {
817   initCommonPartWithoutStateManagement(start);
818   if(start) //complete initialization
819     setState(YACS::READY);
820   else if(_state > YACS::LOADED)// WARNING FuncNode has internal vars (CEA usecase) ! Partial initialization (inside a loop). Exclusivity of funcNode.
821     setState(YACS::TORECONNECT);
822 }
823
824 void PyFuncNode::checkBasicConsistency() const throw(YACS::Exception)
825 {
826   DEBTRACE("checkBasicConsistency");
827   InlineFuncNode::checkBasicConsistency();
828   {
829     AutoGIL agil;
830     PyObject* res;
831     res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
832     if(res == NULL)
833       {
834         std::string error="";
835         PyObject* new_stderr = newPyStdOut(error);
836         PySys_SetObject((char*)"stderr", new_stderr);
837         PyErr_Print();
838         PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
839         Py_DECREF(new_stderr);
840         throw Exception(error);
841       }
842     else
843       Py_XDECREF(res);
844   }
845 }
846
847 void PyFuncNode::load()
848 {
849   DEBTRACE( "---------------PyfuncNode::load function---------------" );
850   if(_mode==PythonNode::REMOTE_NAME)
851     loadRemote();
852   else
853     loadLocal();
854 }
855
856 void PyFuncNode::loadRemote()
857 {
858   commonRemoteLoad(this);
859 }
860
861 void PyFuncNode::loadLocal()
862 {
863   DEBTRACE( "---------------PyFuncNode::load function " << getName() << " ---------------" );
864   DEBTRACE(  _script );
865
866 #ifdef _DEVDEBUG_
867   list<OutputPort *>::iterator iter;
868   for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
869     {
870       OutputPyPort *p=(OutputPyPort *)*iter;
871       DEBTRACE( "port name: " << p->getName() );
872       DEBTRACE( "port kind: " << p->edGetType()->kind() );
873     }
874 #endif
875
876   {
877     AutoGIL agil;
878     DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
879
880     std::ostringstream stream;
881     stream << "/tmp/PythonNode_";
882     stream << getpid();
883
884     PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
885     if(code == NULL)
886       {
887         _errorDetails="";
888         PyObject* new_stderr = newPyStdOut(_errorDetails);
889         PySys_SetObject((char*)"stderr", new_stderr);
890         PyErr_Print();
891         PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
892         Py_DECREF(new_stderr);
893         throw Exception("Error during execution");
894       }
895     PyObject *res = PyEval_EvalCode((PyCodeObject *)code, _context, _context);
896     Py_DECREF(code);
897     Py_XDECREF(res);
898
899     DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
900     if(PyErr_Occurred ())
901       {
902         _errorDetails="";
903         PyObject* new_stderr = newPyStdOut(_errorDetails);
904         PySys_SetObject((char*)"stderr", new_stderr);
905         ofstream errorfile(stream.str().c_str());
906         if (errorfile.is_open())
907           {
908             errorfile << _script;
909             errorfile.close();
910           }
911         PyErr_Print();
912         PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
913         Py_DECREF(new_stderr);
914         throw Exception("Error during execution");
915         return;
916       }
917     _pyfunc=PyDict_GetItemString(_context,_fname.c_str());
918     DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
919     if(_pyfunc == NULL)
920       {
921         _errorDetails="";
922         PyObject* new_stderr = newPyStdOut(_errorDetails);
923         PySys_SetObject((char*)"stderr", new_stderr);
924         PyErr_Print();
925         PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
926         Py_DECREF(new_stderr);
927         throw Exception("Error during execution");
928       }
929     DEBTRACE( "---------------End PyFuncNode::load function---------------" );
930   }
931 }
932
933 void PyFuncNode::execute()
934 {
935   if(_mode==PythonNode::REMOTE_NAME)
936     executeRemote();
937   else
938     executeLocal();
939 }
940
941 void PyFuncNode::executeRemote()
942 {
943   DEBTRACE( "++++++++++++++ PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
944   if(!_pyfuncSer)
945     throw Exception("DistributedPythonNode badly loaded");
946   //
947   if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
948     {
949       bool dummy;
950       commonRemoteLoadPart2(this,dummy);
951       _pynode->executeAnotherPieceOfCode(getScript().c_str());
952     }
953   //
954   Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);;
955   {
956       AutoGIL agil;
957       PyObject *ob(0);
958       //===========================================================================
959       // Get inputs in input ports, build a Python tuple and pickle it
960       //===========================================================================
961       PyObject *args(PyTuple_New(getNumberOfInputPorts()));
962       int pos(0);
963       for(std::list<InputPort *>::iterator iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++,pos++)
964         {
965           InputPyPort *p=(InputPyPort *)*iter2;
966           ob=p->getPyObj();
967           Py_INCREF(ob);
968           PyTuple_SetItem(args,pos,ob);
969         }
970 #ifdef _DEVDEBUG_
971       PyObject_Print(args,stderr,Py_PRINT_RAW);
972       std::cerr << endl;
973 #endif
974       PyObject *serializationInput=PyObject_CallObject(_pyfuncSer,args);
975       Py_DECREF(args);
976       //The pickled string may contain NULL characters so use PyString_AsStringAndSize
977       char *serializationInputC(0);
978       Py_ssize_t len;
979       if (PyString_AsStringAndSize(serializationInput, &serializationInputC, &len))
980         throw Exception("DistributedPythonNode problem in python pickle");
981
982       serializationInputCorba->length(len);
983       for(int i=0; i < len ; i++)
984         serializationInputCorba[i]=serializationInputC[i];
985       Py_DECREF(serializationInput);
986   }
987
988   //===========================================================================
989   // Execute in remote Python node
990   //===========================================================================
991   DEBTRACE( "-----------------starting remote python invocation-----------------" );
992   Engines::pickledArgs_var resultCorba;
993   try
994     {
995       resultCorba=_pynode->execute(getFname().c_str(),serializationInputCorba);
996     }
997   catch( const SALOME::SALOME_Exception& ex )
998     {
999       std::string msg="Exception on remote python invocation";
1000       msg += '\n';
1001       msg += ex.details.text.in();
1002       _errorDetails=msg;
1003       throw Exception(msg);
1004     }
1005   DEBTRACE( "-----------------end of remote python invocation-----------------" );
1006   //===========================================================================
1007   // Get results, unpickle and put them in output ports
1008   //===========================================================================
1009   char *resultCorbaC=new char[resultCorba->length()+1];
1010   resultCorbaC[resultCorba->length()]='\0';
1011   for(int i=0;i<resultCorba->length();i++)
1012     resultCorbaC[i]=resultCorba[i];
1013
1014   {
1015       AutoGIL agil;
1016
1017       PyObject *resultPython(PyString_FromStringAndSize(resultCorbaC,resultCorba->length()));
1018       delete [] resultCorbaC;
1019       PyObject *args(PyTuple_New(1)),*ob(0);
1020       PyTuple_SetItem(args,0,resultPython);
1021       PyObject *finalResult=PyObject_CallObject(_pyfuncUnser,args);
1022       Py_DECREF(args);
1023
1024       DEBTRACE( "-----------------PythonNode::outputs-----------------" );
1025       int nres=1;
1026       if(finalResult == Py_None)
1027         nres=0;
1028       else if(PyTuple_Check(finalResult))
1029         nres=PyTuple_Size(finalResult);
1030
1031       if(getNumberOfOutputPorts() != nres)
1032         {
1033           std::string msg="Number of output arguments : Mismatch between definition and execution";
1034           Py_DECREF(finalResult);
1035           _errorDetails=msg;
1036           throw Exception(msg);
1037         }
1038
1039       try
1040       {
1041           int pos(0);
1042           for(std::list<OutputPort *>::iterator iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++, pos++)
1043             {
1044               OutputPyPort *p=(OutputPyPort *)*iter;
1045               DEBTRACE( "port name: " << p->getName() );
1046               DEBTRACE( "port kind: " << p->edGetType()->kind() );
1047               DEBTRACE( "port pos : " << pos );
1048               if(PyTuple_Check(finalResult))
1049                 ob=PyTuple_GetItem(finalResult,pos) ;
1050               else
1051                 ob=finalResult;
1052               DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1053               p->put(ob);
1054             }
1055           Py_DECREF(finalResult);
1056       }
1057       catch(ConversionException& ex)
1058       {
1059           Py_DECREF(finalResult);
1060           _errorDetails=ex.what();
1061           throw;
1062       }
1063   }
1064
1065   DEBTRACE( "++++++++++++++ ENDOF PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
1066 }
1067
1068 void PyFuncNode::executeLocal()
1069 {
1070   DEBTRACE( "++++++++++++++ PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1071
1072   int pos=0;
1073   PyObject* ob;
1074   if(!_pyfunc)throw Exception("PyFuncNode badly loaded");
1075   {
1076       AutoGIL agil;
1077       DEBTRACE( "---------------PyFuncNode::inputs---------------" );
1078       PyObject* args = PyTuple_New(getNumberOfInputPorts()) ;
1079       list<InputPort *>::iterator iter2;
1080       for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
1081         {
1082           InputPyPort *p=(InputPyPort *)*iter2;
1083           DEBTRACE( "port name: " << p->getName() );
1084           DEBTRACE( "port kind: " << p->edGetType()->kind() );
1085           ob=p->getPyObj();
1086 #ifdef _DEVDEBUG_
1087           PyObject_Print(ob,stderr,Py_PRINT_RAW);
1088           cerr << endl;
1089 #endif
1090           DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1091           Py_INCREF(ob);
1092           PyTuple_SetItem(args,pos,ob);
1093           DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1094           pos++;
1095         }
1096       DEBTRACE( "---------------End PyFuncNode::inputs---------------" );
1097
1098       DEBTRACE( "----------------PyFuncNode::calculation---------------" );
1099 #ifdef _DEVDEBUG_
1100       PyObject_Print(_pyfunc,stderr,Py_PRINT_RAW);
1101       cerr << endl;
1102       PyObject_Print(args,stderr,Py_PRINT_RAW);
1103       cerr << endl;
1104 #endif
1105       DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1106       PyObject* result = PyObject_CallObject( _pyfunc , args ) ;
1107       DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1108       Py_DECREF(args);
1109       fflush(stdout);
1110       fflush(stderr);
1111       if(result == NULL)
1112         {
1113           _errorDetails="";
1114           PyObject* new_stderr = newPyStdOut(_errorDetails);
1115           PySys_SetObject((char*)"stderr", new_stderr);
1116           std::ostringstream stream;
1117           stream << "/tmp/PythonNode_";
1118           stream << getpid();
1119           ofstream errorfile(stream.str().c_str());
1120           if (errorfile.is_open())
1121             {
1122               errorfile << _script;
1123               errorfile.close();
1124             }
1125           PyErr_Print();
1126           PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1127           Py_DECREF(new_stderr);
1128           throw Exception("Error during execution");
1129         }
1130       DEBTRACE( "----------------End PyFuncNode::calculation---------------" );
1131
1132       DEBTRACE( "-----------------PyFuncNode::outputs-----------------" );
1133       int nres=1;
1134       if(result == Py_None)
1135         nres=0;
1136       else if(PyTuple_Check(result))
1137         nres=PyTuple_Size(result);
1138
1139       if(getNumberOfOutputPorts() != nres)
1140         {
1141           std::string msg="Number of output arguments : Mismatch between definition and execution";
1142           Py_DECREF(result);
1143           _errorDetails=msg;
1144           throw Exception(msg);
1145         }
1146
1147       pos=0;
1148 #ifdef _DEVDEBUG_
1149       PyObject_Print(result,stderr,Py_PRINT_RAW);
1150       cerr << endl;
1151 #endif
1152       list<OutputPort *>::iterator iter;
1153       try
1154       {
1155           for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
1156             {
1157               OutputPyPort *p=(OutputPyPort *)*iter;
1158               DEBTRACE( "port name: " << p->getName() );
1159               DEBTRACE( "port kind: " << p->edGetType()->kind() );
1160               DEBTRACE( "port pos : " << pos );
1161               if(PyTuple_Check(result))ob=PyTuple_GetItem(result,pos) ;
1162               else ob=result;
1163               DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1164 #ifdef _DEVDEBUG_
1165               PyObject_Print(ob,stderr,Py_PRINT_RAW);
1166               cerr << endl;
1167 #endif
1168               p->put(ob);
1169               pos++;
1170             }
1171       }
1172       catch(ConversionException& ex)
1173       {
1174           Py_DECREF(result);
1175           _errorDetails=ex.what();
1176           throw;
1177       }
1178       DEBTRACE( "-----------------End PyFuncNode::outputs-----------------" );
1179       Py_DECREF(result);
1180   }
1181   DEBTRACE( "++++++++++++++ End PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1182 }
1183
1184 Node *PyFuncNode::simpleClone(ComposedNode *father, bool editionOnly) const
1185 {
1186   return new PyFuncNode(*this,father);
1187 }
1188
1189 void PyFuncNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
1190 {
1191   if(!CORBA::is_nil(_pynode))
1192     _pynode->UnRegister();
1193   _pynode=objContainer->createPyNode(getName().c_str(),getScript().c_str());
1194 }
1195
1196 Engines::PyNodeBase_var PyFuncNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
1197 {
1198   Engines::PyNode_var ret(objContainer->getDefaultPyNode(getName().c_str()));
1199   if(!CORBA::is_nil(ret))
1200     {
1201       ret->Register();
1202     }
1203   return Engines::PyNodeBase::_narrow(ret);
1204 }
1205
1206 void PyFuncNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
1207 {
1208   if(!CORBA::is_nil(_pynode))
1209     {
1210       Engines::PyNode_var tmpp(Engines::PyNode::_narrow(remoteInterp));
1211       if(_pynode->_is_equivalent(tmpp))
1212         return ;
1213     }
1214   if(!CORBA::is_nil(_pynode))
1215     _pynode->UnRegister();
1216   _pynode=Engines::PyNode::_narrow(remoteInterp);
1217 }
1218
1219 Engines::PyNodeBase_var PyFuncNode::getRemoteInterpreterHandle()
1220 {
1221   return Engines::PyNodeBase::_narrow(_pynode);
1222 }
1223
1224 //! Create a new node of same type with a given name
1225 PyFuncNode* PyFuncNode::cloneNode(const std::string& name)
1226 {
1227   PyFuncNode* n=new PyFuncNode(name);
1228   n->setScript(_script);
1229   n->setFname(_fname);
1230   list<InputPort *>::iterator iter;
1231   for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
1232     {
1233       InputPyPort *p=(InputPyPort *)*iter;
1234       n->edAddInputPort(p->getName(),p->edGetType());
1235     }
1236   list<OutputPort *>::iterator iter2;
1237   for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
1238     {
1239       OutputPyPort *p=(OutputPyPort *)*iter2;
1240       n->edAddOutputPort(p->getName(),p->edGetType());
1241     }
1242   return n;
1243 }
1244
1245 std::string PyFuncNode::getContainerLog()
1246 {
1247   return PythonEntry::GetContainerLog(_mode,_container,this);
1248 }
1249
1250 void PyFuncNode::shutdown(int level)
1251 {
1252   DEBTRACE("PyFuncNode::shutdown " << level);
1253   if(_mode=="local")return;
1254   if(_container)
1255     {
1256       if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
1257       _pynode=Engines::PyNode::_nil();
1258       _container->shutdown(level);
1259     }
1260 }
1261