1 // Copyright (C) 2006-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "RuntimeSALOME.hxx"
21 #include "PythonNode.hxx"
22 #include "PythonPorts.hxx"
23 #include "TypeCode.hxx"
24 #include "AutoGIL.hxx"
25 #include "Container.hxx"
26 #include "SalomeContainer.hxx"
27 #include "SalomeHPContainer.hxx"
28 #include "SalomeContainerTmpForHP.hxx"
29 #include "ConversionException.hxx"
31 #include "PyStdout.hxx"
38 #define getpid _getpid
41 #if PY_VERSION_HEX < 0x02050000
42 typedef int Py_ssize_t;
46 #include "YacsTrace.hxx"
48 using namespace YACS::ENGINE;
51 const char PythonEntry::SCRIPT_FOR_SIMPLE_SERIALIZATION[]="import pickle\n"
52 "def pickleForVarSimplePyth2009(val):\n"
53 " return pickle.dumps(val,-1)\n"
56 const char PythonNode::IMPL_NAME[]="Python";
57 const char PythonNode::KIND[]="Python";
59 const char PythonNode::SCRIPT_FOR_SERIALIZATION[]="import pickle\n"
60 "def pickleForDistPyth2009(kws):\n"
61 " return pickle.dumps(((),kws),-1)\n"
63 "def unPickleForDistPyth2009(st):\n"
64 " args=pickle.loads(st)\n"
67 const char PythonNode::REMOTE_NAME[]="remote";
69 const char PythonNode::DPL_INFO_NAME[]="my_dpl_localization";
71 const char PyFuncNode::SCRIPT_FOR_SERIALIZATION[]="import pickle\n"
72 "def pickleForDistPyth2009(*args,**kws):\n"
73 " return pickle.dumps((args,kws),-1)\n"
75 "def unPickleForDistPyth2009(st):\n"
76 " args=pickle.loads(st)\n"
79 PythonEntry::PythonEntry():_context(0),_pyfuncSer(0),_pyfuncUnser(0),_pyfuncSimpleSer(0)
83 PythonEntry::~PythonEntry()
86 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
87 // not Py_XDECREF of _pyfuncUnser because it is returned by PyDict_GetItem -> borrowed
88 // not Py_XDECREF of _pyfuncSer because it is returned by PyDict_GetItem -> borrowed
92 void PythonEntry::loadRemoteContainer(InlineNode *reqNode)
94 DEBTRACE( "---------------PythonEntry::CommonRemoteLoad function---------------" );
95 Container *container(reqNode->getContainer());
96 bool isContAlreadyStarted(false);
101 if(hasImposedResource())
102 container->start(reqNode, _imposedResource, _imposedContainer);
105 isContAlreadyStarted=container->isAlreadyStarted(reqNode);
106 if(!isContAlreadyStarted)
107 container->start(reqNode);
112 reqNode->setErrorDetails(e.what());
118 std::string what("PythonEntry::CommonRemoteLoad : a load operation requested on \"");
119 what+=reqNode->getName(); what+="\" with no container specified.";
120 reqNode->setErrorDetails(what);
121 throw Exception(what);
125 Engines::Container_var GetContainerObj(InlineNode *reqNode, bool& isStandardCont)
127 isStandardCont = false;
128 Container *container(reqNode->getContainer());
129 Engines::Container_var objContainer(Engines::Container::_nil());
131 throw YACS::Exception("No container specified !");
132 SalomeContainer *containerCast0(dynamic_cast<SalomeContainer *>(container));
133 SalomeHPContainer *containerCast1(dynamic_cast<SalomeHPContainer *>(container));
136 isStandardCont = true;
137 objContainer=containerCast0->getContainerPtr(reqNode);
139 else if(containerCast1)
141 YACS::BASES::AutoCppPtr<SalomeContainerTmpForHP> tmpCont(SalomeContainerTmpForHP::BuildFrom(containerCast1,reqNode));
142 objContainer=tmpCont->getContainerPtr(reqNode);
145 throw YACS::Exception("Unrecognized type of container ! Salome one is expected for PythonNode/PyFuncNode !");
146 if(CORBA::is_nil(objContainer))
147 throw YACS::Exception("Container corba pointer is NULL for PythonNode !");
151 Engines::Container_var PythonEntry::loadPythonAdapter(InlineNode *reqNode, bool& isInitializeRequested)
153 bool isStandardCont(true);
154 Engines::Container_var objContainer(GetContainerObj(reqNode,isStandardCont));
155 isInitializeRequested=false;
158 Engines::PyNodeBase_var dftPyScript(retrieveDftRemotePyInterpretorIfAny(objContainer));
159 if(CORBA::is_nil(dftPyScript))
161 isInitializeRequested=!isStandardCont;
162 createRemoteAdaptedPyInterpretor(objContainer);
165 assignRemotePyInterpretor(dftPyScript);
167 catch( const SALOME::SALOME_Exception& ex )
169 std::string msg="Exception on remote python node creation ";
171 msg += ex.details.text.in();
172 reqNode->setErrorDetails(msg);
173 throw Exception(msg);
175 Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
176 if(CORBA::is_nil(pynode))
177 throw Exception("In PythonNode the ref in NULL ! ");
181 void PythonEntry::loadRemoteContext(InlineNode *reqNode, Engines::Container_ptr objContainer, bool isInitializeRequested)
183 Container *container(reqNode->getContainer());
184 Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
188 const char *picklizeScript(getSerializationScript());
189 PyObject *res=PyRun_String(picklizeScript,Py_file_input,_context,_context);
190 PyObject *res2(PyRun_String(SCRIPT_FOR_SIMPLE_SERIALIZATION,Py_file_input,_context,_context));
191 if(res == NULL || res2==NULL)
193 std::string errorDetails;
194 PyObject* new_stderr = newPyStdOut(errorDetails);
195 reqNode->setErrorDetails(errorDetails);
196 PySys_SetObject((char*)"stderr", new_stderr);
198 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
199 Py_DECREF(new_stderr);
200 throw Exception("Error during load");
202 Py_DECREF(res); Py_DECREF(res2);
203 _pyfuncSer=PyDict_GetItemString(_context,"pickleForDistPyth2009");
204 _pyfuncUnser=PyDict_GetItemString(_context,"unPickleForDistPyth2009");
205 _pyfuncSimpleSer=PyDict_GetItemString(_context,"pickleForVarSimplePyth2009");
206 if(_pyfuncSer == NULL)
208 std::string errorDetails;
209 PyObject *new_stderr(newPyStdOut(errorDetails));
210 reqNode->setErrorDetails(errorDetails);
211 PySys_SetObject((char*)"stderr", new_stderr);
213 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
214 Py_DECREF(new_stderr);
215 throw Exception("Error during load");
217 if(_pyfuncUnser == NULL)
219 std::string errorDetails;
220 PyObject *new_stderr(newPyStdOut(errorDetails));
221 reqNode->setErrorDetails(errorDetails);
222 PySys_SetObject((char*)"stderr", new_stderr);
224 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
225 Py_DECREF(new_stderr);
226 throw Exception("Error during load");
228 if(_pyfuncSimpleSer == NULL)
230 std::string errorDetails;
231 PyObject *new_stderr(newPyStdOut(errorDetails));
232 reqNode->setErrorDetails(errorDetails);
233 PySys_SetObject((char*)"stderr", new_stderr);
235 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
236 Py_DECREF(new_stderr);
237 throw Exception("Error during load");
240 if(isInitializeRequested)
241 {//This one is called only once at initialization in the container if an init-script is specified.
244 std::string zeInitScriptKey(container->getProperty(HomogeneousPoolContainer::INITIALIZE_SCRIPT_KEY));
245 if(!zeInitScriptKey.empty())
246 pynode->executeAnotherPieceOfCode(zeInitScriptKey.c_str());
248 catch( const SALOME::SALOME_Exception& ex )
250 std::string msg="Exception on PythonNode::loadRemote python invocation of initializisation py script !";
252 msg += ex.details.text.in();
253 reqNode->setErrorDetails(msg);
254 throw Exception(msg);
256 DEBTRACE( "---------------End PyNode::loadRemote function---------------" );
260 std::string PythonEntry::GetContainerLog(const std::string& mode, Container *container, const Task *askingTask)
268 SalomeContainer *containerCast(dynamic_cast<SalomeContainer *>(container));
269 SalomeHPContainer *objContainer2(dynamic_cast<SalomeHPContainer *>(container));
272 Engines::Container_var objContainer(containerCast->getContainerPtr(askingTask));
273 CORBA::String_var logname = objContainer->logfilename();
276 std::string::size_type pos = msg.find(":");
277 msg=msg.substr(pos+1);
279 else if(objContainer2)
281 msg="Remote PythonNode is on HP Container : no log because no info of the location by definition of HP Container !";
285 msg="Not implemented yet for container log for that type of container !";
290 msg = "Container no longer reachable";
295 void PythonEntry::commonRemoteLoad(InlineNode *reqNode)
297 loadRemoteContainer(reqNode);
298 bool isInitializeRequested;
299 Engines::Container_var objContainer(loadPythonAdapter(reqNode,isInitializeRequested));
300 loadRemoteContext(reqNode,objContainer,isInitializeRequested);
303 bool PythonEntry::hasImposedResource()const
305 return !_imposedResource.empty() && !_imposedContainer.empty();
308 PythonNode::PythonNode(const PythonNode& other, ComposedNode *father):InlineNode(other,father),_autoSqueeze(other._autoSqueeze)
310 _pynode = Engines::PyScriptNode::_nil();
311 _implementation=IMPL_NAME;
314 _context=PyDict_New();
315 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
318 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
319 _errorDetails=msg.str();
320 throw Exception(msg.str());
325 PythonNode::PythonNode(const std::string& name):InlineNode(name)
327 _pynode = Engines::PyScriptNode::_nil();
328 _implementation=IMPL_NAME;
331 _context=PyDict_New();
332 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
335 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
336 _errorDetails=msg.str();
337 throw Exception(msg.str());
342 PythonNode::~PythonNode()
344 if(!CORBA::is_nil(_pynode))
346 _pynode->UnRegister();
350 void PythonNode::checkBasicConsistency() const
352 DEBTRACE("checkBasicConsistency");
353 InlineNode::checkBasicConsistency();
357 res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
360 std::string error="";
361 PyObject* new_stderr = newPyStdOut(error);
362 PySys_SetObject((char*)"stderr", new_stderr);
364 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
365 Py_DECREF(new_stderr);
366 throw Exception(error);
373 void PythonNode::load()
375 DEBTRACE( "---------------PyNode::load function---------------" );
376 if(_mode==PythonNode::REMOTE_NAME)
382 void PythonNode::loadLocal()
384 DEBTRACE( "---------------PyNode::loadLocal function---------------" );
388 void PythonNode::loadRemote()
390 commonRemoteLoad(this);
393 void PythonNode::execute()
395 if(_mode==PythonNode::REMOTE_NAME)
401 void PythonNode::executeRemote()
403 DEBTRACE( "++++++++++++++ PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
405 throw Exception("PythonNode badly loaded");
407 if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
410 loadPythonAdapter(this,dummy);
411 _pynode->assignNewCompiledCode(getScript().c_str());
414 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);
417 PyObject *args(0),*ob(0);
418 //===========================================================================
419 // Get inputs in input ports, build a Python dict and pickle it
420 //===========================================================================
422 std::list<InputPort *>::iterator iter2;
424 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); ++iter2)
426 InputPyPort *p=(InputPyPort *)*iter2;
428 PyDict_SetItemString(args,p->getName().c_str(),ob);
432 PyObject_Print(args,stderr,Py_PRINT_RAW);
435 PyObject *serializationInput(PyObject_CallFunctionObjArgs(_pyfuncSer,args,NULL));
437 //The pickled string may contain NULL characters so use PyString_AsStringAndSize
438 char *serializationInputC(0);
440 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
441 throw Exception("DistributedPythonNode problem in python pickle");
442 serializationInputCorba->length(len);
443 for(int i=0; i < len ; i++)
444 serializationInputCorba[i]=serializationInputC[i];
445 Py_DECREF(serializationInput);
448 //get the list of output argument names
449 std::list<OutputPort *>::iterator iter;
450 Engines::listofstring myseq;
451 myseq.length(getNumberOfOutputPorts());
453 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
455 OutputPyPort *p=(OutputPyPort *)*iter;
456 myseq[pos]=p->getName().c_str();
457 DEBTRACE( "port name: " << p->getName() );
458 DEBTRACE( "port kind: " << p->edGetType()->kind() );
459 DEBTRACE( "port pos : " << pos );
462 //===========================================================================
463 // Execute in remote Python node
464 //===========================================================================
465 DEBTRACE( "-----------------starting remote python invocation-----------------" );
466 Engines::pickledArgs_var resultCorba;
469 //pass outargsname and dict serialized
470 resultCorba=_pynode->execute(myseq,serializationInputCorba);
472 catch( const SALOME::SALOME_Exception& ex )
474 std::string msg="Exception on remote python invocation";
476 msg += ex.details.text.in();
478 throw Exception(msg);
480 DEBTRACE( "-----------------end of remote python invocation-----------------" );
481 //===========================================================================
482 // Get results, unpickle and put them in output ports
483 //===========================================================================
484 char *resultCorbaC=new char[resultCorba->length()+1];
485 resultCorbaC[resultCorba->length()]='\0';
486 for(int i=0;i<resultCorba->length();i++)
487 resultCorbaC[i]=resultCorba[i];
491 PyObject *args(0),*ob(0);
492 PyObject* resultPython=PyBytes_FromStringAndSize(resultCorbaC,resultCorba->length());
493 delete [] resultCorbaC;
494 args = PyTuple_New(1);
495 PyTuple_SetItem(args,0,resultPython);
496 PyObject *finalResult=PyObject_CallObject(_pyfuncUnser,args);
499 if (finalResult == NULL)
501 std::stringstream msg;
502 msg << "Conversion with pickle of output ports failed !";
503 msg << " : " << __FILE__ << ":" << __LINE__;
504 _errorDetails=msg.str();
505 throw YACS::ENGINE::ConversionException(msg.str());
508 DEBTRACE( "-----------------PythonNode::outputs-----------------" );
510 if(finalResult == Py_None)
512 else if(PyTuple_Check(finalResult))
513 nres=PyTuple_Size(finalResult);
515 if(getNumberOfOutputPorts() != nres)
517 std::string msg="Number of output arguments : Mismatch between definition and execution";
518 Py_DECREF(finalResult);
520 throw Exception(msg);
526 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
528 OutputPyPort *p=(OutputPyPort *)*iter;
529 DEBTRACE( "port name: " << p->getName() );
530 DEBTRACE( "port kind: " << p->edGetType()->kind() );
531 DEBTRACE( "port pos : " << pos );
532 if(PyTuple_Check(finalResult))
533 ob=PyTuple_GetItem(finalResult,pos) ;
536 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
540 Py_DECREF(finalResult);
542 catch(ConversionException& ex)
544 Py_DECREF(finalResult);
545 _errorDetails=ex.what();
549 squeezeMemoryRemote();
552 if(!isUsingPythonCache())
554 if(!CORBA::is_nil(_pynode))
556 _pynode->UnRegister();
558 _pynode = Engines::PyScriptNode::_nil();
560 Engines::Container_var cont(GetContainerObj(this,dummy));
561 cont->removePyScriptNode(getName().c_str());
563 DEBTRACE( "++++++++++++++ ENDOF PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
566 void PythonNode::executeLocal()
568 DEBTRACE( "++++++++++++++ PyNode::executeLocal: " << getName() << " ++++++++++++++++++++" );
572 DEBTRACE( "---------------PyNode::inputs---------------" );
573 list<InputPort *>::iterator iter2;
574 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
576 InputPyPort *p=(InputPyPort *)*iter2;
577 DEBTRACE( "port name: " << p->getName() );
578 DEBTRACE( "port kind: " << p->edGetType()->kind() );
579 PyObject* ob=p->getPyObj();
580 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
582 PyObject_Print(ob,stderr,Py_PRINT_RAW);
585 int ier=PyDict_SetItemString(_context,p->getName().c_str(),ob);
586 DEBTRACE( "after PyDict_SetItemString:ob refcnt: " << ob->ob_refcnt );
589 DEBTRACE( "---------------End PyNode::inputs---------------" );
592 DEBTRACE( "----------------PyNode::calculation---------------" );
594 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
596 std::ostringstream stream;
597 stream << "/tmp/PythonNode_";
600 PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
604 PyObject* new_stderr = newPyStdOut(_errorDetails);
605 PySys_SetObject((char*)"stderr", new_stderr);
607 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
608 Py_DECREF(new_stderr);
609 throw Exception("Error during execution");
611 PyObject *res = PyEval_EvalCode( code, _context, _context);
615 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
618 if(PyErr_Occurred ())
621 PyObject* new_stderr = newPyStdOut(_errorDetails);
622 PySys_SetObject((char*)"stderr", new_stderr);
623 ofstream errorfile(stream.str().c_str());
624 if (errorfile.is_open())
626 errorfile << _script;
630 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
631 Py_DECREF(new_stderr);
632 throw Exception("Error during execution");
635 DEBTRACE( "-----------------PyNode::outputs-----------------" );
636 list<OutputPort *>::iterator iter;
639 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
641 OutputPyPort *p=(OutputPyPort *)*iter;
642 DEBTRACE( "port name: " << p->getName() );
643 DEBTRACE( "port kind: " << p->edGetType()->kind() );
644 PyObject *ob=PyDict_GetItemString(_context,p->getName().c_str());
647 std::string msg="Error during execution: there is no variable ";
648 msg=msg+p->getName()+" in node context";
650 throw Exception(msg);
652 DEBTRACE( "PyNode::outputs::ob refcnt: " << ob->ob_refcnt );
654 PyObject_Print(ob,stderr,Py_PRINT_RAW);
660 catch(ConversionException& ex)
662 _errorDetails=ex.what();
667 DEBTRACE( "-----------------End PyNode::outputs-----------------" );
669 DEBTRACE( "++++++++++++++ End PyNode::execute: " << getName() << " ++++++++++++++++++++" );
672 void PythonNode::squeezeMemorySafe()
675 if(_mode==PythonNode::REMOTE_NAME)
676 this->squeezeMemoryRemote();
678 this->squeezeMemory();
681 void PythonNode::squeezeMemory()
683 for(auto p : _setOfInputPort)
685 PyDict_DelItemString(_context,p->getName().c_str());
686 InputPyPort *p2(static_cast<InputPyPort *>(p));
687 if(p2->canSafelySqueezeMemory())
690 for(auto p : _setOfOutputPort)
692 PyDict_DelItemString(_context,p->getName().c_str());
693 OutputPyPort *p2(static_cast<OutputPyPort *>(p));
694 p2->putWithoutForward(Py_None);
698 void PythonNode::squeezeMemoryRemote()
700 for(auto p : _setOfInputPort)
702 InputPyPort *p2(static_cast<InputPyPort *>(p));
703 if(p2->canSafelySqueezeMemory())
706 for(auto p : _setOfOutputPort)
708 OutputPyPort *p2(static_cast<OutputPyPort *>(p));
709 p2->putWithoutForward(Py_None);
713 std::string PythonNode::getContainerLog()
715 return PythonEntry::GetContainerLog(_mode,_container,this);
718 void PythonNode::shutdown(int level)
720 DEBTRACE("PythonNode::shutdown " << level);
721 if(_mode=="local")return;
724 if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
725 _pynode=Engines::PyScriptNode::_nil();
726 _container->shutdown(level);
730 void PythonNode::imposeResource(const std::string& resource_name,
731 const std::string& container_name)
733 if(!resource_name.empty() && !container_name.empty())
735 _imposedResource = resource_name;
736 _imposedContainer = container_name;
740 bool PythonNode::canAcceptImposedResource()
742 return _container != nullptr && _container->canAcceptImposedResource();
745 bool PythonNode::hasImposedResource()const
747 return PythonEntry::hasImposedResource();
750 std::string PythonNode::pythonEntryName()const
752 if(isUsingPythonCache())
753 return "DEFAULT_NAME_FOR_UNIQUE_PYTHON_NODE_ENTRY";
758 bool PythonNode::isUsingPythonCache()const
762 found = _container->isUsingPythonCache();
766 Node *PythonNode::simpleClone(ComposedNode *father, bool editionOnly) const
768 return new PythonNode(*this,father);
771 void PythonNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
773 if(!CORBA::is_nil(_pynode))
774 _pynode->UnRegister();
775 _pynode=objContainer->createPyScriptNode(pythonEntryName().c_str(),getScript().c_str());
779 Engines::PyNodeBase_var PythonNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
781 Engines::PyScriptNode_var ret(objContainer->getDefaultPyScriptNode(pythonEntryName().c_str()));
782 if(!CORBA::is_nil(ret))
786 return Engines::PyNodeBase::_narrow(ret);
789 void PythonNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
791 if(CORBA::is_nil(_pynode))
792 _pynode=Engines::PyScriptNode::_narrow(remoteInterp);
795 Engines::PyScriptNode_var tmpp(Engines::PyScriptNode::_narrow(remoteInterp));
796 if(!_pynode->_is_equivalent(tmpp))
798 _pynode->UnRegister();
799 _pynode=Engines::PyScriptNode::_narrow(remoteInterp);
802 _pynode->assignNewCompiledCode(getScript().c_str());
805 Engines::PyNodeBase_var PythonNode::getRemoteInterpreterHandle()
807 return Engines::PyNodeBase::_narrow(_pynode);
810 //! Create a new node of same type with a given name
811 PythonNode* PythonNode::cloneNode(const std::string& name)
813 PythonNode* n=new PythonNode(name);
814 n->setScript(_script);
815 list<InputPort *>::iterator iter;
816 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
818 InputPyPort *p=(InputPyPort *)*iter;
819 DEBTRACE( "port name: " << p->getName() );
820 DEBTRACE( "port kind: " << p->edGetType()->kind() );
821 n->edAddInputPort(p->getName(),p->edGetType());
823 list<OutputPort *>::iterator iter2;
824 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
826 OutputPyPort *p=(OutputPyPort *)*iter2;
827 DEBTRACE( "port name: " << p->getName() );
828 DEBTRACE( "port kind: " << p->edGetType()->kind() );
829 n->edAddOutputPort(p->getName(),p->edGetType());
834 void PythonNode::applyDPLScope(ComposedNode *gfn)
836 std::vector< std::pair<std::string,int> > ret(getDPLScopeInfo(gfn));
843 std::size_t sz(ret.size());
845 for(std::size_t i=0;i<sz;i++)
847 const std::pair<std::string,int>& p(ret[i]);
848 PyObject *elt(PyTuple_New(2));
849 PyTuple_SetItem(elt,0,PyUnicode_FromString(p.first.c_str()));
850 PyTuple_SetItem(elt,1,PyLong_FromLong(p.second));
851 PyList_SetItem(ob,i,elt);
854 if(_mode==REMOTE_NAME)
856 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);
859 PyObject *serializationInput(PyObject_CallFunctionObjArgs(_pyfuncSimpleSer,ob,NULL));
861 char *serializationInputC(0);
863 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
864 throw Exception("DistributedPythonNode problem in python pickle");
865 serializationInputCorba->length(len);
866 for(int i=0; i < len ; i++)
867 serializationInputCorba[i]=serializationInputC[i];
868 Py_XDECREF(serializationInput);
870 _pynode->defineNewCustomVar(DPL_INFO_NAME,serializationInputCorba);
875 PyDict_SetItemString(_context,DPL_INFO_NAME,ob);
880 PyFuncNode::PyFuncNode(const PyFuncNode& other, ComposedNode *father):InlineFuncNode(other,father),_pyfunc(0)
882 _implementation = PythonNode::IMPL_NAME;
885 _context=PyDict_New();
886 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
887 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
890 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
891 _errorDetails=msg.str();
892 throw Exception(msg.str());
897 PyFuncNode::PyFuncNode(const std::string& name): InlineFuncNode(name),_pyfunc(0)
900 _implementation = PythonNode::IMPL_NAME;
901 DEBTRACE( "PyFuncNode::PyFuncNode " << name );
904 _context=PyDict_New();
905 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
906 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
909 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
910 _errorDetails=msg.str();
911 throw Exception(msg.str());
916 PyFuncNode::~PyFuncNode()
918 if(!CORBA::is_nil(_pynode))
920 _pynode->UnRegister();
924 void PyFuncNode::init(bool start)
926 initCommonPartWithoutStateManagement(start);
927 if(_state == YACS::DISABLED)
929 exDisabledState(); // to refresh propagation of DISABLED state
932 if(start) //complete initialization
933 setState(YACS::READY);
934 else if(_state > YACS::LOADED)// WARNING FuncNode has internal vars (CEA usecase) ! Partial initialization (inside a loop). Exclusivity of funcNode.
935 setState(YACS::TORECONNECT);
938 void PyFuncNode::checkBasicConsistency() const
940 DEBTRACE("checkBasicConsistency");
941 InlineFuncNode::checkBasicConsistency();
945 res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
948 std::string error="";
949 PyObject* new_stderr = newPyStdOut(error);
950 PySys_SetObject((char*)"stderr", new_stderr);
952 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
953 Py_DECREF(new_stderr);
954 throw Exception(error);
961 void PyFuncNode::load()
963 DEBTRACE( "---------------PyfuncNode::load function---------------" );
964 if(_mode==PythonNode::REMOTE_NAME)
970 void PyFuncNode::loadRemote()
972 commonRemoteLoad(this);
975 void PyFuncNode::loadLocal()
977 DEBTRACE( "---------------PyFuncNode::load function " << getName() << " ---------------" );
981 list<OutputPort *>::iterator iter;
982 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
984 OutputPyPort *p=(OutputPyPort *)*iter;
985 DEBTRACE( "port name: " << p->getName() );
986 DEBTRACE( "port kind: " << p->edGetType()->kind() );
992 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
994 std::ostringstream stream;
995 stream << "/tmp/PythonNode_";
998 PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
1002 PyObject* new_stderr = newPyStdOut(_errorDetails);
1003 PySys_SetObject((char*)"stderr", new_stderr);
1005 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1006 Py_DECREF(new_stderr);
1007 throw Exception("Error during execution");
1009 PyObject *res = PyEval_EvalCode( code, _context, _context);
1013 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
1014 if(PyErr_Occurred ())
1017 PyObject* new_stderr = newPyStdOut(_errorDetails);
1018 PySys_SetObject((char*)"stderr", new_stderr);
1019 ofstream errorfile(stream.str().c_str());
1020 if (errorfile.is_open())
1022 errorfile << _script;
1026 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1027 Py_DECREF(new_stderr);
1028 throw Exception("Error during execution");
1031 _pyfunc=PyDict_GetItemString(_context,_fname.c_str());
1032 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1036 PyObject* new_stderr = newPyStdOut(_errorDetails);
1037 PySys_SetObject((char*)"stderr", new_stderr);
1039 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1040 Py_DECREF(new_stderr);
1041 throw Exception("Error during execution");
1043 DEBTRACE( "---------------End PyFuncNode::load function---------------" );
1047 void PyFuncNode::execute()
1049 if(_mode==PythonNode::REMOTE_NAME)
1055 void PyFuncNode::executeRemote()
1057 DEBTRACE( "++++++++++++++ PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
1059 throw Exception("DistributedPythonNode badly loaded");
1061 if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
1064 loadPythonAdapter(this,dummy);
1065 _pynode->executeAnotherPieceOfCode(getScript().c_str());
1068 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);;
1072 //===========================================================================
1073 // Get inputs in input ports, build a Python tuple and pickle it
1074 //===========================================================================
1075 PyObject *args(PyTuple_New(getNumberOfInputPorts()));
1077 for(std::list<InputPort *>::iterator iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++,pos++)
1079 InputPyPort *p=(InputPyPort *)*iter2;
1082 PyTuple_SetItem(args,pos,ob);
1085 PyObject_Print(args,stderr,Py_PRINT_RAW);
1088 PyObject *serializationInput=PyObject_CallObject(_pyfuncSer,args);
1090 //The pickled string may contain NULL characters so use PyString_AsStringAndSize
1091 char *serializationInputC(0);
1093 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
1094 throw Exception("DistributedPythonNode problem in python pickle");
1096 serializationInputCorba->length(len);
1097 for(int i=0; i < len ; i++)
1098 serializationInputCorba[i]=serializationInputC[i];
1099 Py_DECREF(serializationInput);
1102 //===========================================================================
1103 // Execute in remote Python node
1104 //===========================================================================
1105 DEBTRACE( "-----------------starting remote python invocation-----------------" );
1106 Engines::pickledArgs_var resultCorba;
1109 resultCorba=_pynode->execute(getFname().c_str(),serializationInputCorba);
1111 catch( const SALOME::SALOME_Exception& ex )
1113 std::string msg="Exception on remote python invocation";
1115 msg += ex.details.text.in();
1117 throw Exception(msg);
1119 DEBTRACE( "-----------------end of remote python invocation-----------------" );
1120 //===========================================================================
1121 // Get results, unpickle and put them in output ports
1122 //===========================================================================
1123 char *resultCorbaC=new char[resultCorba->length()+1];
1124 resultCorbaC[resultCorba->length()]='\0';
1125 for(int i=0;i<resultCorba->length();i++)
1126 resultCorbaC[i]=resultCorba[i];
1131 PyObject *resultPython(PyBytes_FromStringAndSize(resultCorbaC,resultCorba->length()));
1132 delete [] resultCorbaC;
1133 PyObject *args(PyTuple_New(1)),*ob(0);
1134 PyTuple_SetItem(args,0,resultPython);
1135 PyObject *finalResult=PyObject_CallObject(_pyfuncUnser,args);
1138 DEBTRACE( "-----------------PythonNode::outputs-----------------" );
1140 if(finalResult == Py_None)
1142 else if(PyTuple_Check(finalResult))
1143 nres=PyTuple_Size(finalResult);
1145 if(getNumberOfOutputPorts() != nres)
1147 std::string msg="Number of output arguments : Mismatch between definition and execution";
1148 Py_DECREF(finalResult);
1150 throw Exception(msg);
1156 for(std::list<OutputPort *>::iterator iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++, pos++)
1158 OutputPyPort *p=(OutputPyPort *)*iter;
1159 DEBTRACE( "port name: " << p->getName() );
1160 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1161 DEBTRACE( "port pos : " << pos );
1162 if(PyTuple_Check(finalResult))
1163 ob=PyTuple_GetItem(finalResult,pos) ;
1166 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1169 Py_DECREF(finalResult);
1171 catch(ConversionException& ex)
1173 Py_DECREF(finalResult);
1174 _errorDetails=ex.what();
1179 DEBTRACE( "++++++++++++++ ENDOF PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
1182 void PyFuncNode::executeLocal()
1184 DEBTRACE( "++++++++++++++ PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1188 if(!_pyfunc)throw Exception("PyFuncNode badly loaded");
1191 DEBTRACE( "---------------PyFuncNode::inputs---------------" );
1192 PyObject* args = PyTuple_New(getNumberOfInputPorts()) ;
1193 list<InputPort *>::iterator iter2;
1194 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
1196 InputPyPort *p=(InputPyPort *)*iter2;
1197 DEBTRACE( "port name: " << p->getName() );
1198 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1201 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1204 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1206 PyTuple_SetItem(args,pos,ob);
1207 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1210 DEBTRACE( "---------------End PyFuncNode::inputs---------------" );
1212 DEBTRACE( "----------------PyFuncNode::calculation---------------" );
1214 PyObject_Print(_pyfunc,stderr,Py_PRINT_RAW);
1216 PyObject_Print(args,stderr,Py_PRINT_RAW);
1219 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1220 PyObject* result = PyObject_CallObject( _pyfunc , args ) ;
1221 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1228 PyObject* new_stderr = newPyStdOut(_errorDetails);
1229 PySys_SetObject((char*)"stderr", new_stderr);
1230 std::ostringstream stream;
1231 stream << "/tmp/PythonNode_";
1233 ofstream errorfile(stream.str().c_str());
1234 if (errorfile.is_open())
1236 errorfile << _script;
1240 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1241 Py_DECREF(new_stderr);
1242 throw Exception("Error during execution");
1244 DEBTRACE( "----------------End PyFuncNode::calculation---------------" );
1246 DEBTRACE( "-----------------PyFuncNode::outputs-----------------" );
1248 if(result == Py_None)
1250 else if(PyTuple_Check(result))
1251 nres=PyTuple_Size(result);
1253 if(getNumberOfOutputPorts() != nres)
1255 std::string msg="Number of output arguments : Mismatch between definition and execution";
1258 throw Exception(msg);
1263 PyObject_Print(result,stderr,Py_PRINT_RAW);
1266 list<OutputPort *>::iterator iter;
1269 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
1271 OutputPyPort *p=(OutputPyPort *)*iter;
1272 DEBTRACE( "port name: " << p->getName() );
1273 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1274 DEBTRACE( "port pos : " << pos );
1275 if(PyTuple_Check(result))ob=PyTuple_GetItem(result,pos) ;
1277 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1279 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1286 catch(ConversionException& ex)
1289 _errorDetails=ex.what();
1292 DEBTRACE( "-----------------End PyFuncNode::outputs-----------------" );
1295 DEBTRACE( "++++++++++++++ End PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1298 Node *PyFuncNode::simpleClone(ComposedNode *father, bool editionOnly) const
1300 return new PyFuncNode(*this,father);
1303 void PyFuncNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
1305 if(!CORBA::is_nil(_pynode))
1306 _pynode->UnRegister();
1307 _pynode=objContainer->createPyNode(getName().c_str(),getScript().c_str());
1310 Engines::PyNodeBase_var PyFuncNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
1312 Engines::PyNode_var ret(objContainer->getDefaultPyNode(getName().c_str()));
1313 if(!CORBA::is_nil(ret))
1317 return Engines::PyNodeBase::_narrow(ret);
1320 void PyFuncNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
1322 if(!CORBA::is_nil(_pynode))
1324 Engines::PyNode_var tmpp(Engines::PyNode::_narrow(remoteInterp));
1325 if(_pynode->_is_equivalent(tmpp))
1328 if(!CORBA::is_nil(_pynode))
1329 _pynode->UnRegister();
1330 _pynode=Engines::PyNode::_narrow(remoteInterp);
1333 Engines::PyNodeBase_var PyFuncNode::getRemoteInterpreterHandle()
1335 return Engines::PyNodeBase::_narrow(_pynode);
1338 //! Create a new node of same type with a given name
1339 PyFuncNode* PyFuncNode::cloneNode(const std::string& name)
1341 PyFuncNode* n=new PyFuncNode(name);
1342 n->setScript(_script);
1343 n->setFname(_fname);
1344 list<InputPort *>::iterator iter;
1345 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
1347 InputPyPort *p=(InputPyPort *)*iter;
1348 n->edAddInputPort(p->getName(),p->edGetType());
1350 list<OutputPort *>::iterator iter2;
1351 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
1353 OutputPyPort *p=(OutputPyPort *)*iter2;
1354 n->edAddOutputPort(p->getName(),p->edGetType());
1359 std::string PyFuncNode::getContainerLog()
1361 return PythonEntry::GetContainerLog(_mode,_container,this);
1364 void PyFuncNode::shutdown(int level)
1366 DEBTRACE("PyFuncNode::shutdown " << level);
1367 if(_mode=="local")return;
1370 if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
1371 _pynode=Engines::PyNode::_nil();
1372 _container->shutdown(level);
1376 void PyFuncNode::imposeResource(const std::string& resource_name,
1377 const std::string& container_name)
1379 if(!resource_name.empty() && !container_name.empty())
1381 _imposedResource = resource_name;
1382 _imposedContainer = container_name;
1386 bool PyFuncNode::canAcceptImposedResource()
1388 return _container != nullptr && _container->canAcceptImposedResource();
1391 bool PyFuncNode::hasImposedResource()const
1393 return PythonEntry::hasImposedResource();