1 // Copyright (C) 2006-2023 CEA, EDF
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::IsProxy( PyObject *ob )
335 if( PyObject_IsInstance( ob, _pyClsBigObject) == 1 )
341 if( PyList_Check( ob ) )
343 auto sz = PyList_Size( ob );
344 for( auto i = 0 ; i < sz ; ++i )
346 PyObject *elt = PyList_GetItem( ob, i );
347 if( PythonEntry::IsProxy(elt) )
355 bool PythonEntry::GetDestroyStatus( PyObject *ob )
359 if( PyObject_IsInstance( ob, _pyClsBigObject) == 1 )
361 AutoPyRef unlinkOnDestructor = PyObject_GetAttrString(ob,"getDestroyStatus");
362 AutoPyRef tmp = PyObject_CallFunctionObjArgs(unlinkOnDestructor,nullptr);
363 if( PyBool_Check(tmp.get()) )
365 return tmp.get() == Py_True;
371 if( PyList_Check( ob ) )
373 auto sz = PyList_Size( ob );
374 for( auto i = 0 ; i < sz ; ++i )
376 PyObject *elt = PyList_GetItem( ob, i );
377 if( PythonEntry::GetDestroyStatus(elt) )
385 void PythonEntry::IfProxyDoSomething( PyObject *ob, const char *meth )
389 if( PyObject_IsInstance( ob, _pyClsBigObject) == 1 )
391 AutoPyRef unlinkOnDestructor = PyObject_GetAttrString(ob,meth);
392 AutoPyRef tmp = PyObject_CallFunctionObjArgs(unlinkOnDestructor,nullptr);
396 if( PyList_Check( ob ) )
398 auto sz = PyList_Size( ob );
399 for( auto i = 0 ; i < sz ; ++i )
401 PyObject *elt = PyList_GetItem( ob, i );
402 PythonEntry::IfProxyDoSomething( elt, meth );
408 void PythonEntry::DoNotTouchFileIfProxy( PyObject *ob )
410 IfProxyDoSomething(ob,"doNotTouchFile");
413 void PythonEntry::UnlinkOnDestructorIfProxy( PyObject *ob )
415 IfProxyDoSomething(ob,"unlinkOnDestructor");
418 PythonNode::PythonNode(const PythonNode& other, ComposedNode *father):InlineNode(other,father),_autoSqueeze(other._autoSqueeze)
420 _pynode = Engines::PyScriptNode::_nil();
421 _implementation=IMPL_NAME;
424 _context=PyDict_New();
425 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
428 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
429 _errorDetails=msg.str();
430 throw Exception(msg.str());
435 PythonNode::PythonNode(const std::string& name):InlineNode(name)
437 _pynode = Engines::PyScriptNode::_nil();
438 _implementation=IMPL_NAME;
441 _context=PyDict_New();
442 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
445 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
446 _errorDetails=msg.str();
447 throw Exception(msg.str());
452 PythonNode::~PythonNode()
457 void PythonNode::checkBasicConsistency() const
459 DEBTRACE("checkBasicConsistency");
460 InlineNode::checkBasicConsistency();
464 res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
467 std::string error="";
468 PyObject* new_stderr = newPyStdOut(error);
469 PySys_SetObject((char*)"stderr", new_stderr);
471 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
472 Py_DECREF(new_stderr);
473 throw Exception(error);
480 void PythonNode::load()
482 DEBTRACE( "---------------PyNode::load function---------------" );
483 if(_mode==PythonNode::REMOTE_NAME)
489 void PythonNode::loadLocal()
491 DEBTRACE( "---------------PyNode::loadLocal function---------------" );
495 void PythonNode::loadRemote()
497 commonRemoteLoad(this);
500 void PythonNode::execute()
502 if(_mode==PythonNode::REMOTE_NAME)
508 void PythonNode::executeRemote()
510 DEBTRACE( "++++++++++++++ PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
512 throw Exception("PythonNode badly loaded");
514 if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
517 loadPythonAdapter(this,dummy);
518 _pynode->assignNewCompiledCode(getScript().c_str());
520 // not managed by unique_ptr here because destructed by the order of client.
521 SenderByteImpl *serializationInputCorba = nullptr;
522 AutoPyRef serializationInput;
524 #if PY_VERSION_HEX < 0x03070000
525 std::unique_lock<std::mutex> lock(data_mutex);
528 PyObject *args(0),*ob(0);
529 //===========================================================================
530 // Get inputs in input ports, build a Python dict and pickle it
531 //===========================================================================
533 std::list<InputPort *>::iterator iter2;
535 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); ++iter2)
537 InputPyPort *p=(InputPyPort *)*iter2;
539 PyDict_SetItemString(args,p->getName().c_str(),ob);
543 PyObject_Print(args,stderr,Py_PRINT_RAW);
546 serializationInput.set(PyObject_CallFunctionObjArgs(_pyfuncSer,args,nullptr));
548 //The pickled string may contain NULL characters so use PyString_AsStringAndSize
549 char *serializationInputC(nullptr);
551 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
552 throw Exception("DistributedPythonNode problem in python pickle");
553 // no copy here. The C byte array of Python is taken as this into CORBA sequence to avoid copy
554 serializationInputCorba = new SenderByteImpl(serializationInputC,len);
557 //get the list of output argument names
558 std::list<OutputPort *>::iterator iter;
559 Engines::listofstring myseq;
560 myseq.length(getNumberOfOutputPorts());
562 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
564 OutputPyPort *p=(OutputPyPort *)*iter;
565 myseq[pos]=p->getName().c_str();
566 DEBTRACE( "port name: " << p->getName() );
567 DEBTRACE( "port kind: " << p->edGetType()->kind() );
568 DEBTRACE( "port pos : " << pos );
571 //===========================================================================
572 // Execute in remote Python node
573 //===========================================================================
574 DEBTRACE( "-----------------starting remote python invocation-----------------" );
575 std::unique_ptr<SALOME::SenderByteSeq> resultCorba;
578 //pass outargsname and dict serialized
579 SALOME::SenderByte_var serializationInputRef = serializationInputCorba->_this();
580 _pynode->executeFirst(serializationInputRef);
581 //serializationInput and serializationInputCorba are no more needed for server. Release it.
582 serializationInput.set(nullptr);
583 resultCorba.reset( _pynode->executeSecond(myseq) );
585 catch( const SALOME::SALOME_Exception& ex )
587 std::ostringstream msg; msg << "Exception on remote python invocation" << std::endl << ex.details.text.in() << std::endl;
588 msg << "PyScriptNode CORBA ref : ";
590 CORBA::ORB_ptr orb(getSALOMERuntime()->getOrb());
591 if(!CORBA::is_nil(orb))
593 CORBA::String_var IOR(orb->object_to_string(_pynode));
598 _errorDetails=msg.str();
599 throw Exception(msg.str());
601 catch(CORBA::COMM_FAILURE& ex)
603 std::ostringstream msg;
604 msg << "Exception on remote python invocation." << std::endl ;
605 msg << "Caught system exception COMM_FAILURE -- unable to contact the "
606 << "object." << std::endl;
607 _errorDetails=msg.str();
608 throw Exception(msg.str());
610 catch(CORBA::SystemException& ex)
612 std::ostringstream msg;
613 msg << "Exception on remote python invocation." << std::endl ;
614 msg << "Caught a CORBA::SystemException." ;
617 CORBA::TypeCode_var tc = tmp.type();
618 const char *p = tc->name();
624 _errorDetails=msg.str();
625 throw Exception(msg.str());
627 catch(CORBA::Exception& ex)
629 std::ostringstream msg;
630 msg << "Exception on remote python invocation." << std::endl ;
631 msg << "Caught CORBA::Exception. " ;
634 CORBA::TypeCode_var tc = tmp.type();
635 const char *p = tc->name();
641 _errorDetails=msg.str();
642 throw Exception(msg.str());
644 catch(omniORB::fatalException& fe)
646 std::ostringstream msg;
647 msg << "Exception on remote python invocation." << std::endl ;
648 msg << "Caught omniORB::fatalException:" << std::endl;
649 msg << " file: " << fe.file() << std::endl;
650 msg << " line: " << fe.line() << std::endl;
651 msg << " mesg: " << fe.errmsg() << std::endl;
652 _errorDetails=msg.str();
653 throw Exception(msg.str());
655 DEBTRACE( "-----------------end of remote python invocation-----------------" );
656 //===========================================================================
657 // Get results, unpickle and put them in output ports
658 //===========================================================================
660 #if PY_VERSION_HEX < 0x03070000
661 std::unique_lock<std::mutex> lock(data_mutex);
664 DEBTRACE( "-----------------PythonNode::outputs-----------------" );
665 int nres( resultCorba->length() );
667 if(getNumberOfOutputPorts() != nres)
669 std::string msg="Number of output arguments : Mismatch between definition and execution";
671 throw Exception(msg);
676 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); ++iter)
678 OutputPyPort *p=(OutputPyPort *)*iter;
679 DEBTRACE( "port name: " << p->getName() );
680 DEBTRACE( "port kind: " << p->edGetType()->kind() );
681 DEBTRACE( "port pos : " << pos );
682 SALOME::SenderByte_var elt = (*resultCorba)[pos];
683 SeqByteReceiver recv(elt);
684 unsigned long length = 0;
685 char *resultCorbaC = recv.data(length);
687 AutoPyRef resultPython=PyMemoryView_FromMemory(resultCorbaC,length,PyBUF_READ);
688 AutoPyRef args = PyTuple_New(1);
689 PyTuple_SetItem(args,0,resultPython.retn());
690 AutoPyRef ob = PyObject_CallObject(_pyfuncUnser,args);
693 std::stringstream msg;
694 msg << "Conversion with pickle of output ports failed !";
695 msg << " : " << __FILE__ << ":" << __LINE__;
696 _errorDetails=msg.str();
697 throw YACS::ENGINE::ConversionException(msg.str());
699 UnlinkOnDestructorIfProxy(ob);
705 catch(ConversionException& ex)
707 _errorDetails=ex.what();
711 squeezeMemoryRemote();
714 if(!isUsingPythonCache())
718 Engines::Container_var cont(GetContainerObj(this,dummy));
719 cont->removePyScriptNode(getName().c_str());
721 DEBTRACE( "++++++++++++++ ENDOF PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
724 void PythonNode::ExecuteLocalInternal(const std::string& codeStr, PyObject *context, std::string& errorDetails)
727 DEBTRACE( "context refcnt: " << context->ob_refcnt );
728 std::ostringstream stream;
729 stream << "/tmp/PythonNode_";
731 AutoPyRef code=Py_CompileString(codeStr.c_str(), stream.str().c_str(), Py_file_input);
735 AutoPyRef new_stderr = newPyStdOut(errorDetails);
736 PySys_SetObject((char*)"stderr", new_stderr);
738 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
739 throw YACS::Exception("Error during execution");
742 AutoPyRef res = PyEval_EvalCode( code, context, context);
744 DEBTRACE( "context refcnt: " << context->ob_refcnt );
747 if(PyErr_Occurred ())
750 AutoPyRef new_stderr = newPyStdOut(errorDetails);
751 PySys_SetObject((char*)"stderr", new_stderr);
752 ofstream errorfile(stream.str().c_str());
753 if (errorfile.is_open())
755 errorfile << codeStr;
759 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
760 throw YACS::Exception("Error during execution");
764 void PythonNode::executeLocalInternal(const std::string& codeStr)
766 ExecuteLocalInternal(codeStr,_context,_errorDetails);
769 void PythonNode::executeLocal()
771 DEBTRACE( "++++++++++++++ PyNode::executeLocal: " << getName() << " ++++++++++++++++++++" );
774 std::ostringstream unpxy; unpxy << "from SALOME_PyNode import UnProxyObjectSimpleLocal" << std::endl;
775 DEBTRACE( "---------------PyNode::inputs---------------" );
776 list<InputPort *>::iterator iter2;
777 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
779 InputPyPort *p=(InputPyPort *)*iter2;
780 DEBTRACE( "port name: " << p->getName() );
781 DEBTRACE( "port kind: " << p->edGetType()->kind() );
782 PyObject* ob=p->getPyObj();
783 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
784 unpxy << p->getName() << " = UnProxyObjectSimpleLocal( " << p->getName() << " )" << std::endl;
786 PyObject_Print(ob,stderr,Py_PRINT_RAW);
789 int ier=PyDict_SetItemString(_context,p->getName().c_str(),ob);
790 DEBTRACE( "after PyDict_SetItemString:ob refcnt: " << ob->ob_refcnt );
793 DEBTRACE( "---------------End PyNode::inputs---------------" );
796 DEBTRACE( "----------------PyNode::calculation---------------" );
798 if( ! getSqueezeStatus() )
799 executeLocalInternal( unpxy.str() );
801 executeLocalInternal( _script );
803 DEBTRACE( "-----------------PyNode::outputs-----------------" );
804 list<OutputPort *>::iterator iter;
807 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
809 OutputPyPort *p=(OutputPyPort *)*iter;
810 DEBTRACE( "port name: " << p->getName() );
811 DEBTRACE( "port kind: " << p->edGetType()->kind() );
812 PyObject *ob=PyDict_GetItemString(_context,p->getName().c_str());
815 std::string msg="Error during execution: there is no variable ";
816 msg=msg+p->getName()+" in node context";
818 throw Exception(msg);
820 DEBTRACE( "PyNode::outputs::ob refcnt: " << ob->ob_refcnt );
822 PyObject_Print(ob,stderr,Py_PRINT_RAW);
826 if(!isUsingPythonCache())
827 PyDict_DelItemString(_context,p->getName().c_str());
830 catch(ConversionException& ex)
832 _errorDetails=ex.what();
837 DEBTRACE( "-----------------End PyNode::outputs-----------------" );
838 if(!isUsingPythonCache())
840 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
842 AutoPyRef pStr = PyUnicode_FromString( (*iter2)->getName().c_str() );
843 if( PyDict_Contains(_context,pStr) == 1 )
844 { PyDict_DelItem(_context,pStr); }
848 DEBTRACE( "++++++++++++++ End PyNode::execute: " << getName() << " ++++++++++++++++++++" );
851 void PythonNode::squeezeMemorySafe()
854 if(_mode==PythonNode::REMOTE_NAME)
855 this->squeezeMemoryRemote();
857 this->squeezeMemory();
860 void PythonNode::squeezeMemory()
862 for(auto p : _setOfInputPort)
864 PyDict_DelItemString(_context,p->getName().c_str());
865 InputPyPort *p2(static_cast<InputPyPort *>(p));
866 if(p2->canSafelySqueezeMemory())
869 for(auto p : _setOfOutputPort)
871 PyDict_DelItemString(_context,p->getName().c_str());
872 OutputPyPort *p2(static_cast<OutputPyPort *>(p));
873 p2->putWithoutForward(Py_None);
877 void PythonNode::squeezeMemoryRemote()
879 for(auto p : _setOfInputPort)
881 InputPyPort *p2(static_cast<InputPyPort *>(p));
882 if(p2->canSafelySqueezeMemory())
885 for(auto p : _setOfOutputPort)
887 OutputPyPort *p2(static_cast<OutputPyPort *>(p));
888 p2->putWithoutForward(Py_None);
892 std::string PythonNode::getContainerLog()
894 return PythonEntry::GetContainerLog(_mode,_container,this);
897 void PythonNode::shutdown(int level)
899 DEBTRACE("PythonNode::shutdown " << level);
900 if(_mode=="local")return;
904 _container->shutdown(level);
908 void PythonNode::imposeResource(const std::string& resource_name,
909 const std::string& container_name)
911 if(!resource_name.empty() && !container_name.empty())
913 _imposedResource = resource_name;
914 _imposedContainer = container_name;
918 bool PythonNode::canAcceptImposedResource()
920 return _container != nullptr && _container->canAcceptImposedResource();
923 bool PythonNode::hasImposedResource()const
925 return PythonEntry::hasImposedResource();
928 std::string PythonNode::pythonEntryName()const
930 if(isUsingPythonCache())
931 return "DEFAULT_NAME_FOR_UNIQUE_PYTHON_NODE_ENTRY";
936 bool PythonNode::isUsingPythonCache()const
940 found = _container->isUsingPythonCache();
944 void PythonNode::freeKernelPynode()
946 if(!CORBA::is_nil(_pynode))
950 _pynode->UnRegister();
954 DEBTRACE("Trouble when pynode->UnRegister!")
956 _pynode = Engines::PyScriptNode::_nil();
960 Node *PythonNode::simpleClone(ComposedNode *father, bool editionOnly) const
962 return new PythonNode(*this,father);
965 void PythonNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
968 _pynode=objContainer->createPyScriptNode(pythonEntryName().c_str(),getScript().c_str());
972 Engines::PyNodeBase_var PythonNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
974 Engines::PyScriptNode_var ret(objContainer->getDefaultPyScriptNode(pythonEntryName().c_str()));
975 if(!CORBA::is_nil(ret))
979 return Engines::PyNodeBase::_narrow(ret);
982 void PythonNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
984 if(CORBA::is_nil(_pynode))
985 _pynode=Engines::PyScriptNode::_narrow(remoteInterp);
988 Engines::PyScriptNode_var tmpp(Engines::PyScriptNode::_narrow(remoteInterp));
989 if(!_pynode->_is_equivalent(tmpp))
992 _pynode=Engines::PyScriptNode::_narrow(remoteInterp);
995 _pynode->assignNewCompiledCode(getScript().c_str());
998 Engines::PyNodeBase_var PythonNode::getRemoteInterpreterHandle()
1000 return Engines::PyNodeBase::_narrow(_pynode);
1003 //! Create a new node of same type with a given name
1004 PythonNode* PythonNode::cloneNode(const std::string& name)
1006 PythonNode* n=new PythonNode(name);
1007 n->setScript(_script);
1008 list<InputPort *>::iterator iter;
1009 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
1011 InputPyPort *p=(InputPyPort *)*iter;
1012 DEBTRACE( "port name: " << p->getName() );
1013 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1014 n->edAddInputPort(p->getName(),p->edGetType());
1016 list<OutputPort *>::iterator iter2;
1017 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
1019 OutputPyPort *p=(OutputPyPort *)*iter2;
1020 DEBTRACE( "port name: " << p->getName() );
1021 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1022 n->edAddOutputPort(p->getName(),p->edGetType());
1027 void PythonNode::applyDPLScope(ComposedNode *gfn)
1029 std::vector< std::pair<std::string,int> > ret(getDPLScopeInfo(gfn));
1036 std::size_t sz(ret.size());
1038 for(std::size_t i=0;i<sz;i++)
1040 const std::pair<std::string,int>& p(ret[i]);
1041 PyObject *elt(PyTuple_New(2));
1042 PyTuple_SetItem(elt,0,PyUnicode_FromString(p.first.c_str()));
1043 PyTuple_SetItem(elt,1,PyLong_FromLong(p.second));
1044 PyList_SetItem(ob,i,elt);
1047 if(_mode==REMOTE_NAME)
1049 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);
1052 PyObject *serializationInput(PyObject_CallFunctionObjArgs(_pyfuncSimpleSer,ob,NULL));
1054 char *serializationInputC(0);
1056 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
1057 throw Exception("DistributedPythonNode problem in python pickle");
1058 serializationInputCorba->length(len);
1059 for(int i=0; i < len ; i++)
1060 serializationInputCorba[i]=serializationInputC[i];
1061 Py_XDECREF(serializationInput);
1063 _pynode->defineNewCustomVar(DPL_INFO_NAME,serializationInputCorba);
1068 PyDict_SetItemString(_context,DPL_INFO_NAME,ob);
1073 PyFuncNode::PyFuncNode(const PyFuncNode& other, ComposedNode *father):InlineFuncNode(other,father),_pyfunc(0)
1075 _implementation = PythonNode::IMPL_NAME;
1078 _context=PyDict_New();
1079 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
1080 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
1083 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
1084 _errorDetails=msg.str();
1085 throw Exception(msg.str());
1090 PyFuncNode::PyFuncNode(const std::string& name): InlineFuncNode(name),_pyfunc(0)
1093 _implementation = PythonNode::IMPL_NAME;
1094 DEBTRACE( "PyFuncNode::PyFuncNode " << name );
1097 _context=PyDict_New();
1098 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
1099 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
1102 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
1103 _errorDetails=msg.str();
1104 throw Exception(msg.str());
1109 PyFuncNode::~PyFuncNode()
1111 if(!CORBA::is_nil(_pynode))
1113 _pynode->UnRegister();
1117 void PyFuncNode::init(bool start)
1119 initCommonPartWithoutStateManagement(start);
1120 if(_state == YACS::DISABLED)
1122 exDisabledState(); // to refresh propagation of DISABLED state
1125 if(start) //complete initialization
1126 setState(YACS::READY);
1127 else if(_state > YACS::LOADED)// WARNING FuncNode has internal vars (CEA usecase) ! Partial initialization (inside a loop). Exclusivity of funcNode.
1128 setState(YACS::TORECONNECT);
1131 void PyFuncNode::checkBasicConsistency() const
1133 DEBTRACE("checkBasicConsistency");
1134 InlineFuncNode::checkBasicConsistency();
1138 res=Py_CompileString(_script.c_str(),getName().c_str(),Py_file_input);
1141 std::string error="";
1142 PyObject* new_stderr = newPyStdOut(error);
1143 PySys_SetObject((char*)"stderr", new_stderr);
1145 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1146 Py_DECREF(new_stderr);
1147 throw Exception(error);
1154 void PyFuncNode::load()
1156 DEBTRACE( "---------------PyfuncNode::load function---------------" );
1157 if(_mode==PythonNode::REMOTE_NAME)
1163 void PyFuncNode::loadRemote()
1165 commonRemoteLoad(this);
1168 void PyFuncNode::loadLocal()
1170 DEBTRACE( "---------------PyFuncNode::load function " << getName() << " ---------------" );
1171 DEBTRACE( _script );
1174 list<OutputPort *>::iterator iter;
1175 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
1177 OutputPyPort *p=(OutputPyPort *)*iter;
1178 DEBTRACE( "port name: " << p->getName() );
1179 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1185 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
1187 std::ostringstream stream;
1188 stream << "/tmp/PythonNode_";
1191 PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
1195 PyObject* new_stderr = newPyStdOut(_errorDetails);
1196 PySys_SetObject((char*)"stderr", new_stderr);
1198 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1199 Py_DECREF(new_stderr);
1200 throw Exception("Error during execution");
1202 PyObject *res = PyEval_EvalCode( code, _context, _context);
1206 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
1207 if(PyErr_Occurred ())
1210 PyObject* new_stderr = newPyStdOut(_errorDetails);
1211 PySys_SetObject((char*)"stderr", new_stderr);
1212 ofstream errorfile(stream.str().c_str());
1213 if (errorfile.is_open())
1215 errorfile << _script;
1219 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1220 Py_DECREF(new_stderr);
1221 throw Exception("Error during execution");
1224 _pyfunc=PyDict_GetItemString(_context,_fname.c_str());
1225 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1229 PyObject* new_stderr = newPyStdOut(_errorDetails);
1230 PySys_SetObject((char*)"stderr", new_stderr);
1232 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1233 Py_DECREF(new_stderr);
1234 throw Exception("Error during execution");
1236 DEBTRACE( "---------------End PyFuncNode::load function---------------" );
1240 void PyFuncNode::execute()
1242 if(_mode==PythonNode::REMOTE_NAME)
1248 void PyFuncNode::executeRemote()
1250 DEBTRACE( "++++++++++++++ PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
1252 throw Exception("DistributedPythonNode badly loaded");
1254 if(dynamic_cast<HomogeneousPoolContainer *>(getContainer()))
1257 loadPythonAdapter(this,dummy);
1258 _pynode->executeAnotherPieceOfCode(getScript().c_str());
1261 Engines::pickledArgs_var serializationInputCorba(new Engines::pickledArgs);;
1263 #if PY_VERSION_HEX < 0x03070000
1264 std::unique_lock<std::mutex> lock(data_mutex);
1268 //===========================================================================
1269 // Get inputs in input ports, build a Python tuple and pickle it
1270 //===========================================================================
1271 PyObject *args(PyTuple_New(getNumberOfInputPorts()));
1273 for(std::list<InputPort *>::iterator iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++,pos++)
1275 InputPyPort *p=(InputPyPort *)*iter2;
1278 PyTuple_SetItem(args,pos,ob);
1281 PyObject_Print(args,stderr,Py_PRINT_RAW);
1284 PyObject *serializationInput=PyObject_CallObject(_pyfuncSer,args);
1286 //The pickled string may contain NULL characters so use PyString_AsStringAndSize
1287 char *serializationInputC(0);
1289 if (PyBytes_AsStringAndSize(serializationInput, &serializationInputC, &len))
1290 throw Exception("DistributedPythonNode problem in python pickle");
1292 serializationInputCorba->length(len);
1293 for(int i=0; i < len ; i++)
1294 serializationInputCorba[i]=serializationInputC[i];
1295 Py_DECREF(serializationInput);
1298 //===========================================================================
1299 // Execute in remote Python node
1300 //===========================================================================
1301 DEBTRACE( "-----------------starting remote python invocation-----------------" );
1302 Engines::pickledArgs_var resultCorba;
1305 resultCorba=_pynode->execute(getFname().c_str(),serializationInputCorba);
1307 catch( const SALOME::SALOME_Exception& ex )
1309 std::string msg="Exception on remote python invocation";
1311 msg += ex.details.text.in();
1313 throw Exception(msg);
1315 catch(CORBA::COMM_FAILURE& ex)
1317 std::ostringstream msg;
1318 msg << "Exception on remote python invocation." << std::endl ;
1319 msg << "Caught system exception COMM_FAILURE -- unable to contact the "
1320 << "object." << std::endl;
1321 _errorDetails=msg.str();
1322 throw Exception(msg.str());
1324 catch(CORBA::SystemException& ex)
1326 std::ostringstream msg;
1327 msg << "Exception on remote python invocation." << std::endl ;
1328 msg << "Caught a CORBA::SystemException." ;
1331 CORBA::TypeCode_var tc = tmp.type();
1332 const char *p = tc->name();
1338 _errorDetails=msg.str();
1339 throw Exception(msg.str());
1341 catch(CORBA::Exception& ex)
1343 std::ostringstream msg;
1344 msg << "Exception on remote python invocation." << std::endl ;
1345 msg << "Caught CORBA::Exception. " ;
1348 CORBA::TypeCode_var tc = tmp.type();
1349 const char *p = tc->name();
1355 _errorDetails=msg.str();
1356 throw Exception(msg.str());
1358 catch(omniORB::fatalException& fe)
1360 std::ostringstream msg;
1361 msg << "Exception on remote python invocation." << std::endl ;
1362 msg << "Caught omniORB::fatalException:" << std::endl;
1363 msg << " file: " << fe.file() << std::endl;
1364 msg << " line: " << fe.line() << std::endl;
1365 msg << " mesg: " << fe.errmsg() << std::endl;
1366 _errorDetails=msg.str();
1367 throw Exception(msg.str());
1369 DEBTRACE( "-----------------end of remote python invocation-----------------" );
1370 //===========================================================================
1371 // Get results, unpickle and put them in output ports
1372 //===========================================================================
1373 char *resultCorbaC=new char[resultCorba->length()+1];
1374 resultCorbaC[resultCorba->length()]='\0';
1375 for(int i=0;i<resultCorba->length();i++)
1376 resultCorbaC[i]=resultCorba[i];
1379 #if PY_VERSION_HEX < 0x03070000
1380 std::unique_lock<std::mutex> lock(data_mutex);
1384 PyObject *resultPython(PyBytes_FromStringAndSize(resultCorbaC,resultCorba->length()));
1385 delete [] resultCorbaC;
1386 PyObject *args(PyTuple_New(1)),*ob(0);
1387 PyTuple_SetItem(args,0,resultPython);
1388 PyObject *finalResult=PyObject_CallObject(_pyfuncUnser,args);
1391 DEBTRACE( "-----------------PythonNode::outputs-----------------" );
1393 if(finalResult == Py_None)
1395 else if(PyTuple_Check(finalResult))
1396 nres=PyTuple_Size(finalResult);
1398 if(getNumberOfOutputPorts() != nres)
1400 std::string msg="Number of output arguments : Mismatch between definition and execution";
1401 Py_DECREF(finalResult);
1403 throw Exception(msg);
1409 for(std::list<OutputPort *>::iterator iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++, pos++)
1411 OutputPyPort *p=(OutputPyPort *)*iter;
1412 DEBTRACE( "port name: " << p->getName() );
1413 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1414 DEBTRACE( "port pos : " << pos );
1415 if(PyTuple_Check(finalResult))
1416 ob=PyTuple_GetItem(finalResult,pos) ;
1419 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1422 Py_DECREF(finalResult);
1424 catch(ConversionException& ex)
1426 Py_DECREF(finalResult);
1427 _errorDetails=ex.what();
1432 DEBTRACE( "++++++++++++++ ENDOF PyFuncNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
1435 void PyFuncNode::executeLocal()
1437 DEBTRACE( "++++++++++++++ PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1441 if(!_pyfunc)throw Exception("PyFuncNode badly loaded");
1444 DEBTRACE( "---------------PyFuncNode::inputs---------------" );
1445 PyObject* args = PyTuple_New(getNumberOfInputPorts()) ;
1446 list<InputPort *>::iterator iter2;
1447 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
1449 InputPyPort *p=(InputPyPort *)*iter2;
1450 DEBTRACE( "port name: " << p->getName() );
1451 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1454 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1457 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1459 PyTuple_SetItem(args,pos,ob);
1460 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1463 DEBTRACE( "---------------End PyFuncNode::inputs---------------" );
1465 DEBTRACE( "----------------PyFuncNode::calculation---------------" );
1467 PyObject_Print(_pyfunc,stderr,Py_PRINT_RAW);
1469 PyObject_Print(args,stderr,Py_PRINT_RAW);
1472 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1473 PyObject* result = PyObject_CallObject( _pyfunc , args ) ;
1474 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
1481 PyObject* new_stderr = newPyStdOut(_errorDetails);
1482 PySys_SetObject((char*)"stderr", new_stderr);
1483 std::ostringstream stream;
1484 stream << "/tmp/PythonNode_";
1486 ofstream errorfile(stream.str().c_str());
1487 if (errorfile.is_open())
1489 errorfile << _script;
1493 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1494 Py_DECREF(new_stderr);
1495 throw Exception("Error during execution");
1497 DEBTRACE( "----------------End PyFuncNode::calculation---------------" );
1499 DEBTRACE( "-----------------PyFuncNode::outputs-----------------" );
1501 if(result == Py_None)
1503 else if(PyTuple_Check(result))
1504 nres=PyTuple_Size(result);
1506 if(getNumberOfOutputPorts() != nres)
1508 std::string msg="Number of output arguments : Mismatch between definition and execution";
1511 throw Exception(msg);
1516 PyObject_Print(result,stderr,Py_PRINT_RAW);
1519 list<OutputPort *>::iterator iter;
1522 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
1524 OutputPyPort *p=(OutputPyPort *)*iter;
1525 DEBTRACE( "port name: " << p->getName() );
1526 DEBTRACE( "port kind: " << p->edGetType()->kind() );
1527 DEBTRACE( "port pos : " << pos );
1528 if(PyTuple_Check(result))ob=PyTuple_GetItem(result,pos) ;
1530 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
1532 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1539 catch(ConversionException& ex)
1542 _errorDetails=ex.what();
1545 DEBTRACE( "-----------------End PyFuncNode::outputs-----------------" );
1548 DEBTRACE( "++++++++++++++ End PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
1551 Node *PyFuncNode::simpleClone(ComposedNode *father, bool editionOnly) const
1553 return new PyFuncNode(*this,father);
1556 void PyFuncNode::createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)
1558 if(!CORBA::is_nil(_pynode))
1559 _pynode->UnRegister();
1560 _pynode=objContainer->createPyNode(getName().c_str(),getScript().c_str());
1563 Engines::PyNodeBase_var PyFuncNode::retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const
1565 Engines::PyNode_var ret(objContainer->getDefaultPyNode(getName().c_str()));
1566 if(!CORBA::is_nil(ret))
1570 return Engines::PyNodeBase::_narrow(ret);
1573 void PyFuncNode::assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)
1575 if(!CORBA::is_nil(_pynode))
1577 Engines::PyNode_var tmpp(Engines::PyNode::_narrow(remoteInterp));
1578 if(_pynode->_is_equivalent(tmpp))
1581 if(!CORBA::is_nil(_pynode))
1582 _pynode->UnRegister();
1583 _pynode=Engines::PyNode::_narrow(remoteInterp);
1586 Engines::PyNodeBase_var PyFuncNode::getRemoteInterpreterHandle()
1588 return Engines::PyNodeBase::_narrow(_pynode);
1591 //! Create a new node of same type with a given name
1592 PyFuncNode* PyFuncNode::cloneNode(const std::string& name)
1594 PyFuncNode* n=new PyFuncNode(name);
1595 n->setScript(_script);
1596 n->setFname(_fname);
1597 list<InputPort *>::iterator iter;
1598 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
1600 InputPyPort *p=(InputPyPort *)*iter;
1601 n->edAddInputPort(p->getName(),p->edGetType());
1603 list<OutputPort *>::iterator iter2;
1604 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
1606 OutputPyPort *p=(OutputPyPort *)*iter2;
1607 n->edAddOutputPort(p->getName(),p->edGetType());
1612 std::string PyFuncNode::getContainerLog()
1614 return PythonEntry::GetContainerLog(_mode,_container,this);
1617 void PyFuncNode::shutdown(int level)
1619 DEBTRACE("PyFuncNode::shutdown " << level);
1620 if(_mode=="local")return;
1623 if(!CORBA::is_nil(_pynode)) _pynode->UnRegister();
1624 _pynode=Engines::PyNode::_nil();
1625 _container->shutdown(level);
1629 void PyFuncNode::imposeResource(const std::string& resource_name,
1630 const std::string& container_name)
1632 if(!resource_name.empty() && !container_name.empty())
1634 _imposedResource = resource_name;
1635 _imposedContainer = container_name;
1639 bool PyFuncNode::canAcceptImposedResource()
1641 return _container != nullptr && _container->canAcceptImposedResource();
1644 bool PyFuncNode::hasImposedResource()const
1646 return PythonEntry::hasImposedResource();