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"
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"
81 PythonEntry::PythonEntry():_context(0),_pyfuncSer(0),_pyfuncUnser(0),_pyfuncSimpleSer(0)
85 PythonEntry::~PythonEntry()
88 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
89 // not Py_XDECREF of _pyfuncUnser because it is returned by PyDict_GetItem -> borrowed
90 // not Py_XDECREF of _pyfuncSer because it is returned by PyDict_GetItem -> borrowed
94 void PythonEntry::commonRemoteLoadPart1(InlineNode *reqNode)
96 DEBTRACE( "---------------PythonEntry::CommonRemoteLoad function---------------" );
97 Container *container(reqNode->getContainer());
98 bool isContAlreadyStarted(false);
101 isContAlreadyStarted=container->isAlreadyStarted(reqNode);
102 if(!isContAlreadyStarted)
106 container->start(reqNode);
110 reqNode->setErrorDetails(e.what());
117 std::string what("PythonEntry::CommonRemoteLoad : a load operation requested on \"");
118 what+=reqNode->getName(); what+="\" with no container specified.";
119 reqNode->setErrorDetails(what);
120 throw Exception(what);
124 Engines::Container_var GetContainerObj(InlineNode *reqNode, bool& isStandardCont)
126 isStandardCont = false;
127 Container *container(reqNode->getContainer());
128 Engines::Container_var objContainer(Engines::Container::_nil());
130 throw YACS::Exception("No container specified !");
131 SalomeContainer *containerCast0(dynamic_cast<SalomeContainer *>(container));
132 SalomeHPContainerBase *containerCast1(dynamic_cast<SalomeHPContainerBase *>(container));
135 isStandardCont = true;
136 objContainer=containerCast0->getContainerPtr(reqNode);
138 else if(containerCast1)
140 objContainer=containerCast1->getContainerPtr(reqNode);
143 throw YACS::Exception("Unrecognized type of container ! Salome one is expected for PythonNode/PyFuncNode !");
144 if(CORBA::is_nil(objContainer))
145 throw YACS::Exception("Container corba pointer is NULL for PythonNode !");
149 Engines::Container_var PythonEntry::commonRemoteLoadPart2(InlineNode *reqNode, bool& isInitializeRequested)
151 bool isStandardCont(true);
152 Engines::Container_var objContainer(GetContainerObj(reqNode,isStandardCont));
153 isInitializeRequested=false;
158 createRemoteAdaptedPyInterpretor(objContainer);
162 Engines::PyNodeBase_var dftPyScript(retrieveDftRemotePyInterpretorIfAny(objContainer));
163 if(CORBA::is_nil(dftPyScript))
165 isInitializeRequested=true;
166 createRemoteAdaptedPyInterpretor(objContainer);
169 assignRemotePyInterpretor(dftPyScript);
172 catch( const SALOME::SALOME_Exception& ex )
174 std::string msg="Exception on remote python node creation ";
176 msg += ex.details.text.in();
177 reqNode->setErrorDetails(msg);
178 throw Exception(msg);
180 Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
181 if(CORBA::is_nil(pynode))
182 throw Exception("In PythonNode the ref in NULL ! ");
186 void PythonEntry::commonRemoteLoadPart3(InlineNode *reqNode, Engines::Container_ptr objContainer, bool isInitializeRequested)
188 Container *container(reqNode->getContainer());
189 Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
193 const char *picklizeScript(getSerializationScript());
194 PyObject *res=PyRun_String(picklizeScript,Py_file_input,_context,_context);
195 PyObject *res2(PyRun_String(SCRIPT_FOR_SIMPLE_SERIALIZATION,Py_file_input,_context,_context));
196 if(res == NULL || res2==NULL)
198 std::string errorDetails;
199 PyObject* new_stderr = newPyStdOut(errorDetails);
200 reqNode->setErrorDetails(errorDetails);
201 PySys_SetObject((char*)"stderr", new_stderr);
203 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
204 Py_DECREF(new_stderr);
205 throw Exception("Error during load");
207 Py_DECREF(res); Py_DECREF(res2);
208 _pyfuncSer=PyDict_GetItemString(_context,"pickleForDistPyth2009");
209 _pyfuncUnser=PyDict_GetItemString(_context,"unPickleForDistPyth2009");
210 _pyfuncSimpleSer=PyDict_GetItemString(_context,"pickleForVarSimplePyth2009");
211 if(_pyfuncSer == NULL)
213 std::string errorDetails;
214 PyObject *new_stderr(newPyStdOut(errorDetails));
215 reqNode->setErrorDetails(errorDetails);
216 PySys_SetObject((char*)"stderr", new_stderr);
218 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
219 Py_DECREF(new_stderr);
220 throw Exception("Error during load");
222 if(_pyfuncUnser == NULL)
224 std::string errorDetails;
225 PyObject *new_stderr(newPyStdOut(errorDetails));
226 reqNode->setErrorDetails(errorDetails);
227 PySys_SetObject((char*)"stderr", new_stderr);
229 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
230 Py_DECREF(new_stderr);
231 throw Exception("Error during load");
233 if(_pyfuncSimpleSer == NULL)
235 std::string errorDetails;
236 PyObject *new_stderr(newPyStdOut(errorDetails));
237 reqNode->setErrorDetails(errorDetails);
238 PySys_SetObject((char*)"stderr", new_stderr);
240 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
241 Py_DECREF(new_stderr);
242 throw Exception("Error during load");
245 if(isInitializeRequested)
246 {//This one is called only once at initialization in the container if an init-script is specified.
249 std::string zeInitScriptKey(container->getProperty(HomogeneousPoolContainer::INITIALIZE_SCRIPT_KEY));
250 if(!zeInitScriptKey.empty())
251 pynode->executeAnotherPieceOfCode(zeInitScriptKey.c_str());
253 catch( const SALOME::SALOME_Exception& ex )
255 std::string msg="Exception on PythonNode::loadRemote python invocation of initializisation py script !";
257 msg += ex.details.text.in();
258 reqNode->setErrorDetails(msg);
259 throw Exception(msg);
261 DEBTRACE( "---------------End PyNode::loadRemote function---------------" );
265 std::string PythonEntry::GetContainerLog(const std::string& mode, Container *container, const Task *askingTask)
273 SalomeContainer *containerCast(dynamic_cast<SalomeContainer *>(container));
274 SalomeHPContainer *objContainer2(dynamic_cast<SalomeHPContainer *>(container));
277 Engines::Container_var objContainer(containerCast->getContainerPtr(askingTask));
278 CORBA::String_var logname = objContainer->logfilename();
281 std::string::size_type pos = msg.find(":");
282 msg=msg.substr(pos+1);
284 else if(objContainer2)
286 msg="Remote PythonNode is on HP Container : no log because no info of the location by definition of HP Container !";
290 msg="Not implemented yet for container log for that type of container !";
295 msg = "Container no longer reachable";
300 void PythonEntry::commonRemoteLoad(InlineNode *reqNode)
302 commonRemoteLoadPart1(reqNode);
303 bool isInitializeRequested;
304 Engines::Container_var objContainer(commonRemoteLoadPart2(reqNode,isInitializeRequested));
305 commonRemoteLoadPart3(reqNode,objContainer,isInitializeRequested);
308 PythonNode::PythonNode(const PythonNode& other, ComposedNode *father):InlineNode(other,father),_autoSqueeze(other._autoSqueeze)
310 _implementation=IMPL_NAME;
313 _context=PyDict_New();
314 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
317 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
318 _errorDetails=msg.str();
319 throw Exception(msg.str());
324 PythonNode::PythonNode(const std::string& name):InlineNode(name)
326 _implementation=IMPL_NAME;
329 _context=PyDict_New();
330 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
333 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
334 _errorDetails=msg.str();
335 throw Exception(msg.str());
340 PythonNode::~PythonNode()
342 if(!CORBA::is_nil(_pynode))
344 _pynode->UnRegister();
348 void PythonNode::checkBasicConsistency() const throw(YACS::Exception)
350 DEBTRACE("checkBasicConsistency");
351 InlineNode::checkBasicConsistency();
355 res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
358 std::string error="";
359 PyObject* new_stderr = newPyStdOut(error);
360 PySys_SetObject((char*)"stderr", new_stderr);
362 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
363 Py_DECREF(new_stderr);
364 throw Exception(error);
371 void PythonNode::load()
373 DEBTRACE( "---------------PyNode::load function---------------" );
374 if(_mode==PythonNode::REMOTE_NAME)
380 void PythonNode::loadLocal()
382 DEBTRACE( "---------------PyNode::loadLocal function---------------" );
386 void PythonNode::loadRemote()
388 commonRemoteLoad(this);
391 void PythonNode::execute()
393 if(_mode==PythonNode::REMOTE_NAME)
399 void PythonNode::executeRemote()
401 DEBTRACE( "++++++++++++++ PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
403 throw Exception("DistributedPythonNode badly loaded");
405 if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
408 commonRemoteLoadPart2(this,dummy);
409 _pynode->assignNewCompiledCode(getScript().c_str());
412 std::unique_ptr<Engines::pickledArgs> serializationInputCorba(new Engines::pickledArgs);
413 AutoPyRef serializationInput;
416 PyObject *args(0),*ob(0);
417 //===========================================================================
418 // Get inputs in input ports, build a Python dict and pickle it
419 //===========================================================================
421 std::list<InputPort *>::iterator iter2;
423 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); ++iter2)
425 InputPyPort *p=(InputPyPort *)*iter2;
427 PyDict_SetItemString(args,p->getName().c_str(),ob);
431 PyObject_Print(args,stderr,Py_PRINT_RAW);
434 serializationInput.set(PyObject_CallFunctionObjArgs(_pyfuncSer,args,nullptr));
436 //The pickled string may contain NULL characters so use PyString_AsStringAndSize
437 char *serializationInputC(0);
439 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
440 throw Exception("DistributedPythonNode problem in python pickle");
441 // no copy here. The C byte array of Python is taken as this into CORBA sequence to avoid copy
442 serializationInputCorba.reset(new Engines::pickledArgs(len,len,reinterpret_cast<CORBA::Octet *>(serializationInputC),0));
445 //get the list of output argument names
446 std::list<OutputPort *>::iterator iter;
447 Engines::listofstring myseq;
448 myseq.length(getNumberOfOutputPorts());
450 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
452 OutputPyPort *p=(OutputPyPort *)*iter;
453 myseq[pos]=p->getName().c_str();
454 DEBTRACE( "port name: " << p->getName() );
455 DEBTRACE( "port kind: " << p->edGetType()->kind() );
456 DEBTRACE( "port pos : " << pos );
459 //===========================================================================
460 // Execute in remote Python node
461 //===========================================================================
462 DEBTRACE( "-----------------starting remote python invocation-----------------" );
463 Engines::pickledArgs *resultCorba(nullptr);
466 //pass outargsname and dict serialized
467 _pynode->executeFirst(*(serializationInputCorba.get()));
468 //serializationInput and serializationInputCorba are no more needed for server. Release it.
469 serializationInputCorba.reset(nullptr); serializationInput.set(nullptr);
470 resultCorba=_pynode->executeSecond(myseq);
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 if(!CORBA::is_nil(_pynode))
482 _pynode->UnRegister();
484 _pynode = Engines::PyScriptNode::_nil();
487 Engines::Container_var cont(GetContainerObj(this,dummy));
488 cont->removePyScriptNode(getName().c_str());
489 DEBTRACE( "-----------------end of remote python invocation-----------------" );
490 //===========================================================================
491 // Get results, unpickle and put them in output ports
492 //===========================================================================
493 auto length(resultCorba->length());
494 char *resultCorbaC(reinterpret_cast<char *>(resultCorba->get_buffer()));
497 PyObject *args(0),*ob(0);
498 PyObject* resultPython=PyMemoryView_FromMemory(resultCorbaC,length,PyBUF_READ);
499 args = PyTuple_New(1);
500 PyTuple_SetItem(args,0,resultPython);
501 PyObject *finalResult=PyObject_CallObject(_pyfuncUnser,args);
502 delete resultCorba; resultCorba=nullptr;
505 if (finalResult == NULL)
507 std::stringstream msg;
508 msg << "Conversion with pickle of output ports failed !";
509 msg << " : " << __FILE__ << ":" << __LINE__;
510 _errorDetails=msg.str();
511 throw YACS::ENGINE::ConversionException(msg.str());
514 DEBTRACE( "-----------------PythonNode::outputs-----------------" );
516 if(finalResult == Py_None)
518 else if(PyTuple_Check(finalResult))
519 nres=PyTuple_Size(finalResult);
521 if(getNumberOfOutputPorts() != nres)
523 std::string msg="Number of output arguments : Mismatch between definition and execution";
524 Py_DECREF(finalResult);
526 throw Exception(msg);
532 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
534 OutputPyPort *p=(OutputPyPort *)*iter;
535 DEBTRACE( "port name: " << p->getName() );
536 DEBTRACE( "port kind: " << p->edGetType()->kind() );
537 DEBTRACE( "port pos : " << pos );
538 if(PyTuple_Check(finalResult))
539 ob=PyTuple_GetItem(finalResult,pos) ;
542 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
546 Py_DECREF(finalResult);
548 catch(ConversionException& ex)
550 Py_DECREF(finalResult);
551 _errorDetails=ex.what();
555 squeezeMemoryRemote();
557 DEBTRACE( "++++++++++++++ ENDOF PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
560 void PythonNode::executeLocal()
562 DEBTRACE( "++++++++++++++ PyNode::executeLocal: " << getName() << " ++++++++++++++++++++" );
566 DEBTRACE( "---------------PyNode::inputs---------------" );
567 list<InputPort *>::iterator iter2;
568 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
570 InputPyPort *p=(InputPyPort *)*iter2;
571 DEBTRACE( "port name: " << p->getName() );
572 DEBTRACE( "port kind: " << p->edGetType()->kind() );
573 PyObject* ob=p->getPyObj();
574 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
576 PyObject_Print(ob,stderr,Py_PRINT_RAW);
579 int ier=PyDict_SetItemString(_context,p->getName().c_str(),ob);
580 DEBTRACE( "after PyDict_SetItemString:ob refcnt: " << ob->ob_refcnt );
583 DEBTRACE( "---------------End PyNode::inputs---------------" );
586 DEBTRACE( "----------------PyNode::calculation---------------" );
588 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
590 std::ostringstream stream;
591 stream << "/tmp/PythonNode_";
594 PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
598 PyObject* new_stderr = newPyStdOut(_errorDetails);
599 PySys_SetObject((char*)"stderr", new_stderr);
601 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
602 Py_DECREF(new_stderr);
603 throw Exception("Error during execution");
605 PyObject *res = PyEval_EvalCode( code, _context, _context);
609 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
612 if(PyErr_Occurred ())
615 PyObject* new_stderr = newPyStdOut(_errorDetails);
616 PySys_SetObject((char*)"stderr", new_stderr);
617 ofstream errorfile(stream.str().c_str());
618 if (errorfile.is_open())
620 errorfile << _script;
624 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
625 Py_DECREF(new_stderr);
626 throw Exception("Error during execution");
629 DEBTRACE( "-----------------PyNode::outputs-----------------" );
630 list<OutputPort *>::iterator iter;
633 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
635 OutputPyPort *p=(OutputPyPort *)*iter;
636 DEBTRACE( "port name: " << p->getName() );
637 DEBTRACE( "port kind: " << p->edGetType()->kind() );
638 PyObject *ob=PyDict_GetItemString(_context,p->getName().c_str());
641 std::string msg="Error during execution: there is no variable ";
642 msg=msg+p->getName()+" in node context";
644 throw Exception(msg);
646 DEBTRACE( "PyNode::outputs::ob refcnt: " << ob->ob_refcnt );
648 PyObject_Print(ob,stderr,Py_PRINT_RAW);
654 catch(ConversionException& ex)
656 _errorDetails=ex.what();
661 DEBTRACE( "-----------------End PyNode::outputs-----------------" );
663 DEBTRACE( "++++++++++++++ End PyNode::execute: " << getName() << " ++++++++++++++++++++" );
666 void PythonNode::squeezeMemorySafe()
669 if(_mode==PythonNode::REMOTE_NAME)
670 this->squeezeMemoryRemote();
672 this->squeezeMemory();
675 void PythonNode::squeezeMemory()
677 for(auto p : _setOfInputPort)
679 PyDict_DelItemString(_context,p->getName().c_str());
680 InputPyPort *p2(static_cast<InputPyPort *>(p));
681 if(p2->canSafelySqueezeMemory())
684 for(auto p : _setOfOutputPort)
686 PyDict_DelItemString(_context,p->getName().c_str());
687 OutputPyPort *p2(static_cast<OutputPyPort *>(p));
688 p2->putWithoutForward(Py_None);
692 void PythonNode::squeezeMemoryRemote()
694 for(auto p : _setOfInputPort)
696 InputPyPort *p2(static_cast<InputPyPort *>(p));
697 if(p2->canSafelySqueezeMemory())
700 for(auto p : _setOfOutputPort)
702 OutputPyPort *p2(static_cast<OutputPyPort *>(p));
703 p2->putWithoutForward(Py_None);
707 std::string PythonNode::getContainerLog()
709 return PythonEntry::GetContainerLog(_mode,_container,this);
712 void PythonNode::shutdown(int level)
714 DEBTRACE("PythonNode::shutdown " << level);
715 if(_mode=="local")return;
718 if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
719 _pynode=Engines::PyScriptNode::_nil();
720 _container->shutdown(level);
724 Node *PythonNode::simpleClone(ComposedNode *father, bool editionOnly) const
726 return new PythonNode(*this,father);
729 void PythonNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
731 if(!CORBA::is_nil(_pynode))
732 _pynode->UnRegister();
733 _pynode=objContainer->createPyScriptNode(getName().c_str(),getScript().c_str());
737 Engines::PyNodeBase_var PythonNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
739 Engines::PyScriptNode_var ret(objContainer->getDefaultPyScriptNode(getName().c_str()));
740 if(!CORBA::is_nil(ret))
744 return Engines::PyNodeBase::_narrow(ret);
747 void PythonNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
749 if(!CORBA::is_nil(_pynode))
751 Engines::PyScriptNode_var tmpp(Engines::PyScriptNode::_narrow(remoteInterp));
752 if(_pynode->_is_equivalent(tmpp))
754 _pynode->UnRegister();
758 if(!CORBA::is_nil(_pynode))
759 _pynode->UnRegister();
760 _pynode=Engines::PyScriptNode::_narrow(remoteInterp);
763 Engines::PyNodeBase_var PythonNode::getRemoteInterpreterHandle()
765 return Engines::PyNodeBase::_narrow(_pynode);
768 //! Create a new node of same type with a given name
769 PythonNode* PythonNode::cloneNode(const std::string& name)
771 PythonNode* n=new PythonNode(name);
772 n->setScript(_script);
773 list<InputPort *>::iterator iter;
774 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
776 InputPyPort *p=(InputPyPort *)*iter;
777 DEBTRACE( "port name: " << p->getName() );
778 DEBTRACE( "port kind: " << p->edGetType()->kind() );
779 n->edAddInputPort(p->getName(),p->edGetType());
781 list<OutputPort *>::iterator iter2;
782 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
784 OutputPyPort *p=(OutputPyPort *)*iter2;
785 DEBTRACE( "port name: " << p->getName() );
786 DEBTRACE( "port kind: " << p->edGetType()->kind() );
787 n->edAddOutputPort(p->getName(),p->edGetType());
792 void PythonNode::applyDPLScope(ComposedNode *gfn)
794 std::vector< std::pair<std::string,int> > ret(getDPLScopeInfo(gfn));
801 std::size_t sz(ret.size());
803 for(std::size_t i=0;i<sz;i++)
805 const std::pair<std::string,int>& p(ret[i]);
806 PyObject *elt(PyTuple_New(2));
807 PyTuple_SetItem(elt,0,PyUnicode_FromString(p.first.c_str()));
808 PyTuple_SetItem(elt,1,PyLong_FromLong(p.second));
809 PyList_SetItem(ob,i,elt);
812 if(_mode==REMOTE_NAME)
814 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);
817 PyObject *serializationInput(PyObject_CallFunctionObjArgs(_pyfuncSimpleSer,ob,NULL));
819 char *serializationInputC(0);
821 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
822 throw Exception("DistributedPythonNode problem in python pickle");
823 serializationInputCorba->length(len);
824 for(int i=0; i < len ; i++)
825 serializationInputCorba[i]=serializationInputC[i];
826 Py_XDECREF(serializationInput);
828 _pynode->defineNewCustomVar(DPL_INFO_NAME,serializationInputCorba);
833 PyDict_SetItemString(_context,DPL_INFO_NAME,ob);
838 PyFuncNode::PyFuncNode(const PyFuncNode& other, ComposedNode *father):InlineFuncNode(other,father),_pyfunc(0)
840 _implementation = PythonNode::IMPL_NAME;
843 _context=PyDict_New();
844 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
845 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
848 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
849 _errorDetails=msg.str();
850 throw Exception(msg.str());
855 PyFuncNode::PyFuncNode(const std::string& name): InlineFuncNode(name),_pyfunc(0)
858 _implementation = PythonNode::IMPL_NAME;
859 DEBTRACE( "PyFuncNode::PyFuncNode " << name );
862 _context=PyDict_New();
863 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
864 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
867 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
868 _errorDetails=msg.str();
869 throw Exception(msg.str());
874 PyFuncNode::~PyFuncNode()
876 if(!CORBA::is_nil(_pynode))
878 _pynode->UnRegister();
882 void PyFuncNode::init(bool start)
884 initCommonPartWithoutStateManagement(start);
885 if(_state == YACS::DISABLED)
887 exDisabledState(); // to refresh propagation of DISABLED state
890 if(start) //complete initialization
891 setState(YACS::READY);
892 else if(_state > YACS::LOADED)// WARNING FuncNode has internal vars (CEA usecase) ! Partial initialization (inside a loop). Exclusivity of funcNode.
893 setState(YACS::TORECONNECT);
896 void PyFuncNode::checkBasicConsistency() const throw(YACS::Exception)
898 DEBTRACE("checkBasicConsistency");
899 InlineFuncNode::checkBasicConsistency();
903 res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
906 std::string error="";
907 PyObject* new_stderr = newPyStdOut(error);
908 PySys_SetObject((char*)"stderr", new_stderr);
910 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
911 Py_DECREF(new_stderr);
912 throw Exception(error);
919 void PyFuncNode::load()
921 DEBTRACE( "---------------PyfuncNode::load function---------------" );
922 if(_mode==PythonNode::REMOTE_NAME)
928 void PyFuncNode::loadRemote()
930 commonRemoteLoad(this);
933 void PyFuncNode::loadLocal()
935 DEBTRACE( "---------------PyFuncNode::load function " << getName() << " ---------------" );
939 list<OutputPort *>::iterator iter;
940 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
942 OutputPyPort *p=(OutputPyPort *)*iter;
943 DEBTRACE( "port name: " << p->getName() );
944 DEBTRACE( "port kind: " << p->edGetType()->kind() );
950 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
952 std::ostringstream stream;
953 stream << "/tmp/PythonNode_";
956 PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
960 PyObject* new_stderr = newPyStdOut(_errorDetails);
961 PySys_SetObject((char*)"stderr", new_stderr);
963 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
964 Py_DECREF(new_stderr);
965 throw Exception("Error during execution");
967 PyObject *res = PyEval_EvalCode( code, _context, _context);
971 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
972 if(PyErr_Occurred ())
975 PyObject* new_stderr = newPyStdOut(_errorDetails);
976 PySys_SetObject((char*)"stderr", new_stderr);
977 ofstream errorfile(stream.str().c_str());
978 if (errorfile.is_open())
980 errorfile << _script;
984 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
985 Py_DECREF(new_stderr);
986 throw Exception("Error during execution");
989 _pyfunc=PyDict_GetItemString(_context,_fname.c_str());
990 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
994 PyObject* new_stderr = newPyStdOut(_errorDetails);
995 PySys_SetObject((char*)"stderr", new_stderr);
997 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
998 Py_DECREF(new_stderr);
999 throw Exception("Error during execution");
1001 DEBTRACE( "---------------End PyFuncNode::load function---------------" );
1005 void PyFuncNode::execute()
1007 if(_mode==PythonNode::REMOTE_NAME)
1013 void PyFuncNode::executeRemote()
1015 DEBTRACE( "++++++++++++++ PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
1017 throw Exception("DistributedPythonNode badly loaded");
1019 if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
1022 commonRemoteLoadPart2(this,dummy);
1023 _pynode->executeAnotherPieceOfCode(getScript().c_str());
1026 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);;
1030 //===========================================================================
1031 // Get inputs in input ports, build a Python tuple and pickle it
1032 //===========================================================================
1033 PyObject *args(PyTuple_New(getNumberOfInputPorts()));
1035 for(std::list<InputPort *>::iterator iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++,pos++)
1037 InputPyPort *p=(InputPyPort *)*iter2;
1040 PyTuple_SetItem(args,pos,ob);
1043 PyObject_Print(args,stderr,Py_PRINT_RAW);
1046 PyObject *serializationInput=PyObject_CallObject(_pyfuncSer,args);
1048 //The pickled string may contain NULL characters so use PyString_AsStringAndSize
1049 char *serializationInputC(0);
1051 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
1052 throw Exception("DistributedPythonNode problem in python pickle");
1054 serializationInputCorba->length(len);
1055 for(int i=0; i < len ; i++)
1056 serializationInputCorba[i]=serializationInputC[i];
1057 Py_DECREF(serializationInput);
1060 //===========================================================================
1061 // Execute in remote Python node
1062 //===========================================================================
1063 DEBTRACE( "-----------------starting remote python invocation-----------------" );
1064 Engines::pickledArgs_var resultCorba;
1067 resultCorba=_pynode->execute(getFname().c_str(),serializationInputCorba);
1069 catch( const SALOME::SALOME_Exception& ex )
1071 std::string msg="Exception on remote python invocation";
1073 msg += ex.details.text.in();
1075 throw Exception(msg);
1077 DEBTRACE( "-----------------end of remote python invocation-----------------" );
1078 //===========================================================================
1079 // Get results, unpickle and put them in output ports
1080 //===========================================================================
1081 char *resultCorbaC=new char[resultCorba->length()+1];
1082 resultCorbaC[resultCorba->length()]='\0';
1083 for(int i=0;i<resultCorba->length();i++)
1084 resultCorbaC[i]=resultCorba[i];
1089 PyObject *resultPython(PyBytes_FromStringAndSize(resultCorbaC,resultCorba->length()));
1090 delete [] resultCorbaC;
1091 PyObject *args(PyTuple_New(1)),*ob(0);
1092 PyTuple_SetItem(args,0,resultPython);
1093 PyObject *finalResult=PyObject_CallObject(_pyfuncUnser,args);
1096 DEBTRACE( "-----------------PythonNode::outputs-----------------" );
1098 if(finalResult == Py_None)
1100 else if(PyTuple_Check(finalResult))
1101 nres=PyTuple_Size(finalResult);
1103 if(getNumberOfOutputPorts() != nres)
1105 std::string msg="Number of output arguments : Mismatch between definition and execution";
1106 Py_DECREF(finalResult);
1108 throw Exception(msg);
1114 for(std::list<OutputPort *>::iterator iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++, pos++)
1116 OutputPyPort *p=(OutputPyPort *)*iter;
1117 DEBTRACE( "port name: " << p->getName() );
1118 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1119 DEBTRACE( "port pos : " << pos );
1120 if(PyTuple_Check(finalResult))
1121 ob=PyTuple_GetItem(finalResult,pos) ;
1124 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1127 Py_DECREF(finalResult);
1129 catch(ConversionException& ex)
1131 Py_DECREF(finalResult);
1132 _errorDetails=ex.what();
1137 DEBTRACE( "++++++++++++++ ENDOF PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
1140 void PyFuncNode::executeLocal()
1142 DEBTRACE( "++++++++++++++ PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1146 if(!_pyfunc)throw Exception("PyFuncNode badly loaded");
1149 DEBTRACE( "---------------PyFuncNode::inputs---------------" );
1150 PyObject* args = PyTuple_New(getNumberOfInputPorts()) ;
1151 list<InputPort *>::iterator iter2;
1152 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
1154 InputPyPort *p=(InputPyPort *)*iter2;
1155 DEBTRACE( "port name: " << p->getName() );
1156 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1159 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1162 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1164 PyTuple_SetItem(args,pos,ob);
1165 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1168 DEBTRACE( "---------------End PyFuncNode::inputs---------------" );
1170 DEBTRACE( "----------------PyFuncNode::calculation---------------" );
1172 PyObject_Print(_pyfunc,stderr,Py_PRINT_RAW);
1174 PyObject_Print(args,stderr,Py_PRINT_RAW);
1177 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1178 PyObject* result = PyObject_CallObject( _pyfunc , args ) ;
1179 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1186 PyObject* new_stderr = newPyStdOut(_errorDetails);
1187 PySys_SetObject((char*)"stderr", new_stderr);
1188 std::ostringstream stream;
1189 stream << "/tmp/PythonNode_";
1191 ofstream errorfile(stream.str().c_str());
1192 if (errorfile.is_open())
1194 errorfile << _script;
1198 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1199 Py_DECREF(new_stderr);
1200 throw Exception("Error during execution");
1202 DEBTRACE( "----------------End PyFuncNode::calculation---------------" );
1204 DEBTRACE( "-----------------PyFuncNode::outputs-----------------" );
1206 if(result == Py_None)
1208 else if(PyTuple_Check(result))
1209 nres=PyTuple_Size(result);
1211 if(getNumberOfOutputPorts() != nres)
1213 std::string msg="Number of output arguments : Mismatch between definition and execution";
1216 throw Exception(msg);
1221 PyObject_Print(result,stderr,Py_PRINT_RAW);
1224 list<OutputPort *>::iterator iter;
1227 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
1229 OutputPyPort *p=(OutputPyPort *)*iter;
1230 DEBTRACE( "port name: " << p->getName() );
1231 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1232 DEBTRACE( "port pos : " << pos );
1233 if(PyTuple_Check(result))ob=PyTuple_GetItem(result,pos) ;
1235 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1237 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1244 catch(ConversionException& ex)
1247 _errorDetails=ex.what();
1250 DEBTRACE( "-----------------End PyFuncNode::outputs-----------------" );
1253 DEBTRACE( "++++++++++++++ End PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1256 Node *PyFuncNode::simpleClone(ComposedNode *father, bool editionOnly) const
1258 return new PyFuncNode(*this,father);
1261 void PyFuncNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
1263 if(!CORBA::is_nil(_pynode))
1264 _pynode->UnRegister();
1265 _pynode=objContainer->createPyNode(getName().c_str(),getScript().c_str());
1268 Engines::PyNodeBase_var PyFuncNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
1270 Engines::PyNode_var ret(objContainer->getDefaultPyNode(getName().c_str()));
1271 if(!CORBA::is_nil(ret))
1275 return Engines::PyNodeBase::_narrow(ret);
1278 void PyFuncNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
1280 if(!CORBA::is_nil(_pynode))
1282 Engines::PyNode_var tmpp(Engines::PyNode::_narrow(remoteInterp));
1283 if(_pynode->_is_equivalent(tmpp))
1286 if(!CORBA::is_nil(_pynode))
1287 _pynode->UnRegister();
1288 _pynode=Engines::PyNode::_narrow(remoteInterp);
1291 Engines::PyNodeBase_var PyFuncNode::getRemoteInterpreterHandle()
1293 return Engines::PyNodeBase::_narrow(_pynode);
1296 //! Create a new node of same type with a given name
1297 PyFuncNode* PyFuncNode::cloneNode(const std::string& name)
1299 PyFuncNode* n=new PyFuncNode(name);
1300 n->setScript(_script);
1301 n->setFname(_fname);
1302 list<InputPort *>::iterator iter;
1303 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
1305 InputPyPort *p=(InputPyPort *)*iter;
1306 n->edAddInputPort(p->getName(),p->edGetType());
1308 list<OutputPort *>::iterator iter2;
1309 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
1311 OutputPyPort *p=(OutputPyPort *)*iter2;
1312 n->edAddOutputPort(p->getName(),p->edGetType());
1317 std::string PyFuncNode::getContainerLog()
1319 return PythonEntry::GetContainerLog(_mode,_container,this);
1322 void PyFuncNode::shutdown(int level)
1324 DEBTRACE("PyFuncNode::shutdown " << level);
1325 if(_mode=="local")return;
1328 if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
1329 _pynode=Engines::PyNode::_nil();
1330 _container->shutdown(level);