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 if(!_imposedResource.empty() && !_imposedContainer.empty())
106 container->start(reqNode, _imposedResource, _imposedContainer);
108 container->start(reqNode);
112 reqNode->setErrorDetails(e.what());
119 std::string what("PythonEntry::CommonRemoteLoad : a load operation requested on \"");
120 what+=reqNode->getName(); what+="\" with no container specified.";
121 reqNode->setErrorDetails(what);
122 throw Exception(what);
126 Engines::Container_var GetContainerObj(InlineNode *reqNode, bool& isStandardCont)
128 isStandardCont = false;
129 Container *container(reqNode->getContainer());
130 Engines::Container_var objContainer(Engines::Container::_nil());
132 throw YACS::Exception("No container specified !");
133 SalomeContainer *containerCast0(dynamic_cast<SalomeContainer *>(container));
134 SalomeHPContainer *containerCast1(dynamic_cast<SalomeHPContainer *>(container));
137 isStandardCont = true;
138 objContainer=containerCast0->getContainerPtr(reqNode);
140 else if(containerCast1)
142 YACS::BASES::AutoCppPtr<SalomeContainerTmpForHP> tmpCont(SalomeContainerTmpForHP::BuildFrom(containerCast1,reqNode));
143 objContainer=tmpCont->getContainerPtr(reqNode);
146 throw YACS::Exception("Unrecognized type of container ! Salome one is expected for PythonNode/PyFuncNode !");
147 if(CORBA::is_nil(objContainer))
148 throw YACS::Exception("Container corba pointer is NULL for PythonNode !");
152 Engines::Container_var PythonEntry::commonRemoteLoadPart2(InlineNode *reqNode, bool& isInitializeRequested)
154 bool isStandardCont(true);
155 Engines::Container_var objContainer(GetContainerObj(reqNode,isStandardCont));
156 isInitializeRequested=false;
161 createRemoteAdaptedPyInterpretor(objContainer);
165 Engines::PyNodeBase_var dftPyScript(retrieveDftRemotePyInterpretorIfAny(objContainer));
166 if(CORBA::is_nil(dftPyScript))
168 isInitializeRequested=true;
169 createRemoteAdaptedPyInterpretor(objContainer);
172 assignRemotePyInterpretor(dftPyScript);
175 catch( const SALOME::SALOME_Exception& ex )
177 std::string msg="Exception on remote python node creation ";
179 msg += ex.details.text.in();
180 reqNode->setErrorDetails(msg);
181 throw Exception(msg);
183 Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
184 if(CORBA::is_nil(pynode))
185 throw Exception("In PythonNode the ref in NULL ! ");
189 void PythonEntry::commonRemoteLoadPart3(InlineNode *reqNode, Engines::Container_ptr objContainer, bool isInitializeRequested)
191 Container *container(reqNode->getContainer());
192 Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
196 const char *picklizeScript(getSerializationScript());
197 PyObject *res=PyRun_String(picklizeScript,Py_file_input,_context,_context);
198 PyObject *res2(PyRun_String(SCRIPT_FOR_SIMPLE_SERIALIZATION,Py_file_input,_context,_context));
199 if(res == NULL || res2==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 Py_DECREF(res); Py_DECREF(res2);
211 _pyfuncSer=PyDict_GetItemString(_context,"pickleForDistPyth2009");
212 _pyfuncUnser=PyDict_GetItemString(_context,"unPickleForDistPyth2009");
213 _pyfuncSimpleSer=PyDict_GetItemString(_context,"pickleForVarSimplePyth2009");
214 if(_pyfuncSer == NULL)
216 std::string errorDetails;
217 PyObject *new_stderr(newPyStdOut(errorDetails));
218 reqNode->setErrorDetails(errorDetails);
219 PySys_SetObject((char*)"stderr", new_stderr);
221 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
222 Py_DECREF(new_stderr);
223 throw Exception("Error during load");
225 if(_pyfuncUnser == NULL)
227 std::string errorDetails;
228 PyObject *new_stderr(newPyStdOut(errorDetails));
229 reqNode->setErrorDetails(errorDetails);
230 PySys_SetObject((char*)"stderr", new_stderr);
232 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
233 Py_DECREF(new_stderr);
234 throw Exception("Error during load");
236 if(_pyfuncSimpleSer == NULL)
238 std::string errorDetails;
239 PyObject *new_stderr(newPyStdOut(errorDetails));
240 reqNode->setErrorDetails(errorDetails);
241 PySys_SetObject((char*)"stderr", new_stderr);
243 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
244 Py_DECREF(new_stderr);
245 throw Exception("Error during load");
248 if(isInitializeRequested)
249 {//This one is called only once at initialization in the container if an init-script is specified.
252 std::string zeInitScriptKey(container->getProperty(HomogeneousPoolContainer::INITIALIZE_SCRIPT_KEY));
253 if(!zeInitScriptKey.empty())
254 pynode->executeAnotherPieceOfCode(zeInitScriptKey.c_str());
256 catch( const SALOME::SALOME_Exception& ex )
258 std::string msg="Exception on PythonNode::loadRemote python invocation of initializisation py script !";
260 msg += ex.details.text.in();
261 reqNode->setErrorDetails(msg);
262 throw Exception(msg);
264 DEBTRACE( "---------------End PyNode::loadRemote function---------------" );
268 std::string PythonEntry::GetContainerLog(const std::string& mode, Container *container, const Task *askingTask)
276 SalomeContainer *containerCast(dynamic_cast<SalomeContainer *>(container));
277 SalomeHPContainer *objContainer2(dynamic_cast<SalomeHPContainer *>(container));
280 Engines::Container_var objContainer(containerCast->getContainerPtr(askingTask));
281 CORBA::String_var logname = objContainer->logfilename();
284 std::string::size_type pos = msg.find(":");
285 msg=msg.substr(pos+1);
287 else if(objContainer2)
289 msg="Remote PythonNode is on HP Container : no log because no info of the location by definition of HP Container !";
293 msg="Not implemented yet for container log for that type of container !";
298 msg = "Container no longer reachable";
303 void PythonEntry::commonRemoteLoad(InlineNode *reqNode)
305 commonRemoteLoadPart1(reqNode);
306 bool isInitializeRequested;
307 Engines::Container_var objContainer(commonRemoteLoadPart2(reqNode,isInitializeRequested));
308 commonRemoteLoadPart3(reqNode,objContainer,isInitializeRequested);
311 PythonNode::PythonNode(const PythonNode& other, ComposedNode *father):InlineNode(other,father),_autoSqueeze(other._autoSqueeze)
313 _implementation=IMPL_NAME;
316 _context=PyDict_New();
317 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
320 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
321 _errorDetails=msg.str();
322 throw Exception(msg.str());
327 PythonNode::PythonNode(const std::string& name):InlineNode(name)
329 _implementation=IMPL_NAME;
332 _context=PyDict_New();
333 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
336 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
337 _errorDetails=msg.str();
338 throw Exception(msg.str());
343 PythonNode::~PythonNode()
345 if(!CORBA::is_nil(_pynode))
347 _pynode->UnRegister();
351 void PythonNode::checkBasicConsistency() const
353 DEBTRACE("checkBasicConsistency");
354 InlineNode::checkBasicConsistency();
358 res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
361 std::string error="";
362 PyObject* new_stderr = newPyStdOut(error);
363 PySys_SetObject((char*)"stderr", new_stderr);
365 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
366 Py_DECREF(new_stderr);
367 throw Exception(error);
374 void PythonNode::load()
376 DEBTRACE( "---------------PyNode::load function---------------" );
377 if(_mode==PythonNode::REMOTE_NAME)
383 void PythonNode::loadLocal()
385 DEBTRACE( "---------------PyNode::loadLocal function---------------" );
389 void PythonNode::loadRemote()
391 commonRemoteLoad(this);
394 void PythonNode::execute()
396 if(_mode==PythonNode::REMOTE_NAME)
402 void PythonNode::executeRemote()
404 DEBTRACE( "++++++++++++++ PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
406 throw Exception("DistributedPythonNode badly loaded");
408 if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
411 commonRemoteLoadPart2(this,dummy);
412 _pynode->assignNewCompiledCode(getScript().c_str());
415 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);
418 PyObject *args(0),*ob(0);
419 //===========================================================================
420 // Get inputs in input ports, build a Python dict and pickle it
421 //===========================================================================
423 std::list<InputPort *>::iterator iter2;
425 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); ++iter2)
427 InputPyPort *p=(InputPyPort *)*iter2;
429 PyDict_SetItemString(args,p->getName().c_str(),ob);
433 PyObject_Print(args,stderr,Py_PRINT_RAW);
436 PyObject *serializationInput(PyObject_CallFunctionObjArgs(_pyfuncSer,args,NULL));
438 //The pickled string may contain NULL characters so use PyString_AsStringAndSize
439 char *serializationInputC(0);
441 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
442 throw Exception("DistributedPythonNode problem in python pickle");
443 serializationInputCorba->length(len);
444 for(int i=0; i < len ; i++)
445 serializationInputCorba[i]=serializationInputC[i];
446 Py_DECREF(serializationInput);
449 //get the list of output argument names
450 std::list<OutputPort *>::iterator iter;
451 Engines::listofstring myseq;
452 myseq.length(getNumberOfOutputPorts());
454 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
456 OutputPyPort *p=(OutputPyPort *)*iter;
457 myseq[pos]=p->getName().c_str();
458 DEBTRACE( "port name: " << p->getName() );
459 DEBTRACE( "port kind: " << p->edGetType()->kind() );
460 DEBTRACE( "port pos : " << pos );
463 //===========================================================================
464 // Execute in remote Python node
465 //===========================================================================
466 DEBTRACE( "-----------------starting remote python invocation-----------------" );
467 Engines::pickledArgs_var resultCorba;
470 //pass outargsname and dict serialized
471 resultCorba=_pynode->execute(myseq,serializationInputCorba);
473 catch( const SALOME::SALOME_Exception& ex )
475 std::string msg="Exception on remote python invocation";
477 msg += ex.details.text.in();
479 throw Exception(msg);
481 DEBTRACE( "-----------------end of remote python invocation-----------------" );
482 //===========================================================================
483 // Get results, unpickle and put them in output ports
484 //===========================================================================
485 char *resultCorbaC=new char[resultCorba->length()+1];
486 resultCorbaC[resultCorba->length()]='\0';
487 for(int i=0;i<resultCorba->length();i++)
488 resultCorbaC[i]=resultCorba[i];
492 PyObject *args(0),*ob(0);
493 PyObject* resultPython=PyBytes_FromStringAndSize(resultCorbaC,resultCorba->length());
494 delete [] resultCorbaC;
495 args = PyTuple_New(1);
496 PyTuple_SetItem(args,0,resultPython);
497 PyObject *finalResult=PyObject_CallObject(_pyfuncUnser,args);
500 if (finalResult == NULL)
502 std::stringstream msg;
503 msg << "Conversion with pickle of output ports failed !";
504 msg << " : " << __FILE__ << ":" << __LINE__;
505 _errorDetails=msg.str();
506 throw YACS::ENGINE::ConversionException(msg.str());
509 DEBTRACE( "-----------------PythonNode::outputs-----------------" );
511 if(finalResult == Py_None)
513 else if(PyTuple_Check(finalResult))
514 nres=PyTuple_Size(finalResult);
516 if(getNumberOfOutputPorts() != nres)
518 std::string msg="Number of output arguments : Mismatch between definition and execution";
519 Py_DECREF(finalResult);
521 throw Exception(msg);
527 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
529 OutputPyPort *p=(OutputPyPort *)*iter;
530 DEBTRACE( "port name: " << p->getName() );
531 DEBTRACE( "port kind: " << p->edGetType()->kind() );
532 DEBTRACE( "port pos : " << pos );
533 if(PyTuple_Check(finalResult))
534 ob=PyTuple_GetItem(finalResult,pos) ;
537 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
541 Py_DECREF(finalResult);
543 catch(ConversionException& ex)
545 Py_DECREF(finalResult);
546 _errorDetails=ex.what();
550 squeezeMemoryRemote();
553 if(!CORBA::is_nil(_pynode))
555 _pynode->UnRegister();
557 _pynode = Engines::PyScriptNode::_nil();
559 Engines::Container_var cont(GetContainerObj(this,dummy));
560 cont->removePyScriptNode(getName().c_str());
561 DEBTRACE( "++++++++++++++ ENDOF PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
564 void PythonNode::executeLocal()
566 DEBTRACE( "++++++++++++++ PyNode::executeLocal: " << getName() << " ++++++++++++++++++++" );
570 DEBTRACE( "---------------PyNode::inputs---------------" );
571 list<InputPort *>::iterator iter2;
572 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
574 InputPyPort *p=(InputPyPort *)*iter2;
575 DEBTRACE( "port name: " << p->getName() );
576 DEBTRACE( "port kind: " << p->edGetType()->kind() );
577 PyObject* ob=p->getPyObj();
578 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
580 PyObject_Print(ob,stderr,Py_PRINT_RAW);
583 int ier=PyDict_SetItemString(_context,p->getName().c_str(),ob);
584 DEBTRACE( "after PyDict_SetItemString:ob refcnt: " << ob->ob_refcnt );
587 DEBTRACE( "---------------End PyNode::inputs---------------" );
590 DEBTRACE( "----------------PyNode::calculation---------------" );
592 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
594 std::ostringstream stream;
595 stream << "/tmp/PythonNode_";
598 PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
602 PyObject* new_stderr = newPyStdOut(_errorDetails);
603 PySys_SetObject((char*)"stderr", new_stderr);
605 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
606 Py_DECREF(new_stderr);
607 throw Exception("Error during execution");
609 PyObject *res = PyEval_EvalCode( code, _context, _context);
613 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
616 if(PyErr_Occurred ())
619 PyObject* new_stderr = newPyStdOut(_errorDetails);
620 PySys_SetObject((char*)"stderr", new_stderr);
621 ofstream errorfile(stream.str().c_str());
622 if (errorfile.is_open())
624 errorfile << _script;
628 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
629 Py_DECREF(new_stderr);
630 throw Exception("Error during execution");
633 DEBTRACE( "-----------------PyNode::outputs-----------------" );
634 list<OutputPort *>::iterator iter;
637 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
639 OutputPyPort *p=(OutputPyPort *)*iter;
640 DEBTRACE( "port name: " << p->getName() );
641 DEBTRACE( "port kind: " << p->edGetType()->kind() );
642 PyObject *ob=PyDict_GetItemString(_context,p->getName().c_str());
645 std::string msg="Error during execution: there is no variable ";
646 msg=msg+p->getName()+" in node context";
648 throw Exception(msg);
650 DEBTRACE( "PyNode::outputs::ob refcnt: " << ob->ob_refcnt );
652 PyObject_Print(ob,stderr,Py_PRINT_RAW);
658 catch(ConversionException& ex)
660 _errorDetails=ex.what();
665 DEBTRACE( "-----------------End PyNode::outputs-----------------" );
667 DEBTRACE( "++++++++++++++ End PyNode::execute: " << getName() << " ++++++++++++++++++++" );
670 void PythonNode::squeezeMemorySafe()
673 if(_mode==PythonNode::REMOTE_NAME)
674 this->squeezeMemoryRemote();
676 this->squeezeMemory();
679 void PythonNode::squeezeMemory()
681 for(auto p : _setOfInputPort)
683 PyDict_DelItemString(_context,p->getName().c_str());
684 InputPyPort *p2(static_cast<InputPyPort *>(p));
685 if(p2->canSafelySqueezeMemory())
688 for(auto p : _setOfOutputPort)
690 PyDict_DelItemString(_context,p->getName().c_str());
691 OutputPyPort *p2(static_cast<OutputPyPort *>(p));
692 p2->putWithoutForward(Py_None);
696 void PythonNode::squeezeMemoryRemote()
698 for(auto p : _setOfInputPort)
700 InputPyPort *p2(static_cast<InputPyPort *>(p));
701 if(p2->canSafelySqueezeMemory())
704 for(auto p : _setOfOutputPort)
706 OutputPyPort *p2(static_cast<OutputPyPort *>(p));
707 p2->putWithoutForward(Py_None);
711 std::string PythonNode::getContainerLog()
713 return PythonEntry::GetContainerLog(_mode,_container,this);
716 void PythonNode::shutdown(int level)
718 DEBTRACE("PythonNode::shutdown " << level);
719 if(_mode=="local")return;
722 if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
723 _pynode=Engines::PyScriptNode::_nil();
724 _container->shutdown(level);
728 void PythonNode::imposeResource(const std::string& resource_name,
729 const std::string& container_name)
731 if(!resource_name.empty() && !container_name.empty())
733 _imposedResource = resource_name;
734 _imposedContainer = container_name;
738 bool PythonNode::canAcceptImposedResource()
740 return _container != nullptr && _container->canAcceptImposedResource();
743 Node *PythonNode::simpleClone(ComposedNode *father, bool editionOnly) const
745 return new PythonNode(*this,father);
748 void PythonNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
750 if(!CORBA::is_nil(_pynode))
751 _pynode->UnRegister();
752 _pynode=objContainer->createPyScriptNode(getName().c_str(),getScript().c_str());
756 Engines::PyNodeBase_var PythonNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
758 Engines::PyScriptNode_var ret(objContainer->getDefaultPyScriptNode(getName().c_str()));
759 if(!CORBA::is_nil(ret))
763 return Engines::PyNodeBase::_narrow(ret);
766 void PythonNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
768 if(!CORBA::is_nil(_pynode))
770 Engines::PyScriptNode_var tmpp(Engines::PyScriptNode::_narrow(remoteInterp));
771 if(_pynode->_is_equivalent(tmpp))
773 _pynode->UnRegister();
777 if(!CORBA::is_nil(_pynode))
778 _pynode->UnRegister();
779 _pynode=Engines::PyScriptNode::_narrow(remoteInterp);
782 Engines::PyNodeBase_var PythonNode::getRemoteInterpreterHandle()
784 return Engines::PyNodeBase::_narrow(_pynode);
787 //! Create a new node of same type with a given name
788 PythonNode* PythonNode::cloneNode(const std::string& name)
790 PythonNode* n=new PythonNode(name);
791 n->setScript(_script);
792 list<InputPort *>::iterator iter;
793 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
795 InputPyPort *p=(InputPyPort *)*iter;
796 DEBTRACE( "port name: " << p->getName() );
797 DEBTRACE( "port kind: " << p->edGetType()->kind() );
798 n->edAddInputPort(p->getName(),p->edGetType());
800 list<OutputPort *>::iterator iter2;
801 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
803 OutputPyPort *p=(OutputPyPort *)*iter2;
804 DEBTRACE( "port name: " << p->getName() );
805 DEBTRACE( "port kind: " << p->edGetType()->kind() );
806 n->edAddOutputPort(p->getName(),p->edGetType());
811 void PythonNode::applyDPLScope(ComposedNode *gfn)
813 std::vector< std::pair<std::string,int> > ret(getDPLScopeInfo(gfn));
820 std::size_t sz(ret.size());
822 for(std::size_t i=0;i<sz;i++)
824 const std::pair<std::string,int>& p(ret[i]);
825 PyObject *elt(PyTuple_New(2));
826 PyTuple_SetItem(elt,0,PyUnicode_FromString(p.first.c_str()));
827 PyTuple_SetItem(elt,1,PyLong_FromLong(p.second));
828 PyList_SetItem(ob,i,elt);
831 if(_mode==REMOTE_NAME)
833 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);
836 PyObject *serializationInput(PyObject_CallFunctionObjArgs(_pyfuncSimpleSer,ob,NULL));
838 char *serializationInputC(0);
840 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
841 throw Exception("DistributedPythonNode problem in python pickle");
842 serializationInputCorba->length(len);
843 for(int i=0; i < len ; i++)
844 serializationInputCorba[i]=serializationInputC[i];
845 Py_XDECREF(serializationInput);
847 _pynode->defineNewCustomVar(DPL_INFO_NAME,serializationInputCorba);
852 PyDict_SetItemString(_context,DPL_INFO_NAME,ob);
857 PyFuncNode::PyFuncNode(const PyFuncNode& other, ComposedNode *father):InlineFuncNode(other,father),_pyfunc(0)
859 _implementation = PythonNode::IMPL_NAME;
862 _context=PyDict_New();
863 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
864 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
867 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
868 _errorDetails=msg.str();
869 throw Exception(msg.str());
874 PyFuncNode::PyFuncNode(const std::string& name): InlineFuncNode(name),_pyfunc(0)
877 _implementation = PythonNode::IMPL_NAME;
878 DEBTRACE( "PyFuncNode::PyFuncNode " << name );
881 _context=PyDict_New();
882 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
883 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
886 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
887 _errorDetails=msg.str();
888 throw Exception(msg.str());
893 PyFuncNode::~PyFuncNode()
895 if(!CORBA::is_nil(_pynode))
897 _pynode->UnRegister();
901 void PyFuncNode::init(bool start)
903 initCommonPartWithoutStateManagement(start);
904 if(_state == YACS::DISABLED)
906 exDisabledState(); // to refresh propagation of DISABLED state
909 if(start) //complete initialization
910 setState(YACS::READY);
911 else if(_state > YACS::LOADED)// WARNING FuncNode has internal vars (CEA usecase) ! Partial initialization (inside a loop). Exclusivity of funcNode.
912 setState(YACS::TORECONNECT);
915 void PyFuncNode::checkBasicConsistency() const
917 DEBTRACE("checkBasicConsistency");
918 InlineFuncNode::checkBasicConsistency();
922 res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
925 std::string error="";
926 PyObject* new_stderr = newPyStdOut(error);
927 PySys_SetObject((char*)"stderr", new_stderr);
929 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
930 Py_DECREF(new_stderr);
931 throw Exception(error);
938 void PyFuncNode::load()
940 DEBTRACE( "---------------PyfuncNode::load function---------------" );
941 if(_mode==PythonNode::REMOTE_NAME)
947 void PyFuncNode::loadRemote()
949 commonRemoteLoad(this);
952 void PyFuncNode::loadLocal()
954 DEBTRACE( "---------------PyFuncNode::load function " << getName() << " ---------------" );
958 list<OutputPort *>::iterator iter;
959 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
961 OutputPyPort *p=(OutputPyPort *)*iter;
962 DEBTRACE( "port name: " << p->getName() );
963 DEBTRACE( "port kind: " << p->edGetType()->kind() );
969 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
971 std::ostringstream stream;
972 stream << "/tmp/PythonNode_";
975 PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
979 PyObject* new_stderr = newPyStdOut(_errorDetails);
980 PySys_SetObject((char*)"stderr", new_stderr);
982 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
983 Py_DECREF(new_stderr);
984 throw Exception("Error during execution");
986 PyObject *res = PyEval_EvalCode( code, _context, _context);
990 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
991 if(PyErr_Occurred ())
994 PyObject* new_stderr = newPyStdOut(_errorDetails);
995 PySys_SetObject((char*)"stderr", new_stderr);
996 ofstream errorfile(stream.str().c_str());
997 if (errorfile.is_open())
999 errorfile << _script;
1003 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1004 Py_DECREF(new_stderr);
1005 throw Exception("Error during execution");
1008 _pyfunc=PyDict_GetItemString(_context,_fname.c_str());
1009 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1013 PyObject* new_stderr = newPyStdOut(_errorDetails);
1014 PySys_SetObject((char*)"stderr", new_stderr);
1016 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1017 Py_DECREF(new_stderr);
1018 throw Exception("Error during execution");
1020 DEBTRACE( "---------------End PyFuncNode::load function---------------" );
1024 void PyFuncNode::execute()
1026 if(_mode==PythonNode::REMOTE_NAME)
1032 void PyFuncNode::executeRemote()
1034 DEBTRACE( "++++++++++++++ PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
1036 throw Exception("DistributedPythonNode badly loaded");
1038 if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
1041 commonRemoteLoadPart2(this,dummy);
1042 _pynode->executeAnotherPieceOfCode(getScript().c_str());
1045 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);;
1049 //===========================================================================
1050 // Get inputs in input ports, build a Python tuple and pickle it
1051 //===========================================================================
1052 PyObject *args(PyTuple_New(getNumberOfInputPorts()));
1054 for(std::list<InputPort *>::iterator iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++,pos++)
1056 InputPyPort *p=(InputPyPort *)*iter2;
1059 PyTuple_SetItem(args,pos,ob);
1062 PyObject_Print(args,stderr,Py_PRINT_RAW);
1065 PyObject *serializationInput=PyObject_CallObject(_pyfuncSer,args);
1067 //The pickled string may contain NULL characters so use PyString_AsStringAndSize
1068 char *serializationInputC(0);
1070 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
1071 throw Exception("DistributedPythonNode problem in python pickle");
1073 serializationInputCorba->length(len);
1074 for(int i=0; i < len ; i++)
1075 serializationInputCorba[i]=serializationInputC[i];
1076 Py_DECREF(serializationInput);
1079 //===========================================================================
1080 // Execute in remote Python node
1081 //===========================================================================
1082 DEBTRACE( "-----------------starting remote python invocation-----------------" );
1083 Engines::pickledArgs_var resultCorba;
1086 resultCorba=_pynode->execute(getFname().c_str(),serializationInputCorba);
1088 catch( const SALOME::SALOME_Exception& ex )
1090 std::string msg="Exception on remote python invocation";
1092 msg += ex.details.text.in();
1094 throw Exception(msg);
1096 DEBTRACE( "-----------------end of remote python invocation-----------------" );
1097 //===========================================================================
1098 // Get results, unpickle and put them in output ports
1099 //===========================================================================
1100 char *resultCorbaC=new char[resultCorba->length()+1];
1101 resultCorbaC[resultCorba->length()]='\0';
1102 for(int i=0;i<resultCorba->length();i++)
1103 resultCorbaC[i]=resultCorba[i];
1108 PyObject *resultPython(PyBytes_FromStringAndSize(resultCorbaC,resultCorba->length()));
1109 delete [] resultCorbaC;
1110 PyObject *args(PyTuple_New(1)),*ob(0);
1111 PyTuple_SetItem(args,0,resultPython);
1112 PyObject *finalResult=PyObject_CallObject(_pyfuncUnser,args);
1115 DEBTRACE( "-----------------PythonNode::outputs-----------------" );
1117 if(finalResult == Py_None)
1119 else if(PyTuple_Check(finalResult))
1120 nres=PyTuple_Size(finalResult);
1122 if(getNumberOfOutputPorts() != nres)
1124 std::string msg="Number of output arguments : Mismatch between definition and execution";
1125 Py_DECREF(finalResult);
1127 throw Exception(msg);
1133 for(std::list<OutputPort *>::iterator iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++, pos++)
1135 OutputPyPort *p=(OutputPyPort *)*iter;
1136 DEBTRACE( "port name: " << p->getName() );
1137 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1138 DEBTRACE( "port pos : " << pos );
1139 if(PyTuple_Check(finalResult))
1140 ob=PyTuple_GetItem(finalResult,pos) ;
1143 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1146 Py_DECREF(finalResult);
1148 catch(ConversionException& ex)
1150 Py_DECREF(finalResult);
1151 _errorDetails=ex.what();
1156 DEBTRACE( "++++++++++++++ ENDOF PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
1159 void PyFuncNode::executeLocal()
1161 DEBTRACE( "++++++++++++++ PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1165 if(!_pyfunc)throw Exception("PyFuncNode badly loaded");
1168 DEBTRACE( "---------------PyFuncNode::inputs---------------" );
1169 PyObject* args = PyTuple_New(getNumberOfInputPorts()) ;
1170 list<InputPort *>::iterator iter2;
1171 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
1173 InputPyPort *p=(InputPyPort *)*iter2;
1174 DEBTRACE( "port name: " << p->getName() );
1175 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1178 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1181 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1183 PyTuple_SetItem(args,pos,ob);
1184 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1187 DEBTRACE( "---------------End PyFuncNode::inputs---------------" );
1189 DEBTRACE( "----------------PyFuncNode::calculation---------------" );
1191 PyObject_Print(_pyfunc,stderr,Py_PRINT_RAW);
1193 PyObject_Print(args,stderr,Py_PRINT_RAW);
1196 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1197 PyObject* result = PyObject_CallObject( _pyfunc , args ) ;
1198 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1205 PyObject* new_stderr = newPyStdOut(_errorDetails);
1206 PySys_SetObject((char*)"stderr", new_stderr);
1207 std::ostringstream stream;
1208 stream << "/tmp/PythonNode_";
1210 ofstream errorfile(stream.str().c_str());
1211 if (errorfile.is_open())
1213 errorfile << _script;
1217 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1218 Py_DECREF(new_stderr);
1219 throw Exception("Error during execution");
1221 DEBTRACE( "----------------End PyFuncNode::calculation---------------" );
1223 DEBTRACE( "-----------------PyFuncNode::outputs-----------------" );
1225 if(result == Py_None)
1227 else if(PyTuple_Check(result))
1228 nres=PyTuple_Size(result);
1230 if(getNumberOfOutputPorts() != nres)
1232 std::string msg="Number of output arguments : Mismatch between definition and execution";
1235 throw Exception(msg);
1240 PyObject_Print(result,stderr,Py_PRINT_RAW);
1243 list<OutputPort *>::iterator iter;
1246 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
1248 OutputPyPort *p=(OutputPyPort *)*iter;
1249 DEBTRACE( "port name: " << p->getName() );
1250 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1251 DEBTRACE( "port pos : " << pos );
1252 if(PyTuple_Check(result))ob=PyTuple_GetItem(result,pos) ;
1254 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1256 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1263 catch(ConversionException& ex)
1266 _errorDetails=ex.what();
1269 DEBTRACE( "-----------------End PyFuncNode::outputs-----------------" );
1272 DEBTRACE( "++++++++++++++ End PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1275 Node *PyFuncNode::simpleClone(ComposedNode *father, bool editionOnly) const
1277 return new PyFuncNode(*this,father);
1280 void PyFuncNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
1282 if(!CORBA::is_nil(_pynode))
1283 _pynode->UnRegister();
1284 _pynode=objContainer->createPyNode(getName().c_str(),getScript().c_str());
1287 Engines::PyNodeBase_var PyFuncNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
1289 Engines::PyNode_var ret(objContainer->getDefaultPyNode(getName().c_str()));
1290 if(!CORBA::is_nil(ret))
1294 return Engines::PyNodeBase::_narrow(ret);
1297 void PyFuncNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
1299 if(!CORBA::is_nil(_pynode))
1301 Engines::PyNode_var tmpp(Engines::PyNode::_narrow(remoteInterp));
1302 if(_pynode->_is_equivalent(tmpp))
1305 if(!CORBA::is_nil(_pynode))
1306 _pynode->UnRegister();
1307 _pynode=Engines::PyNode::_narrow(remoteInterp);
1310 Engines::PyNodeBase_var PyFuncNode::getRemoteInterpreterHandle()
1312 return Engines::PyNodeBase::_narrow(_pynode);
1315 //! Create a new node of same type with a given name
1316 PyFuncNode* PyFuncNode::cloneNode(const std::string& name)
1318 PyFuncNode* n=new PyFuncNode(name);
1319 n->setScript(_script);
1320 n->setFname(_fname);
1321 list<InputPort *>::iterator iter;
1322 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
1324 InputPyPort *p=(InputPyPort *)*iter;
1325 n->edAddInputPort(p->getName(),p->edGetType());
1327 list<OutputPort *>::iterator iter2;
1328 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
1330 OutputPyPort *p=(OutputPyPort *)*iter2;
1331 n->edAddOutputPort(p->getName(),p->edGetType());
1336 std::string PyFuncNode::getContainerLog()
1338 return PythonEntry::GetContainerLog(_mode,_container,this);
1341 void PyFuncNode::shutdown(int level)
1343 DEBTRACE("PyFuncNode::shutdown " << level);
1344 if(_mode=="local")return;
1347 if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
1348 _pynode=Engines::PyNode::_nil();
1349 _container->shutdown(level);
1353 void PyFuncNode::imposeResource(const std::string& resource_name,
1354 const std::string& container_name)
1356 if(!resource_name.empty() && !container_name.empty())
1358 _imposedResource = resource_name;
1359 _imposedContainer = container_name;
1363 bool PyFuncNode::canAcceptImposedResource()
1365 return _container != nullptr && _container->canAcceptImposedResource();