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"
79 PythonEntry::PythonEntry():_context(0),_pyfuncSer(0),_pyfuncUnser(0),_pyfuncSimpleSer(0)
83 PythonEntry::~PythonEntry()
86 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
87 // not Py_XDECREF of _pyfuncUnser because it is returned by PyDict_GetItem -> borrowed
88 // not Py_XDECREF of _pyfuncSer because it is returned by PyDict_GetItem -> borrowed
92 void PythonEntry::loadRemoteContainer(InlineNode *reqNode)
94 DEBTRACE( "---------------PythonEntry::CommonRemoteLoad function---------------" );
95 Container *container(reqNode->getContainer());
96 bool isContAlreadyStarted(false);
99 isContAlreadyStarted=container->isAlreadyStarted(reqNode);
100 if(!isContAlreadyStarted)
104 if(hasImposedResource())
105 container->start(reqNode, _imposedResource, _imposedContainer);
107 container->start(reqNode);
111 reqNode->setErrorDetails(e.what());
118 std::string what("PythonEntry::CommonRemoteLoad : a load operation requested on \"");
119 what+=reqNode->getName(); what+="\" with no container specified.";
120 reqNode->setErrorDetails(what);
121 throw Exception(what);
125 Engines::Container_var GetContainerObj(InlineNode *reqNode, bool& isStandardCont)
127 isStandardCont = false;
128 Container *container(reqNode->getContainer());
129 Engines::Container_var objContainer(Engines::Container::_nil());
131 throw YACS::Exception("No container specified !");
132 SalomeContainer *containerCast0(dynamic_cast<SalomeContainer *>(container));
133 SalomeHPContainer *containerCast1(dynamic_cast<SalomeHPContainer *>(container));
136 isStandardCont = true;
137 objContainer=containerCast0->getContainerPtr(reqNode);
139 else if(containerCast1)
141 YACS::BASES::AutoCppPtr<SalomeContainerTmpForHP> tmpCont(SalomeContainerTmpForHP::BuildFrom(containerCast1,reqNode));
142 objContainer=tmpCont->getContainerPtr(reqNode);
145 throw YACS::Exception("Unrecognized type of container ! Salome one is expected for PythonNode/PyFuncNode !");
146 if(CORBA::is_nil(objContainer))
147 throw YACS::Exception("Container corba pointer is NULL for PythonNode !");
151 Engines::Container_var PythonEntry::loadPythonAdapter(InlineNode *reqNode, bool& isInitializeRequested)
153 bool isStandardCont(true);
154 Engines::Container_var objContainer(GetContainerObj(reqNode,isStandardCont));
155 isInitializeRequested=false;
158 Engines::PyNodeBase_var dftPyScript(retrieveDftRemotePyInterpretorIfAny(objContainer));
159 if(CORBA::is_nil(dftPyScript))
161 isInitializeRequested=!isStandardCont;
162 createRemoteAdaptedPyInterpretor(objContainer);
165 assignRemotePyInterpretor(dftPyScript);
167 catch( const SALOME::SALOME_Exception& ex )
169 std::string msg="Exception on remote python node creation ";
171 msg += ex.details.text.in();
172 reqNode->setErrorDetails(msg);
173 throw Exception(msg);
175 Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
176 if(CORBA::is_nil(pynode))
177 throw Exception("In PythonNode the ref in NULL ! ");
181 void PythonEntry::loadRemoteContext(InlineNode *reqNode, Engines::Container_ptr objContainer, bool isInitializeRequested)
183 Container *container(reqNode->getContainer());
184 Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
188 const char *picklizeScript(getSerializationScript());
189 PyObject *res=PyRun_String(picklizeScript,Py_file_input,_context,_context);
190 PyObject *res2(PyRun_String(SCRIPT_FOR_SIMPLE_SERIALIZATION,Py_file_input,_context,_context));
191 if(res == NULL || res2==NULL)
193 std::string errorDetails;
194 PyObject* new_stderr = newPyStdOut(errorDetails);
195 reqNode->setErrorDetails(errorDetails);
196 PySys_SetObject((char*)"stderr", new_stderr);
198 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
199 Py_DECREF(new_stderr);
200 throw Exception("Error during load");
202 Py_DECREF(res); Py_DECREF(res2);
203 _pyfuncSer=PyDict_GetItemString(_context,"pickleForDistPyth2009");
204 _pyfuncUnser=PyDict_GetItemString(_context,"unPickleForDistPyth2009");
205 _pyfuncSimpleSer=PyDict_GetItemString(_context,"pickleForVarSimplePyth2009");
206 if(_pyfuncSer == NULL)
208 std::string errorDetails;
209 PyObject *new_stderr(newPyStdOut(errorDetails));
210 reqNode->setErrorDetails(errorDetails);
211 PySys_SetObject((char*)"stderr", new_stderr);
213 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
214 Py_DECREF(new_stderr);
215 throw Exception("Error during load");
217 if(_pyfuncUnser == NULL)
219 std::string errorDetails;
220 PyObject *new_stderr(newPyStdOut(errorDetails));
221 reqNode->setErrorDetails(errorDetails);
222 PySys_SetObject((char*)"stderr", new_stderr);
224 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
225 Py_DECREF(new_stderr);
226 throw Exception("Error during load");
228 if(_pyfuncSimpleSer == NULL)
230 std::string errorDetails;
231 PyObject *new_stderr(newPyStdOut(errorDetails));
232 reqNode->setErrorDetails(errorDetails);
233 PySys_SetObject((char*)"stderr", new_stderr);
235 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
236 Py_DECREF(new_stderr);
237 throw Exception("Error during load");
240 if(isInitializeRequested)
241 {//This one is called only once at initialization in the container if an init-script is specified.
244 std::string zeInitScriptKey(container->getProperty(HomogeneousPoolContainer::INITIALIZE_SCRIPT_KEY));
245 if(!zeInitScriptKey.empty())
246 pynode->executeAnotherPieceOfCode(zeInitScriptKey.c_str());
248 catch( const SALOME::SALOME_Exception& ex )
250 std::string msg="Exception on PythonNode::loadRemote python invocation of initializisation py script !";
252 msg += ex.details.text.in();
253 reqNode->setErrorDetails(msg);
254 throw Exception(msg);
256 DEBTRACE( "---------------End PyNode::loadRemote function---------------" );
260 std::string PythonEntry::GetContainerLog(const std::string& mode, Container *container, const Task *askingTask)
268 SalomeContainer *containerCast(dynamic_cast<SalomeContainer *>(container));
269 SalomeHPContainer *objContainer2(dynamic_cast<SalomeHPContainer *>(container));
272 Engines::Container_var objContainer(containerCast->getContainerPtr(askingTask));
273 CORBA::String_var logname = objContainer->logfilename();
276 std::string::size_type pos = msg.find(":");
277 msg=msg.substr(pos+1);
279 else if(objContainer2)
281 msg="Remote PythonNode is on HP Container : no log because no info of the location by definition of HP Container !";
285 msg="Not implemented yet for container log for that type of container !";
290 msg = "Container no longer reachable";
295 void PythonEntry::commonRemoteLoad(InlineNode *reqNode)
297 loadRemoteContainer(reqNode);
298 bool isInitializeRequested;
299 Engines::Container_var objContainer(loadPythonAdapter(reqNode,isInitializeRequested));
300 loadRemoteContext(reqNode,objContainer,isInitializeRequested);
303 bool PythonEntry::hasImposedResource()const
305 return !_imposedResource.empty() && !_imposedContainer.empty();
308 PythonNode::PythonNode(const PythonNode& other, ComposedNode *father):InlineNode(other,father),_autoSqueeze(other._autoSqueeze)
310 _pynode = Engines::PyScriptNode::_nil();
311 _implementation=IMPL_NAME;
314 _context=PyDict_New();
315 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
318 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
319 _errorDetails=msg.str();
320 throw Exception(msg.str());
325 PythonNode::PythonNode(const std::string& name):InlineNode(name)
327 _pynode = Engines::PyScriptNode::_nil();
328 _implementation=IMPL_NAME;
331 _context=PyDict_New();
332 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
335 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
336 _errorDetails=msg.str();
337 throw Exception(msg.str());
342 PythonNode::~PythonNode()
344 if(!CORBA::is_nil(_pynode))
346 _pynode->UnRegister();
350 void PythonNode::checkBasicConsistency() const
352 DEBTRACE("checkBasicConsistency");
353 InlineNode::checkBasicConsistency();
357 res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
360 std::string error="";
361 PyObject* new_stderr = newPyStdOut(error);
362 PySys_SetObject((char*)"stderr", new_stderr);
364 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
365 Py_DECREF(new_stderr);
366 throw Exception(error);
373 void PythonNode::load()
375 DEBTRACE( "---------------PyNode::load function---------------" );
376 if(_mode==PythonNode::REMOTE_NAME)
382 void PythonNode::loadLocal()
384 DEBTRACE( "---------------PyNode::loadLocal function---------------" );
388 void PythonNode::loadRemote()
390 commonRemoteLoad(this);
393 void PythonNode::execute()
395 if(_mode==PythonNode::REMOTE_NAME)
401 void PythonNode::executeRemote()
403 DEBTRACE( "++++++++++++++ PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
405 throw Exception("DistributedPythonNode badly loaded");
407 if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
410 loadPythonAdapter(this,dummy);
411 _pynode->assignNewCompiledCode(getScript().c_str());
414 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);
417 PyObject *args(0),*ob(0);
418 //===========================================================================
419 // Get inputs in input ports, build a Python dict and pickle it
420 //===========================================================================
422 std::list<InputPort *>::iterator iter2;
424 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); ++iter2)
426 InputPyPort *p=(InputPyPort *)*iter2;
428 PyDict_SetItemString(args,p->getName().c_str(),ob);
432 PyObject_Print(args,stderr,Py_PRINT_RAW);
435 PyObject *serializationInput(PyObject_CallFunctionObjArgs(_pyfuncSer,args,NULL));
437 //The pickled string may contain NULL characters so use PyString_AsStringAndSize
438 char *serializationInputC(0);
440 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
441 throw Exception("DistributedPythonNode problem in python pickle");
442 serializationInputCorba->length(len);
443 for(int i=0; i < len ; i++)
444 serializationInputCorba[i]=serializationInputC[i];
445 Py_DECREF(serializationInput);
448 //get the list of output argument names
449 std::list<OutputPort *>::iterator iter;
450 Engines::listofstring myseq;
451 myseq.length(getNumberOfOutputPorts());
453 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
455 OutputPyPort *p=(OutputPyPort *)*iter;
456 myseq[pos]=p->getName().c_str();
457 DEBTRACE( "port name: " << p->getName() );
458 DEBTRACE( "port kind: " << p->edGetType()->kind() );
459 DEBTRACE( "port pos : " << pos );
462 //===========================================================================
463 // Execute in remote Python node
464 //===========================================================================
465 DEBTRACE( "-----------------starting remote python invocation-----------------" );
466 Engines::pickledArgs_var resultCorba;
469 //pass outargsname and dict serialized
470 resultCorba=_pynode->execute(myseq,serializationInputCorba);
472 catch( const SALOME::SALOME_Exception& ex )
474 std::string msg="Exception on remote python invocation";
476 msg += ex.details.text.in();
478 throw Exception(msg);
480 DEBTRACE( "-----------------end of remote python invocation-----------------" );
481 //===========================================================================
482 // Get results, unpickle and put them in output ports
483 //===========================================================================
484 char *resultCorbaC=new char[resultCorba->length()+1];
485 resultCorbaC[resultCorba->length()]='\0';
486 for(int i=0;i<resultCorba->length();i++)
487 resultCorbaC[i]=resultCorba[i];
491 PyObject *args(0),*ob(0);
492 PyObject* resultPython=PyBytes_FromStringAndSize(resultCorbaC,resultCorba->length());
493 delete [] resultCorbaC;
494 args = PyTuple_New(1);
495 PyTuple_SetItem(args,0,resultPython);
496 PyObject *finalResult=PyObject_CallObject(_pyfuncUnser,args);
499 if (finalResult == NULL)
501 std::stringstream msg;
502 msg << "Conversion with pickle of output ports failed !";
503 msg << " : " << __FILE__ << ":" << __LINE__;
504 _errorDetails=msg.str();
505 throw YACS::ENGINE::ConversionException(msg.str());
508 DEBTRACE( "-----------------PythonNode::outputs-----------------" );
510 if(finalResult == Py_None)
512 else if(PyTuple_Check(finalResult))
513 nres=PyTuple_Size(finalResult);
515 if(getNumberOfOutputPorts() != nres)
517 std::string msg="Number of output arguments : Mismatch between definition and execution";
518 Py_DECREF(finalResult);
520 throw Exception(msg);
526 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
528 OutputPyPort *p=(OutputPyPort *)*iter;
529 DEBTRACE( "port name: " << p->getName() );
530 DEBTRACE( "port kind: " << p->edGetType()->kind() );
531 DEBTRACE( "port pos : " << pos );
532 if(PyTuple_Check(finalResult))
533 ob=PyTuple_GetItem(finalResult,pos) ;
536 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
540 Py_DECREF(finalResult);
542 catch(ConversionException& ex)
544 Py_DECREF(finalResult);
545 _errorDetails=ex.what();
549 squeezeMemoryRemote();
554 if(!CORBA::is_nil(_pynode))
556 _pynode->UnRegister();
558 _pynode = Engines::PyScriptNode::_nil();
560 Engines::Container_var cont(GetContainerObj(this,dummy));
561 cont->removePyScriptNode(getName().c_str());
563 DEBTRACE( "++++++++++++++ ENDOF PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
566 void PythonNode::executeLocal()
568 DEBTRACE( "++++++++++++++ PyNode::executeLocal: " << getName() << " ++++++++++++++++++++" );
572 DEBTRACE( "---------------PyNode::inputs---------------" );
573 list<InputPort *>::iterator iter2;
574 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
576 InputPyPort *p=(InputPyPort *)*iter2;
577 DEBTRACE( "port name: " << p->getName() );
578 DEBTRACE( "port kind: " << p->edGetType()->kind() );
579 PyObject* ob=p->getPyObj();
580 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
582 PyObject_Print(ob,stderr,Py_PRINT_RAW);
585 int ier=PyDict_SetItemString(_context,p->getName().c_str(),ob);
586 DEBTRACE( "after PyDict_SetItemString:ob refcnt: " << ob->ob_refcnt );
589 DEBTRACE( "---------------End PyNode::inputs---------------" );
592 DEBTRACE( "----------------PyNode::calculation---------------" );
594 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
596 std::ostringstream stream;
597 stream << "/tmp/PythonNode_";
600 PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
604 PyObject* new_stderr = newPyStdOut(_errorDetails);
605 PySys_SetObject((char*)"stderr", new_stderr);
607 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
608 Py_DECREF(new_stderr);
609 throw Exception("Error during execution");
611 PyObject *res = PyEval_EvalCode( code, _context, _context);
615 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
618 if(PyErr_Occurred ())
621 PyObject* new_stderr = newPyStdOut(_errorDetails);
622 PySys_SetObject((char*)"stderr", new_stderr);
623 ofstream errorfile(stream.str().c_str());
624 if (errorfile.is_open())
626 errorfile << _script;
630 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
631 Py_DECREF(new_stderr);
632 throw Exception("Error during execution");
635 DEBTRACE( "-----------------PyNode::outputs-----------------" );
636 list<OutputPort *>::iterator iter;
639 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
641 OutputPyPort *p=(OutputPyPort *)*iter;
642 DEBTRACE( "port name: " << p->getName() );
643 DEBTRACE( "port kind: " << p->edGetType()->kind() );
644 PyObject *ob=PyDict_GetItemString(_context,p->getName().c_str());
647 std::string msg="Error during execution: there is no variable ";
648 msg=msg+p->getName()+" in node context";
650 throw Exception(msg);
652 DEBTRACE( "PyNode::outputs::ob refcnt: " << ob->ob_refcnt );
654 PyObject_Print(ob,stderr,Py_PRINT_RAW);
660 catch(ConversionException& ex)
662 _errorDetails=ex.what();
667 DEBTRACE( "-----------------End PyNode::outputs-----------------" );
669 DEBTRACE( "++++++++++++++ End PyNode::execute: " << getName() << " ++++++++++++++++++++" );
672 void PythonNode::squeezeMemorySafe()
675 if(_mode==PythonNode::REMOTE_NAME)
676 this->squeezeMemoryRemote();
678 this->squeezeMemory();
681 void PythonNode::squeezeMemory()
683 for(auto p : _setOfInputPort)
685 PyDict_DelItemString(_context,p->getName().c_str());
686 InputPyPort *p2(static_cast<InputPyPort *>(p));
687 if(p2->canSafelySqueezeMemory())
690 for(auto p : _setOfOutputPort)
692 PyDict_DelItemString(_context,p->getName().c_str());
693 OutputPyPort *p2(static_cast<OutputPyPort *>(p));
694 p2->putWithoutForward(Py_None);
698 void PythonNode::squeezeMemoryRemote()
700 for(auto p : _setOfInputPort)
702 InputPyPort *p2(static_cast<InputPyPort *>(p));
703 if(p2->canSafelySqueezeMemory())
706 for(auto p : _setOfOutputPort)
708 OutputPyPort *p2(static_cast<OutputPyPort *>(p));
709 p2->putWithoutForward(Py_None);
713 std::string PythonNode::getContainerLog()
715 return PythonEntry::GetContainerLog(_mode,_container,this);
718 void PythonNode::shutdown(int level)
720 DEBTRACE("PythonNode::shutdown " << level);
721 if(_mode=="local")return;
724 if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
725 _pynode=Engines::PyScriptNode::_nil();
726 _container->shutdown(level);
730 void PythonNode::imposeResource(const std::string& resource_name,
731 const std::string& container_name)
733 if(!resource_name.empty() && !container_name.empty())
735 _imposedResource = resource_name;
736 _imposedContainer = container_name;
740 bool PythonNode::canAcceptImposedResource()
742 return _container != nullptr && _container->canAcceptImposedResource();
745 std::string PythonNode::pythonEntryName()const
748 return "DEFAULT_NAME_FOR_UNIQUE_PYTHON_NODE_ENTRY";
753 bool PythonNode::storeContext()const
757 found = _container->storeContext();
761 Node *PythonNode::simpleClone(ComposedNode *father, bool editionOnly) const
763 return new PythonNode(*this,father);
766 void PythonNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
768 if(!CORBA::is_nil(_pynode))
769 _pynode->UnRegister();
770 _pynode=objContainer->createPyScriptNode(pythonEntryName().c_str(),getScript().c_str());
774 Engines::PyNodeBase_var PythonNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
776 Engines::PyScriptNode_var ret(objContainer->getDefaultPyScriptNode(pythonEntryName().c_str()));
777 if(!CORBA::is_nil(ret))
781 return Engines::PyNodeBase::_narrow(ret);
784 void PythonNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
786 if(CORBA::is_nil(_pynode))
787 _pynode=Engines::PyScriptNode::_narrow(remoteInterp);
790 Engines::PyScriptNode_var tmpp(Engines::PyScriptNode::_narrow(remoteInterp));
791 if(!_pynode->_is_equivalent(tmpp))
793 _pynode->UnRegister();
794 _pynode=Engines::PyScriptNode::_narrow(remoteInterp);
797 _pynode->assignNewCompiledCode(getScript().c_str());
800 Engines::PyNodeBase_var PythonNode::getRemoteInterpreterHandle()
802 return Engines::PyNodeBase::_narrow(_pynode);
805 //! Create a new node of same type with a given name
806 PythonNode* PythonNode::cloneNode(const std::string& name)
808 PythonNode* n=new PythonNode(name);
809 n->setScript(_script);
810 list<InputPort *>::iterator iter;
811 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
813 InputPyPort *p=(InputPyPort *)*iter;
814 DEBTRACE( "port name: " << p->getName() );
815 DEBTRACE( "port kind: " << p->edGetType()->kind() );
816 n->edAddInputPort(p->getName(),p->edGetType());
818 list<OutputPort *>::iterator iter2;
819 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
821 OutputPyPort *p=(OutputPyPort *)*iter2;
822 DEBTRACE( "port name: " << p->getName() );
823 DEBTRACE( "port kind: " << p->edGetType()->kind() );
824 n->edAddOutputPort(p->getName(),p->edGetType());
829 void PythonNode::applyDPLScope(ComposedNode *gfn)
831 std::vector< std::pair<std::string,int> > ret(getDPLScopeInfo(gfn));
838 std::size_t sz(ret.size());
840 for(std::size_t i=0;i<sz;i++)
842 const std::pair<std::string,int>& p(ret[i]);
843 PyObject *elt(PyTuple_New(2));
844 PyTuple_SetItem(elt,0,PyUnicode_FromString(p.first.c_str()));
845 PyTuple_SetItem(elt,1,PyLong_FromLong(p.second));
846 PyList_SetItem(ob,i,elt);
849 if(_mode==REMOTE_NAME)
851 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);
854 PyObject *serializationInput(PyObject_CallFunctionObjArgs(_pyfuncSimpleSer,ob,NULL));
856 char *serializationInputC(0);
858 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
859 throw Exception("DistributedPythonNode problem in python pickle");
860 serializationInputCorba->length(len);
861 for(int i=0; i < len ; i++)
862 serializationInputCorba[i]=serializationInputC[i];
863 Py_XDECREF(serializationInput);
865 _pynode->defineNewCustomVar(DPL_INFO_NAME,serializationInputCorba);
870 PyDict_SetItemString(_context,DPL_INFO_NAME,ob);
875 PyFuncNode::PyFuncNode(const PyFuncNode& other, ComposedNode *father):InlineFuncNode(other,father),_pyfunc(0)
877 _implementation = PythonNode::IMPL_NAME;
880 _context=PyDict_New();
881 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
882 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
885 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
886 _errorDetails=msg.str();
887 throw Exception(msg.str());
892 PyFuncNode::PyFuncNode(const std::string& name): InlineFuncNode(name),_pyfunc(0)
895 _implementation = PythonNode::IMPL_NAME;
896 DEBTRACE( "PyFuncNode::PyFuncNode " << name );
899 _context=PyDict_New();
900 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
901 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
904 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
905 _errorDetails=msg.str();
906 throw Exception(msg.str());
911 PyFuncNode::~PyFuncNode()
913 if(!CORBA::is_nil(_pynode))
915 _pynode->UnRegister();
919 void PyFuncNode::init(bool start)
921 initCommonPartWithoutStateManagement(start);
922 if(_state == YACS::DISABLED)
924 exDisabledState(); // to refresh propagation of DISABLED state
927 if(start) //complete initialization
928 setState(YACS::READY);
929 else if(_state > YACS::LOADED)// WARNING FuncNode has internal vars (CEA usecase) ! Partial initialization (inside a loop). Exclusivity of funcNode.
930 setState(YACS::TORECONNECT);
933 void PyFuncNode::checkBasicConsistency() const
935 DEBTRACE("checkBasicConsistency");
936 InlineFuncNode::checkBasicConsistency();
940 res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
943 std::string error="";
944 PyObject* new_stderr = newPyStdOut(error);
945 PySys_SetObject((char*)"stderr", new_stderr);
947 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
948 Py_DECREF(new_stderr);
949 throw Exception(error);
956 void PyFuncNode::load()
958 DEBTRACE( "---------------PyfuncNode::load function---------------" );
959 if(_mode==PythonNode::REMOTE_NAME)
965 void PyFuncNode::loadRemote()
967 commonRemoteLoad(this);
970 void PyFuncNode::loadLocal()
972 DEBTRACE( "---------------PyFuncNode::load function " << getName() << " ---------------" );
976 list<OutputPort *>::iterator iter;
977 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
979 OutputPyPort *p=(OutputPyPort *)*iter;
980 DEBTRACE( "port name: " << p->getName() );
981 DEBTRACE( "port kind: " << p->edGetType()->kind() );
987 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
989 std::ostringstream stream;
990 stream << "/tmp/PythonNode_";
993 PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
997 PyObject* new_stderr = newPyStdOut(_errorDetails);
998 PySys_SetObject((char*)"stderr", new_stderr);
1000 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1001 Py_DECREF(new_stderr);
1002 throw Exception("Error during execution");
1004 PyObject *res = PyEval_EvalCode( code, _context, _context);
1008 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
1009 if(PyErr_Occurred ())
1012 PyObject* new_stderr = newPyStdOut(_errorDetails);
1013 PySys_SetObject((char*)"stderr", new_stderr);
1014 ofstream errorfile(stream.str().c_str());
1015 if (errorfile.is_open())
1017 errorfile << _script;
1021 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1022 Py_DECREF(new_stderr);
1023 throw Exception("Error during execution");
1026 _pyfunc=PyDict_GetItemString(_context,_fname.c_str());
1027 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1031 PyObject* new_stderr = newPyStdOut(_errorDetails);
1032 PySys_SetObject((char*)"stderr", new_stderr);
1034 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1035 Py_DECREF(new_stderr);
1036 throw Exception("Error during execution");
1038 DEBTRACE( "---------------End PyFuncNode::load function---------------" );
1042 void PyFuncNode::execute()
1044 if(_mode==PythonNode::REMOTE_NAME)
1050 void PyFuncNode::executeRemote()
1052 DEBTRACE( "++++++++++++++ PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
1054 throw Exception("DistributedPythonNode badly loaded");
1056 if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
1059 loadPythonAdapter(this,dummy);
1060 _pynode->executeAnotherPieceOfCode(getScript().c_str());
1063 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);;
1067 //===========================================================================
1068 // Get inputs in input ports, build a Python tuple and pickle it
1069 //===========================================================================
1070 PyObject *args(PyTuple_New(getNumberOfInputPorts()));
1072 for(std::list<InputPort *>::iterator iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++,pos++)
1074 InputPyPort *p=(InputPyPort *)*iter2;
1077 PyTuple_SetItem(args,pos,ob);
1080 PyObject_Print(args,stderr,Py_PRINT_RAW);
1083 PyObject *serializationInput=PyObject_CallObject(_pyfuncSer,args);
1085 //The pickled string may contain NULL characters so use PyString_AsStringAndSize
1086 char *serializationInputC(0);
1088 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
1089 throw Exception("DistributedPythonNode problem in python pickle");
1091 serializationInputCorba->length(len);
1092 for(int i=0; i < len ; i++)
1093 serializationInputCorba[i]=serializationInputC[i];
1094 Py_DECREF(serializationInput);
1097 //===========================================================================
1098 // Execute in remote Python node
1099 //===========================================================================
1100 DEBTRACE( "-----------------starting remote python invocation-----------------" );
1101 Engines::pickledArgs_var resultCorba;
1104 resultCorba=_pynode->execute(getFname().c_str(),serializationInputCorba);
1106 catch( const SALOME::SALOME_Exception& ex )
1108 std::string msg="Exception on remote python invocation";
1110 msg += ex.details.text.in();
1112 throw Exception(msg);
1114 DEBTRACE( "-----------------end of remote python invocation-----------------" );
1115 //===========================================================================
1116 // Get results, unpickle and put them in output ports
1117 //===========================================================================
1118 char *resultCorbaC=new char[resultCorba->length()+1];
1119 resultCorbaC[resultCorba->length()]='\0';
1120 for(int i=0;i<resultCorba->length();i++)
1121 resultCorbaC[i]=resultCorba[i];
1126 PyObject *resultPython(PyBytes_FromStringAndSize(resultCorbaC,resultCorba->length()));
1127 delete [] resultCorbaC;
1128 PyObject *args(PyTuple_New(1)),*ob(0);
1129 PyTuple_SetItem(args,0,resultPython);
1130 PyObject *finalResult=PyObject_CallObject(_pyfuncUnser,args);
1133 DEBTRACE( "-----------------PythonNode::outputs-----------------" );
1135 if(finalResult == Py_None)
1137 else if(PyTuple_Check(finalResult))
1138 nres=PyTuple_Size(finalResult);
1140 if(getNumberOfOutputPorts() != nres)
1142 std::string msg="Number of output arguments : Mismatch between definition and execution";
1143 Py_DECREF(finalResult);
1145 throw Exception(msg);
1151 for(std::list<OutputPort *>::iterator iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++, pos++)
1153 OutputPyPort *p=(OutputPyPort *)*iter;
1154 DEBTRACE( "port name: " << p->getName() );
1155 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1156 DEBTRACE( "port pos : " << pos );
1157 if(PyTuple_Check(finalResult))
1158 ob=PyTuple_GetItem(finalResult,pos) ;
1161 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1164 Py_DECREF(finalResult);
1166 catch(ConversionException& ex)
1168 Py_DECREF(finalResult);
1169 _errorDetails=ex.what();
1174 DEBTRACE( "++++++++++++++ ENDOF PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
1177 void PyFuncNode::executeLocal()
1179 DEBTRACE( "++++++++++++++ PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1183 if(!_pyfunc)throw Exception("PyFuncNode badly loaded");
1186 DEBTRACE( "---------------PyFuncNode::inputs---------------" );
1187 PyObject* args = PyTuple_New(getNumberOfInputPorts()) ;
1188 list<InputPort *>::iterator iter2;
1189 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
1191 InputPyPort *p=(InputPyPort *)*iter2;
1192 DEBTRACE( "port name: " << p->getName() );
1193 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1196 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1199 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1201 PyTuple_SetItem(args,pos,ob);
1202 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1205 DEBTRACE( "---------------End PyFuncNode::inputs---------------" );
1207 DEBTRACE( "----------------PyFuncNode::calculation---------------" );
1209 PyObject_Print(_pyfunc,stderr,Py_PRINT_RAW);
1211 PyObject_Print(args,stderr,Py_PRINT_RAW);
1214 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1215 PyObject* result = PyObject_CallObject( _pyfunc , args ) ;
1216 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1223 PyObject* new_stderr = newPyStdOut(_errorDetails);
1224 PySys_SetObject((char*)"stderr", new_stderr);
1225 std::ostringstream stream;
1226 stream << "/tmp/PythonNode_";
1228 ofstream errorfile(stream.str().c_str());
1229 if (errorfile.is_open())
1231 errorfile << _script;
1235 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1236 Py_DECREF(new_stderr);
1237 throw Exception("Error during execution");
1239 DEBTRACE( "----------------End PyFuncNode::calculation---------------" );
1241 DEBTRACE( "-----------------PyFuncNode::outputs-----------------" );
1243 if(result == Py_None)
1245 else if(PyTuple_Check(result))
1246 nres=PyTuple_Size(result);
1248 if(getNumberOfOutputPorts() != nres)
1250 std::string msg="Number of output arguments : Mismatch between definition and execution";
1253 throw Exception(msg);
1258 PyObject_Print(result,stderr,Py_PRINT_RAW);
1261 list<OutputPort *>::iterator iter;
1264 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
1266 OutputPyPort *p=(OutputPyPort *)*iter;
1267 DEBTRACE( "port name: " << p->getName() );
1268 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1269 DEBTRACE( "port pos : " << pos );
1270 if(PyTuple_Check(result))ob=PyTuple_GetItem(result,pos) ;
1272 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1274 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1281 catch(ConversionException& ex)
1284 _errorDetails=ex.what();
1287 DEBTRACE( "-----------------End PyFuncNode::outputs-----------------" );
1290 DEBTRACE( "++++++++++++++ End PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1293 Node *PyFuncNode::simpleClone(ComposedNode *father, bool editionOnly) const
1295 return new PyFuncNode(*this,father);
1298 void PyFuncNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
1300 if(!CORBA::is_nil(_pynode))
1301 _pynode->UnRegister();
1302 _pynode=objContainer->createPyNode(getName().c_str(),getScript().c_str());
1305 Engines::PyNodeBase_var PyFuncNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
1307 Engines::PyNode_var ret(objContainer->getDefaultPyNode(getName().c_str()));
1308 if(!CORBA::is_nil(ret))
1312 return Engines::PyNodeBase::_narrow(ret);
1315 void PyFuncNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
1317 if(!CORBA::is_nil(_pynode))
1319 Engines::PyNode_var tmpp(Engines::PyNode::_narrow(remoteInterp));
1320 if(_pynode->_is_equivalent(tmpp))
1323 if(!CORBA::is_nil(_pynode))
1324 _pynode->UnRegister();
1325 _pynode=Engines::PyNode::_narrow(remoteInterp);
1328 Engines::PyNodeBase_var PyFuncNode::getRemoteInterpreterHandle()
1330 return Engines::PyNodeBase::_narrow(_pynode);
1333 //! Create a new node of same type with a given name
1334 PyFuncNode* PyFuncNode::cloneNode(const std::string& name)
1336 PyFuncNode* n=new PyFuncNode(name);
1337 n->setScript(_script);
1338 n->setFname(_fname);
1339 list<InputPort *>::iterator iter;
1340 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
1342 InputPyPort *p=(InputPyPort *)*iter;
1343 n->edAddInputPort(p->getName(),p->edGetType());
1345 list<OutputPort *>::iterator iter2;
1346 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
1348 OutputPyPort *p=(OutputPyPort *)*iter2;
1349 n->edAddOutputPort(p->getName(),p->edGetType());
1354 std::string PyFuncNode::getContainerLog()
1356 return PythonEntry::GetContainerLog(_mode,_container,this);
1359 void PyFuncNode::shutdown(int level)
1361 DEBTRACE("PyFuncNode::shutdown " << level);
1362 if(_mode=="local")return;
1365 if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
1366 _pynode=Engines::PyNode::_nil();
1367 _container->shutdown(level);
1371 void PyFuncNode::imposeResource(const std::string& resource_name,
1372 const std::string& container_name)
1374 if(!resource_name.empty() && !container_name.empty())
1376 _imposedResource = resource_name;
1377 _imposedContainer = container_name;
1381 bool PyFuncNode::canAcceptImposedResource()
1383 return _container != nullptr && _container->canAcceptImposedResource();