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"
30 #include "ReceiverFactory.hxx"
31 #include "SenderByteImpl.hxx"
33 #include "PyStdout.hxx"
41 #define getpid _getpid
44 #if PY_VERSION_HEX < 0x02050000
45 typedef int Py_ssize_t;
49 #include "YacsTrace.hxx"
51 using namespace YACS::ENGINE;
54 const char PythonEntry::SCRIPT_FOR_SIMPLE_SERIALIZATION[]="import pickle\n"
55 "def pickleForVarSimplePyth2009(val):\n"
56 " return pickle.dumps(val,-1)\n"
59 PyObject *PythonEntry::_pyClsBigObject = nullptr;
61 const char PythonNode::IMPL_NAME[]="Python";
62 const char PythonNode::KIND[]="Python";
64 const char PythonNode::SCRIPT_FOR_SERIALIZATION[]="import pickle\n"
65 "def pickleForDistPyth2009(kws):\n"
66 " return pickle.dumps(((),kws),-1)\n"
68 "def unPickleForDistPyth2009(st):\n"
69 " args=pickle.loads(st)\n"
72 const char PythonNode::REMOTE_NAME[]="remote";
74 const char PythonNode::DPL_INFO_NAME[]="my_dpl_localization";
76 const char PyFuncNode::SCRIPT_FOR_SERIALIZATION[]="import pickle\n"
77 "def pickleForDistPyth2009(*args,**kws):\n"
78 " return pickle.dumps((args,kws),-1)\n"
80 "def unPickleForDistPyth2009(st):\n"
81 " args=pickle.loads(st)\n"
84 static char SCRIPT_FOR_BIGOBJECT[]="import SALOME_PyNode\n"
85 "BigObjectOnDiskBase = SALOME_PyNode.BigObjectOnDiskBase\n";
87 // pickle.load concurrency issue : see https://bugs.python.org/issue12680
88 #if PY_VERSION_HEX < 0x03070000
90 static std::mutex data_mutex;
93 PythonEntry::PythonEntry():_context(0),_pyfuncSer(0),_pyfuncUnser(0),_pyfuncSimpleSer(0)
97 PythonEntry::~PythonEntry()
100 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
101 // not Py_XDECREF of _pyfuncUnser because it is returned by PyDict_GetItem -> borrowed
102 // not Py_XDECREF of _pyfuncSer because it is returned by PyDict_GetItem -> borrowed
103 Py_XDECREF(_context);
106 void PythonEntry::loadRemoteContainer(InlineNode *reqNode)
108 DEBTRACE( "---------------PythonEntry::CommonRemoteLoad function---------------" );
109 Container *container(reqNode->getContainer());
110 bool isContAlreadyStarted(false);
115 if(hasImposedResource())
116 container->start(reqNode, _imposedResource, _imposedContainer);
119 isContAlreadyStarted=container->isAlreadyStarted(reqNode);
120 if(!isContAlreadyStarted)
121 container->start(reqNode);
126 reqNode->setErrorDetails(e.what());
132 std::string what("PythonEntry::CommonRemoteLoad : a load operation requested on \"");
133 what+=reqNode->getName(); what+="\" with no container specified.";
134 reqNode->setErrorDetails(what);
135 throw Exception(what);
139 Engines::Container_var GetContainerObj(InlineNode *reqNode, bool& isStandardCont)
141 isStandardCont = false;
142 Container *container(reqNode->getContainer());
143 Engines::Container_var objContainer(Engines::Container::_nil());
145 throw YACS::Exception("No container specified !");
146 SalomeContainer *containerCast0(dynamic_cast<SalomeContainer *>(container));
147 SalomeHPContainer *containerCast1(dynamic_cast<SalomeHPContainer *>(container));
150 isStandardCont = true;
151 objContainer=containerCast0->getContainerPtr(reqNode);
153 else if(containerCast1)
155 YACS::BASES::AutoCppPtr<SalomeContainerTmpForHP> tmpCont(SalomeContainerTmpForHP::BuildFrom(containerCast1,reqNode));
156 objContainer=tmpCont->getContainerPtr(reqNode);
159 throw YACS::Exception("Unrecognized type of container ! Salome one is expected for PythonNode/PyFuncNode !");
160 if(CORBA::is_nil(objContainer))
161 throw YACS::Exception("Container corba pointer is NULL for PythonNode !");
165 Engines::Container_var PythonEntry::loadPythonAdapter(InlineNode *reqNode, bool& isInitializeRequested)
167 bool isStandardCont(true);
168 Engines::Container_var objContainer(GetContainerObj(reqNode,isStandardCont));
169 isInitializeRequested=false;
172 Engines::PyNodeBase_var dftPyScript(retrieveDftRemotePyInterpretorIfAny(objContainer));
173 if(CORBA::is_nil(dftPyScript))
175 isInitializeRequested=!isStandardCont;
176 createRemoteAdaptedPyInterpretor(objContainer);
179 assignRemotePyInterpretor(dftPyScript);
181 catch( const SALOME::SALOME_Exception& ex )
183 std::string msg="Exception on remote python node creation ";
185 msg += ex.details.text.in();
186 reqNode->setErrorDetails(msg);
187 throw Exception(msg);
189 Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
190 if(CORBA::is_nil(pynode))
191 throw Exception("In PythonNode the ref in NULL ! ");
195 void PythonEntry::loadRemoteContext(InlineNode *reqNode, Engines::Container_ptr objContainer, bool isInitializeRequested)
197 Container *container(reqNode->getContainer());
198 Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
201 #if PY_VERSION_HEX < 0x03070000
202 std::unique_lock<std::mutex> lock(data_mutex);
205 const char *picklizeScript(getSerializationScript());
206 PyObject *res=PyRun_String(picklizeScript,Py_file_input,_context,_context);
207 PyObject *res2(PyRun_String(SCRIPT_FOR_SIMPLE_SERIALIZATION,Py_file_input,_context,_context));
208 if(res == NULL || res2==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 Py_DECREF(res); Py_DECREF(res2);
220 AutoPyRef res3(PyRun_String(SCRIPT_FOR_BIGOBJECT,Py_file_input,_context,_context));
221 _pyfuncSer=PyDict_GetItemString(_context,"pickleForDistPyth2009");
222 _pyfuncUnser=PyDict_GetItemString(_context,"unPickleForDistPyth2009");
223 _pyfuncSimpleSer=PyDict_GetItemString(_context,"pickleForVarSimplePyth2009");
224 if(! _pyClsBigObject )
226 _pyClsBigObject=PyDict_GetItemString(_context,"BigObjectOnDiskBase");
227 Py_INCREF(_pyClsBigObject);
229 if(_pyfuncSer == 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");
240 if(_pyfuncUnser == NULL)
242 std::string errorDetails;
243 PyObject *new_stderr(newPyStdOut(errorDetails));
244 reqNode->setErrorDetails(errorDetails);
245 PySys_SetObject((char*)"stderr", new_stderr);
247 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
248 Py_DECREF(new_stderr);
249 throw Exception("Error during load");
251 if(_pyfuncSimpleSer == NULL)
253 std::string errorDetails;
254 PyObject *new_stderr(newPyStdOut(errorDetails));
255 reqNode->setErrorDetails(errorDetails);
256 PySys_SetObject((char*)"stderr", new_stderr);
258 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
259 Py_DECREF(new_stderr);
260 throw Exception("Error during load");
263 if(isInitializeRequested)
264 {//This one is called only once at initialization in the container if an init-script is specified.
267 std::string zeInitScriptKey(container->getProperty(HomogeneousPoolContainer::INITIALIZE_SCRIPT_KEY));
268 if(!zeInitScriptKey.empty())
269 pynode->executeAnotherPieceOfCode(zeInitScriptKey.c_str());
271 catch( const SALOME::SALOME_Exception& ex )
273 std::string msg="Exception on PythonNode::loadRemote python invocation of initializisation py script !";
275 msg += ex.details.text.in();
276 reqNode->setErrorDetails(msg);
277 throw Exception(msg);
279 DEBTRACE( "---------------End PyNode::loadRemote function---------------" );
283 std::string PythonEntry::GetContainerLog(const std::string& mode, Container *container, const Task *askingTask)
291 SalomeContainer *containerCast(dynamic_cast<SalomeContainer *>(container));
292 SalomeHPContainer *objContainer2(dynamic_cast<SalomeHPContainer *>(container));
295 Engines::Container_var objContainer(containerCast->getContainerPtr(askingTask));
296 CORBA::String_var logname = objContainer->logfilename();
299 std::string::size_type pos = msg.find(":");
300 msg=msg.substr(pos+1);
302 else if(objContainer2)
304 msg="Remote PythonNode is on HP Container : no log because no info of the location by definition of HP Container !";
308 msg="Not implemented yet for container log for that type of container !";
313 msg = "Container no longer reachable";
318 void PythonEntry::commonRemoteLoad(InlineNode *reqNode)
320 loadRemoteContainer(reqNode);
321 bool isInitializeRequested;
322 Engines::Container_var objContainer(loadPythonAdapter(reqNode,isInitializeRequested));
323 loadRemoteContext(reqNode,objContainer,isInitializeRequested);
326 bool PythonEntry::hasImposedResource()const
328 return !_imposedResource.empty() && !_imposedContainer.empty();
331 bool PythonEntry::GetDestroyStatus( PyObject *ob )
335 if( PyObject_IsInstance( ob, _pyClsBigObject) == 1 )
337 AutoPyRef unlinkOnDestructor = PyObject_GetAttrString(ob,"getDestroyStatus");
338 AutoPyRef tmp = PyObject_CallFunctionObjArgs(unlinkOnDestructor,nullptr);
339 if( PyBool_Check(tmp.get()) )
341 return tmp.get() == Py_True;
348 void PythonEntry::IfProxyDoSomething( PyObject *ob, const char *meth )
352 if( PyObject_IsInstance( ob, _pyClsBigObject) == 1 )
354 AutoPyRef unlinkOnDestructor = PyObject_GetAttrString(ob,meth);
355 AutoPyRef tmp = PyObject_CallFunctionObjArgs(unlinkOnDestructor,nullptr);
359 void PythonEntry::DoNotTouchFileIfProxy( PyObject *ob )
361 IfProxyDoSomething(ob,"doNotTouchFile");
364 void PythonEntry::UnlinkOnDestructorIfProxy( PyObject *ob )
366 IfProxyDoSomething(ob,"unlinkOnDestructor");
369 PythonNode::PythonNode(const PythonNode& other, ComposedNode *father):InlineNode(other,father),_autoSqueeze(other._autoSqueeze)
371 _pynode = Engines::PyScriptNode::_nil();
372 _implementation=IMPL_NAME;
375 _context=PyDict_New();
376 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
379 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
380 _errorDetails=msg.str();
381 throw Exception(msg.str());
386 PythonNode::PythonNode(const std::string& name):InlineNode(name)
388 _pynode = Engines::PyScriptNode::_nil();
389 _implementation=IMPL_NAME;
392 _context=PyDict_New();
393 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
396 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
397 _errorDetails=msg.str();
398 throw Exception(msg.str());
403 PythonNode::~PythonNode()
408 void PythonNode::checkBasicConsistency() const
410 DEBTRACE("checkBasicConsistency");
411 InlineNode::checkBasicConsistency();
415 res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
418 std::string error="";
419 PyObject* new_stderr = newPyStdOut(error);
420 PySys_SetObject((char*)"stderr", new_stderr);
422 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
423 Py_DECREF(new_stderr);
424 throw Exception(error);
431 void PythonNode::load()
433 DEBTRACE( "---------------PyNode::load function---------------" );
434 if(_mode==PythonNode::REMOTE_NAME)
440 void PythonNode::loadLocal()
442 DEBTRACE( "---------------PyNode::loadLocal function---------------" );
446 void PythonNode::loadRemote()
448 commonRemoteLoad(this);
451 void PythonNode::execute()
453 if(_mode==PythonNode::REMOTE_NAME)
459 void PythonNode::executeRemote()
461 DEBTRACE( "++++++++++++++ PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
463 throw Exception("PythonNode badly loaded");
465 if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
468 loadPythonAdapter(this,dummy);
469 _pynode->assignNewCompiledCode(getScript().c_str());
471 // not managed by unique_ptr here because destructed by the order of client.
472 SenderByteImpl *serializationInputCorba = nullptr;
473 AutoPyRef serializationInput;
475 #if PY_VERSION_HEX < 0x03070000
476 std::unique_lock<std::mutex> lock(data_mutex);
479 PyObject *args(0),*ob(0);
480 //===========================================================================
481 // Get inputs in input ports, build a Python dict and pickle it
482 //===========================================================================
484 std::list<InputPort *>::iterator iter2;
486 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); ++iter2)
488 InputPyPort *p=(InputPyPort *)*iter2;
490 PyDict_SetItemString(args,p->getName().c_str(),ob);
494 PyObject_Print(args,stderr,Py_PRINT_RAW);
497 serializationInput.set(PyObject_CallFunctionObjArgs(_pyfuncSer,args,nullptr));
499 //The pickled string may contain NULL characters so use PyString_AsStringAndSize
500 char *serializationInputC(nullptr);
502 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
503 throw Exception("DistributedPythonNode problem in python pickle");
504 // no copy here. The C byte array of Python is taken as this into CORBA sequence to avoid copy
505 serializationInputCorba = new SenderByteImpl(serializationInputC,len);
508 //get the list of output argument names
509 std::list<OutputPort *>::iterator iter;
510 Engines::listofstring myseq;
511 myseq.length(getNumberOfOutputPorts());
513 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
515 OutputPyPort *p=(OutputPyPort *)*iter;
516 myseq[pos]=p->getName().c_str();
517 DEBTRACE( "port name: " << p->getName() );
518 DEBTRACE( "port kind: " << p->edGetType()->kind() );
519 DEBTRACE( "port pos : " << pos );
522 //===========================================================================
523 // Execute in remote Python node
524 //===========================================================================
525 DEBTRACE( "-----------------starting remote python invocation-----------------" );
526 std::unique_ptr<SALOME::SenderByteSeq> resultCorba;
529 //pass outargsname and dict serialized
530 SALOME::SenderByte_var serializationInputRef = serializationInputCorba->_this();
531 _pynode->executeFirst(serializationInputRef);
532 //serializationInput and serializationInputCorba are no more needed for server. Release it.
533 serializationInput.set(nullptr);
534 resultCorba.reset( _pynode->executeSecond(myseq) );
535 if( ! this->isUsingPythonCache() )
536 _pynode->removeAllVarsInContext();
538 catch( const SALOME::SALOME_Exception& ex )
540 std::ostringstream msg; msg << "Exception on remote python invocation" << std::endl << ex.details.text.in() << std::endl;
541 msg << "PyScriptNode CORBA ref : ";
543 CORBA::ORB_ptr orb(getSALOMERuntime()->getOrb());
544 if(!CORBA::is_nil(orb))
546 CORBA::String_var IOR(orb->object_to_string(_pynode));
551 _errorDetails=msg.str();
552 throw Exception(msg.str());
554 catch(CORBA::COMM_FAILURE& ex)
556 std::ostringstream msg;
557 msg << "Exception on remote python invocation." << std::endl ;
558 msg << "Caught system exception COMM_FAILURE -- unable to contact the "
559 << "object." << std::endl;
560 _errorDetails=msg.str();
561 throw Exception(msg.str());
563 catch(CORBA::SystemException& ex)
565 std::ostringstream msg;
566 msg << "Exception on remote python invocation." << std::endl ;
567 msg << "Caught a CORBA::SystemException." ;
570 CORBA::TypeCode_var tc = tmp.type();
571 const char *p = tc->name();
577 _errorDetails=msg.str();
578 throw Exception(msg.str());
580 catch(CORBA::Exception& ex)
582 std::ostringstream msg;
583 msg << "Exception on remote python invocation." << std::endl ;
584 msg << "Caught CORBA::Exception. " ;
587 CORBA::TypeCode_var tc = tmp.type();
588 const char *p = tc->name();
594 _errorDetails=msg.str();
595 throw Exception(msg.str());
597 catch(omniORB::fatalException& fe)
599 std::ostringstream msg;
600 msg << "Exception on remote python invocation." << std::endl ;
601 msg << "Caught omniORB::fatalException:" << std::endl;
602 msg << " file: " << fe.file() << std::endl;
603 msg << " line: " << fe.line() << std::endl;
604 msg << " mesg: " << fe.errmsg() << std::endl;
605 _errorDetails=msg.str();
606 throw Exception(msg.str());
608 DEBTRACE( "-----------------end of remote python invocation-----------------" );
609 //===========================================================================
610 // Get results, unpickle and put them in output ports
611 //===========================================================================
613 #if PY_VERSION_HEX < 0x03070000
614 std::unique_lock<std::mutex> lock(data_mutex);
617 DEBTRACE( "-----------------PythonNode::outputs-----------------" );
618 int nres( resultCorba->length() );
620 if(getNumberOfOutputPorts() != nres)
622 std::string msg="Number of output arguments : Mismatch between definition and execution";
624 throw Exception(msg);
629 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
631 OutputPyPort *p=(OutputPyPort *)*iter;
632 DEBTRACE( "port name: " << p->getName() );
633 DEBTRACE( "port kind: " << p->edGetType()->kind() );
634 DEBTRACE( "port pos : " << pos );
635 SALOME::SenderByte_var elt = (*resultCorba)[pos];
636 SeqByteReceiver recv(elt);
637 unsigned long length = 0;
638 char *resultCorbaC = recv.data(length);
640 AutoPyRef resultPython=PyMemoryView_FromMemory(resultCorbaC,length,PyBUF_READ);
641 AutoPyRef args = PyTuple_New(1);
642 PyTuple_SetItem(args,0,resultPython.retn());
643 AutoPyRef ob = PyObject_CallObject(_pyfuncUnser,args);
646 std::stringstream msg;
647 msg << "Conversion with pickle of output ports failed !";
648 msg << " : " << __FILE__ << ":" << __LINE__;
649 _errorDetails=msg.str();
650 throw YACS::ENGINE::ConversionException(msg.str());
652 UnlinkOnDestructorIfProxy(ob);
658 catch(ConversionException& ex)
660 _errorDetails=ex.what();
664 squeezeMemoryRemote();
667 if(!isUsingPythonCache())
671 Engines::Container_var cont(GetContainerObj(this,dummy));
672 cont->removePyScriptNode(getName().c_str());
674 DEBTRACE( "++++++++++++++ ENDOF PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
677 void PythonNode::executeLocal()
679 DEBTRACE( "++++++++++++++ PyNode::executeLocal: " << getName() << " ++++++++++++++++++++" );
683 DEBTRACE( "---------------PyNode::inputs---------------" );
684 list<InputPort *>::iterator iter2;
685 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
687 InputPyPort *p=(InputPyPort *)*iter2;
688 DEBTRACE( "port name: " << p->getName() );
689 DEBTRACE( "port kind: " << p->edGetType()->kind() );
690 PyObject* ob=p->getPyObj();
691 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
693 PyObject_Print(ob,stderr,Py_PRINT_RAW);
696 int ier=PyDict_SetItemString(_context,p->getName().c_str(),ob);
697 DEBTRACE( "after PyDict_SetItemString:ob refcnt: " << ob->ob_refcnt );
700 DEBTRACE( "---------------End PyNode::inputs---------------" );
703 DEBTRACE( "----------------PyNode::calculation---------------" );
705 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
707 std::ostringstream stream;
708 stream << "/tmp/PythonNode_";
711 PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
715 PyObject* new_stderr = newPyStdOut(_errorDetails);
716 PySys_SetObject((char*)"stderr", new_stderr);
718 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
719 Py_DECREF(new_stderr);
720 throw Exception("Error during execution");
722 PyObject *res = PyEval_EvalCode( code, _context, _context);
726 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
729 if(PyErr_Occurred ())
732 PyObject* new_stderr = newPyStdOut(_errorDetails);
733 PySys_SetObject((char*)"stderr", new_stderr);
734 ofstream errorfile(stream.str().c_str());
735 if (errorfile.is_open())
737 errorfile << _script;
741 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
742 Py_DECREF(new_stderr);
743 throw Exception("Error during execution");
746 DEBTRACE( "-----------------PyNode::outputs-----------------" );
747 list<OutputPort *>::iterator iter;
750 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
752 OutputPyPort *p=(OutputPyPort *)*iter;
753 DEBTRACE( "port name: " << p->getName() );
754 DEBTRACE( "port kind: " << p->edGetType()->kind() );
755 PyObject *ob=PyDict_GetItemString(_context,p->getName().c_str());
758 std::string msg="Error during execution: there is no variable ";
759 msg=msg+p->getName()+" in node context";
761 throw Exception(msg);
763 DEBTRACE( "PyNode::outputs::ob refcnt: " << ob->ob_refcnt );
765 PyObject_Print(ob,stderr,Py_PRINT_RAW);
771 catch(ConversionException& ex)
773 _errorDetails=ex.what();
778 DEBTRACE( "-----------------End PyNode::outputs-----------------" );
780 DEBTRACE( "++++++++++++++ End PyNode::execute: " << getName() << " ++++++++++++++++++++" );
783 void PythonNode::squeezeMemorySafe()
786 if(_mode==PythonNode::REMOTE_NAME)
787 this->squeezeMemoryRemote();
789 this->squeezeMemory();
792 void PythonNode::squeezeMemory()
794 for(auto p : _setOfInputPort)
796 PyDict_DelItemString(_context,p->getName().c_str());
797 InputPyPort *p2(static_cast<InputPyPort *>(p));
798 if(p2->canSafelySqueezeMemory())
801 for(auto p : _setOfOutputPort)
803 PyDict_DelItemString(_context,p->getName().c_str());
804 OutputPyPort *p2(static_cast<OutputPyPort *>(p));
805 p2->putWithoutForward(Py_None);
809 void PythonNode::squeezeMemoryRemote()
811 for(auto p : _setOfInputPort)
813 InputPyPort *p2(static_cast<InputPyPort *>(p));
814 if(p2->canSafelySqueezeMemory())
817 for(auto p : _setOfOutputPort)
819 OutputPyPort *p2(static_cast<OutputPyPort *>(p));
820 p2->putWithoutForward(Py_None);
824 std::string PythonNode::getContainerLog()
826 return PythonEntry::GetContainerLog(_mode,_container,this);
829 void PythonNode::shutdown(int level)
831 DEBTRACE("PythonNode::shutdown " << level);
832 if(_mode=="local")return;
836 _container->shutdown(level);
840 void PythonNode::imposeResource(const std::string& resource_name,
841 const std::string& container_name)
843 if(!resource_name.empty() && !container_name.empty())
845 _imposedResource = resource_name;
846 _imposedContainer = container_name;
850 bool PythonNode::canAcceptImposedResource()
852 return _container != nullptr && _container->canAcceptImposedResource();
855 bool PythonNode::hasImposedResource()const
857 return PythonEntry::hasImposedResource();
860 std::string PythonNode::pythonEntryName()const
862 if(isUsingPythonCache())
863 return "DEFAULT_NAME_FOR_UNIQUE_PYTHON_NODE_ENTRY";
868 bool PythonNode::isUsingPythonCache()const
872 found = _container->isUsingPythonCache();
876 void PythonNode::freeKernelPynode()
878 if(!CORBA::is_nil(_pynode))
882 _pynode->UnRegister();
886 DEBTRACE("Trouble when pynode->UnRegister!")
888 _pynode = Engines::PyScriptNode::_nil();
892 Node *PythonNode::simpleClone(ComposedNode *father, bool editionOnly) const
894 return new PythonNode(*this,father);
897 void PythonNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
900 _pynode=objContainer->createPyScriptNode(pythonEntryName().c_str(),getScript().c_str());
904 Engines::PyNodeBase_var PythonNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
906 Engines::PyScriptNode_var ret(objContainer->getDefaultPyScriptNode(pythonEntryName().c_str()));
907 if(!CORBA::is_nil(ret))
911 return Engines::PyNodeBase::_narrow(ret);
914 void PythonNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
916 if(CORBA::is_nil(_pynode))
917 _pynode=Engines::PyScriptNode::_narrow(remoteInterp);
920 Engines::PyScriptNode_var tmpp(Engines::PyScriptNode::_narrow(remoteInterp));
921 if(!_pynode->_is_equivalent(tmpp))
924 _pynode=Engines::PyScriptNode::_narrow(remoteInterp);
927 _pynode->assignNewCompiledCode(getScript().c_str());
930 Engines::PyNodeBase_var PythonNode::getRemoteInterpreterHandle()
932 return Engines::PyNodeBase::_narrow(_pynode);
935 //! Create a new node of same type with a given name
936 PythonNode* PythonNode::cloneNode(const std::string& name)
938 PythonNode* n=new PythonNode(name);
939 n->setScript(_script);
940 list<InputPort *>::iterator iter;
941 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
943 InputPyPort *p=(InputPyPort *)*iter;
944 DEBTRACE( "port name: " << p->getName() );
945 DEBTRACE( "port kind: " << p->edGetType()->kind() );
946 n->edAddInputPort(p->getName(),p->edGetType());
948 list<OutputPort *>::iterator iter2;
949 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
951 OutputPyPort *p=(OutputPyPort *)*iter2;
952 DEBTRACE( "port name: " << p->getName() );
953 DEBTRACE( "port kind: " << p->edGetType()->kind() );
954 n->edAddOutputPort(p->getName(),p->edGetType());
959 void PythonNode::applyDPLScope(ComposedNode *gfn)
961 std::vector< std::pair<std::string,int> > ret(getDPLScopeInfo(gfn));
968 std::size_t sz(ret.size());
970 for(std::size_t i=0;i<sz;i++)
972 const std::pair<std::string,int>& p(ret[i]);
973 PyObject *elt(PyTuple_New(2));
974 PyTuple_SetItem(elt,0,PyUnicode_FromString(p.first.c_str()));
975 PyTuple_SetItem(elt,1,PyLong_FromLong(p.second));
976 PyList_SetItem(ob,i,elt);
979 if(_mode==REMOTE_NAME)
981 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);
984 PyObject *serializationInput(PyObject_CallFunctionObjArgs(_pyfuncSimpleSer,ob,NULL));
986 char *serializationInputC(0);
988 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
989 throw Exception("DistributedPythonNode problem in python pickle");
990 serializationInputCorba->length(len);
991 for(int i=0; i < len ; i++)
992 serializationInputCorba[i]=serializationInputC[i];
993 Py_XDECREF(serializationInput);
995 _pynode->defineNewCustomVar(DPL_INFO_NAME,serializationInputCorba);
1000 PyDict_SetItemString(_context,DPL_INFO_NAME,ob);
1005 PyFuncNode::PyFuncNode(const PyFuncNode& other, ComposedNode *father):InlineFuncNode(other,father),_pyfunc(0)
1007 _implementation = PythonNode::IMPL_NAME;
1010 _context=PyDict_New();
1011 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
1012 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
1015 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
1016 _errorDetails=msg.str();
1017 throw Exception(msg.str());
1022 PyFuncNode::PyFuncNode(const std::string& name): InlineFuncNode(name),_pyfunc(0)
1025 _implementation = PythonNode::IMPL_NAME;
1026 DEBTRACE( "PyFuncNode::PyFuncNode " << name );
1029 _context=PyDict_New();
1030 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
1031 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
1034 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
1035 _errorDetails=msg.str();
1036 throw Exception(msg.str());
1041 PyFuncNode::~PyFuncNode()
1043 if(!CORBA::is_nil(_pynode))
1045 _pynode->UnRegister();
1049 void PyFuncNode::init(bool start)
1051 initCommonPartWithoutStateManagement(start);
1052 if(_state == YACS::DISABLED)
1054 exDisabledState(); // to refresh propagation of DISABLED state
1057 if(start) //complete initialization
1058 setState(YACS::READY);
1059 else if(_state > YACS::LOADED)// WARNING FuncNode has internal vars (CEA usecase) ! Partial initialization (inside a loop). Exclusivity of funcNode.
1060 setState(YACS::TORECONNECT);
1063 void PyFuncNode::checkBasicConsistency() const
1065 DEBTRACE("checkBasicConsistency");
1066 InlineFuncNode::checkBasicConsistency();
1070 res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
1073 std::string error="";
1074 PyObject* new_stderr = newPyStdOut(error);
1075 PySys_SetObject((char*)"stderr", new_stderr);
1077 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1078 Py_DECREF(new_stderr);
1079 throw Exception(error);
1086 void PyFuncNode::load()
1088 DEBTRACE( "---------------PyfuncNode::load function---------------" );
1089 if(_mode==PythonNode::REMOTE_NAME)
1095 void PyFuncNode::loadRemote()
1097 commonRemoteLoad(this);
1100 void PyFuncNode::loadLocal()
1102 DEBTRACE( "---------------PyFuncNode::load function " << getName() << " ---------------" );
1103 DEBTRACE( _script );
1106 list<OutputPort *>::iterator iter;
1107 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
1109 OutputPyPort *p=(OutputPyPort *)*iter;
1110 DEBTRACE( "port name: " << p->getName() );
1111 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1117 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
1119 std::ostringstream stream;
1120 stream << "/tmp/PythonNode_";
1123 PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
1127 PyObject* new_stderr = newPyStdOut(_errorDetails);
1128 PySys_SetObject((char*)"stderr", new_stderr);
1130 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1131 Py_DECREF(new_stderr);
1132 throw Exception("Error during execution");
1134 PyObject *res = PyEval_EvalCode( code, _context, _context);
1138 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
1139 if(PyErr_Occurred ())
1142 PyObject* new_stderr = newPyStdOut(_errorDetails);
1143 PySys_SetObject((char*)"stderr", new_stderr);
1144 ofstream errorfile(stream.str().c_str());
1145 if (errorfile.is_open())
1147 errorfile << _script;
1151 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1152 Py_DECREF(new_stderr);
1153 throw Exception("Error during execution");
1156 _pyfunc=PyDict_GetItemString(_context,_fname.c_str());
1157 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1161 PyObject* new_stderr = newPyStdOut(_errorDetails);
1162 PySys_SetObject((char*)"stderr", new_stderr);
1164 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1165 Py_DECREF(new_stderr);
1166 throw Exception("Error during execution");
1168 DEBTRACE( "---------------End PyFuncNode::load function---------------" );
1172 void PyFuncNode::execute()
1174 if(_mode==PythonNode::REMOTE_NAME)
1180 void PyFuncNode::executeRemote()
1182 DEBTRACE( "++++++++++++++ PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
1184 throw Exception("DistributedPythonNode badly loaded");
1186 if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
1189 loadPythonAdapter(this,dummy);
1190 _pynode->executeAnotherPieceOfCode(getScript().c_str());
1193 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);;
1195 #if PY_VERSION_HEX < 0x03070000
1196 std::unique_lock<std::mutex> lock(data_mutex);
1200 //===========================================================================
1201 // Get inputs in input ports, build a Python tuple and pickle it
1202 //===========================================================================
1203 PyObject *args(PyTuple_New(getNumberOfInputPorts()));
1205 for(std::list<InputPort *>::iterator iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++,pos++)
1207 InputPyPort *p=(InputPyPort *)*iter2;
1210 PyTuple_SetItem(args,pos,ob);
1213 PyObject_Print(args,stderr,Py_PRINT_RAW);
1216 PyObject *serializationInput=PyObject_CallObject(_pyfuncSer,args);
1218 //The pickled string may contain NULL characters so use PyString_AsStringAndSize
1219 char *serializationInputC(0);
1221 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
1222 throw Exception("DistributedPythonNode problem in python pickle");
1224 serializationInputCorba->length(len);
1225 for(int i=0; i < len ; i++)
1226 serializationInputCorba[i]=serializationInputC[i];
1227 Py_DECREF(serializationInput);
1230 //===========================================================================
1231 // Execute in remote Python node
1232 //===========================================================================
1233 DEBTRACE( "-----------------starting remote python invocation-----------------" );
1234 Engines::pickledArgs_var resultCorba;
1237 resultCorba=_pynode->execute(getFname().c_str(),serializationInputCorba);
1239 catch( const SALOME::SALOME_Exception& ex )
1241 std::string msg="Exception on remote python invocation";
1243 msg += ex.details.text.in();
1245 throw Exception(msg);
1247 catch(CORBA::COMM_FAILURE& ex)
1249 std::ostringstream msg;
1250 msg << "Exception on remote python invocation." << std::endl ;
1251 msg << "Caught system exception COMM_FAILURE -- unable to contact the "
1252 << "object." << std::endl;
1253 _errorDetails=msg.str();
1254 throw Exception(msg.str());
1256 catch(CORBA::SystemException& ex)
1258 std::ostringstream msg;
1259 msg << "Exception on remote python invocation." << std::endl ;
1260 msg << "Caught a CORBA::SystemException." ;
1263 CORBA::TypeCode_var tc = tmp.type();
1264 const char *p = tc->name();
1270 _errorDetails=msg.str();
1271 throw Exception(msg.str());
1273 catch(CORBA::Exception& ex)
1275 std::ostringstream msg;
1276 msg << "Exception on remote python invocation." << std::endl ;
1277 msg << "Caught CORBA::Exception. " ;
1280 CORBA::TypeCode_var tc = tmp.type();
1281 const char *p = tc->name();
1287 _errorDetails=msg.str();
1288 throw Exception(msg.str());
1290 catch(omniORB::fatalException& fe)
1292 std::ostringstream msg;
1293 msg << "Exception on remote python invocation." << std::endl ;
1294 msg << "Caught omniORB::fatalException:" << std::endl;
1295 msg << " file: " << fe.file() << std::endl;
1296 msg << " line: " << fe.line() << std::endl;
1297 msg << " mesg: " << fe.errmsg() << std::endl;
1298 _errorDetails=msg.str();
1299 throw Exception(msg.str());
1301 DEBTRACE( "-----------------end of remote python invocation-----------------" );
1302 //===========================================================================
1303 // Get results, unpickle and put them in output ports
1304 //===========================================================================
1305 char *resultCorbaC=new char[resultCorba->length()+1];
1306 resultCorbaC[resultCorba->length()]='\0';
1307 for(int i=0;i<resultCorba->length();i++)
1308 resultCorbaC[i]=resultCorba[i];
1311 #if PY_VERSION_HEX < 0x03070000
1312 std::unique_lock<std::mutex> lock(data_mutex);
1316 PyObject *resultPython(PyBytes_FromStringAndSize(resultCorbaC,resultCorba->length()));
1317 delete [] resultCorbaC;
1318 PyObject *args(PyTuple_New(1)),*ob(0);
1319 PyTuple_SetItem(args,0,resultPython);
1320 PyObject *finalResult=PyObject_CallObject(_pyfuncUnser,args);
1323 DEBTRACE( "-----------------PythonNode::outputs-----------------" );
1325 if(finalResult == Py_None)
1327 else if(PyTuple_Check(finalResult))
1328 nres=PyTuple_Size(finalResult);
1330 if(getNumberOfOutputPorts() != nres)
1332 std::string msg="Number of output arguments : Mismatch between definition and execution";
1333 Py_DECREF(finalResult);
1335 throw Exception(msg);
1341 for(std::list<OutputPort *>::iterator iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++, pos++)
1343 OutputPyPort *p=(OutputPyPort *)*iter;
1344 DEBTRACE( "port name: " << p->getName() );
1345 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1346 DEBTRACE( "port pos : " << pos );
1347 if(PyTuple_Check(finalResult))
1348 ob=PyTuple_GetItem(finalResult,pos) ;
1351 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1354 Py_DECREF(finalResult);
1356 catch(ConversionException& ex)
1358 Py_DECREF(finalResult);
1359 _errorDetails=ex.what();
1364 DEBTRACE( "++++++++++++++ ENDOF PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
1367 void PyFuncNode::executeLocal()
1369 DEBTRACE( "++++++++++++++ PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1373 if(!_pyfunc)throw Exception("PyFuncNode badly loaded");
1376 DEBTRACE( "---------------PyFuncNode::inputs---------------" );
1377 PyObject* args = PyTuple_New(getNumberOfInputPorts()) ;
1378 list<InputPort *>::iterator iter2;
1379 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
1381 InputPyPort *p=(InputPyPort *)*iter2;
1382 DEBTRACE( "port name: " << p->getName() );
1383 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1386 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1389 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1391 PyTuple_SetItem(args,pos,ob);
1392 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1395 DEBTRACE( "---------------End PyFuncNode::inputs---------------" );
1397 DEBTRACE( "----------------PyFuncNode::calculation---------------" );
1399 PyObject_Print(_pyfunc,stderr,Py_PRINT_RAW);
1401 PyObject_Print(args,stderr,Py_PRINT_RAW);
1404 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1405 PyObject* result = PyObject_CallObject( _pyfunc , args ) ;
1406 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1413 PyObject* new_stderr = newPyStdOut(_errorDetails);
1414 PySys_SetObject((char*)"stderr", new_stderr);
1415 std::ostringstream stream;
1416 stream << "/tmp/PythonNode_";
1418 ofstream errorfile(stream.str().c_str());
1419 if (errorfile.is_open())
1421 errorfile << _script;
1425 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1426 Py_DECREF(new_stderr);
1427 throw Exception("Error during execution");
1429 DEBTRACE( "----------------End PyFuncNode::calculation---------------" );
1431 DEBTRACE( "-----------------PyFuncNode::outputs-----------------" );
1433 if(result == Py_None)
1435 else if(PyTuple_Check(result))
1436 nres=PyTuple_Size(result);
1438 if(getNumberOfOutputPorts() != nres)
1440 std::string msg="Number of output arguments : Mismatch between definition and execution";
1443 throw Exception(msg);
1448 PyObject_Print(result,stderr,Py_PRINT_RAW);
1451 list<OutputPort *>::iterator iter;
1454 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
1456 OutputPyPort *p=(OutputPyPort *)*iter;
1457 DEBTRACE( "port name: " << p->getName() );
1458 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1459 DEBTRACE( "port pos : " << pos );
1460 if(PyTuple_Check(result))ob=PyTuple_GetItem(result,pos) ;
1462 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1464 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1471 catch(ConversionException& ex)
1474 _errorDetails=ex.what();
1477 DEBTRACE( "-----------------End PyFuncNode::outputs-----------------" );
1480 DEBTRACE( "++++++++++++++ End PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1483 Node *PyFuncNode::simpleClone(ComposedNode *father, bool editionOnly) const
1485 return new PyFuncNode(*this,father);
1488 void PyFuncNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
1490 if(!CORBA::is_nil(_pynode))
1491 _pynode->UnRegister();
1492 _pynode=objContainer->createPyNode(getName().c_str(),getScript().c_str());
1495 Engines::PyNodeBase_var PyFuncNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
1497 Engines::PyNode_var ret(objContainer->getDefaultPyNode(getName().c_str()));
1498 if(!CORBA::is_nil(ret))
1502 return Engines::PyNodeBase::_narrow(ret);
1505 void PyFuncNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
1507 if(!CORBA::is_nil(_pynode))
1509 Engines::PyNode_var tmpp(Engines::PyNode::_narrow(remoteInterp));
1510 if(_pynode->_is_equivalent(tmpp))
1513 if(!CORBA::is_nil(_pynode))
1514 _pynode->UnRegister();
1515 _pynode=Engines::PyNode::_narrow(remoteInterp);
1518 Engines::PyNodeBase_var PyFuncNode::getRemoteInterpreterHandle()
1520 return Engines::PyNodeBase::_narrow(_pynode);
1523 //! Create a new node of same type with a given name
1524 PyFuncNode* PyFuncNode::cloneNode(const std::string& name)
1526 PyFuncNode* n=new PyFuncNode(name);
1527 n->setScript(_script);
1528 n->setFname(_fname);
1529 list<InputPort *>::iterator iter;
1530 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
1532 InputPyPort *p=(InputPyPort *)*iter;
1533 n->edAddInputPort(p->getName(),p->edGetType());
1535 list<OutputPort *>::iterator iter2;
1536 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
1538 OutputPyPort *p=(OutputPyPort *)*iter2;
1539 n->edAddOutputPort(p->getName(),p->edGetType());
1544 std::string PyFuncNode::getContainerLog()
1546 return PythonEntry::GetContainerLog(_mode,_container,this);
1549 void PyFuncNode::shutdown(int level)
1551 DEBTRACE("PyFuncNode::shutdown " << level);
1552 if(_mode=="local")return;
1555 if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
1556 _pynode=Engines::PyNode::_nil();
1557 _container->shutdown(level);
1561 void PyFuncNode::imposeResource(const std::string& resource_name,
1562 const std::string& container_name)
1564 if(!resource_name.empty() && !container_name.empty())
1566 _imposedResource = resource_name;
1567 _imposedContainer = container_name;
1571 bool PyFuncNode::canAcceptImposedResource()
1573 return _container != nullptr && _container->canAcceptImposedResource();
1576 bool PyFuncNode::hasImposedResource()const
1578 return PythonEntry::hasImposedResource();