1 // Copyright (C) 2006-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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"
31 #include "PyStdout.hxx"
38 #define getpid _getpid
41 #if PY_VERSION_HEX < 0x02050000
42 typedef int Py_ssize_t;
46 #include "YacsTrace.hxx"
48 using namespace YACS::ENGINE;
51 const char PythonEntry::SCRIPT_FOR_SIMPLE_SERIALIZATION[]="import pickle\n"
52 "def pickleForVarSimplePyth2009(val):\n"
53 " return pickle.dumps(val,-1)\n"
56 const char PythonNode::IMPL_NAME[]="Python";
57 const char PythonNode::KIND[]="Python";
59 const char PythonNode::SCRIPT_FOR_SERIALIZATION[]="import pickle\n"
60 "def pickleForDistPyth2009(kws):\n"
61 " return pickle.dumps(((),kws),-1)\n"
63 "def unPickleForDistPyth2009(st):\n"
64 " args=pickle.loads(st)\n"
67 const char PythonNode::REMOTE_NAME[]="remote";
69 const char PythonNode::DPL_INFO_NAME[]="my_dpl_localization";
71 const char PyFuncNode::SCRIPT_FOR_SERIALIZATION[]="import pickle\n"
72 "def pickleForDistPyth2009(*args,**kws):\n"
73 " return pickle.dumps((args,kws),-1)\n"
75 "def unPickleForDistPyth2009(st):\n"
76 " args=pickle.loads(st)\n"
80 PythonEntry::PythonEntry():_context(0),_pyfuncSer(0),_pyfuncUnser(0),_pyfuncSimpleSer(0)
84 PythonEntry::~PythonEntry()
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
93 void PythonEntry::commonRemoteLoadPart1(InlineNode *reqNode)
95 DEBTRACE( "---------------PythonEntry::CommonRemoteLoad function---------------" );
96 Container *container(reqNode->getContainer());
97 bool isContAlreadyStarted(false);
100 isContAlreadyStarted=container->isAlreadyStarted(reqNode);
101 if(!isContAlreadyStarted)
105 container->start(reqNode);
109 reqNode->setErrorDetails(e.what());
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);
123 Engines::Container_var PythonEntry::commonRemoteLoadPart2(InlineNode *reqNode, bool& isInitializeRequested)
125 Container *container(reqNode->getContainer());
126 Engines::Container_var objContainer=Engines::Container::_nil();
128 throw Exception("No container specified !");
129 SalomeContainer *containerCast0(dynamic_cast<SalomeContainer *>(container));
130 SalomeHPContainerBase *containerCast1(dynamic_cast<SalomeHPContainerBase *>(container));
132 objContainer=containerCast0->getContainerPtr(reqNode);
133 else if(containerCast1)
135 objContainer=containerCast1->getContainerPtr(reqNode);
138 throw Exception("Unrecognized type of container ! Salome one is expected for PythonNode/PyFuncNode !");
139 if(CORBA::is_nil(objContainer))
140 throw Exception("Container corba pointer is NULL for PythonNode !");
141 isInitializeRequested=false;
146 createRemoteAdaptedPyInterpretor(objContainer);
150 Engines::PyNodeBase_var dftPyScript(retrieveDftRemotePyInterpretorIfAny(objContainer));
151 if(CORBA::is_nil(dftPyScript))
153 isInitializeRequested=true;
154 createRemoteAdaptedPyInterpretor(objContainer);
157 assignRemotePyInterpretor(dftPyScript);
160 catch( const SALOME::SALOME_Exception& ex )
162 std::string msg="Exception on remote python node creation ";
164 msg += ex.details.text.in();
165 reqNode->setErrorDetails(msg);
166 throw Exception(msg);
168 Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
169 if(CORBA::is_nil(pynode))
170 throw Exception("In PythonNode the ref in NULL ! ");
174 void PythonEntry::commonRemoteLoadPart3(InlineNode *reqNode, Engines::Container_ptr objContainer, bool isInitializeRequested)
176 Container *container(reqNode->getContainer());
177 Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
181 const char *picklizeScript(getSerializationScript());
182 PyObject *res=PyRun_String(picklizeScript,Py_file_input,_context,_context);
183 PyObject *res2(PyRun_String(SCRIPT_FOR_SIMPLE_SERIALIZATION,Py_file_input,_context,_context));
184 if(res == NULL || res2==NULL)
186 std::string errorDetails;
187 PyObject* new_stderr = newPyStdOut(errorDetails);
188 reqNode->setErrorDetails(errorDetails);
189 PySys_SetObject((char*)"stderr", new_stderr);
191 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
192 Py_DECREF(new_stderr);
193 throw Exception("Error during load");
195 Py_DECREF(res); Py_DECREF(res2);
196 _pyfuncSer=PyDict_GetItemString(_context,"pickleForDistPyth2009");
197 _pyfuncUnser=PyDict_GetItemString(_context,"unPickleForDistPyth2009");
198 _pyfuncSimpleSer=PyDict_GetItemString(_context,"pickleForVarSimplePyth2009");
199 if(_pyfuncSer == NULL)
201 std::string errorDetails;
202 PyObject *new_stderr(newPyStdOut(errorDetails));
203 reqNode->setErrorDetails(errorDetails);
204 PySys_SetObject((char*)"stderr", new_stderr);
206 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
207 Py_DECREF(new_stderr);
208 throw Exception("Error during load");
210 if(_pyfuncUnser == NULL)
212 std::string errorDetails;
213 PyObject *new_stderr(newPyStdOut(errorDetails));
214 reqNode->setErrorDetails(errorDetails);
215 PySys_SetObject((char*)"stderr", new_stderr);
217 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
218 Py_DECREF(new_stderr);
219 throw Exception("Error during load");
221 if(_pyfuncSimpleSer == NULL)
223 std::string errorDetails;
224 PyObject *new_stderr(newPyStdOut(errorDetails));
225 reqNode->setErrorDetails(errorDetails);
226 PySys_SetObject((char*)"stderr", new_stderr);
228 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
229 Py_DECREF(new_stderr);
230 throw Exception("Error during load");
233 if(isInitializeRequested)
234 {//This one is called only once at initialization in the container if an init-script is specified.
237 std::string zeInitScriptKey(container->getProperty(HomogeneousPoolContainer::INITIALIZE_SCRIPT_KEY));
238 if(!zeInitScriptKey.empty())
239 pynode->executeAnotherPieceOfCode(zeInitScriptKey.c_str());
241 catch( const SALOME::SALOME_Exception& ex )
243 std::string msg="Exception on PythonNode::loadRemote python invocation of initializisation py script !";
245 msg += ex.details.text.in();
246 reqNode->setErrorDetails(msg);
247 throw Exception(msg);
249 DEBTRACE( "---------------End PyNode::loadRemote function---------------" );
253 std::string PythonEntry::GetContainerLog(const std::string& mode, Container *container, const Task *askingTask)
261 SalomeContainer *containerCast(dynamic_cast<SalomeContainer *>(container));
262 SalomeHPContainer *objContainer2(dynamic_cast<SalomeHPContainer *>(container));
265 Engines::Container_var objContainer(containerCast->getContainerPtr(askingTask));
266 CORBA::String_var logname = objContainer->logfilename();
269 std::string::size_type pos = msg.find(":");
270 msg=msg.substr(pos+1);
272 else if(objContainer2)
274 msg="Remote PythonNode is on HP Container : no log because no info of the location by definition of HP Container !";
278 msg="Not implemented yet for container log for that type of container !";
283 msg = "Container no longer reachable";
288 void PythonEntry::commonRemoteLoad(InlineNode *reqNode)
290 commonRemoteLoadPart1(reqNode);
291 bool isInitializeRequested;
292 Engines::Container_var objContainer(commonRemoteLoadPart2(reqNode,isInitializeRequested));
293 commonRemoteLoadPart3(reqNode,objContainer,isInitializeRequested);
296 PythonNode::PythonNode(const PythonNode& other, ComposedNode *father):InlineNode(other,father)
298 _implementation=IMPL_NAME;
301 _context=PyDict_New();
302 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
305 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
306 _errorDetails=msg.str();
307 throw Exception(msg.str());
312 PythonNode::PythonNode(const std::string& name):InlineNode(name)
314 _implementation=IMPL_NAME;
317 _context=PyDict_New();
318 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
321 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
322 _errorDetails=msg.str();
323 throw Exception(msg.str());
328 PythonNode::~PythonNode()
330 if(!CORBA::is_nil(_pynode))
332 _pynode->UnRegister();
336 void PythonNode::checkBasicConsistency() const throw(YACS::Exception)
338 DEBTRACE("checkBasicConsistency");
339 InlineNode::checkBasicConsistency();
343 res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
346 std::string error="";
347 PyObject* new_stderr = newPyStdOut(error);
348 PySys_SetObject((char*)"stderr", new_stderr);
350 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
351 Py_DECREF(new_stderr);
352 throw Exception(error);
359 void PythonNode::load()
361 DEBTRACE( "---------------PyNode::load function---------------" );
362 if(_mode==PythonNode::REMOTE_NAME)
368 void PythonNode::loadLocal()
370 DEBTRACE( "---------------PyNode::loadLocal function---------------" );
374 void PythonNode::loadRemote()
376 commonRemoteLoad(this);
379 void PythonNode::execute()
381 if(_mode==PythonNode::REMOTE_NAME)
387 void PythonNode::executeRemote()
389 DEBTRACE( "++++++++++++++ PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
391 throw Exception("DistributedPythonNode badly loaded");
393 if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
396 commonRemoteLoadPart2(this,dummy);
397 _pynode->assignNewCompiledCode(getScript().c_str());
400 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);
403 PyObject *args(0),*ob(0);
404 //===========================================================================
405 // Get inputs in input ports, build a Python dict and pickle it
406 //===========================================================================
408 std::list<InputPort *>::iterator iter2;
410 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); ++iter2)
412 InputPyPort *p=(InputPyPort *)*iter2;
414 PyDict_SetItemString(args,p->getName().c_str(),ob);
418 PyObject_Print(args,stderr,Py_PRINT_RAW);
421 PyObject *serializationInput(PyObject_CallFunctionObjArgs(_pyfuncSer,args,NULL));
423 //The pickled string may contain NULL characters so use PyString_AsStringAndSize
424 char *serializationInputC(0);
426 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
427 throw Exception("DistributedPythonNode problem in python pickle");
428 serializationInputCorba->length(len);
429 for(int i=0; i < len ; i++)
430 serializationInputCorba[i]=serializationInputC[i];
431 Py_DECREF(serializationInput);
434 //get the list of output argument names
435 std::list<OutputPort *>::iterator iter;
436 Engines::listofstring myseq;
437 myseq.length(getNumberOfOutputPorts());
439 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
441 OutputPyPort *p=(OutputPyPort *)*iter;
442 myseq[pos]=p->getName().c_str();
443 DEBTRACE( "port name: " << p->getName() );
444 DEBTRACE( "port kind: " << p->edGetType()->kind() );
445 DEBTRACE( "port pos : " << pos );
448 //===========================================================================
449 // Execute in remote Python node
450 //===========================================================================
451 DEBTRACE( "-----------------starting remote python invocation-----------------" );
452 Engines::pickledArgs_var resultCorba;
455 //pass outargsname and dict serialized
456 resultCorba=_pynode->execute(myseq,serializationInputCorba);
458 catch( const SALOME::SALOME_Exception& ex )
460 std::string msg="Exception on remote python invocation";
462 msg += ex.details.text.in();
464 throw Exception(msg);
466 DEBTRACE( "-----------------end of remote python invocation-----------------" );
467 //===========================================================================
468 // Get results, unpickle and put them in output ports
469 //===========================================================================
470 char *resultCorbaC=new char[resultCorba->length()+1];
471 resultCorbaC[resultCorba->length()]='\0';
472 for(int i=0;i<resultCorba->length();i++)
473 resultCorbaC[i]=resultCorba[i];
477 PyObject *args(0),*ob(0);
478 PyObject* resultPython=PyBytes_FromStringAndSize(resultCorbaC,resultCorba->length());
479 delete [] resultCorbaC;
480 args = PyTuple_New(1);
481 PyTuple_SetItem(args,0,resultPython);
482 PyObject *finalResult=PyObject_CallObject(_pyfuncUnser,args);
485 if (finalResult == NULL)
487 std::stringstream msg;
488 msg << "Conversion with pickle of output ports failed !";
489 msg << " : " << __FILE__ << ":" << __LINE__;
490 _errorDetails=msg.str();
491 throw YACS::ENGINE::ConversionException(msg.str());
494 DEBTRACE( "-----------------PythonNode::outputs-----------------" );
496 if(finalResult == Py_None)
498 else if(PyTuple_Check(finalResult))
499 nres=PyTuple_Size(finalResult);
501 if(getNumberOfOutputPorts() != nres)
503 std::string msg="Number of output arguments : Mismatch between definition and execution";
504 Py_DECREF(finalResult);
506 throw Exception(msg);
512 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
514 OutputPyPort *p=(OutputPyPort *)*iter;
515 DEBTRACE( "port name: " << p->getName() );
516 DEBTRACE( "port kind: " << p->edGetType()->kind() );
517 DEBTRACE( "port pos : " << pos );
518 if(PyTuple_Check(finalResult))
519 ob=PyTuple_GetItem(finalResult,pos) ;
522 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
526 Py_DECREF(finalResult);
528 catch(ConversionException& ex)
530 Py_DECREF(finalResult);
531 _errorDetails=ex.what();
536 if(!CORBA::is_nil(_pynode))
538 _pynode->UnRegister();
540 _pynode = Engines::PyScriptNode::_nil();
541 DEBTRACE( "++++++++++++++ ENDOF PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
544 void PythonNode::executeLocal()
546 DEBTRACE( "++++++++++++++ PyNode::executeLocal: " << getName() << " ++++++++++++++++++++" );
550 DEBTRACE( "---------------PyNode::inputs---------------" );
551 list<InputPort *>::iterator iter2;
552 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
554 InputPyPort *p=(InputPyPort *)*iter2;
555 DEBTRACE( "port name: " << p->getName() );
556 DEBTRACE( "port kind: " << p->edGetType()->kind() );
557 PyObject* ob=p->getPyObj();
558 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
560 PyObject_Print(ob,stderr,Py_PRINT_RAW);
563 int ier=PyDict_SetItemString(_context,p->getName().c_str(),ob);
564 DEBTRACE( "after PyDict_SetItemString:ob refcnt: " << ob->ob_refcnt );
567 DEBTRACE( "---------------End PyNode::inputs---------------" );
570 DEBTRACE( "----------------PyNode::calculation---------------" );
572 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
574 std::ostringstream stream;
575 stream << "/tmp/PythonNode_";
578 PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
582 PyObject* new_stderr = newPyStdOut(_errorDetails);
583 PySys_SetObject((char*)"stderr", new_stderr);
585 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
586 Py_DECREF(new_stderr);
587 throw Exception("Error during execution");
589 PyObject *res = PyEval_EvalCode( code, _context, _context);
593 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
596 if(PyErr_Occurred ())
599 PyObject* new_stderr = newPyStdOut(_errorDetails);
600 PySys_SetObject((char*)"stderr", new_stderr);
601 ofstream errorfile(stream.str().c_str());
602 if (errorfile.is_open())
604 errorfile << _script;
608 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
609 Py_DECREF(new_stderr);
610 throw Exception("Error during execution");
613 DEBTRACE( "-----------------PyNode::outputs-----------------" );
614 list<OutputPort *>::iterator iter;
617 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
619 OutputPyPort *p=(OutputPyPort *)*iter;
620 DEBTRACE( "port name: " << p->getName() );
621 DEBTRACE( "port kind: " << p->edGetType()->kind() );
622 PyObject *ob=PyDict_GetItemString(_context,p->getName().c_str());
625 std::string msg="Error during execution: there is no variable ";
626 msg=msg+p->getName()+" in node context";
628 throw Exception(msg);
630 DEBTRACE( "PyNode::outputs::ob refcnt: " << ob->ob_refcnt );
632 PyObject_Print(ob,stderr,Py_PRINT_RAW);
638 catch(ConversionException& ex)
640 _errorDetails=ex.what();
644 DEBTRACE( "-----------------End PyNode::outputs-----------------" );
646 DEBTRACE( "++++++++++++++ End PyNode::execute: " << getName() << " ++++++++++++++++++++" );
649 std::string PythonNode::getContainerLog()
651 return PythonEntry::GetContainerLog(_mode,_container,this);
654 void PythonNode::shutdown(int level)
656 DEBTRACE("PythonNode::shutdown " << level);
657 if(_mode=="local")return;
660 if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
661 _pynode=Engines::PyScriptNode::_nil();
662 _container->shutdown(level);
666 Node *PythonNode::simpleClone(ComposedNode *father, bool editionOnly) const
668 return new PythonNode(*this,father);
671 void PythonNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
673 if(!CORBA::is_nil(_pynode))
674 _pynode->UnRegister();
675 objContainer->cleanAllPyScripts();
676 _pynode=objContainer->createPyScriptNode(getName().c_str(),getScript().c_str());
680 Engines::PyNodeBase_var PythonNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
682 Engines::PyScriptNode_var ret(objContainer->getDefaultPyScriptNode(getName().c_str()));
683 if(!CORBA::is_nil(ret))
687 return Engines::PyNodeBase::_narrow(ret);
690 void PythonNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
692 if(!CORBA::is_nil(_pynode))
694 Engines::PyScriptNode_var tmpp(Engines::PyScriptNode::_narrow(remoteInterp));
695 if(_pynode->_is_equivalent(tmpp))
697 _pynode->UnRegister();
701 if(!CORBA::is_nil(_pynode))
702 _pynode->UnRegister();
703 _pynode=Engines::PyScriptNode::_narrow(remoteInterp);
706 Engines::PyNodeBase_var PythonNode::getRemoteInterpreterHandle()
708 return Engines::PyNodeBase::_narrow(_pynode);
711 //! Create a new node of same type with a given name
712 PythonNode* PythonNode::cloneNode(const std::string& name)
714 PythonNode* n=new PythonNode(name);
715 n->setScript(_script);
716 list<InputPort *>::iterator iter;
717 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
719 InputPyPort *p=(InputPyPort *)*iter;
720 DEBTRACE( "port name: " << p->getName() );
721 DEBTRACE( "port kind: " << p->edGetType()->kind() );
722 n->edAddInputPort(p->getName(),p->edGetType());
724 list<OutputPort *>::iterator iter2;
725 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
727 OutputPyPort *p=(OutputPyPort *)*iter2;
728 DEBTRACE( "port name: " << p->getName() );
729 DEBTRACE( "port kind: " << p->edGetType()->kind() );
730 n->edAddOutputPort(p->getName(),p->edGetType());
735 void PythonNode::applyDPLScope(ComposedNode *gfn)
737 std::vector< std::pair<std::string,int> > ret(getDPLScopeInfo(gfn));
744 std::size_t sz(ret.size());
746 for(std::size_t i=0;i<sz;i++)
748 const std::pair<std::string,int>& p(ret[i]);
749 PyObject *elt(PyTuple_New(2));
750 PyTuple_SetItem(elt,0,PyUnicode_FromString(p.first.c_str()));
751 PyTuple_SetItem(elt,1,PyLong_FromLong(p.second));
752 PyList_SetItem(ob,i,elt);
755 if(_mode==REMOTE_NAME)
757 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);
760 PyObject *serializationInput(PyObject_CallFunctionObjArgs(_pyfuncSimpleSer,ob,NULL));
762 char *serializationInputC(0);
764 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
765 throw Exception("DistributedPythonNode problem in python pickle");
766 serializationInputCorba->length(len);
767 for(int i=0; i < len ; i++)
768 serializationInputCorba[i]=serializationInputC[i];
769 Py_XDECREF(serializationInput);
771 _pynode->defineNewCustomVar(DPL_INFO_NAME,serializationInputCorba);
776 PyDict_SetItemString(_context,DPL_INFO_NAME,ob);
781 PyFuncNode::PyFuncNode(const PyFuncNode& other, ComposedNode *father):InlineFuncNode(other,father),_pyfunc(0)
783 _implementation = PythonNode::IMPL_NAME;
786 _context=PyDict_New();
787 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
788 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
791 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
792 _errorDetails=msg.str();
793 throw Exception(msg.str());
798 PyFuncNode::PyFuncNode(const std::string& name): InlineFuncNode(name),_pyfunc(0)
801 _implementation = PythonNode::IMPL_NAME;
802 DEBTRACE( "PyFuncNode::PyFuncNode " << name );
805 _context=PyDict_New();
806 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
807 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
810 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
811 _errorDetails=msg.str();
812 throw Exception(msg.str());
817 PyFuncNode::~PyFuncNode()
819 if(!CORBA::is_nil(_pynode))
821 _pynode->UnRegister();
825 void PyFuncNode::init(bool start)
827 initCommonPartWithoutStateManagement(start);
828 if(_state == YACS::DISABLED)
830 exDisabledState(); // to refresh propagation of DISABLED state
833 if(start) //complete initialization
834 setState(YACS::READY);
835 else if(_state > YACS::LOADED)// WARNING FuncNode has internal vars (CEA usecase) ! Partial initialization (inside a loop). Exclusivity of funcNode.
836 setState(YACS::TORECONNECT);
839 void PyFuncNode::checkBasicConsistency() const throw(YACS::Exception)
841 DEBTRACE("checkBasicConsistency");
842 InlineFuncNode::checkBasicConsistency();
846 res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
849 std::string error="";
850 PyObject* new_stderr = newPyStdOut(error);
851 PySys_SetObject((char*)"stderr", new_stderr);
853 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
854 Py_DECREF(new_stderr);
855 throw Exception(error);
862 void PyFuncNode::load()
864 DEBTRACE( "---------------PyfuncNode::load function---------------" );
865 if(_mode==PythonNode::REMOTE_NAME)
871 void PyFuncNode::loadRemote()
873 commonRemoteLoad(this);
876 void PyFuncNode::loadLocal()
878 DEBTRACE( "---------------PyFuncNode::load function " << getName() << " ---------------" );
882 list<OutputPort *>::iterator iter;
883 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
885 OutputPyPort *p=(OutputPyPort *)*iter;
886 DEBTRACE( "port name: " << p->getName() );
887 DEBTRACE( "port kind: " << p->edGetType()->kind() );
893 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
895 std::ostringstream stream;
896 stream << "/tmp/PythonNode_";
899 PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
903 PyObject* new_stderr = newPyStdOut(_errorDetails);
904 PySys_SetObject((char*)"stderr", new_stderr);
906 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
907 Py_DECREF(new_stderr);
908 throw Exception("Error during execution");
910 PyObject *res = PyEval_EvalCode( code, _context, _context);
914 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
915 if(PyErr_Occurred ())
918 PyObject* new_stderr = newPyStdOut(_errorDetails);
919 PySys_SetObject((char*)"stderr", new_stderr);
920 ofstream errorfile(stream.str().c_str());
921 if (errorfile.is_open())
923 errorfile << _script;
927 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
928 Py_DECREF(new_stderr);
929 throw Exception("Error during execution");
932 _pyfunc=PyDict_GetItemString(_context,_fname.c_str());
933 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
937 PyObject* new_stderr = newPyStdOut(_errorDetails);
938 PySys_SetObject((char*)"stderr", new_stderr);
940 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
941 Py_DECREF(new_stderr);
942 throw Exception("Error during execution");
944 DEBTRACE( "---------------End PyFuncNode::load function---------------" );
948 void PyFuncNode::execute()
950 if(_mode==PythonNode::REMOTE_NAME)
956 void PyFuncNode::executeRemote()
958 DEBTRACE( "++++++++++++++ PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
960 throw Exception("DistributedPythonNode badly loaded");
962 if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
965 commonRemoteLoadPart2(this,dummy);
966 _pynode->executeAnotherPieceOfCode(getScript().c_str());
969 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);;
973 //===========================================================================
974 // Get inputs in input ports, build a Python tuple and pickle it
975 //===========================================================================
976 PyObject *args(PyTuple_New(getNumberOfInputPorts()));
978 for(std::list<InputPort *>::iterator iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++,pos++)
980 InputPyPort *p=(InputPyPort *)*iter2;
983 PyTuple_SetItem(args,pos,ob);
986 PyObject_Print(args,stderr,Py_PRINT_RAW);
989 PyObject *serializationInput=PyObject_CallObject(_pyfuncSer,args);
991 //The pickled string may contain NULL characters so use PyString_AsStringAndSize
992 char *serializationInputC(0);
994 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
995 throw Exception("DistributedPythonNode problem in python pickle");
997 serializationInputCorba->length(len);
998 for(int i=0; i < len ; i++)
999 serializationInputCorba[i]=serializationInputC[i];
1000 Py_DECREF(serializationInput);
1003 //===========================================================================
1004 // Execute in remote Python node
1005 //===========================================================================
1006 DEBTRACE( "-----------------starting remote python invocation-----------------" );
1007 Engines::pickledArgs_var resultCorba;
1010 resultCorba=_pynode->execute(getFname().c_str(),serializationInputCorba);
1012 catch( const SALOME::SALOME_Exception& ex )
1014 std::string msg="Exception on remote python invocation";
1016 msg += ex.details.text.in();
1018 throw Exception(msg);
1020 DEBTRACE( "-----------------end of remote python invocation-----------------" );
1021 //===========================================================================
1022 // Get results, unpickle and put them in output ports
1023 //===========================================================================
1024 char *resultCorbaC=new char[resultCorba->length()+1];
1025 resultCorbaC[resultCorba->length()]='\0';
1026 for(int i=0;i<resultCorba->length();i++)
1027 resultCorbaC[i]=resultCorba[i];
1032 PyObject *resultPython(PyBytes_FromStringAndSize(resultCorbaC,resultCorba->length()));
1033 delete [] resultCorbaC;
1034 PyObject *args(PyTuple_New(1)),*ob(0);
1035 PyTuple_SetItem(args,0,resultPython);
1036 PyObject *finalResult=PyObject_CallObject(_pyfuncUnser,args);
1039 DEBTRACE( "-----------------PythonNode::outputs-----------------" );
1041 if(finalResult == Py_None)
1043 else if(PyTuple_Check(finalResult))
1044 nres=PyTuple_Size(finalResult);
1046 if(getNumberOfOutputPorts() != nres)
1048 std::string msg="Number of output arguments : Mismatch between definition and execution";
1049 Py_DECREF(finalResult);
1051 throw Exception(msg);
1057 for(std::list<OutputPort *>::iterator iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++, pos++)
1059 OutputPyPort *p=(OutputPyPort *)*iter;
1060 DEBTRACE( "port name: " << p->getName() );
1061 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1062 DEBTRACE( "port pos : " << pos );
1063 if(PyTuple_Check(finalResult))
1064 ob=PyTuple_GetItem(finalResult,pos) ;
1067 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1070 Py_DECREF(finalResult);
1072 catch(ConversionException& ex)
1074 Py_DECREF(finalResult);
1075 _errorDetails=ex.what();
1080 DEBTRACE( "++++++++++++++ ENDOF PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
1083 void PyFuncNode::executeLocal()
1085 DEBTRACE( "++++++++++++++ PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1089 if(!_pyfunc)throw Exception("PyFuncNode badly loaded");
1092 DEBTRACE( "---------------PyFuncNode::inputs---------------" );
1093 PyObject* args = PyTuple_New(getNumberOfInputPorts()) ;
1094 list<InputPort *>::iterator iter2;
1095 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
1097 InputPyPort *p=(InputPyPort *)*iter2;
1098 DEBTRACE( "port name: " << p->getName() );
1099 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1102 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1105 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1107 PyTuple_SetItem(args,pos,ob);
1108 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1111 DEBTRACE( "---------------End PyFuncNode::inputs---------------" );
1113 DEBTRACE( "----------------PyFuncNode::calculation---------------" );
1115 PyObject_Print(_pyfunc,stderr,Py_PRINT_RAW);
1117 PyObject_Print(args,stderr,Py_PRINT_RAW);
1120 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1121 PyObject* result = PyObject_CallObject( _pyfunc , args ) ;
1122 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1129 PyObject* new_stderr = newPyStdOut(_errorDetails);
1130 PySys_SetObject((char*)"stderr", new_stderr);
1131 std::ostringstream stream;
1132 stream << "/tmp/PythonNode_";
1134 ofstream errorfile(stream.str().c_str());
1135 if (errorfile.is_open())
1137 errorfile << _script;
1141 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1142 Py_DECREF(new_stderr);
1143 throw Exception("Error during execution");
1145 DEBTRACE( "----------------End PyFuncNode::calculation---------------" );
1147 DEBTRACE( "-----------------PyFuncNode::outputs-----------------" );
1149 if(result == Py_None)
1151 else if(PyTuple_Check(result))
1152 nres=PyTuple_Size(result);
1154 if(getNumberOfOutputPorts() != nres)
1156 std::string msg="Number of output arguments : Mismatch between definition and execution";
1159 throw Exception(msg);
1164 PyObject_Print(result,stderr,Py_PRINT_RAW);
1167 list<OutputPort *>::iterator iter;
1170 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
1172 OutputPyPort *p=(OutputPyPort *)*iter;
1173 DEBTRACE( "port name: " << p->getName() );
1174 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1175 DEBTRACE( "port pos : " << pos );
1176 if(PyTuple_Check(result))ob=PyTuple_GetItem(result,pos) ;
1178 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1180 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1187 catch(ConversionException& ex)
1190 _errorDetails=ex.what();
1193 DEBTRACE( "-----------------End PyFuncNode::outputs-----------------" );
1196 DEBTRACE( "++++++++++++++ End PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1199 Node *PyFuncNode::simpleClone(ComposedNode *father, bool editionOnly) const
1201 return new PyFuncNode(*this,father);
1204 void PyFuncNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
1206 if(!CORBA::is_nil(_pynode))
1207 _pynode->UnRegister();
1208 _pynode=objContainer->createPyNode(getName().c_str(),getScript().c_str());
1211 Engines::PyNodeBase_var PyFuncNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
1213 Engines::PyNode_var ret(objContainer->getDefaultPyNode(getName().c_str()));
1214 if(!CORBA::is_nil(ret))
1218 return Engines::PyNodeBase::_narrow(ret);
1221 void PyFuncNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
1223 if(!CORBA::is_nil(_pynode))
1225 Engines::PyNode_var tmpp(Engines::PyNode::_narrow(remoteInterp));
1226 if(_pynode->_is_equivalent(tmpp))
1229 if(!CORBA::is_nil(_pynode))
1230 _pynode->UnRegister();
1231 _pynode=Engines::PyNode::_narrow(remoteInterp);
1234 Engines::PyNodeBase_var PyFuncNode::getRemoteInterpreterHandle()
1236 return Engines::PyNodeBase::_narrow(_pynode);
1239 //! Create a new node of same type with a given name
1240 PyFuncNode* PyFuncNode::cloneNode(const std::string& name)
1242 PyFuncNode* n=new PyFuncNode(name);
1243 n->setScript(_script);
1244 n->setFname(_fname);
1245 list<InputPort *>::iterator iter;
1246 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
1248 InputPyPort *p=(InputPyPort *)*iter;
1249 n->edAddInputPort(p->getName(),p->edGetType());
1251 list<OutputPort *>::iterator iter2;
1252 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
1254 OutputPyPort *p=(OutputPyPort *)*iter2;
1255 n->edAddOutputPort(p->getName(),p->edGetType());
1260 std::string PyFuncNode::getContainerLog()
1262 return PythonEntry::GetContainerLog(_mode,_container,this);
1265 void PyFuncNode::shutdown(int level)
1267 DEBTRACE("PyFuncNode::shutdown " << level);
1268 if(_mode=="local")return;
1271 if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
1272 _pynode=Engines::PyNode::_nil();
1273 _container->shutdown(level);