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 const char PythonNode::KEEP_CONTEXT_PROPERTY[]="KEEP_CONTEXT";
81 PythonEntry::PythonEntry():_context(0),_pyfuncSer(0),_pyfuncUnser(0),_pyfuncSimpleSer(0)
85 PythonEntry::~PythonEntry()
88 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
89 // not Py_XDECREF of _pyfuncUnser because it is returned by PyDict_GetItem -> borrowed
90 // not Py_XDECREF of _pyfuncSer because it is returned by PyDict_GetItem -> borrowed
94 void PythonEntry::loadRemoteContainer(InlineNode *reqNode)
96 DEBTRACE( "---------------PythonEntry::CommonRemoteLoad function---------------" );
97 Container *container(reqNode->getContainer());
98 bool isContAlreadyStarted(false);
101 isContAlreadyStarted=container->isAlreadyStarted(reqNode);
102 if(!isContAlreadyStarted)
106 if(hasImposedResource())
107 container->start(reqNode, _imposedResource, _imposedContainer);
109 container->start(reqNode);
113 reqNode->setErrorDetails(e.what());
120 std::string what("PythonEntry::CommonRemoteLoad : a load operation requested on \"");
121 what+=reqNode->getName(); what+="\" with no container specified.";
122 reqNode->setErrorDetails(what);
123 throw Exception(what);
127 Engines::Container_var GetContainerObj(InlineNode *reqNode, bool& isStandardCont)
129 isStandardCont = false;
130 Container *container(reqNode->getContainer());
131 Engines::Container_var objContainer(Engines::Container::_nil());
133 throw YACS::Exception("No container specified !");
134 SalomeContainer *containerCast0(dynamic_cast<SalomeContainer *>(container));
135 SalomeHPContainer *containerCast1(dynamic_cast<SalomeHPContainer *>(container));
138 isStandardCont = true;
139 objContainer=containerCast0->getContainerPtr(reqNode);
141 else if(containerCast1)
143 YACS::BASES::AutoCppPtr<SalomeContainerTmpForHP> tmpCont(SalomeContainerTmpForHP::BuildFrom(containerCast1,reqNode));
144 objContainer=tmpCont->getContainerPtr(reqNode);
147 throw YACS::Exception("Unrecognized type of container ! Salome one is expected for PythonNode/PyFuncNode !");
148 if(CORBA::is_nil(objContainer))
149 throw YACS::Exception("Container corba pointer is NULL for PythonNode !");
153 Engines::Container_var PythonEntry::loadPythonAdapter(InlineNode *reqNode, bool& isInitializeRequested)
155 bool isStandardCont(true);
156 Engines::Container_var objContainer(GetContainerObj(reqNode,isStandardCont));
157 isInitializeRequested=false;
160 Engines::PyNodeBase_var dftPyScript(retrieveDftRemotePyInterpretorIfAny(objContainer));
161 if(CORBA::is_nil(dftPyScript))
163 isInitializeRequested=!isStandardCont;
164 createRemoteAdaptedPyInterpretor(objContainer);
167 assignRemotePyInterpretor(dftPyScript);
169 catch( const SALOME::SALOME_Exception& ex )
171 std::string msg="Exception on remote python node creation ";
173 msg += ex.details.text.in();
174 reqNode->setErrorDetails(msg);
175 throw Exception(msg);
177 Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
178 if(CORBA::is_nil(pynode))
179 throw Exception("In PythonNode the ref in NULL ! ");
183 void PythonEntry::loadRemoteContext(InlineNode *reqNode, Engines::Container_ptr objContainer, bool isInitializeRequested)
185 Container *container(reqNode->getContainer());
186 Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
190 const char *picklizeScript(getSerializationScript());
191 PyObject *res=PyRun_String(picklizeScript,Py_file_input,_context,_context);
192 PyObject *res2(PyRun_String(SCRIPT_FOR_SIMPLE_SERIALIZATION,Py_file_input,_context,_context));
193 if(res == NULL || res2==NULL)
195 std::string errorDetails;
196 PyObject* new_stderr = newPyStdOut(errorDetails);
197 reqNode->setErrorDetails(errorDetails);
198 PySys_SetObject((char*)"stderr", new_stderr);
200 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
201 Py_DECREF(new_stderr);
202 throw Exception("Error during load");
204 Py_DECREF(res); Py_DECREF(res2);
205 _pyfuncSer=PyDict_GetItemString(_context,"pickleForDistPyth2009");
206 _pyfuncUnser=PyDict_GetItemString(_context,"unPickleForDistPyth2009");
207 _pyfuncSimpleSer=PyDict_GetItemString(_context,"pickleForVarSimplePyth2009");
208 if(_pyfuncSer == NULL)
210 std::string errorDetails;
211 PyObject *new_stderr(newPyStdOut(errorDetails));
212 reqNode->setErrorDetails(errorDetails);
213 PySys_SetObject((char*)"stderr", new_stderr);
215 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
216 Py_DECREF(new_stderr);
217 throw Exception("Error during load");
219 if(_pyfuncUnser == NULL)
221 std::string errorDetails;
222 PyObject *new_stderr(newPyStdOut(errorDetails));
223 reqNode->setErrorDetails(errorDetails);
224 PySys_SetObject((char*)"stderr", new_stderr);
226 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
227 Py_DECREF(new_stderr);
228 throw Exception("Error during load");
230 if(_pyfuncSimpleSer == NULL)
232 std::string errorDetails;
233 PyObject *new_stderr(newPyStdOut(errorDetails));
234 reqNode->setErrorDetails(errorDetails);
235 PySys_SetObject((char*)"stderr", new_stderr);
237 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
238 Py_DECREF(new_stderr);
239 throw Exception("Error during load");
242 if(isInitializeRequested)
243 {//This one is called only once at initialization in the container if an init-script is specified.
246 std::string zeInitScriptKey(container->getProperty(HomogeneousPoolContainer::INITIALIZE_SCRIPT_KEY));
247 if(!zeInitScriptKey.empty())
248 pynode->executeAnotherPieceOfCode(zeInitScriptKey.c_str());
250 catch( const SALOME::SALOME_Exception& ex )
252 std::string msg="Exception on PythonNode::loadRemote python invocation of initializisation py script !";
254 msg += ex.details.text.in();
255 reqNode->setErrorDetails(msg);
256 throw Exception(msg);
258 DEBTRACE( "---------------End PyNode::loadRemote function---------------" );
262 // TODO: verify removing
263 std::string PythonEntry::GetContainerLog(const std::string& mode, Container *container, const Task *askingTask)
271 SalomeContainer *containerCast(dynamic_cast<SalomeContainer *>(container));
272 SalomeHPContainer *objContainer2(dynamic_cast<SalomeHPContainer *>(container));
275 Engines::Container_var objContainer(containerCast->getContainerPtr(askingTask));
276 CORBA::String_var logname = objContainer->logfilename();
279 std::string::size_type pos = msg.find(":");
280 msg=msg.substr(pos+1);
282 else if(objContainer2)
284 msg="Remote PythonNode is on HP Container : no log because no info of the location by definition of HP Container !";
288 msg="Not implemented yet for container log for that type of container !";
293 msg = "Container no longer reachable";
298 void PythonEntry::commonRemoteLoad(InlineNode *reqNode)
300 loadRemoteContainer(reqNode);
301 bool isInitializeRequested;
302 Engines::Container_var objContainer(loadPythonAdapter(reqNode,isInitializeRequested));
303 loadRemoteContext(reqNode,objContainer,isInitializeRequested);
306 bool PythonEntry::hasImposedResource()const
308 return !_imposedResource.empty() && !_imposedContainer.empty();
311 PythonNode::PythonNode(const PythonNode& other, ComposedNode *father):InlineNode(other,father),_autoSqueeze(other._autoSqueeze)
313 _pynode = Engines::PyScriptNode::_nil();
314 _implementation=IMPL_NAME;
317 _context=PyDict_New();
318 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
321 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
322 _errorDetails=msg.str();
323 throw Exception(msg.str());
328 PythonNode::PythonNode(const std::string& name):InlineNode(name)
330 _pynode = Engines::PyScriptNode::_nil();
331 _implementation=IMPL_NAME;
334 _context=PyDict_New();
335 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
338 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
339 _errorDetails=msg.str();
340 throw Exception(msg.str());
345 PythonNode::~PythonNode()
347 if(!CORBA::is_nil(_pynode))
349 _pynode->UnRegister();
353 void PythonNode::checkBasicConsistency() const
355 DEBTRACE("checkBasicConsistency");
356 InlineNode::checkBasicConsistency();
360 res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
363 std::string error="";
364 PyObject* new_stderr = newPyStdOut(error);
365 PySys_SetObject((char*)"stderr", new_stderr);
367 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
368 Py_DECREF(new_stderr);
369 throw Exception(error);
376 void PythonNode::load()
378 DEBTRACE( "---------------PyNode::load function---------------" );
379 if(_mode==PythonNode::REMOTE_NAME)
385 void PythonNode::loadLocal()
387 DEBTRACE( "---------------PyNode::loadLocal function---------------" );
391 void PythonNode::loadRemote()
393 commonRemoteLoad(this);
396 void PythonNode::execute()
398 if(_mode==PythonNode::REMOTE_NAME)
404 void PythonNode::executeRemote()
406 DEBTRACE( "++++++++++++++ PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
408 throw Exception("DistributedPythonNode badly loaded");
410 if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
413 loadPythonAdapter(this,dummy);
414 _pynode->assignNewCompiledCode(getScript().c_str());
417 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);
420 PyObject *args(0),*ob(0);
421 //===========================================================================
422 // Get inputs in input ports, build a Python dict and pickle it
423 //===========================================================================
425 std::list<InputPort *>::iterator iter2;
427 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); ++iter2)
429 InputPyPort *p=(InputPyPort *)*iter2;
431 PyDict_SetItemString(args,p->getName().c_str(),ob);
435 PyObject_Print(args,stderr,Py_PRINT_RAW);
438 PyObject *serializationInput(PyObject_CallFunctionObjArgs(_pyfuncSer,args,NULL));
440 //The pickled string may contain NULL characters so use PyString_AsStringAndSize
441 char *serializationInputC(0);
443 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
444 throw Exception("DistributedPythonNode problem in python pickle");
445 serializationInputCorba->length(len);
446 for(int i=0; i < len ; i++)
447 serializationInputCorba[i]=serializationInputC[i];
448 Py_DECREF(serializationInput);
451 //get the list of output argument names
452 std::list<OutputPort *>::iterator iter;
453 Engines::listofstring myseq;
454 myseq.length(getNumberOfOutputPorts());
456 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
458 OutputPyPort *p=(OutputPyPort *)*iter;
459 myseq[pos]=p->getName().c_str();
460 DEBTRACE( "port name: " << p->getName() );
461 DEBTRACE( "port kind: " << p->edGetType()->kind() );
462 DEBTRACE( "port pos : " << pos );
465 //===========================================================================
466 // Execute in remote Python node
467 //===========================================================================
468 DEBTRACE( "-----------------starting remote python invocation-----------------" );
469 Engines::pickledArgs_var resultCorba;
472 //pass outargsname and dict serialized
473 resultCorba=_pynode->execute(myseq,serializationInputCorba);
475 catch( const SALOME::SALOME_Exception& ex )
477 std::string msg="Exception on remote python invocation";
479 msg += ex.details.text.in();
481 throw Exception(msg);
483 DEBTRACE( "-----------------end of remote python invocation-----------------" );
484 //===========================================================================
485 // Get results, unpickle and put them in output ports
486 //===========================================================================
487 char *resultCorbaC=new char[resultCorba->length()+1];
488 resultCorbaC[resultCorba->length()]='\0';
489 for(int i=0;i<resultCorba->length();i++)
490 resultCorbaC[i]=resultCorba[i];
494 PyObject *args(0),*ob(0);
495 PyObject* resultPython=PyBytes_FromStringAndSize(resultCorbaC,resultCorba->length());
496 delete [] resultCorbaC;
497 args = PyTuple_New(1);
498 PyTuple_SetItem(args,0,resultPython);
499 PyObject *finalResult=PyObject_CallObject(_pyfuncUnser,args);
502 if (finalResult == NULL)
504 std::stringstream msg;
505 msg << "Conversion with pickle of output ports failed !";
506 msg << " : " << __FILE__ << ":" << __LINE__;
507 _errorDetails=msg.str();
508 throw YACS::ENGINE::ConversionException(msg.str());
511 DEBTRACE( "-----------------PythonNode::outputs-----------------" );
513 if(finalResult == Py_None)
515 else if(PyTuple_Check(finalResult))
516 nres=PyTuple_Size(finalResult);
518 if(getNumberOfOutputPorts() != nres)
520 std::string msg="Number of output arguments : Mismatch between definition and execution";
521 Py_DECREF(finalResult);
523 throw Exception(msg);
529 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
531 OutputPyPort *p=(OutputPyPort *)*iter;
532 DEBTRACE( "port name: " << p->getName() );
533 DEBTRACE( "port kind: " << p->edGetType()->kind() );
534 DEBTRACE( "port pos : " << pos );
535 if(PyTuple_Check(finalResult))
536 ob=PyTuple_GetItem(finalResult,pos) ;
539 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
543 Py_DECREF(finalResult);
545 catch(ConversionException& ex)
547 Py_DECREF(finalResult);
548 _errorDetails=ex.what();
552 squeezeMemoryRemote();
557 if(!CORBA::is_nil(_pynode))
559 _pynode->UnRegister();
561 _pynode = Engines::PyScriptNode::_nil();
563 Engines::Container_var cont(GetContainerObj(this,dummy));
564 cont->removePyScriptNode(getName().c_str());
566 DEBTRACE( "++++++++++++++ ENDOF PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
569 void PythonNode::executeLocal()
571 DEBTRACE( "++++++++++++++ PyNode::executeLocal: " << getName() << " ++++++++++++++++++++" );
575 DEBTRACE( "---------------PyNode::inputs---------------" );
576 list<InputPort *>::iterator iter2;
577 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
579 InputPyPort *p=(InputPyPort *)*iter2;
580 DEBTRACE( "port name: " << p->getName() );
581 DEBTRACE( "port kind: " << p->edGetType()->kind() );
582 PyObject* ob=p->getPyObj();
583 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
585 PyObject_Print(ob,stderr,Py_PRINT_RAW);
588 int ier=PyDict_SetItemString(_context,p->getName().c_str(),ob);
589 DEBTRACE( "after PyDict_SetItemString:ob refcnt: " << ob->ob_refcnt );
592 DEBTRACE( "---------------End PyNode::inputs---------------" );
595 DEBTRACE( "----------------PyNode::calculation---------------" );
597 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
599 std::ostringstream stream;
600 stream << "/tmp/PythonNode_";
603 PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
607 PyObject* new_stderr = newPyStdOut(_errorDetails);
608 PySys_SetObject((char*)"stderr", new_stderr);
610 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
611 Py_DECREF(new_stderr);
612 throw Exception("Error during execution");
614 PyObject *res = PyEval_EvalCode( code, _context, _context);
618 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
621 if(PyErr_Occurred ())
624 PyObject* new_stderr = newPyStdOut(_errorDetails);
625 PySys_SetObject((char*)"stderr", new_stderr);
626 ofstream errorfile(stream.str().c_str());
627 if (errorfile.is_open())
629 errorfile << _script;
633 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
634 Py_DECREF(new_stderr);
635 throw Exception("Error during execution");
638 DEBTRACE( "-----------------PyNode::outputs-----------------" );
639 list<OutputPort *>::iterator iter;
642 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
644 OutputPyPort *p=(OutputPyPort *)*iter;
645 DEBTRACE( "port name: " << p->getName() );
646 DEBTRACE( "port kind: " << p->edGetType()->kind() );
647 PyObject *ob=PyDict_GetItemString(_context,p->getName().c_str());
650 std::string msg="Error during execution: there is no variable ";
651 msg=msg+p->getName()+" in node context";
653 throw Exception(msg);
655 DEBTRACE( "PyNode::outputs::ob refcnt: " << ob->ob_refcnt );
657 PyObject_Print(ob,stderr,Py_PRINT_RAW);
663 catch(ConversionException& ex)
665 _errorDetails=ex.what();
670 DEBTRACE( "-----------------End PyNode::outputs-----------------" );
672 DEBTRACE( "++++++++++++++ End PyNode::execute: " << getName() << " ++++++++++++++++++++" );
675 void PythonNode::squeezeMemorySafe()
678 if(_mode==PythonNode::REMOTE_NAME)
679 this->squeezeMemoryRemote();
681 this->squeezeMemory();
684 void PythonNode::squeezeMemory()
686 for(auto p : _setOfInputPort)
688 PyDict_DelItemString(_context,p->getName().c_str());
689 InputPyPort *p2(static_cast<InputPyPort *>(p));
690 if(p2->canSafelySqueezeMemory())
693 for(auto p : _setOfOutputPort)
695 PyDict_DelItemString(_context,p->getName().c_str());
696 OutputPyPort *p2(static_cast<OutputPyPort *>(p));
697 p2->putWithoutForward(Py_None);
701 void PythonNode::squeezeMemoryRemote()
703 for(auto p : _setOfInputPort)
705 InputPyPort *p2(static_cast<InputPyPort *>(p));
706 if(p2->canSafelySqueezeMemory())
709 for(auto p : _setOfOutputPort)
711 OutputPyPort *p2(static_cast<OutputPyPort *>(p));
712 p2->putWithoutForward(Py_None);
716 std::string PythonNode::getContainerLog()
718 return PythonEntry::GetContainerLog(_mode,_container,this);
721 void PythonNode::shutdown(int level)
723 DEBTRACE("PythonNode::shutdown " << level);
724 if(_mode=="local")return;
727 if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
728 _pynode=Engines::PyScriptNode::_nil();
729 _container->shutdown(level);
733 void PythonNode::imposeResource(const std::string& resource_name,
734 const std::string& container_name)
736 if(!resource_name.empty() && !container_name.empty())
738 _imposedResource = resource_name;
739 _imposedContainer = container_name;
743 bool PythonNode::canAcceptImposedResource()
745 return _container != nullptr && _container->canAcceptImposedResource();
748 std::string PythonNode::pythonEntryName()const
751 return "DEFAULT_NAME_FOR_UNIQUE_PYTHON_NODE_ENTRY";
756 bool PythonNode::keepContext()const
758 std::string str_value = const_cast<PythonNode*>(this)->getProperty(KEEP_CONTEXT_PROPERTY);
759 const char* yes_values[] = {"YES", "Yes", "yes", "TRUE", "True", "true", "1",
762 for(const char* v : yes_values)
771 void PythonNode::setKeepContext(bool keep)
774 setProperty(KEEP_CONTEXT_PROPERTY, "true");
776 setProperty(KEEP_CONTEXT_PROPERTY, "false");
779 Node *PythonNode::simpleClone(ComposedNode *father, bool editionOnly) const
781 return new PythonNode(*this,father);
784 void PythonNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
786 if(!CORBA::is_nil(_pynode))
787 _pynode->UnRegister();
788 _pynode=objContainer->createPyScriptNode(pythonEntryName().c_str(),getScript().c_str());
792 Engines::PyNodeBase_var PythonNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
794 Engines::PyScriptNode_var ret(objContainer->getDefaultPyScriptNode(pythonEntryName().c_str()));
795 if(!CORBA::is_nil(ret))
799 return Engines::PyNodeBase::_narrow(ret);
802 void PythonNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
804 if(CORBA::is_nil(_pynode))
805 _pynode=Engines::PyScriptNode::_narrow(remoteInterp);
808 Engines::PyScriptNode_var tmpp(Engines::PyScriptNode::_narrow(remoteInterp));
809 if(!_pynode->_is_equivalent(tmpp))
811 _pynode->UnRegister();
812 _pynode=Engines::PyScriptNode::_narrow(remoteInterp);
815 _pynode->assignNewCompiledCode(getScript().c_str());
818 Engines::PyNodeBase_var PythonNode::getRemoteInterpreterHandle()
820 return Engines::PyNodeBase::_narrow(_pynode);
823 //! Create a new node of same type with a given name
824 PythonNode* PythonNode::cloneNode(const std::string& name)
826 PythonNode* n=new PythonNode(name);
827 n->setScript(_script);
828 list<InputPort *>::iterator iter;
829 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
831 InputPyPort *p=(InputPyPort *)*iter;
832 DEBTRACE( "port name: " << p->getName() );
833 DEBTRACE( "port kind: " << p->edGetType()->kind() );
834 n->edAddInputPort(p->getName(),p->edGetType());
836 list<OutputPort *>::iterator iter2;
837 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
839 OutputPyPort *p=(OutputPyPort *)*iter2;
840 DEBTRACE( "port name: " << p->getName() );
841 DEBTRACE( "port kind: " << p->edGetType()->kind() );
842 n->edAddOutputPort(p->getName(),p->edGetType());
847 void PythonNode::applyDPLScope(ComposedNode *gfn)
849 std::vector< std::pair<std::string,int> > ret(getDPLScopeInfo(gfn));
856 std::size_t sz(ret.size());
858 for(std::size_t i=0;i<sz;i++)
860 const std::pair<std::string,int>& p(ret[i]);
861 PyObject *elt(PyTuple_New(2));
862 PyTuple_SetItem(elt,0,PyUnicode_FromString(p.first.c_str()));
863 PyTuple_SetItem(elt,1,PyLong_FromLong(p.second));
864 PyList_SetItem(ob,i,elt);
867 if(_mode==REMOTE_NAME)
869 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);
872 PyObject *serializationInput(PyObject_CallFunctionObjArgs(_pyfuncSimpleSer,ob,NULL));
874 char *serializationInputC(0);
876 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
877 throw Exception("DistributedPythonNode problem in python pickle");
878 serializationInputCorba->length(len);
879 for(int i=0; i < len ; i++)
880 serializationInputCorba[i]=serializationInputC[i];
881 Py_XDECREF(serializationInput);
883 _pynode->defineNewCustomVar(DPL_INFO_NAME,serializationInputCorba);
888 PyDict_SetItemString(_context,DPL_INFO_NAME,ob);
893 PyFuncNode::PyFuncNode(const PyFuncNode& other, ComposedNode *father):InlineFuncNode(other,father),_pyfunc(0)
895 _implementation = PythonNode::IMPL_NAME;
898 _context=PyDict_New();
899 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
900 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
903 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
904 _errorDetails=msg.str();
905 throw Exception(msg.str());
910 PyFuncNode::PyFuncNode(const std::string& name): InlineFuncNode(name),_pyfunc(0)
913 _implementation = PythonNode::IMPL_NAME;
914 DEBTRACE( "PyFuncNode::PyFuncNode " << name );
917 _context=PyDict_New();
918 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
919 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
922 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
923 _errorDetails=msg.str();
924 throw Exception(msg.str());
929 PyFuncNode::~PyFuncNode()
931 if(!CORBA::is_nil(_pynode))
933 _pynode->UnRegister();
937 void PyFuncNode::init(bool start)
939 initCommonPartWithoutStateManagement(start);
940 if(_state == YACS::DISABLED)
942 exDisabledState(); // to refresh propagation of DISABLED state
945 if(start) //complete initialization
946 setState(YACS::READY);
947 else if(_state > YACS::LOADED)// WARNING FuncNode has internal vars (CEA usecase) ! Partial initialization (inside a loop). Exclusivity of funcNode.
948 setState(YACS::TORECONNECT);
951 void PyFuncNode::checkBasicConsistency() const
953 DEBTRACE("checkBasicConsistency");
954 InlineFuncNode::checkBasicConsistency();
958 res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
961 std::string error="";
962 PyObject* new_stderr = newPyStdOut(error);
963 PySys_SetObject((char*)"stderr", new_stderr);
965 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
966 Py_DECREF(new_stderr);
967 throw Exception(error);
974 void PyFuncNode::load()
976 DEBTRACE( "---------------PyfuncNode::load function---------------" );
977 if(_mode==PythonNode::REMOTE_NAME)
983 void PyFuncNode::loadRemote()
985 commonRemoteLoad(this);
988 void PyFuncNode::loadLocal()
990 DEBTRACE( "---------------PyFuncNode::load function " << getName() << " ---------------" );
994 list<OutputPort *>::iterator iter;
995 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
997 OutputPyPort *p=(OutputPyPort *)*iter;
998 DEBTRACE( "port name: " << p->getName() );
999 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1005 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
1007 std::ostringstream stream;
1008 stream << "/tmp/PythonNode_";
1011 PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
1015 PyObject* new_stderr = newPyStdOut(_errorDetails);
1016 PySys_SetObject((char*)"stderr", new_stderr);
1018 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1019 Py_DECREF(new_stderr);
1020 throw Exception("Error during execution");
1022 PyObject *res = PyEval_EvalCode( code, _context, _context);
1026 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
1027 if(PyErr_Occurred ())
1030 PyObject* new_stderr = newPyStdOut(_errorDetails);
1031 PySys_SetObject((char*)"stderr", new_stderr);
1032 ofstream errorfile(stream.str().c_str());
1033 if (errorfile.is_open())
1035 errorfile << _script;
1039 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1040 Py_DECREF(new_stderr);
1041 throw Exception("Error during execution");
1044 _pyfunc=PyDict_GetItemString(_context,_fname.c_str());
1045 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1049 PyObject* new_stderr = newPyStdOut(_errorDetails);
1050 PySys_SetObject((char*)"stderr", new_stderr);
1052 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1053 Py_DECREF(new_stderr);
1054 throw Exception("Error during execution");
1056 DEBTRACE( "---------------End PyFuncNode::load function---------------" );
1060 void PyFuncNode::execute()
1062 if(_mode==PythonNode::REMOTE_NAME)
1068 void PyFuncNode::executeRemote()
1070 DEBTRACE( "++++++++++++++ PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
1072 throw Exception("DistributedPythonNode badly loaded");
1074 if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
1077 loadPythonAdapter(this,dummy);
1078 _pynode->executeAnotherPieceOfCode(getScript().c_str());
1081 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);;
1085 //===========================================================================
1086 // Get inputs in input ports, build a Python tuple and pickle it
1087 //===========================================================================
1088 PyObject *args(PyTuple_New(getNumberOfInputPorts()));
1090 for(std::list<InputPort *>::iterator iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++,pos++)
1092 InputPyPort *p=(InputPyPort *)*iter2;
1095 PyTuple_SetItem(args,pos,ob);
1098 PyObject_Print(args,stderr,Py_PRINT_RAW);
1101 PyObject *serializationInput=PyObject_CallObject(_pyfuncSer,args);
1103 //The pickled string may contain NULL characters so use PyString_AsStringAndSize
1104 char *serializationInputC(0);
1106 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
1107 throw Exception("DistributedPythonNode problem in python pickle");
1109 serializationInputCorba->length(len);
1110 for(int i=0; i < len ; i++)
1111 serializationInputCorba[i]=serializationInputC[i];
1112 Py_DECREF(serializationInput);
1115 //===========================================================================
1116 // Execute in remote Python node
1117 //===========================================================================
1118 DEBTRACE( "-----------------starting remote python invocation-----------------" );
1119 Engines::pickledArgs_var resultCorba;
1122 resultCorba=_pynode->execute(getFname().c_str(),serializationInputCorba);
1124 catch( const SALOME::SALOME_Exception& ex )
1126 std::string msg="Exception on remote python invocation";
1128 msg += ex.details.text.in();
1130 throw Exception(msg);
1132 DEBTRACE( "-----------------end of remote python invocation-----------------" );
1133 //===========================================================================
1134 // Get results, unpickle and put them in output ports
1135 //===========================================================================
1136 char *resultCorbaC=new char[resultCorba->length()+1];
1137 resultCorbaC[resultCorba->length()]='\0';
1138 for(int i=0;i<resultCorba->length();i++)
1139 resultCorbaC[i]=resultCorba[i];
1144 PyObject *resultPython(PyBytes_FromStringAndSize(resultCorbaC,resultCorba->length()));
1145 delete [] resultCorbaC;
1146 PyObject *args(PyTuple_New(1)),*ob(0);
1147 PyTuple_SetItem(args,0,resultPython);
1148 PyObject *finalResult=PyObject_CallObject(_pyfuncUnser,args);
1151 DEBTRACE( "-----------------PythonNode::outputs-----------------" );
1153 if(finalResult == Py_None)
1155 else if(PyTuple_Check(finalResult))
1156 nres=PyTuple_Size(finalResult);
1158 if(getNumberOfOutputPorts() != nres)
1160 std::string msg="Number of output arguments : Mismatch between definition and execution";
1161 Py_DECREF(finalResult);
1163 throw Exception(msg);
1169 for(std::list<OutputPort *>::iterator iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++, pos++)
1171 OutputPyPort *p=(OutputPyPort *)*iter;
1172 DEBTRACE( "port name: " << p->getName() );
1173 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1174 DEBTRACE( "port pos : " << pos );
1175 if(PyTuple_Check(finalResult))
1176 ob=PyTuple_GetItem(finalResult,pos) ;
1179 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1182 Py_DECREF(finalResult);
1184 catch(ConversionException& ex)
1186 Py_DECREF(finalResult);
1187 _errorDetails=ex.what();
1192 DEBTRACE( "++++++++++++++ ENDOF PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
1195 void PyFuncNode::executeLocal()
1197 DEBTRACE( "++++++++++++++ PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1201 if(!_pyfunc)throw Exception("PyFuncNode badly loaded");
1204 DEBTRACE( "---------------PyFuncNode::inputs---------------" );
1205 PyObject* args = PyTuple_New(getNumberOfInputPorts()) ;
1206 list<InputPort *>::iterator iter2;
1207 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
1209 InputPyPort *p=(InputPyPort *)*iter2;
1210 DEBTRACE( "port name: " << p->getName() );
1211 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1214 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1217 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1219 PyTuple_SetItem(args,pos,ob);
1220 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1223 DEBTRACE( "---------------End PyFuncNode::inputs---------------" );
1225 DEBTRACE( "----------------PyFuncNode::calculation---------------" );
1227 PyObject_Print(_pyfunc,stderr,Py_PRINT_RAW);
1229 PyObject_Print(args,stderr,Py_PRINT_RAW);
1232 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1233 PyObject* result = PyObject_CallObject( _pyfunc , args ) ;
1234 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1241 PyObject* new_stderr = newPyStdOut(_errorDetails);
1242 PySys_SetObject((char*)"stderr", new_stderr);
1243 std::ostringstream stream;
1244 stream << "/tmp/PythonNode_";
1246 ofstream errorfile(stream.str().c_str());
1247 if (errorfile.is_open())
1249 errorfile << _script;
1253 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1254 Py_DECREF(new_stderr);
1255 throw Exception("Error during execution");
1257 DEBTRACE( "----------------End PyFuncNode::calculation---------------" );
1259 DEBTRACE( "-----------------PyFuncNode::outputs-----------------" );
1261 if(result == Py_None)
1263 else if(PyTuple_Check(result))
1264 nres=PyTuple_Size(result);
1266 if(getNumberOfOutputPorts() != nres)
1268 std::string msg="Number of output arguments : Mismatch between definition and execution";
1271 throw Exception(msg);
1276 PyObject_Print(result,stderr,Py_PRINT_RAW);
1279 list<OutputPort *>::iterator iter;
1282 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
1284 OutputPyPort *p=(OutputPyPort *)*iter;
1285 DEBTRACE( "port name: " << p->getName() );
1286 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1287 DEBTRACE( "port pos : " << pos );
1288 if(PyTuple_Check(result))ob=PyTuple_GetItem(result,pos) ;
1290 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1292 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1299 catch(ConversionException& ex)
1302 _errorDetails=ex.what();
1305 DEBTRACE( "-----------------End PyFuncNode::outputs-----------------" );
1308 DEBTRACE( "++++++++++++++ End PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1311 Node *PyFuncNode::simpleClone(ComposedNode *father, bool editionOnly) const
1313 return new PyFuncNode(*this,father);
1316 void PyFuncNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
1318 if(!CORBA::is_nil(_pynode))
1319 _pynode->UnRegister();
1320 _pynode=objContainer->createPyNode(getName().c_str(),getScript().c_str());
1323 Engines::PyNodeBase_var PyFuncNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
1325 Engines::PyNode_var ret(objContainer->getDefaultPyNode(getName().c_str()));
1326 if(!CORBA::is_nil(ret))
1330 return Engines::PyNodeBase::_narrow(ret);
1333 void PyFuncNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
1335 if(!CORBA::is_nil(_pynode))
1337 Engines::PyNode_var tmpp(Engines::PyNode::_narrow(remoteInterp));
1338 if(_pynode->_is_equivalent(tmpp))
1341 if(!CORBA::is_nil(_pynode))
1342 _pynode->UnRegister();
1343 _pynode=Engines::PyNode::_narrow(remoteInterp);
1346 Engines::PyNodeBase_var PyFuncNode::getRemoteInterpreterHandle()
1348 return Engines::PyNodeBase::_narrow(_pynode);
1351 //! Create a new node of same type with a given name
1352 PyFuncNode* PyFuncNode::cloneNode(const std::string& name)
1354 PyFuncNode* n=new PyFuncNode(name);
1355 n->setScript(_script);
1356 n->setFname(_fname);
1357 list<InputPort *>::iterator iter;
1358 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
1360 InputPyPort *p=(InputPyPort *)*iter;
1361 n->edAddInputPort(p->getName(),p->edGetType());
1363 list<OutputPort *>::iterator iter2;
1364 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
1366 OutputPyPort *p=(OutputPyPort *)*iter2;
1367 n->edAddOutputPort(p->getName(),p->edGetType());
1372 std::string PyFuncNode::getContainerLog()
1374 return PythonEntry::GetContainerLog(_mode,_container,this);
1377 void PyFuncNode::shutdown(int level)
1379 DEBTRACE("PyFuncNode::shutdown " << level);
1380 if(_mode=="local")return;
1383 if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
1384 _pynode=Engines::PyNode::_nil();
1385 _container->shutdown(level);
1389 void PyFuncNode::imposeResource(const std::string& resource_name,
1390 const std::string& container_name)
1392 if(!resource_name.empty() && !container_name.empty())
1394 _imposedResource = resource_name;
1395 _imposedContainer = container_name;
1399 bool PyFuncNode::canAcceptImposedResource()
1401 return _container != nullptr && _container->canAcceptImposedResource();