1 // Copyright (C) 2006-2020 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"
39 #define getpid _getpid
42 #if PY_VERSION_HEX < 0x02050000
43 typedef int Py_ssize_t;
47 #include "YacsTrace.hxx"
49 using namespace YACS::ENGINE;
52 const char PythonEntry::SCRIPT_FOR_SIMPLE_SERIALIZATION[]="import pickle\n"
53 "def pickleForVarSimplePyth2009(val):\n"
54 " return pickle.dumps(val,-1)\n"
57 const char PythonNode::IMPL_NAME[]="Python";
58 const char PythonNode::KIND[]="Python";
60 const char PythonNode::SCRIPT_FOR_SERIALIZATION[]="import pickle\n"
61 "def pickleForDistPyth2009(kws):\n"
62 " return pickle.dumps(((),kws),-1)\n"
64 "def unPickleForDistPyth2009(st):\n"
65 " args=pickle.loads(st)\n"
68 const char PythonNode::REMOTE_NAME[]="remote";
70 const char PythonNode::DPL_INFO_NAME[]="my_dpl_localization";
72 const char PyFuncNode::SCRIPT_FOR_SERIALIZATION[]="import pickle\n"
73 "def pickleForDistPyth2009(*args,**kws):\n"
74 " return pickle.dumps((args,kws),-1)\n"
76 "def unPickleForDistPyth2009(st):\n"
77 " 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::loadRemoteContainer(InlineNode *reqNode)
95 DEBTRACE( "---------------PythonEntry::CommonRemoteLoad function---------------" );
96 Container *container(reqNode->getContainer());
97 bool isContAlreadyStarted(false);
102 if(hasImposedResource())
103 container->start(reqNode, _imposedResource, _imposedContainer);
106 isContAlreadyStarted=container->isAlreadyStarted(reqNode);
107 if(!isContAlreadyStarted)
108 container->start(reqNode);
113 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::loadPythonAdapter(InlineNode *reqNode, bool& isInitializeRequested)
154 bool isStandardCont(true);
155 Engines::Container_var objContainer(GetContainerObj(reqNode,isStandardCont));
156 isInitializeRequested=false;
159 Engines::PyNodeBase_var dftPyScript(retrieveDftRemotePyInterpretorIfAny(objContainer));
160 if(CORBA::is_nil(dftPyScript))
162 isInitializeRequested=!isStandardCont;
163 createRemoteAdaptedPyInterpretor(objContainer);
166 assignRemotePyInterpretor(dftPyScript);
168 catch( const SALOME::SALOME_Exception& ex )
170 std::string msg="Exception on remote python node creation ";
172 msg += ex.details.text.in();
173 reqNode->setErrorDetails(msg);
174 throw Exception(msg);
176 Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
177 if(CORBA::is_nil(pynode))
178 throw Exception("In PythonNode the ref in NULL ! ");
182 void PythonEntry::loadRemoteContext(InlineNode *reqNode, Engines::Container_ptr objContainer, bool isInitializeRequested)
184 Container *container(reqNode->getContainer());
185 Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
189 const char *picklizeScript(getSerializationScript());
190 PyObject *res=PyRun_String(picklizeScript,Py_file_input,_context,_context);
191 PyObject *res2(PyRun_String(SCRIPT_FOR_SIMPLE_SERIALIZATION,Py_file_input,_context,_context));
192 if(res == NULL || res2==NULL)
194 std::string errorDetails;
195 PyObject* new_stderr = newPyStdOut(errorDetails);
196 reqNode->setErrorDetails(errorDetails);
197 PySys_SetObject((char*)"stderr", new_stderr);
199 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
200 Py_DECREF(new_stderr);
201 throw Exception("Error during load");
203 Py_DECREF(res); Py_DECREF(res2);
204 _pyfuncSer=PyDict_GetItemString(_context,"pickleForDistPyth2009");
205 _pyfuncUnser=PyDict_GetItemString(_context,"unPickleForDistPyth2009");
206 _pyfuncSimpleSer=PyDict_GetItemString(_context,"pickleForVarSimplePyth2009");
207 if(_pyfuncSer == NULL)
209 std::string errorDetails;
210 PyObject *new_stderr(newPyStdOut(errorDetails));
211 reqNode->setErrorDetails(errorDetails);
212 PySys_SetObject((char*)"stderr", new_stderr);
214 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
215 Py_DECREF(new_stderr);
216 throw Exception("Error during load");
218 if(_pyfuncUnser == NULL)
220 std::string errorDetails;
221 PyObject *new_stderr(newPyStdOut(errorDetails));
222 reqNode->setErrorDetails(errorDetails);
223 PySys_SetObject((char*)"stderr", new_stderr);
225 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
226 Py_DECREF(new_stderr);
227 throw Exception("Error during load");
229 if(_pyfuncSimpleSer == NULL)
231 std::string errorDetails;
232 PyObject *new_stderr(newPyStdOut(errorDetails));
233 reqNode->setErrorDetails(errorDetails);
234 PySys_SetObject((char*)"stderr", new_stderr);
236 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
237 Py_DECREF(new_stderr);
238 throw Exception("Error during load");
241 if(isInitializeRequested)
242 {//This one is called only once at initialization in the container if an init-script is specified.
245 std::string zeInitScriptKey(container->getProperty(HomogeneousPoolContainer::INITIALIZE_SCRIPT_KEY));
246 if(!zeInitScriptKey.empty())
247 pynode->executeAnotherPieceOfCode(zeInitScriptKey.c_str());
249 catch( const SALOME::SALOME_Exception& ex )
251 std::string msg="Exception on PythonNode::loadRemote python invocation of initializisation py script !";
253 msg += ex.details.text.in();
254 reqNode->setErrorDetails(msg);
255 throw Exception(msg);
257 DEBTRACE( "---------------End PyNode::loadRemote function---------------" );
261 std::string PythonEntry::GetContainerLog(const std::string& mode, Container *container, const Task *askingTask)
269 SalomeContainer *containerCast(dynamic_cast<SalomeContainer *>(container));
270 SalomeHPContainer *objContainer2(dynamic_cast<SalomeHPContainer *>(container));
273 Engines::Container_var objContainer(containerCast->getContainerPtr(askingTask));
274 CORBA::String_var logname = objContainer->logfilename();
277 std::string::size_type pos = msg.find(":");
278 msg=msg.substr(pos+1);
280 else if(objContainer2)
282 msg="Remote PythonNode is on HP Container : no log because no info of the location by definition of HP Container !";
286 msg="Not implemented yet for container log for that type of container !";
291 msg = "Container no longer reachable";
296 void PythonEntry::commonRemoteLoad(InlineNode *reqNode)
298 loadRemoteContainer(reqNode);
299 bool isInitializeRequested;
300 Engines::Container_var objContainer(loadPythonAdapter(reqNode,isInitializeRequested));
301 loadRemoteContext(reqNode,objContainer,isInitializeRequested);
304 bool PythonEntry::hasImposedResource()const
306 return !_imposedResource.empty() && !_imposedContainer.empty();
309 PythonNode::PythonNode(const PythonNode& other, ComposedNode *father):InlineNode(other,father),_autoSqueeze(other._autoSqueeze)
311 _pynode = Engines::PyScriptNode::_nil();
312 _implementation=IMPL_NAME;
315 _context=PyDict_New();
316 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
319 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
320 _errorDetails=msg.str();
321 throw Exception(msg.str());
326 PythonNode::PythonNode(const std::string& name):InlineNode(name)
328 _pynode = Engines::PyScriptNode::_nil();
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("PythonNode badly loaded");
408 if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
411 loadPythonAdapter(this,dummy);
412 _pynode->assignNewCompiledCode(getScript().c_str());
415 std::unique_ptr<Engines::pickledArgs> serializationInputCorba(new Engines::pickledArgs);
416 AutoPyRef serializationInput;
419 PyObject *args(0),*ob(0);
420 //===========================================================================
421 // Get inputs in input ports, build a Python dict and pickle it
422 //===========================================================================
424 std::list<InputPort *>::iterator iter2;
426 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); ++iter2)
428 InputPyPort *p=(InputPyPort *)*iter2;
430 PyDict_SetItemString(args,p->getName().c_str(),ob);
434 PyObject_Print(args,stderr,Py_PRINT_RAW);
437 serializationInput.set(PyObject_CallFunctionObjArgs(_pyfuncSer,args,nullptr));
439 //The pickled string may contain NULL characters so use PyString_AsStringAndSize
440 char *serializationInputC(0);
442 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
443 throw Exception("DistributedPythonNode problem in python pickle");
444 // no copy here. The C byte array of Python is taken as this into CORBA sequence to avoid copy
445 serializationInputCorba.reset(new Engines::pickledArgs(len,len,reinterpret_cast<CORBA::Octet *>(serializationInputC),0));
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 std::unique_ptr<Engines::pickledArgs> resultCorba;
469 //pass outargsname and dict serialized
470 _pynode->executeFirst(*(serializationInputCorba.get()));
471 //serializationInput and serializationInputCorba are no more needed for server. Release it.
472 serializationInputCorba.reset(nullptr); serializationInput.set(nullptr);
473 resultCorba.reset(_pynode->executeSecond(myseq));
475 catch( const SALOME::SALOME_Exception& ex )
477 std::ostringstream msg; msg << "Exception on remote python invocation" << std::endl << ex.details.text.in() << std::endl;
478 msg << "PyScriptNode CORBA ref : ";
480 CORBA::ORB_ptr orb(getSALOMERuntime()->getOrb());
481 if(!CORBA::is_nil(orb))
483 CORBA::String_var IOR(orb->object_to_string(_pynode));
488 _errorDetails=msg.str();
489 throw Exception(msg.str());
491 // if(!CORBA::is_nil(_pynode))
493 // _pynode->UnRegister();
495 // _pynode = Engines::PyScriptNode::_nil();
498 // Engines::Container_var cont(GetContainerObj(this,dummy));
499 // cont->removePyScriptNode(getName().c_str());
500 DEBTRACE( "-----------------end of remote python invocation-----------------" );
501 //===========================================================================
502 // Get results, unpickle and put them in output ports
503 //===========================================================================
504 auto length(resultCorba->length());
505 char *resultCorbaC(reinterpret_cast<char *>(resultCorba->get_buffer()));
508 PyObject *args(0),*ob(0);
509 PyObject* resultPython=PyMemoryView_FromMemory(resultCorbaC,length,PyBUF_READ);
510 args = PyTuple_New(1);
511 PyTuple_SetItem(args,0,resultPython);
512 PyObject *finalResult=PyObject_CallObject(_pyfuncUnser,args);
513 resultCorba.reset(nullptr);
516 if (finalResult == NULL)
518 std::stringstream msg;
519 msg << "Conversion with pickle of output ports failed !";
520 msg << " : " << __FILE__ << ":" << __LINE__;
521 _errorDetails=msg.str();
522 throw YACS::ENGINE::ConversionException(msg.str());
525 DEBTRACE( "-----------------PythonNode::outputs-----------------" );
527 if(finalResult == Py_None)
529 else if(PyTuple_Check(finalResult))
530 nres=PyTuple_Size(finalResult);
532 if(getNumberOfOutputPorts() != nres)
534 std::string msg="Number of output arguments : Mismatch between definition and execution";
535 Py_DECREF(finalResult);
537 throw Exception(msg);
543 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
545 OutputPyPort *p=(OutputPyPort *)*iter;
546 DEBTRACE( "port name: " << p->getName() );
547 DEBTRACE( "port kind: " << p->edGetType()->kind() );
548 DEBTRACE( "port pos : " << pos );
549 if(PyTuple_Check(finalResult))
550 ob=PyTuple_GetItem(finalResult,pos) ;
553 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
557 Py_DECREF(finalResult);
559 catch(ConversionException& ex)
561 Py_DECREF(finalResult);
562 _errorDetails=ex.what();
566 squeezeMemoryRemote();
569 if(!isUsingPythonCache())
571 if(!CORBA::is_nil(_pynode))
573 _pynode->UnRegister();
575 _pynode = Engines::PyScriptNode::_nil();
577 Engines::Container_var cont(GetContainerObj(this,dummy));
578 cont->removePyScriptNode(getName().c_str());
580 DEBTRACE( "++++++++++++++ ENDOF PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
583 void PythonNode::executeLocal()
585 DEBTRACE( "++++++++++++++ PyNode::executeLocal: " << getName() << " ++++++++++++++++++++" );
589 DEBTRACE( "---------------PyNode::inputs---------------" );
590 list<InputPort *>::iterator iter2;
591 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
593 InputPyPort *p=(InputPyPort *)*iter2;
594 DEBTRACE( "port name: " << p->getName() );
595 DEBTRACE( "port kind: " << p->edGetType()->kind() );
596 PyObject* ob=p->getPyObj();
597 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
599 PyObject_Print(ob,stderr,Py_PRINT_RAW);
602 int ier=PyDict_SetItemString(_context,p->getName().c_str(),ob);
603 DEBTRACE( "after PyDict_SetItemString:ob refcnt: " << ob->ob_refcnt );
606 DEBTRACE( "---------------End PyNode::inputs---------------" );
609 DEBTRACE( "----------------PyNode::calculation---------------" );
611 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
613 std::ostringstream stream;
614 stream << "/tmp/PythonNode_";
617 PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
621 PyObject* new_stderr = newPyStdOut(_errorDetails);
622 PySys_SetObject((char*)"stderr", new_stderr);
624 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
625 Py_DECREF(new_stderr);
626 throw Exception("Error during execution");
628 PyObject *res = PyEval_EvalCode( code, _context, _context);
632 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
635 if(PyErr_Occurred ())
638 PyObject* new_stderr = newPyStdOut(_errorDetails);
639 PySys_SetObject((char*)"stderr", new_stderr);
640 ofstream errorfile(stream.str().c_str());
641 if (errorfile.is_open())
643 errorfile << _script;
647 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
648 Py_DECREF(new_stderr);
649 throw Exception("Error during execution");
652 DEBTRACE( "-----------------PyNode::outputs-----------------" );
653 list<OutputPort *>::iterator iter;
656 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
658 OutputPyPort *p=(OutputPyPort *)*iter;
659 DEBTRACE( "port name: " << p->getName() );
660 DEBTRACE( "port kind: " << p->edGetType()->kind() );
661 PyObject *ob=PyDict_GetItemString(_context,p->getName().c_str());
664 std::string msg="Error during execution: there is no variable ";
665 msg=msg+p->getName()+" in node context";
667 throw Exception(msg);
669 DEBTRACE( "PyNode::outputs::ob refcnt: " << ob->ob_refcnt );
671 PyObject_Print(ob,stderr,Py_PRINT_RAW);
677 catch(ConversionException& ex)
679 _errorDetails=ex.what();
684 DEBTRACE( "-----------------End PyNode::outputs-----------------" );
686 DEBTRACE( "++++++++++++++ End PyNode::execute: " << getName() << " ++++++++++++++++++++" );
689 void PythonNode::squeezeMemorySafe()
692 if(_mode==PythonNode::REMOTE_NAME)
693 this->squeezeMemoryRemote();
695 this->squeezeMemory();
698 void PythonNode::squeezeMemory()
700 for(auto p : _setOfInputPort)
702 PyDict_DelItemString(_context,p->getName().c_str());
703 InputPyPort *p2(static_cast<InputPyPort *>(p));
704 if(p2->canSafelySqueezeMemory())
707 for(auto p : _setOfOutputPort)
709 PyDict_DelItemString(_context,p->getName().c_str());
710 OutputPyPort *p2(static_cast<OutputPyPort *>(p));
711 p2->putWithoutForward(Py_None);
715 void PythonNode::squeezeMemoryRemote()
717 for(auto p : _setOfInputPort)
719 InputPyPort *p2(static_cast<InputPyPort *>(p));
720 if(p2->canSafelySqueezeMemory())
723 for(auto p : _setOfOutputPort)
725 OutputPyPort *p2(static_cast<OutputPyPort *>(p));
726 p2->putWithoutForward(Py_None);
730 std::string PythonNode::getContainerLog()
732 return PythonEntry::GetContainerLog(_mode,_container,this);
735 void PythonNode::shutdown(int level)
737 DEBTRACE("PythonNode::shutdown " << level);
738 if(_mode=="local")return;
741 if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
742 _pynode=Engines::PyScriptNode::_nil();
743 _container->shutdown(level);
747 void PythonNode::imposeResource(const std::string& resource_name,
748 const std::string& container_name)
750 if(!resource_name.empty() && !container_name.empty())
752 _imposedResource = resource_name;
753 _imposedContainer = container_name;
757 bool PythonNode::canAcceptImposedResource()
759 return _container != nullptr && _container->canAcceptImposedResource();
762 bool PythonNode::hasImposedResource()const
764 return PythonEntry::hasImposedResource();
767 std::string PythonNode::pythonEntryName()const
769 if(isUsingPythonCache())
770 return "DEFAULT_NAME_FOR_UNIQUE_PYTHON_NODE_ENTRY";
775 bool PythonNode::isUsingPythonCache()const
779 found = _container->isUsingPythonCache();
783 Node *PythonNode::simpleClone(ComposedNode *father, bool editionOnly) const
785 return new PythonNode(*this,father);
788 void PythonNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
790 if(!CORBA::is_nil(_pynode))
791 _pynode->UnRegister();
792 _pynode=objContainer->createPyScriptNode(pythonEntryName().c_str(),getScript().c_str());
796 Engines::PyNodeBase_var PythonNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
798 Engines::PyScriptNode_var ret(objContainer->getDefaultPyScriptNode(pythonEntryName().c_str()));
799 if(!CORBA::is_nil(ret))
803 return Engines::PyNodeBase::_narrow(ret);
806 void PythonNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
808 if(CORBA::is_nil(_pynode))
809 _pynode=Engines::PyScriptNode::_narrow(remoteInterp);
812 Engines::PyScriptNode_var tmpp(Engines::PyScriptNode::_narrow(remoteInterp));
813 if(!_pynode->_is_equivalent(tmpp))
815 _pynode->UnRegister();
816 _pynode=Engines::PyScriptNode::_narrow(remoteInterp);
819 _pynode->assignNewCompiledCode(getScript().c_str());
822 Engines::PyNodeBase_var PythonNode::getRemoteInterpreterHandle()
824 return Engines::PyNodeBase::_narrow(_pynode);
827 //! Create a new node of same type with a given name
828 PythonNode* PythonNode::cloneNode(const std::string& name)
830 PythonNode* n=new PythonNode(name);
831 n->setScript(_script);
832 list<InputPort *>::iterator iter;
833 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
835 InputPyPort *p=(InputPyPort *)*iter;
836 DEBTRACE( "port name: " << p->getName() );
837 DEBTRACE( "port kind: " << p->edGetType()->kind() );
838 n->edAddInputPort(p->getName(),p->edGetType());
840 list<OutputPort *>::iterator iter2;
841 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
843 OutputPyPort *p=(OutputPyPort *)*iter2;
844 DEBTRACE( "port name: " << p->getName() );
845 DEBTRACE( "port kind: " << p->edGetType()->kind() );
846 n->edAddOutputPort(p->getName(),p->edGetType());
851 void PythonNode::applyDPLScope(ComposedNode *gfn)
853 std::vector< std::pair<std::string,int> > ret(getDPLScopeInfo(gfn));
860 std::size_t sz(ret.size());
862 for(std::size_t i=0;i<sz;i++)
864 const std::pair<std::string,int>& p(ret[i]);
865 PyObject *elt(PyTuple_New(2));
866 PyTuple_SetItem(elt,0,PyUnicode_FromString(p.first.c_str()));
867 PyTuple_SetItem(elt,1,PyLong_FromLong(p.second));
868 PyList_SetItem(ob,i,elt);
871 if(_mode==REMOTE_NAME)
873 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);
876 PyObject *serializationInput(PyObject_CallFunctionObjArgs(_pyfuncSimpleSer,ob,NULL));
878 char *serializationInputC(0);
880 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
881 throw Exception("DistributedPythonNode problem in python pickle");
882 serializationInputCorba->length(len);
883 for(int i=0; i < len ; i++)
884 serializationInputCorba[i]=serializationInputC[i];
885 Py_XDECREF(serializationInput);
887 _pynode->defineNewCustomVar(DPL_INFO_NAME,serializationInputCorba);
892 PyDict_SetItemString(_context,DPL_INFO_NAME,ob);
897 PyFuncNode::PyFuncNode(const PyFuncNode& other, ComposedNode *father):InlineFuncNode(other,father),_pyfunc(0)
899 _implementation = PythonNode::IMPL_NAME;
902 _context=PyDict_New();
903 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
904 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
907 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
908 _errorDetails=msg.str();
909 throw Exception(msg.str());
914 PyFuncNode::PyFuncNode(const std::string& name): InlineFuncNode(name),_pyfunc(0)
917 _implementation = PythonNode::IMPL_NAME;
918 DEBTRACE( "PyFuncNode::PyFuncNode " << name );
921 _context=PyDict_New();
922 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
923 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
926 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
927 _errorDetails=msg.str();
928 throw Exception(msg.str());
933 PyFuncNode::~PyFuncNode()
935 if(!CORBA::is_nil(_pynode))
937 _pynode->UnRegister();
941 void PyFuncNode::init(bool start)
943 initCommonPartWithoutStateManagement(start);
944 if(_state == YACS::DISABLED)
946 exDisabledState(); // to refresh propagation of DISABLED state
949 if(start) //complete initialization
950 setState(YACS::READY);
951 else if(_state > YACS::LOADED)// WARNING FuncNode has internal vars (CEA usecase) ! Partial initialization (inside a loop). Exclusivity of funcNode.
952 setState(YACS::TORECONNECT);
955 void PyFuncNode::checkBasicConsistency() const
957 DEBTRACE("checkBasicConsistency");
958 InlineFuncNode::checkBasicConsistency();
962 res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
965 std::string error="";
966 PyObject* new_stderr = newPyStdOut(error);
967 PySys_SetObject((char*)"stderr", new_stderr);
969 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
970 Py_DECREF(new_stderr);
971 throw Exception(error);
978 void PyFuncNode::load()
980 DEBTRACE( "---------------PyfuncNode::load function---------------" );
981 if(_mode==PythonNode::REMOTE_NAME)
987 void PyFuncNode::loadRemote()
989 commonRemoteLoad(this);
992 void PyFuncNode::loadLocal()
994 DEBTRACE( "---------------PyFuncNode::load function " << getName() << " ---------------" );
998 list<OutputPort *>::iterator iter;
999 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
1001 OutputPyPort *p=(OutputPyPort *)*iter;
1002 DEBTRACE( "port name: " << p->getName() );
1003 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1009 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
1011 std::ostringstream stream;
1012 stream << "/tmp/PythonNode_";
1015 PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
1019 PyObject* new_stderr = newPyStdOut(_errorDetails);
1020 PySys_SetObject((char*)"stderr", new_stderr);
1022 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1023 Py_DECREF(new_stderr);
1024 throw Exception("Error during execution");
1026 PyObject *res = PyEval_EvalCode( code, _context, _context);
1030 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
1031 if(PyErr_Occurred ())
1034 PyObject* new_stderr = newPyStdOut(_errorDetails);
1035 PySys_SetObject((char*)"stderr", new_stderr);
1036 ofstream errorfile(stream.str().c_str());
1037 if (errorfile.is_open())
1039 errorfile << _script;
1043 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1044 Py_DECREF(new_stderr);
1045 throw Exception("Error during execution");
1048 _pyfunc=PyDict_GetItemString(_context,_fname.c_str());
1049 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1053 PyObject* new_stderr = newPyStdOut(_errorDetails);
1054 PySys_SetObject((char*)"stderr", new_stderr);
1056 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1057 Py_DECREF(new_stderr);
1058 throw Exception("Error during execution");
1060 DEBTRACE( "---------------End PyFuncNode::load function---------------" );
1064 void PyFuncNode::execute()
1066 if(_mode==PythonNode::REMOTE_NAME)
1072 void PyFuncNode::executeRemote()
1074 DEBTRACE( "++++++++++++++ PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
1076 throw Exception("DistributedPythonNode badly loaded");
1078 if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
1081 loadPythonAdapter(this,dummy);
1082 _pynode->executeAnotherPieceOfCode(getScript().c_str());
1085 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);;
1089 //===========================================================================
1090 // Get inputs in input ports, build a Python tuple and pickle it
1091 //===========================================================================
1092 PyObject *args(PyTuple_New(getNumberOfInputPorts()));
1094 for(std::list<InputPort *>::iterator iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++,pos++)
1096 InputPyPort *p=(InputPyPort *)*iter2;
1099 PyTuple_SetItem(args,pos,ob);
1102 PyObject_Print(args,stderr,Py_PRINT_RAW);
1105 PyObject *serializationInput=PyObject_CallObject(_pyfuncSer,args);
1107 //The pickled string may contain NULL characters so use PyString_AsStringAndSize
1108 char *serializationInputC(0);
1110 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
1111 throw Exception("DistributedPythonNode problem in python pickle");
1113 serializationInputCorba->length(len);
1114 for(int i=0; i < len ; i++)
1115 serializationInputCorba[i]=serializationInputC[i];
1116 Py_DECREF(serializationInput);
1119 //===========================================================================
1120 // Execute in remote Python node
1121 //===========================================================================
1122 DEBTRACE( "-----------------starting remote python invocation-----------------" );
1123 Engines::pickledArgs_var resultCorba;
1126 resultCorba=_pynode->execute(getFname().c_str(),serializationInputCorba);
1128 catch( const SALOME::SALOME_Exception& ex )
1130 std::string msg="Exception on remote python invocation";
1132 msg += ex.details.text.in();
1134 throw Exception(msg);
1136 DEBTRACE( "-----------------end of remote python invocation-----------------" );
1137 //===========================================================================
1138 // Get results, unpickle and put them in output ports
1139 //===========================================================================
1140 char *resultCorbaC=new char[resultCorba->length()+1];
1141 resultCorbaC[resultCorba->length()]='\0';
1142 for(int i=0;i<resultCorba->length();i++)
1143 resultCorbaC[i]=resultCorba[i];
1148 PyObject *resultPython(PyBytes_FromStringAndSize(resultCorbaC,resultCorba->length()));
1149 delete [] resultCorbaC;
1150 PyObject *args(PyTuple_New(1)),*ob(0);
1151 PyTuple_SetItem(args,0,resultPython);
1152 PyObject *finalResult=PyObject_CallObject(_pyfuncUnser,args);
1155 DEBTRACE( "-----------------PythonNode::outputs-----------------" );
1157 if(finalResult == Py_None)
1159 else if(PyTuple_Check(finalResult))
1160 nres=PyTuple_Size(finalResult);
1162 if(getNumberOfOutputPorts() != nres)
1164 std::string msg="Number of output arguments : Mismatch between definition and execution";
1165 Py_DECREF(finalResult);
1167 throw Exception(msg);
1173 for(std::list<OutputPort *>::iterator iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++, pos++)
1175 OutputPyPort *p=(OutputPyPort *)*iter;
1176 DEBTRACE( "port name: " << p->getName() );
1177 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1178 DEBTRACE( "port pos : " << pos );
1179 if(PyTuple_Check(finalResult))
1180 ob=PyTuple_GetItem(finalResult,pos) ;
1183 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1186 Py_DECREF(finalResult);
1188 catch(ConversionException& ex)
1190 Py_DECREF(finalResult);
1191 _errorDetails=ex.what();
1196 DEBTRACE( "++++++++++++++ ENDOF PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
1199 void PyFuncNode::executeLocal()
1201 DEBTRACE( "++++++++++++++ PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1205 if(!_pyfunc)throw Exception("PyFuncNode badly loaded");
1208 DEBTRACE( "---------------PyFuncNode::inputs---------------" );
1209 PyObject* args = PyTuple_New(getNumberOfInputPorts()) ;
1210 list<InputPort *>::iterator iter2;
1211 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
1213 InputPyPort *p=(InputPyPort *)*iter2;
1214 DEBTRACE( "port name: " << p->getName() );
1215 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1218 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1221 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1223 PyTuple_SetItem(args,pos,ob);
1224 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1227 DEBTRACE( "---------------End PyFuncNode::inputs---------------" );
1229 DEBTRACE( "----------------PyFuncNode::calculation---------------" );
1231 PyObject_Print(_pyfunc,stderr,Py_PRINT_RAW);
1233 PyObject_Print(args,stderr,Py_PRINT_RAW);
1236 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1237 PyObject* result = PyObject_CallObject( _pyfunc , args ) ;
1238 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1245 PyObject* new_stderr = newPyStdOut(_errorDetails);
1246 PySys_SetObject((char*)"stderr", new_stderr);
1247 std::ostringstream stream;
1248 stream << "/tmp/PythonNode_";
1250 ofstream errorfile(stream.str().c_str());
1251 if (errorfile.is_open())
1253 errorfile << _script;
1257 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1258 Py_DECREF(new_stderr);
1259 throw Exception("Error during execution");
1261 DEBTRACE( "----------------End PyFuncNode::calculation---------------" );
1263 DEBTRACE( "-----------------PyFuncNode::outputs-----------------" );
1265 if(result == Py_None)
1267 else if(PyTuple_Check(result))
1268 nres=PyTuple_Size(result);
1270 if(getNumberOfOutputPorts() != nres)
1272 std::string msg="Number of output arguments : Mismatch between definition and execution";
1275 throw Exception(msg);
1280 PyObject_Print(result,stderr,Py_PRINT_RAW);
1283 list<OutputPort *>::iterator iter;
1286 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
1288 OutputPyPort *p=(OutputPyPort *)*iter;
1289 DEBTRACE( "port name: " << p->getName() );
1290 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1291 DEBTRACE( "port pos : " << pos );
1292 if(PyTuple_Check(result))ob=PyTuple_GetItem(result,pos) ;
1294 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1296 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1303 catch(ConversionException& ex)
1306 _errorDetails=ex.what();
1309 DEBTRACE( "-----------------End PyFuncNode::outputs-----------------" );
1312 DEBTRACE( "++++++++++++++ End PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1315 Node *PyFuncNode::simpleClone(ComposedNode *father, bool editionOnly) const
1317 return new PyFuncNode(*this,father);
1320 void PyFuncNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
1322 if(!CORBA::is_nil(_pynode))
1323 _pynode->UnRegister();
1324 _pynode=objContainer->createPyNode(getName().c_str(),getScript().c_str());
1327 Engines::PyNodeBase_var PyFuncNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
1329 Engines::PyNode_var ret(objContainer->getDefaultPyNode(getName().c_str()));
1330 if(!CORBA::is_nil(ret))
1334 return Engines::PyNodeBase::_narrow(ret);
1337 void PyFuncNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
1339 if(!CORBA::is_nil(_pynode))
1341 Engines::PyNode_var tmpp(Engines::PyNode::_narrow(remoteInterp));
1342 if(_pynode->_is_equivalent(tmpp))
1345 if(!CORBA::is_nil(_pynode))
1346 _pynode->UnRegister();
1347 _pynode=Engines::PyNode::_narrow(remoteInterp);
1350 Engines::PyNodeBase_var PyFuncNode::getRemoteInterpreterHandle()
1352 return Engines::PyNodeBase::_narrow(_pynode);
1355 //! Create a new node of same type with a given name
1356 PyFuncNode* PyFuncNode::cloneNode(const std::string& name)
1358 PyFuncNode* n=new PyFuncNode(name);
1359 n->setScript(_script);
1360 n->setFname(_fname);
1361 list<InputPort *>::iterator iter;
1362 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
1364 InputPyPort *p=(InputPyPort *)*iter;
1365 n->edAddInputPort(p->getName(),p->edGetType());
1367 list<OutputPort *>::iterator iter2;
1368 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
1370 OutputPyPort *p=(OutputPyPort *)*iter2;
1371 n->edAddOutputPort(p->getName(),p->edGetType());
1376 std::string PyFuncNode::getContainerLog()
1378 return PythonEntry::GetContainerLog(_mode,_container,this);
1381 void PyFuncNode::shutdown(int level)
1383 DEBTRACE("PyFuncNode::shutdown " << level);
1384 if(_mode=="local")return;
1387 if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
1388 _pynode=Engines::PyNode::_nil();
1389 _container->shutdown(level);
1393 void PyFuncNode::imposeResource(const std::string& resource_name,
1394 const std::string& container_name)
1396 if(!resource_name.empty() && !container_name.empty())
1398 _imposedResource = resource_name;
1399 _imposedContainer = container_name;
1403 bool PyFuncNode::canAcceptImposedResource()
1405 return _container != nullptr && _container->canAcceptImposedResource();
1408 bool PyFuncNode::hasImposedResource()const
1410 return PythonEntry::hasImposedResource();