1 // Copyright (C) 2006-2015 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 cPickle\n"
52 "def pickleForVarSimplePyth2009(val):\n"
53 " return cPickle.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 cPickle\n"
60 "def pickleForDistPyth2009(kws):\n"
61 " return cPickle.dumps(((),kws),-1)\n"
63 "def unPickleForDistPyth2009(st):\n"
64 " args=cPickle.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 cPickle\n"
72 "def pickleForDistPyth2009(*args,**kws):\n"
73 " return cPickle.dumps((args,kws),-1)\n"
75 "def unPickleForDistPyth2009(st):\n"
76 " args=cPickle.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 SalomeHPContainer *containerCast1(dynamic_cast<SalomeHPContainer *>(container));
132 objContainer=containerCast0->getContainerPtr(reqNode);
133 else if(containerCast1)
135 YACS::BASES::AutoCppPtr<SalomeContainerTmpForHP> tmpCont(SalomeContainerTmpForHP::BuildFrom(containerCast1,reqNode));
136 objContainer=tmpCont->getContainerPtr(reqNode);
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;
147 createRemoteAdaptedPyInterpretor(objContainer);
151 Engines::PyNodeBase_var dftPyScript(retrieveDftRemotePyInterpretorIfAny(objContainer));
152 if(CORBA::is_nil(dftPyScript))
154 isInitializeRequested=true;
155 createRemoteAdaptedPyInterpretor(objContainer);
158 assignRemotePyInterpretor(dftPyScript);
161 catch( const SALOME::SALOME_Exception& ex )
163 std::string msg="Exception on remote python node creation ";
165 msg += ex.details.text.in();
166 reqNode->setErrorDetails(msg);
167 throw Exception(msg);
169 Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
170 if(CORBA::is_nil(pynode))
171 throw Exception("In PythonNode the ref in NULL ! ");
175 void PythonEntry::commonRemoteLoadPart3(InlineNode *reqNode, Engines::Container_ptr objContainer, bool isInitializeRequested)
177 Container *container(reqNode->getContainer());
178 Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
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)
187 std::string errorDetails;
188 PyObject* new_stderr = newPyStdOut(errorDetails);
189 reqNode->setErrorDetails(errorDetails);
190 PySys_SetObject((char*)"stderr", new_stderr);
192 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
193 Py_DECREF(new_stderr);
194 throw Exception("Error during load");
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)
202 std::string errorDetails;
203 PyObject *new_stderr(newPyStdOut(errorDetails));
204 reqNode->setErrorDetails(errorDetails);
205 PySys_SetObject((char*)"stderr", new_stderr);
207 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
208 Py_DECREF(new_stderr);
209 throw Exception("Error during load");
211 if(_pyfuncUnser == NULL)
213 std::string errorDetails;
214 PyObject *new_stderr(newPyStdOut(errorDetails));
215 reqNode->setErrorDetails(errorDetails);
216 PySys_SetObject((char*)"stderr", new_stderr);
218 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
219 Py_DECREF(new_stderr);
220 throw Exception("Error during load");
222 if(_pyfuncSimpleSer == NULL)
224 std::string errorDetails;
225 PyObject *new_stderr(newPyStdOut(errorDetails));
226 reqNode->setErrorDetails(errorDetails);
227 PySys_SetObject((char*)"stderr", new_stderr);
229 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
230 Py_DECREF(new_stderr);
231 throw Exception("Error during load");
234 if(isInitializeRequested)
235 {//This one is called only once at initialization in the container if an init-script is specified.
238 std::string zeInitScriptKey(container->getProperty(HomogeneousPoolContainer::INITIALIZE_SCRIPT_KEY));
239 if(!zeInitScriptKey.empty())
240 pynode->executeAnotherPieceOfCode(zeInitScriptKey.c_str());
242 catch( const SALOME::SALOME_Exception& ex )
244 std::string msg="Exception on PythonNode::loadRemote python invocation of initializisation py script !";
246 msg += ex.details.text.in();
247 reqNode->setErrorDetails(msg);
248 throw Exception(msg);
250 DEBTRACE( "---------------End PyNode::loadRemote function---------------" );
254 std::string PythonEntry::GetContainerLog(const std::string& mode, Container *container, const Task *askingTask)
262 SalomeContainer *containerCast(dynamic_cast<SalomeContainer *>(container));
263 SalomeHPContainer *objContainer2(dynamic_cast<SalomeHPContainer *>(container));
266 Engines::Container_var objContainer(containerCast->getContainerPtr(askingTask));
267 CORBA::String_var logname = objContainer->logfilename();
270 std::string::size_type pos = msg.find(":");
271 msg=msg.substr(pos+1);
273 else if(objContainer2)
275 msg="Remote PythonNode is on HP Container : no log because no info of the location by definition of HP Container !";
279 msg="Not implemented yet for container log for that type of container !";
284 msg = "Container no longer reachable";
289 void PythonEntry::commonRemoteLoad(InlineNode *reqNode)
291 commonRemoteLoadPart1(reqNode);
292 bool isInitializeRequested;
293 Engines::Container_var objContainer(commonRemoteLoadPart2(reqNode,isInitializeRequested));
294 commonRemoteLoadPart3(reqNode,objContainer,isInitializeRequested);
297 PythonNode::PythonNode(const PythonNode& other, ComposedNode *father):InlineNode(other,father)
299 _implementation=IMPL_NAME;
302 _context=PyDict_New();
303 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
306 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
307 _errorDetails=msg.str();
308 throw Exception(msg.str());
313 PythonNode::PythonNode(const std::string& name):InlineNode(name)
315 _implementation=IMPL_NAME;
318 _context=PyDict_New();
319 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
322 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
323 _errorDetails=msg.str();
324 throw Exception(msg.str());
329 PythonNode::~PythonNode()
331 if(!CORBA::is_nil(_pynode))
333 _pynode->UnRegister();
337 void PythonNode::checkBasicConsistency() const throw(YACS::Exception)
339 DEBTRACE("checkBasicConsistency");
340 InlineNode::checkBasicConsistency();
344 res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
347 std::string error="";
348 PyObject* new_stderr = newPyStdOut(error);
349 PySys_SetObject((char*)"stderr", new_stderr);
351 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
352 Py_DECREF(new_stderr);
353 throw Exception(error);
360 void PythonNode::load()
362 DEBTRACE( "---------------PyNode::load function---------------" );
363 if(_mode==PythonNode::REMOTE_NAME)
369 void PythonNode::loadLocal()
371 DEBTRACE( "---------------PyNode::loadLocal function---------------" );
375 void PythonNode::loadRemote()
377 commonRemoteLoad(this);
380 void PythonNode::execute()
382 if(_mode==PythonNode::REMOTE_NAME)
388 void PythonNode::executeRemote()
390 DEBTRACE( "++++++++++++++ PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
392 throw Exception("DistributedPythonNode badly loaded");
394 if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
397 commonRemoteLoadPart2(this,dummy);
398 _pynode->assignNewCompiledCode(getScript().c_str());
401 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);
404 PyObject *args(0),*ob(0);
405 //===========================================================================
406 // Get inputs in input ports, build a Python dict and pickle it
407 //===========================================================================
409 std::list<InputPort *>::iterator iter2;
411 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); ++iter2)
413 InputPyPort *p=(InputPyPort *)*iter2;
415 PyDict_SetItemString(args,p->getName().c_str(),ob);
419 PyObject_Print(args,stderr,Py_PRINT_RAW);
422 PyObject *serializationInput(PyObject_CallFunctionObjArgs(_pyfuncSer,args,NULL));
424 //The pickled string may contain NULL characters so use PyString_AsStringAndSize
425 char *serializationInputC(0);
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);
435 //get the list of output argument names
436 std::list<OutputPort *>::iterator iter;
437 Engines::listofstring myseq;
438 myseq.length(getNumberOfOutputPorts());
440 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
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 );
449 //===========================================================================
450 // Execute in remote Python node
451 //===========================================================================
452 DEBTRACE( "-----------------starting remote python invocation-----------------" );
453 Engines::pickledArgs_var resultCorba;
456 //pass outargsname and dict serialized
457 resultCorba=_pynode->execute(myseq,serializationInputCorba);
459 catch( const SALOME::SALOME_Exception& ex )
461 std::string msg="Exception on remote python invocation";
463 msg += ex.details.text.in();
465 throw Exception(msg);
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];
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);
486 if (finalResult == NULL)
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());
495 DEBTRACE( "-----------------PythonNode::outputs-----------------" );
497 if(finalResult == Py_None)
499 else if(PyTuple_Check(finalResult))
500 nres=PyTuple_Size(finalResult);
502 if(getNumberOfOutputPorts() != nres)
504 std::string msg="Number of output arguments : Mismatch between definition and execution";
505 Py_DECREF(finalResult);
507 throw Exception(msg);
513 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
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) ;
523 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
527 Py_DECREF(finalResult);
529 catch(ConversionException& ex)
531 Py_DECREF(finalResult);
532 _errorDetails=ex.what();
536 DEBTRACE( "++++++++++++++ ENDOF PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
539 void PythonNode::executeLocal()
541 DEBTRACE( "++++++++++++++ PyNode::executeLocal: " << getName() << " ++++++++++++++++++++" );
545 DEBTRACE( "---------------PyNode::inputs---------------" );
546 list<InputPort *>::iterator iter2;
547 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
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 );
555 PyObject_Print(ob,stderr,Py_PRINT_RAW);
558 int ier=PyDict_SetItemString(_context,p->getName().c_str(),ob);
559 DEBTRACE( "after PyDict_SetItemString:ob refcnt: " << ob->ob_refcnt );
562 DEBTRACE( "---------------End PyNode::inputs---------------" );
565 DEBTRACE( "----------------PyNode::calculation---------------" );
567 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
569 std::ostringstream stream;
570 stream << "/tmp/PythonNode_";
573 PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
577 PyObject* new_stderr = newPyStdOut(_errorDetails);
578 PySys_SetObject((char*)"stderr", new_stderr);
580 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
581 Py_DECREF(new_stderr);
582 throw Exception("Error during execution");
584 PyObject *res = PyEval_EvalCode((PyCodeObject *)code, _context, _context);
588 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
591 if(PyErr_Occurred ())
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())
599 errorfile << _script;
603 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
604 Py_DECREF(new_stderr);
605 throw Exception("Error during execution");
608 DEBTRACE( "-----------------PyNode::outputs-----------------" );
609 list<OutputPort *>::iterator iter;
612 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
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());
620 std::string msg="Error during execution: there is no variable ";
621 msg=msg+p->getName()+" in node context";
623 throw Exception(msg);
625 DEBTRACE( "PyNode::outputs::ob refcnt: " << ob->ob_refcnt );
627 PyObject_Print(ob,stderr,Py_PRINT_RAW);
633 catch(ConversionException& ex)
635 _errorDetails=ex.what();
639 DEBTRACE( "-----------------End PyNode::outputs-----------------" );
641 DEBTRACE( "++++++++++++++ End PyNode::execute: " << getName() << " ++++++++++++++++++++" );
644 std::string PythonNode::getContainerLog()
646 return PythonEntry::GetContainerLog(_mode,_container,this);
649 void PythonNode::shutdown(int level)
651 DEBTRACE("PythonNode::shutdown " << level);
652 if(_mode=="local")return;
655 if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
656 _pynode=Engines::PyScriptNode::_nil();
657 _container->shutdown(level);
661 Node *PythonNode::simpleClone(ComposedNode *father, bool editionOnly) const
663 return new PythonNode(*this,father);
666 void PythonNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
668 if(!CORBA::is_nil(_pynode))
669 _pynode->UnRegister();
670 _pynode=objContainer->createPyScriptNode(getName().c_str(),getScript().c_str());
673 Engines::PyNodeBase_var PythonNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
675 Engines::PyScriptNode_var ret(objContainer->getDefaultPyScriptNode(getName().c_str()));
676 if(!CORBA::is_nil(ret))
680 return Engines::PyNodeBase::_narrow(ret);
683 void PythonNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
685 if(!CORBA::is_nil(_pynode))
687 Engines::PyScriptNode_var tmpp(Engines::PyScriptNode::_narrow(remoteInterp));
688 if(_pynode->_is_equivalent(tmpp))
691 if(!CORBA::is_nil(_pynode))
692 _pynode->UnRegister();
693 _pynode=Engines::PyScriptNode::_narrow(remoteInterp);
696 Engines::PyNodeBase_var PythonNode::getRemoteInterpreterHandle()
698 return Engines::PyNodeBase::_narrow(_pynode);
701 //! Create a new node of same type with a given name
702 PythonNode* PythonNode::cloneNode(const std::string& name)
704 PythonNode* n=new PythonNode(name);
705 n->setScript(_script);
706 list<InputPort *>::iterator iter;
707 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
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());
714 list<OutputPort *>::iterator iter2;
715 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
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());
725 void PythonNode::applyDPLScope(ComposedNode *gfn)
727 std::vector< std::pair<std::string,int> > ret(getDPLScopeInfo(gfn));
734 std::size_t sz(ret.size());
736 for(std::size_t i=0;i<sz;i++)
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);
745 if(_mode==REMOTE_NAME)
747 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);
750 PyObject *serializationInput(PyObject_CallFunctionObjArgs(_pyfuncSimpleSer,ob,NULL));
752 char *serializationInputC(0);
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);
761 _pynode->defineNewCustomVar(DPL_INFO_NAME,serializationInputCorba);
766 PyDict_SetItemString(_context,DPL_INFO_NAME,ob);
771 PyFuncNode::PyFuncNode(const PyFuncNode& other, ComposedNode *father):InlineFuncNode(other,father),_pyfunc(0)
773 _implementation = PythonNode::IMPL_NAME;
776 _context=PyDict_New();
777 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
778 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
781 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
782 _errorDetails=msg.str();
783 throw Exception(msg.str());
788 PyFuncNode::PyFuncNode(const std::string& name): InlineFuncNode(name),_pyfunc(0)
791 _implementation = PythonNode::IMPL_NAME;
792 DEBTRACE( "PyFuncNode::PyFuncNode " << name );
795 _context=PyDict_New();
796 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
797 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
800 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
801 _errorDetails=msg.str();
802 throw Exception(msg.str());
807 PyFuncNode::~PyFuncNode()
809 if(!CORBA::is_nil(_pynode))
811 _pynode->UnRegister();
815 void PyFuncNode::init(bool start)
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);
824 void PyFuncNode::checkBasicConsistency() const throw(YACS::Exception)
826 DEBTRACE("checkBasicConsistency");
827 InlineFuncNode::checkBasicConsistency();
831 res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
834 std::string error="";
835 PyObject* new_stderr = newPyStdOut(error);
836 PySys_SetObject((char*)"stderr", new_stderr);
838 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
839 Py_DECREF(new_stderr);
840 throw Exception(error);
847 void PyFuncNode::load()
849 DEBTRACE( "---------------PyfuncNode::load function---------------" );
850 if(_mode==PythonNode::REMOTE_NAME)
856 void PyFuncNode::loadRemote()
858 commonRemoteLoad(this);
861 void PyFuncNode::loadLocal()
863 DEBTRACE( "---------------PyFuncNode::load function " << getName() << " ---------------" );
867 list<OutputPort *>::iterator iter;
868 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
870 OutputPyPort *p=(OutputPyPort *)*iter;
871 DEBTRACE( "port name: " << p->getName() );
872 DEBTRACE( "port kind: " << p->edGetType()->kind() );
878 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
880 std::ostringstream stream;
881 stream << "/tmp/PythonNode_";
884 PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
888 PyObject* new_stderr = newPyStdOut(_errorDetails);
889 PySys_SetObject((char*)"stderr", new_stderr);
891 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
892 Py_DECREF(new_stderr);
893 throw Exception("Error during execution");
895 PyObject *res = PyEval_EvalCode((PyCodeObject *)code, _context, _context);
899 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
900 if(PyErr_Occurred ())
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())
908 errorfile << _script;
912 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
913 Py_DECREF(new_stderr);
914 throw Exception("Error during execution");
917 _pyfunc=PyDict_GetItemString(_context,_fname.c_str());
918 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
922 PyObject* new_stderr = newPyStdOut(_errorDetails);
923 PySys_SetObject((char*)"stderr", new_stderr);
925 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
926 Py_DECREF(new_stderr);
927 throw Exception("Error during execution");
929 DEBTRACE( "---------------End PyFuncNode::load function---------------" );
933 void PyFuncNode::execute()
935 if(_mode==PythonNode::REMOTE_NAME)
941 void PyFuncNode::executeRemote()
943 DEBTRACE( "++++++++++++++ PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
945 throw Exception("DistributedPythonNode badly loaded");
947 if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
950 commonRemoteLoadPart2(this,dummy);
951 _pynode->executeAnotherPieceOfCode(getScript().c_str());
954 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);;
958 //===========================================================================
959 // Get inputs in input ports, build a Python tuple and pickle it
960 //===========================================================================
961 PyObject *args(PyTuple_New(getNumberOfInputPorts()));
963 for(std::list<InputPort *>::iterator iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++,pos++)
965 InputPyPort *p=(InputPyPort *)*iter2;
968 PyTuple_SetItem(args,pos,ob);
971 PyObject_Print(args,stderr,Py_PRINT_RAW);
974 PyObject *serializationInput=PyObject_CallObject(_pyfuncSer,args);
976 //The pickled string may contain NULL characters so use PyString_AsStringAndSize
977 char *serializationInputC(0);
979 if (PyString_AsStringAndSize(serializationInput, &serializationInputC, &len))
980 throw Exception("DistributedPythonNode problem in python pickle");
982 serializationInputCorba->length(len);
983 for(int i=0; i < len ; i++)
984 serializationInputCorba[i]=serializationInputC[i];
985 Py_DECREF(serializationInput);
988 //===========================================================================
989 // Execute in remote Python node
990 //===========================================================================
991 DEBTRACE( "-----------------starting remote python invocation-----------------" );
992 Engines::pickledArgs_var resultCorba;
995 resultCorba=_pynode->execute(getFname().c_str(),serializationInputCorba);
997 catch( const SALOME::SALOME_Exception& ex )
999 std::string msg="Exception on remote python invocation";
1001 msg += ex.details.text.in();
1003 throw Exception(msg);
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];
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);
1024 DEBTRACE( "-----------------PythonNode::outputs-----------------" );
1026 if(finalResult == Py_None)
1028 else if(PyTuple_Check(finalResult))
1029 nres=PyTuple_Size(finalResult);
1031 if(getNumberOfOutputPorts() != nres)
1033 std::string msg="Number of output arguments : Mismatch between definition and execution";
1034 Py_DECREF(finalResult);
1036 throw Exception(msg);
1042 for(std::list<OutputPort *>::iterator iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++, pos++)
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) ;
1052 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1055 Py_DECREF(finalResult);
1057 catch(ConversionException& ex)
1059 Py_DECREF(finalResult);
1060 _errorDetails=ex.what();
1065 DEBTRACE( "++++++++++++++ ENDOF PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
1068 void PyFuncNode::executeLocal()
1070 DEBTRACE( "++++++++++++++ PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1074 if(!_pyfunc)throw Exception("PyFuncNode badly loaded");
1077 DEBTRACE( "---------------PyFuncNode::inputs---------------" );
1078 PyObject* args = PyTuple_New(getNumberOfInputPorts()) ;
1079 list<InputPort *>::iterator iter2;
1080 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
1082 InputPyPort *p=(InputPyPort *)*iter2;
1083 DEBTRACE( "port name: " << p->getName() );
1084 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1087 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1090 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1092 PyTuple_SetItem(args,pos,ob);
1093 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1096 DEBTRACE( "---------------End PyFuncNode::inputs---------------" );
1098 DEBTRACE( "----------------PyFuncNode::calculation---------------" );
1100 PyObject_Print(_pyfunc,stderr,Py_PRINT_RAW);
1102 PyObject_Print(args,stderr,Py_PRINT_RAW);
1105 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1106 PyObject* result = PyObject_CallObject( _pyfunc , args ) ;
1107 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1114 PyObject* new_stderr = newPyStdOut(_errorDetails);
1115 PySys_SetObject((char*)"stderr", new_stderr);
1116 std::ostringstream stream;
1117 stream << "/tmp/PythonNode_";
1119 ofstream errorfile(stream.str().c_str());
1120 if (errorfile.is_open())
1122 errorfile << _script;
1126 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1127 Py_DECREF(new_stderr);
1128 throw Exception("Error during execution");
1130 DEBTRACE( "----------------End PyFuncNode::calculation---------------" );
1132 DEBTRACE( "-----------------PyFuncNode::outputs-----------------" );
1134 if(result == Py_None)
1136 else if(PyTuple_Check(result))
1137 nres=PyTuple_Size(result);
1139 if(getNumberOfOutputPorts() != nres)
1141 std::string msg="Number of output arguments : Mismatch between definition and execution";
1144 throw Exception(msg);
1149 PyObject_Print(result,stderr,Py_PRINT_RAW);
1152 list<OutputPort *>::iterator iter;
1155 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
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) ;
1163 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1165 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1172 catch(ConversionException& ex)
1175 _errorDetails=ex.what();
1178 DEBTRACE( "-----------------End PyFuncNode::outputs-----------------" );
1181 DEBTRACE( "++++++++++++++ End PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1184 Node *PyFuncNode::simpleClone(ComposedNode *father, bool editionOnly) const
1186 return new PyFuncNode(*this,father);
1189 void PyFuncNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
1191 if(!CORBA::is_nil(_pynode))
1192 _pynode->UnRegister();
1193 _pynode=objContainer->createPyNode(getName().c_str(),getScript().c_str());
1196 Engines::PyNodeBase_var PyFuncNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
1198 Engines::PyNode_var ret(objContainer->getDefaultPyNode(getName().c_str()));
1199 if(!CORBA::is_nil(ret))
1203 return Engines::PyNodeBase::_narrow(ret);
1206 void PyFuncNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
1208 if(!CORBA::is_nil(_pynode))
1210 Engines::PyNode_var tmpp(Engines::PyNode::_narrow(remoteInterp));
1211 if(_pynode->_is_equivalent(tmpp))
1214 if(!CORBA::is_nil(_pynode))
1215 _pynode->UnRegister();
1216 _pynode=Engines::PyNode::_narrow(remoteInterp);
1219 Engines::PyNodeBase_var PyFuncNode::getRemoteInterpreterHandle()
1221 return Engines::PyNodeBase::_narrow(_pynode);
1224 //! Create a new node of same type with a given name
1225 PyFuncNode* PyFuncNode::cloneNode(const std::string& name)
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++)
1233 InputPyPort *p=(InputPyPort *)*iter;
1234 n->edAddInputPort(p->getName(),p->edGetType());
1236 list<OutputPort *>::iterator iter2;
1237 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
1239 OutputPyPort *p=(OutputPyPort *)*iter2;
1240 n->edAddOutputPort(p->getName(),p->edGetType());
1245 std::string PyFuncNode::getContainerLog()
1247 return PythonEntry::GetContainerLog(_mode,_container,this);
1250 void PyFuncNode::shutdown(int level)
1252 DEBTRACE("PyFuncNode::shutdown " << level);
1253 if(_mode=="local")return;
1256 if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
1257 _pynode=Engines::PyNode::_nil();
1258 _container->shutdown(level);