1 // Copyright (C) 2006-2022 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 "PythonCppUtils.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 // pickle.load concurrency issue : see https://bugs.python.org/issue12680
81 #if PY_VERSION_HEX < 0x03070000
83 static std::mutex data_mutex;
86 PythonEntry::PythonEntry():_context(0),_pyfuncSer(0),_pyfuncUnser(0),_pyfuncSimpleSer(0)
90 PythonEntry::~PythonEntry()
93 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
94 // not Py_XDECREF of _pyfuncUnser because it is returned by PyDict_GetItem -> borrowed
95 // not Py_XDECREF of _pyfuncSer because it is returned by PyDict_GetItem -> borrowed
99 void PythonEntry::loadRemoteContainer(InlineNode *reqNode)
101 DEBTRACE( "---------------PythonEntry::CommonRemoteLoad function---------------" );
102 Container *container(reqNode->getContainer());
103 bool isContAlreadyStarted(false);
108 if(hasImposedResource())
109 container->start(reqNode, _imposedResource, _imposedContainer);
112 isContAlreadyStarted=container->isAlreadyStarted(reqNode);
113 if(!isContAlreadyStarted)
114 container->start(reqNode);
119 reqNode->setErrorDetails(e.what());
125 std::string what("PythonEntry::CommonRemoteLoad : a load operation requested on \"");
126 what+=reqNode->getName(); what+="\" with no container specified.";
127 reqNode->setErrorDetails(what);
128 throw Exception(what);
132 Engines::Container_var GetContainerObj(InlineNode *reqNode, bool& isStandardCont)
134 isStandardCont = false;
135 Container *container(reqNode->getContainer());
136 Engines::Container_var objContainer(Engines::Container::_nil());
138 throw YACS::Exception("No container specified !");
139 SalomeContainer *containerCast0(dynamic_cast<SalomeContainer *>(container));
140 SalomeHPContainer *containerCast1(dynamic_cast<SalomeHPContainer *>(container));
143 isStandardCont = true;
144 objContainer=containerCast0->getContainerPtr(reqNode);
146 else if(containerCast1)
148 YACS::BASES::AutoCppPtr<SalomeContainerTmpForHP> tmpCont(SalomeContainerTmpForHP::BuildFrom(containerCast1,reqNode));
149 objContainer=tmpCont->getContainerPtr(reqNode);
152 throw YACS::Exception("Unrecognized type of container ! Salome one is expected for PythonNode/PyFuncNode !");
153 if(CORBA::is_nil(objContainer))
154 throw YACS::Exception("Container corba pointer is NULL for PythonNode !");
158 Engines::Container_var PythonEntry::loadPythonAdapter(InlineNode *reqNode, bool& isInitializeRequested)
160 bool isStandardCont(true);
161 Engines::Container_var objContainer(GetContainerObj(reqNode,isStandardCont));
162 isInitializeRequested=false;
165 Engines::PyNodeBase_var dftPyScript(retrieveDftRemotePyInterpretorIfAny(objContainer));
166 if(CORBA::is_nil(dftPyScript))
168 isInitializeRequested=!isStandardCont;
169 createRemoteAdaptedPyInterpretor(objContainer);
172 assignRemotePyInterpretor(dftPyScript);
174 catch( const SALOME::SALOME_Exception& ex )
176 std::string msg="Exception on remote python node creation ";
178 msg += ex.details.text.in();
179 reqNode->setErrorDetails(msg);
180 throw Exception(msg);
182 Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
183 if(CORBA::is_nil(pynode))
184 throw Exception("In PythonNode the ref in NULL ! ");
188 void PythonEntry::loadRemoteContext(InlineNode *reqNode, Engines::Container_ptr objContainer, bool isInitializeRequested)
190 Container *container(reqNode->getContainer());
191 Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
194 #if PY_VERSION_HEX < 0x03070000
195 std::unique_lock<std::mutex> lock(data_mutex);
198 const char *picklizeScript(getSerializationScript());
199 PyObject *res=PyRun_String(picklizeScript,Py_file_input,_context,_context);
200 PyObject *res2(PyRun_String(SCRIPT_FOR_SIMPLE_SERIALIZATION,Py_file_input,_context,_context));
201 if(res == NULL || res2==NULL)
203 std::string errorDetails;
204 PyObject* new_stderr = newPyStdOut(errorDetails);
205 reqNode->setErrorDetails(errorDetails);
206 PySys_SetObject((char*)"stderr", new_stderr);
208 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
209 Py_DECREF(new_stderr);
210 throw Exception("Error during load");
212 Py_DECREF(res); Py_DECREF(res2);
213 _pyfuncSer=PyDict_GetItemString(_context,"pickleForDistPyth2009");
214 _pyfuncUnser=PyDict_GetItemString(_context,"unPickleForDistPyth2009");
215 _pyfuncSimpleSer=PyDict_GetItemString(_context,"pickleForVarSimplePyth2009");
216 if(_pyfuncSer == NULL)
218 std::string errorDetails;
219 PyObject *new_stderr(newPyStdOut(errorDetails));
220 reqNode->setErrorDetails(errorDetails);
221 PySys_SetObject((char*)"stderr", new_stderr);
223 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
224 Py_DECREF(new_stderr);
225 throw Exception("Error during load");
227 if(_pyfuncUnser == NULL)
229 std::string errorDetails;
230 PyObject *new_stderr(newPyStdOut(errorDetails));
231 reqNode->setErrorDetails(errorDetails);
232 PySys_SetObject((char*)"stderr", new_stderr);
234 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
235 Py_DECREF(new_stderr);
236 throw Exception("Error during load");
238 if(_pyfuncSimpleSer == NULL)
240 std::string errorDetails;
241 PyObject *new_stderr(newPyStdOut(errorDetails));
242 reqNode->setErrorDetails(errorDetails);
243 PySys_SetObject((char*)"stderr", new_stderr);
245 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
246 Py_DECREF(new_stderr);
247 throw Exception("Error during load");
250 if(isInitializeRequested)
251 {//This one is called only once at initialization in the container if an init-script is specified.
254 std::string zeInitScriptKey(container->getProperty(HomogeneousPoolContainer::INITIALIZE_SCRIPT_KEY));
255 if(!zeInitScriptKey.empty())
256 pynode->executeAnotherPieceOfCode(zeInitScriptKey.c_str());
258 catch( const SALOME::SALOME_Exception& ex )
260 std::string msg="Exception on PythonNode::loadRemote python invocation of initializisation py script !";
262 msg += ex.details.text.in();
263 reqNode->setErrorDetails(msg);
264 throw Exception(msg);
266 DEBTRACE( "---------------End PyNode::loadRemote function---------------" );
270 std::string PythonEntry::GetContainerLog(const std::string& mode, Container *container, const Task *askingTask)
278 SalomeContainer *containerCast(dynamic_cast<SalomeContainer *>(container));
279 SalomeHPContainer *objContainer2(dynamic_cast<SalomeHPContainer *>(container));
282 Engines::Container_var objContainer(containerCast->getContainerPtr(askingTask));
283 CORBA::String_var logname = objContainer->logfilename();
286 std::string::size_type pos = msg.find(":");
287 msg=msg.substr(pos+1);
289 else if(objContainer2)
291 msg="Remote PythonNode is on HP Container : no log because no info of the location by definition of HP Container !";
295 msg="Not implemented yet for container log for that type of container !";
300 msg = "Container no longer reachable";
305 void PythonEntry::commonRemoteLoad(InlineNode *reqNode)
307 loadRemoteContainer(reqNode);
308 bool isInitializeRequested;
309 Engines::Container_var objContainer(loadPythonAdapter(reqNode,isInitializeRequested));
310 loadRemoteContext(reqNode,objContainer,isInitializeRequested);
313 bool PythonEntry::hasImposedResource()const
315 return !_imposedResource.empty() && !_imposedContainer.empty();
318 PythonNode::PythonNode(const PythonNode& other, ComposedNode *father):InlineNode(other,father),_autoSqueeze(other._autoSqueeze)
320 _pynode = Engines::PyScriptNode::_nil();
321 _implementation=IMPL_NAME;
324 _context=PyDict_New();
325 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
328 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
329 _errorDetails=msg.str();
330 throw Exception(msg.str());
335 PythonNode::PythonNode(const std::string& name):InlineNode(name)
337 _pynode = Engines::PyScriptNode::_nil();
338 _implementation=IMPL_NAME;
341 _context=PyDict_New();
342 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
345 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
346 _errorDetails=msg.str();
347 throw Exception(msg.str());
352 PythonNode::~PythonNode()
357 void PythonNode::checkBasicConsistency() const
359 DEBTRACE("checkBasicConsistency");
360 InlineNode::checkBasicConsistency();
364 res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
367 std::string error="";
368 PyObject* new_stderr = newPyStdOut(error);
369 PySys_SetObject((char*)"stderr", new_stderr);
371 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
372 Py_DECREF(new_stderr);
373 throw Exception(error);
380 void PythonNode::load()
382 DEBTRACE( "---------------PyNode::load function---------------" );
383 if(_mode==PythonNode::REMOTE_NAME)
389 void PythonNode::loadLocal()
391 DEBTRACE( "---------------PyNode::loadLocal function---------------" );
395 void PythonNode::loadRemote()
397 commonRemoteLoad(this);
400 void PythonNode::execute()
402 if(_mode==PythonNode::REMOTE_NAME)
408 void PythonNode::executeRemote()
410 DEBTRACE( "++++++++++++++ PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
412 throw Exception("PythonNode badly loaded");
414 if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
417 loadPythonAdapter(this,dummy);
418 _pynode->assignNewCompiledCode(getScript().c_str());
421 std::unique_ptr<Engines::pickledArgs> serializationInputCorba(new Engines::pickledArgs);
422 AutoPyRef serializationInput;
424 #if PY_VERSION_HEX < 0x03070000
425 std::unique_lock<std::mutex> lock(data_mutex);
428 PyObject *args(0),*ob(0);
429 //===========================================================================
430 // Get inputs in input ports, build a Python dict and pickle it
431 //===========================================================================
433 std::list<InputPort *>::iterator iter2;
435 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); ++iter2)
437 InputPyPort *p=(InputPyPort *)*iter2;
439 PyDict_SetItemString(args,p->getName().c_str(),ob);
443 PyObject_Print(args,stderr,Py_PRINT_RAW);
446 serializationInput.set(PyObject_CallFunctionObjArgs(_pyfuncSer,args,nullptr));
448 //The pickled string may contain NULL characters so use PyString_AsStringAndSize
449 char *serializationInputC(0);
451 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
452 throw Exception("DistributedPythonNode problem in python pickle");
453 // no copy here. The C byte array of Python is taken as this into CORBA sequence to avoid copy
454 serializationInputCorba.reset(new Engines::pickledArgs(len,len,reinterpret_cast<CORBA::Octet *>(serializationInputC),0));
457 //get the list of output argument names
458 std::list<OutputPort *>::iterator iter;
459 Engines::listofstring myseq;
460 myseq.length(getNumberOfOutputPorts());
462 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
464 OutputPyPort *p=(OutputPyPort *)*iter;
465 myseq[pos]=p->getName().c_str();
466 DEBTRACE( "port name: " << p->getName() );
467 DEBTRACE( "port kind: " << p->edGetType()->kind() );
468 DEBTRACE( "port pos : " << pos );
471 //===========================================================================
472 // Execute in remote Python node
473 //===========================================================================
474 DEBTRACE( "-----------------starting remote python invocation-----------------" );
475 std::unique_ptr<Engines::pickledArgs> resultCorba;
478 //pass outargsname and dict serialized
479 _pynode->executeFirst(*(serializationInputCorba.get()));
480 //serializationInput and serializationInputCorba are no more needed for server. Release it.
481 serializationInputCorba.reset(nullptr); serializationInput.set(nullptr);
482 resultCorba.reset(_pynode->executeSecond(myseq));
484 catch( const SALOME::SALOME_Exception& ex )
486 std::ostringstream msg; msg << "Exception on remote python invocation" << std::endl << ex.details.text.in() << std::endl;
487 msg << "PyScriptNode CORBA ref : ";
489 CORBA::ORB_ptr orb(getSALOMERuntime()->getOrb());
490 if(!CORBA::is_nil(orb))
492 CORBA::String_var IOR(orb->object_to_string(_pynode));
497 _errorDetails=msg.str();
498 throw Exception(msg.str());
500 // if(!CORBA::is_nil(_pynode))
502 // _pynode->UnRegister();
504 // _pynode = Engines::PyScriptNode::_nil();
507 // Engines::Container_var cont(GetContainerObj(this,dummy));
508 // cont->removePyScriptNode(getName().c_str());
509 DEBTRACE( "-----------------end of remote python invocation-----------------" );
510 //===========================================================================
511 // Get results, unpickle and put them in output ports
512 //===========================================================================
513 auto length(resultCorba->length());
514 char *resultCorbaC(reinterpret_cast<char *>(resultCorba->get_buffer()));
516 #if PY_VERSION_HEX < 0x03070000
517 std::unique_lock<std::mutex> lock(data_mutex);
520 PyObject *args(0),*ob(0);
521 PyObject* resultPython=PyMemoryView_FromMemory(resultCorbaC,length,PyBUF_READ);
522 args = PyTuple_New(1);
523 PyTuple_SetItem(args,0,resultPython);
524 PyObject *finalResult=PyObject_CallObject(_pyfuncUnser,args);
525 resultCorba.reset(nullptr);
528 if (finalResult == NULL)
530 std::stringstream msg;
531 msg << "Conversion with pickle of output ports failed !";
532 msg << " : " << __FILE__ << ":" << __LINE__;
533 _errorDetails=msg.str();
534 throw YACS::ENGINE::ConversionException(msg.str());
537 DEBTRACE( "-----------------PythonNode::outputs-----------------" );
539 if(finalResult == Py_None)
541 else if(PyTuple_Check(finalResult))
542 nres=PyTuple_Size(finalResult);
544 if(getNumberOfOutputPorts() != nres)
546 std::string msg="Number of output arguments : Mismatch between definition and execution";
547 Py_DECREF(finalResult);
549 throw Exception(msg);
555 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
557 OutputPyPort *p=(OutputPyPort *)*iter;
558 DEBTRACE( "port name: " << p->getName() );
559 DEBTRACE( "port kind: " << p->edGetType()->kind() );
560 DEBTRACE( "port pos : " << pos );
561 if(PyTuple_Check(finalResult))
562 ob=PyTuple_GetItem(finalResult,pos) ;
565 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
569 Py_DECREF(finalResult);
571 catch(ConversionException& ex)
573 Py_DECREF(finalResult);
574 _errorDetails=ex.what();
578 squeezeMemoryRemote();
581 if(!isUsingPythonCache())
585 Engines::Container_var cont(GetContainerObj(this,dummy));
586 cont->removePyScriptNode(getName().c_str());
588 DEBTRACE( "++++++++++++++ ENDOF PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
591 void PythonNode::executeLocal()
593 DEBTRACE( "++++++++++++++ PyNode::executeLocal: " << getName() << " ++++++++++++++++++++" );
597 DEBTRACE( "---------------PyNode::inputs---------------" );
598 list<InputPort *>::iterator iter2;
599 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
601 InputPyPort *p=(InputPyPort *)*iter2;
602 DEBTRACE( "port name: " << p->getName() );
603 DEBTRACE( "port kind: " << p->edGetType()->kind() );
604 PyObject* ob=p->getPyObj();
605 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
607 PyObject_Print(ob,stderr,Py_PRINT_RAW);
610 int ier=PyDict_SetItemString(_context,p->getName().c_str(),ob);
611 DEBTRACE( "after PyDict_SetItemString:ob refcnt: " << ob->ob_refcnt );
614 DEBTRACE( "---------------End PyNode::inputs---------------" );
617 DEBTRACE( "----------------PyNode::calculation---------------" );
619 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
621 std::ostringstream stream;
622 stream << "/tmp/PythonNode_";
625 PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
629 PyObject* new_stderr = newPyStdOut(_errorDetails);
630 PySys_SetObject((char*)"stderr", new_stderr);
632 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
633 Py_DECREF(new_stderr);
634 throw Exception("Error during execution");
636 PyObject *res = PyEval_EvalCode( code, _context, _context);
640 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
643 if(PyErr_Occurred ())
646 PyObject* new_stderr = newPyStdOut(_errorDetails);
647 PySys_SetObject((char*)"stderr", new_stderr);
648 ofstream errorfile(stream.str().c_str());
649 if (errorfile.is_open())
651 errorfile << _script;
655 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
656 Py_DECREF(new_stderr);
657 throw Exception("Error during execution");
660 DEBTRACE( "-----------------PyNode::outputs-----------------" );
661 list<OutputPort *>::iterator iter;
664 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
666 OutputPyPort *p=(OutputPyPort *)*iter;
667 DEBTRACE( "port name: " << p->getName() );
668 DEBTRACE( "port kind: " << p->edGetType()->kind() );
669 PyObject *ob=PyDict_GetItemString(_context,p->getName().c_str());
672 std::string msg="Error during execution: there is no variable ";
673 msg=msg+p->getName()+" in node context";
675 throw Exception(msg);
677 DEBTRACE( "PyNode::outputs::ob refcnt: " << ob->ob_refcnt );
679 PyObject_Print(ob,stderr,Py_PRINT_RAW);
685 catch(ConversionException& ex)
687 _errorDetails=ex.what();
692 DEBTRACE( "-----------------End PyNode::outputs-----------------" );
694 DEBTRACE( "++++++++++++++ End PyNode::execute: " << getName() << " ++++++++++++++++++++" );
697 void PythonNode::squeezeMemorySafe()
700 if(_mode==PythonNode::REMOTE_NAME)
701 this->squeezeMemoryRemote();
703 this->squeezeMemory();
706 void PythonNode::squeezeMemory()
708 for(auto p : _setOfInputPort)
710 PyDict_DelItemString(_context,p->getName().c_str());
711 InputPyPort *p2(static_cast<InputPyPort *>(p));
712 if(p2->canSafelySqueezeMemory())
715 for(auto p : _setOfOutputPort)
717 PyDict_DelItemString(_context,p->getName().c_str());
718 OutputPyPort *p2(static_cast<OutputPyPort *>(p));
719 p2->putWithoutForward(Py_None);
723 void PythonNode::squeezeMemoryRemote()
725 for(auto p : _setOfInputPort)
727 InputPyPort *p2(static_cast<InputPyPort *>(p));
728 if(p2->canSafelySqueezeMemory())
731 for(auto p : _setOfOutputPort)
733 OutputPyPort *p2(static_cast<OutputPyPort *>(p));
734 p2->putWithoutForward(Py_None);
738 std::string PythonNode::getContainerLog()
740 return PythonEntry::GetContainerLog(_mode,_container,this);
743 void PythonNode::shutdown(int level)
745 DEBTRACE("PythonNode::shutdown " << level);
746 if(_mode=="local")return;
750 _container->shutdown(level);
754 void PythonNode::imposeResource(const std::string& resource_name,
755 const std::string& container_name)
757 if(!resource_name.empty() && !container_name.empty())
759 _imposedResource = resource_name;
760 _imposedContainer = container_name;
764 bool PythonNode::canAcceptImposedResource()
766 return _container != nullptr && _container->canAcceptImposedResource();
769 bool PythonNode::hasImposedResource()const
771 return PythonEntry::hasImposedResource();
774 std::string PythonNode::pythonEntryName()const
776 if(isUsingPythonCache())
777 return "DEFAULT_NAME_FOR_UNIQUE_PYTHON_NODE_ENTRY";
782 bool PythonNode::isUsingPythonCache()const
786 found = _container->isUsingPythonCache();
790 void PythonNode::freeKernelPynode()
792 if(!CORBA::is_nil(_pynode))
796 _pynode->UnRegister();
800 DEBTRACE("Trouble when pynode->UnRegister!")
802 _pynode = Engines::PyScriptNode::_nil();
806 Node *PythonNode::simpleClone(ComposedNode *father, bool editionOnly) const
808 return new PythonNode(*this,father);
811 void PythonNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
814 _pynode=objContainer->createPyScriptNode(pythonEntryName().c_str(),getScript().c_str());
818 Engines::PyNodeBase_var PythonNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
820 Engines::PyScriptNode_var ret(objContainer->getDefaultPyScriptNode(pythonEntryName().c_str()));
821 if(!CORBA::is_nil(ret))
825 return Engines::PyNodeBase::_narrow(ret);
828 void PythonNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
830 if(CORBA::is_nil(_pynode))
831 _pynode=Engines::PyScriptNode::_narrow(remoteInterp);
834 Engines::PyScriptNode_var tmpp(Engines::PyScriptNode::_narrow(remoteInterp));
835 if(!_pynode->_is_equivalent(tmpp))
838 _pynode=Engines::PyScriptNode::_narrow(remoteInterp);
841 _pynode->assignNewCompiledCode(getScript().c_str());
844 Engines::PyNodeBase_var PythonNode::getRemoteInterpreterHandle()
846 return Engines::PyNodeBase::_narrow(_pynode);
849 //! Create a new node of same type with a given name
850 PythonNode* PythonNode::cloneNode(const std::string& name)
852 PythonNode* n=new PythonNode(name);
853 n->setScript(_script);
854 list<InputPort *>::iterator iter;
855 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
857 InputPyPort *p=(InputPyPort *)*iter;
858 DEBTRACE( "port name: " << p->getName() );
859 DEBTRACE( "port kind: " << p->edGetType()->kind() );
860 n->edAddInputPort(p->getName(),p->edGetType());
862 list<OutputPort *>::iterator iter2;
863 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
865 OutputPyPort *p=(OutputPyPort *)*iter2;
866 DEBTRACE( "port name: " << p->getName() );
867 DEBTRACE( "port kind: " << p->edGetType()->kind() );
868 n->edAddOutputPort(p->getName(),p->edGetType());
873 void PythonNode::applyDPLScope(ComposedNode *gfn)
875 std::vector< std::pair<std::string,int> > ret(getDPLScopeInfo(gfn));
882 std::size_t sz(ret.size());
884 for(std::size_t i=0;i<sz;i++)
886 const std::pair<std::string,int>& p(ret[i]);
887 PyObject *elt(PyTuple_New(2));
888 PyTuple_SetItem(elt,0,PyUnicode_FromString(p.first.c_str()));
889 PyTuple_SetItem(elt,1,PyLong_FromLong(p.second));
890 PyList_SetItem(ob,i,elt);
893 if(_mode==REMOTE_NAME)
895 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);
898 PyObject *serializationInput(PyObject_CallFunctionObjArgs(_pyfuncSimpleSer,ob,NULL));
900 char *serializationInputC(0);
902 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
903 throw Exception("DistributedPythonNode problem in python pickle");
904 serializationInputCorba->length(len);
905 for(int i=0; i < len ; i++)
906 serializationInputCorba[i]=serializationInputC[i];
907 Py_XDECREF(serializationInput);
909 _pynode->defineNewCustomVar(DPL_INFO_NAME,serializationInputCorba);
914 PyDict_SetItemString(_context,DPL_INFO_NAME,ob);
919 PyFuncNode::PyFuncNode(const PyFuncNode& other, ComposedNode *father):InlineFuncNode(other,father),_pyfunc(0)
921 _implementation = PythonNode::IMPL_NAME;
924 _context=PyDict_New();
925 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
926 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
929 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
930 _errorDetails=msg.str();
931 throw Exception(msg.str());
936 PyFuncNode::PyFuncNode(const std::string& name): InlineFuncNode(name),_pyfunc(0)
939 _implementation = PythonNode::IMPL_NAME;
940 DEBTRACE( "PyFuncNode::PyFuncNode " << name );
943 _context=PyDict_New();
944 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
945 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
948 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
949 _errorDetails=msg.str();
950 throw Exception(msg.str());
955 PyFuncNode::~PyFuncNode()
957 if(!CORBA::is_nil(_pynode))
959 _pynode->UnRegister();
963 void PyFuncNode::init(bool start)
965 initCommonPartWithoutStateManagement(start);
966 if(_state == YACS::DISABLED)
968 exDisabledState(); // to refresh propagation of DISABLED state
971 if(start) //complete initialization
972 setState(YACS::READY);
973 else if(_state > YACS::LOADED)// WARNING FuncNode has internal vars (CEA usecase) ! Partial initialization (inside a loop). Exclusivity of funcNode.
974 setState(YACS::TORECONNECT);
977 void PyFuncNode::checkBasicConsistency() const
979 DEBTRACE("checkBasicConsistency");
980 InlineFuncNode::checkBasicConsistency();
984 res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
987 std::string error="";
988 PyObject* new_stderr = newPyStdOut(error);
989 PySys_SetObject((char*)"stderr", new_stderr);
991 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
992 Py_DECREF(new_stderr);
993 throw Exception(error);
1000 void PyFuncNode::load()
1002 DEBTRACE( "---------------PyfuncNode::load function---------------" );
1003 if(_mode==PythonNode::REMOTE_NAME)
1009 void PyFuncNode::loadRemote()
1011 commonRemoteLoad(this);
1014 void PyFuncNode::loadLocal()
1016 DEBTRACE( "---------------PyFuncNode::load function " << getName() << " ---------------" );
1017 DEBTRACE( _script );
1020 list<OutputPort *>::iterator iter;
1021 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
1023 OutputPyPort *p=(OutputPyPort *)*iter;
1024 DEBTRACE( "port name: " << p->getName() );
1025 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1031 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
1033 std::ostringstream stream;
1034 stream << "/tmp/PythonNode_";
1037 PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
1041 PyObject* new_stderr = newPyStdOut(_errorDetails);
1042 PySys_SetObject((char*)"stderr", new_stderr);
1044 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1045 Py_DECREF(new_stderr);
1046 throw Exception("Error during execution");
1048 PyObject *res = PyEval_EvalCode( code, _context, _context);
1052 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
1053 if(PyErr_Occurred ())
1056 PyObject* new_stderr = newPyStdOut(_errorDetails);
1057 PySys_SetObject((char*)"stderr", new_stderr);
1058 ofstream errorfile(stream.str().c_str());
1059 if (errorfile.is_open())
1061 errorfile << _script;
1065 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1066 Py_DECREF(new_stderr);
1067 throw Exception("Error during execution");
1070 _pyfunc=PyDict_GetItemString(_context,_fname.c_str());
1071 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1075 PyObject* new_stderr = newPyStdOut(_errorDetails);
1076 PySys_SetObject((char*)"stderr", new_stderr);
1078 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1079 Py_DECREF(new_stderr);
1080 throw Exception("Error during execution");
1082 DEBTRACE( "---------------End PyFuncNode::load function---------------" );
1086 void PyFuncNode::execute()
1088 if(_mode==PythonNode::REMOTE_NAME)
1094 void PyFuncNode::executeRemote()
1096 DEBTRACE( "++++++++++++++ PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
1098 throw Exception("DistributedPythonNode badly loaded");
1100 if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
1103 loadPythonAdapter(this,dummy);
1104 _pynode->executeAnotherPieceOfCode(getScript().c_str());
1107 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);;
1109 #if PY_VERSION_HEX < 0x03070000
1110 std::unique_lock<std::mutex> lock(data_mutex);
1114 //===========================================================================
1115 // Get inputs in input ports, build a Python tuple and pickle it
1116 //===========================================================================
1117 PyObject *args(PyTuple_New(getNumberOfInputPorts()));
1119 for(std::list<InputPort *>::iterator iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++,pos++)
1121 InputPyPort *p=(InputPyPort *)*iter2;
1124 PyTuple_SetItem(args,pos,ob);
1127 PyObject_Print(args,stderr,Py_PRINT_RAW);
1130 PyObject *serializationInput=PyObject_CallObject(_pyfuncSer,args);
1132 //The pickled string may contain NULL characters so use PyString_AsStringAndSize
1133 char *serializationInputC(0);
1135 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
1136 throw Exception("DistributedPythonNode problem in python pickle");
1138 serializationInputCorba->length(len);
1139 for(int i=0; i < len ; i++)
1140 serializationInputCorba[i]=serializationInputC[i];
1141 Py_DECREF(serializationInput);
1144 //===========================================================================
1145 // Execute in remote Python node
1146 //===========================================================================
1147 DEBTRACE( "-----------------starting remote python invocation-----------------" );
1148 Engines::pickledArgs_var resultCorba;
1151 resultCorba=_pynode->execute(getFname().c_str(),serializationInputCorba);
1153 catch( const SALOME::SALOME_Exception& ex )
1155 std::string msg="Exception on remote python invocation";
1157 msg += ex.details.text.in();
1159 throw Exception(msg);
1161 DEBTRACE( "-----------------end of remote python invocation-----------------" );
1162 //===========================================================================
1163 // Get results, unpickle and put them in output ports
1164 //===========================================================================
1165 char *resultCorbaC=new char[resultCorba->length()+1];
1166 resultCorbaC[resultCorba->length()]='\0';
1167 for(int i=0;i<resultCorba->length();i++)
1168 resultCorbaC[i]=resultCorba[i];
1171 #if PY_VERSION_HEX < 0x03070000
1172 std::unique_lock<std::mutex> lock(data_mutex);
1176 PyObject *resultPython(PyBytes_FromStringAndSize(resultCorbaC,resultCorba->length()));
1177 delete [] resultCorbaC;
1178 PyObject *args(PyTuple_New(1)),*ob(0);
1179 PyTuple_SetItem(args,0,resultPython);
1180 PyObject *finalResult=PyObject_CallObject(_pyfuncUnser,args);
1183 DEBTRACE( "-----------------PythonNode::outputs-----------------" );
1185 if(finalResult == Py_None)
1187 else if(PyTuple_Check(finalResult))
1188 nres=PyTuple_Size(finalResult);
1190 if(getNumberOfOutputPorts() != nres)
1192 std::string msg="Number of output arguments : Mismatch between definition and execution";
1193 Py_DECREF(finalResult);
1195 throw Exception(msg);
1201 for(std::list<OutputPort *>::iterator iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++, pos++)
1203 OutputPyPort *p=(OutputPyPort *)*iter;
1204 DEBTRACE( "port name: " << p->getName() );
1205 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1206 DEBTRACE( "port pos : " << pos );
1207 if(PyTuple_Check(finalResult))
1208 ob=PyTuple_GetItem(finalResult,pos) ;
1211 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1214 Py_DECREF(finalResult);
1216 catch(ConversionException& ex)
1218 Py_DECREF(finalResult);
1219 _errorDetails=ex.what();
1224 DEBTRACE( "++++++++++++++ ENDOF PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
1227 void PyFuncNode::executeLocal()
1229 DEBTRACE( "++++++++++++++ PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1233 if(!_pyfunc)throw Exception("PyFuncNode badly loaded");
1236 DEBTRACE( "---------------PyFuncNode::inputs---------------" );
1237 PyObject* args = PyTuple_New(getNumberOfInputPorts()) ;
1238 list<InputPort *>::iterator iter2;
1239 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
1241 InputPyPort *p=(InputPyPort *)*iter2;
1242 DEBTRACE( "port name: " << p->getName() );
1243 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1246 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1249 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1251 PyTuple_SetItem(args,pos,ob);
1252 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1255 DEBTRACE( "---------------End PyFuncNode::inputs---------------" );
1257 DEBTRACE( "----------------PyFuncNode::calculation---------------" );
1259 PyObject_Print(_pyfunc,stderr,Py_PRINT_RAW);
1261 PyObject_Print(args,stderr,Py_PRINT_RAW);
1264 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1265 PyObject* result = PyObject_CallObject( _pyfunc , args ) ;
1266 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1273 PyObject* new_stderr = newPyStdOut(_errorDetails);
1274 PySys_SetObject((char*)"stderr", new_stderr);
1275 std::ostringstream stream;
1276 stream << "/tmp/PythonNode_";
1278 ofstream errorfile(stream.str().c_str());
1279 if (errorfile.is_open())
1281 errorfile << _script;
1285 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1286 Py_DECREF(new_stderr);
1287 throw Exception("Error during execution");
1289 DEBTRACE( "----------------End PyFuncNode::calculation---------------" );
1291 DEBTRACE( "-----------------PyFuncNode::outputs-----------------" );
1293 if(result == Py_None)
1295 else if(PyTuple_Check(result))
1296 nres=PyTuple_Size(result);
1298 if(getNumberOfOutputPorts() != nres)
1300 std::string msg="Number of output arguments : Mismatch between definition and execution";
1303 throw Exception(msg);
1308 PyObject_Print(result,stderr,Py_PRINT_RAW);
1311 list<OutputPort *>::iterator iter;
1314 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
1316 OutputPyPort *p=(OutputPyPort *)*iter;
1317 DEBTRACE( "port name: " << p->getName() );
1318 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1319 DEBTRACE( "port pos : " << pos );
1320 if(PyTuple_Check(result))ob=PyTuple_GetItem(result,pos) ;
1322 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1324 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1331 catch(ConversionException& ex)
1334 _errorDetails=ex.what();
1337 DEBTRACE( "-----------------End PyFuncNode::outputs-----------------" );
1340 DEBTRACE( "++++++++++++++ End PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1343 Node *PyFuncNode::simpleClone(ComposedNode *father, bool editionOnly) const
1345 return new PyFuncNode(*this,father);
1348 void PyFuncNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
1350 if(!CORBA::is_nil(_pynode))
1351 _pynode->UnRegister();
1352 _pynode=objContainer->createPyNode(getName().c_str(),getScript().c_str());
1355 Engines::PyNodeBase_var PyFuncNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
1357 Engines::PyNode_var ret(objContainer->getDefaultPyNode(getName().c_str()));
1358 if(!CORBA::is_nil(ret))
1362 return Engines::PyNodeBase::_narrow(ret);
1365 void PyFuncNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
1367 if(!CORBA::is_nil(_pynode))
1369 Engines::PyNode_var tmpp(Engines::PyNode::_narrow(remoteInterp));
1370 if(_pynode->_is_equivalent(tmpp))
1373 if(!CORBA::is_nil(_pynode))
1374 _pynode->UnRegister();
1375 _pynode=Engines::PyNode::_narrow(remoteInterp);
1378 Engines::PyNodeBase_var PyFuncNode::getRemoteInterpreterHandle()
1380 return Engines::PyNodeBase::_narrow(_pynode);
1383 //! Create a new node of same type with a given name
1384 PyFuncNode* PyFuncNode::cloneNode(const std::string& name)
1386 PyFuncNode* n=new PyFuncNode(name);
1387 n->setScript(_script);
1388 n->setFname(_fname);
1389 list<InputPort *>::iterator iter;
1390 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
1392 InputPyPort *p=(InputPyPort *)*iter;
1393 n->edAddInputPort(p->getName(),p->edGetType());
1395 list<OutputPort *>::iterator iter2;
1396 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
1398 OutputPyPort *p=(OutputPyPort *)*iter2;
1399 n->edAddOutputPort(p->getName(),p->edGetType());
1404 std::string PyFuncNode::getContainerLog()
1406 return PythonEntry::GetContainerLog(_mode,_container,this);
1409 void PyFuncNode::shutdown(int level)
1411 DEBTRACE("PyFuncNode::shutdown " << level);
1412 if(_mode=="local")return;
1415 if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
1416 _pynode=Engines::PyNode::_nil();
1417 _container->shutdown(level);
1421 void PyFuncNode::imposeResource(const std::string& resource_name,
1422 const std::string& container_name)
1424 if(!resource_name.empty() && !container_name.empty())
1426 _imposedResource = resource_name;
1427 _imposedContainer = container_name;
1431 bool PyFuncNode::canAcceptImposedResource()
1433 return _container != nullptr && _container->canAcceptImposedResource();
1436 bool PyFuncNode::hasImposedResource()const
1438 return PythonEntry::hasImposedResource();