+const char PythonNode::SCRIPT_FOR_SERIALIZATION[]="import cPickle\n"
+ "def pickleForDistPyth2009(kws):\n"
+ " return cPickle.dumps(((),kws),-1)\n"
+ "\n"
+ "def unPickleForDistPyth2009(st):\n"
+ " args=cPickle.loads(st)\n"
+ " return args\n";
+
+const char PyFuncNode::SCRIPT_FOR_SERIALIZATION[]="import cPickle\n"
+ "def pickleForDistPyth2009(*args,**kws):\n"
+ " return cPickle.dumps((args,kws),-1)\n"
+ "\n"
+ "def unPickleForDistPyth2009(st):\n"
+ " args=cPickle.loads(st)\n"
+ " return args\n";
+
+PythonEntry::PythonEntry():_context(0),_pyfuncSer(0),_pyfuncUnser(0)
+{
+}
+
+PythonEntry::~PythonEntry()
+{
+ AutoGIL agil;
+ DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
+ // not Py_XDECREF of _pyfuncUnser because it is returned by PyDict_GetItem -> borrowed
+ // not Py_XDECREF of _pyfuncSer because it is returned by PyDict_GetItem -> borrowed
+ Py_XDECREF(_context);
+}
+
+void PythonEntry::commonRemoteLoadPart1(InlineNode *reqNode)
+{
+ DEBTRACE( "---------------PythonEntry::CommonRemoteLoad function---------------" );
+ Container *container(reqNode->getContainer());
+ bool isContAlreadyStarted(false);
+ if(container)
+ {
+ isContAlreadyStarted=container->isAlreadyStarted(reqNode);
+ if(!isContAlreadyStarted)
+ {
+ try
+ {
+ container->start(reqNode);
+ }
+ catch(Exception& e)
+ {
+ reqNode->setErrorDetails(e.what());
+ throw e;
+ }
+ }
+ }
+ else
+ {
+ std::string what("PythonEntry::CommonRemoteLoad : a load operation requested on \"");
+ what+=reqNode->getName(); what+="\" with no container specified.";
+ reqNode->setErrorDetails(what);
+ throw Exception(what);
+ }
+}
+
+Engines::Container_var PythonEntry::commonRemoteLoadPart2(InlineNode *reqNode, bool& isInitializeRequested)
+{
+ Container *container(reqNode->getContainer());
+ Engines::Container_var objContainer=Engines::Container::_nil();
+ if(!container)
+ throw Exception("No container specified !");
+ SalomeContainer *containerCast0(dynamic_cast<SalomeContainer *>(container));
+ SalomeHPContainer *containerCast1(dynamic_cast<SalomeHPContainer *>(container));
+ if(containerCast0)
+ objContainer=containerCast0->getContainerPtr(reqNode);
+ else if(containerCast1)
+ {
+ YACS::BASES::AutoCppPtr<SalomeContainerTmpForHP> tmpCont(SalomeContainerTmpForHP::BuildFrom(containerCast1,reqNode));
+ objContainer=tmpCont->getContainerPtr(reqNode);
+ }
+ else
+ throw Exception("Unrecognized type of container ! Salome one is expected for PythonNode/PyFuncNode !");
+ if(CORBA::is_nil(objContainer))
+ throw Exception("Container corba pointer is NULL for PythonNode !");
+ isInitializeRequested=false;
+ try
+ {
+ if(containerCast0)
+ {
+ createRemoteAdaptedPyInterpretor(objContainer);
+ }
+ else
+ {
+ Engines::PyNodeBase_var dftPyScript(retrieveDftRemotePyInterpretorIfAny(objContainer));
+ if(CORBA::is_nil(dftPyScript))
+ {
+ isInitializeRequested=true;
+ createRemoteAdaptedPyInterpretor(objContainer);
+ }
+ else
+ assignRemotePyInterpretor(dftPyScript);
+ }
+ }
+ catch( const SALOME::SALOME_Exception& ex )
+ {
+ std::string msg="Exception on remote python node creation ";
+ msg += '\n';
+ msg += ex.details.text.in();
+ reqNode->setErrorDetails(msg);
+ throw Exception(msg);
+ }
+ Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
+ if(CORBA::is_nil(pynode))
+ throw Exception("In PythonNode the ref in NULL ! ");
+ return objContainer;
+}
+
+void PythonEntry::commonRemoteLoadPart3(InlineNode *reqNode, Engines::Container_ptr objContainer, bool isInitializeRequested)
+{
+ Container *container(reqNode->getContainer());
+ Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
+ ///
+ {
+ AutoGIL agil;
+ const char *picklizeScript(getSerializationScript());
+ PyObject *res=PyRun_String(picklizeScript,Py_file_input,_context,_context);
+ if(res == NULL)
+ {
+ std::string errorDetails;
+ PyObject* new_stderr = newPyStdOut(errorDetails);
+ reqNode->setErrorDetails(errorDetails);
+ PySys_SetObject((char*)"stderr", new_stderr);
+ PyErr_Print();
+ PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+ Py_DECREF(new_stderr);
+ throw Exception("Error during load");
+ }
+ Py_DECREF(res);
+ _pyfuncSer=PyDict_GetItemString(_context,"pickleForDistPyth2009");
+ _pyfuncUnser=PyDict_GetItemString(_context,"unPickleForDistPyth2009");
+ if(_pyfuncSer == NULL)
+ {
+ std::string errorDetails;
+ PyObject *new_stderr(newPyStdOut(errorDetails));
+ reqNode->setErrorDetails(errorDetails);
+ PySys_SetObject((char*)"stderr", new_stderr);
+ PyErr_Print();
+ PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+ Py_DECREF(new_stderr);
+ throw Exception("Error during load");
+ }
+ if(_pyfuncUnser == NULL)
+ {
+ std::string errorDetails;
+ PyObject *new_stderr(newPyStdOut(errorDetails));
+ reqNode->setErrorDetails(errorDetails);
+ PySys_SetObject((char*)"stderr", new_stderr);
+ PyErr_Print();
+ PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+ Py_DECREF(new_stderr);
+ throw Exception("Error during load");
+ }
+ }
+ if(isInitializeRequested)
+ {//This one is called only once at initialization in the container if an init-script is specified.
+ try
+ {
+ std::string zeInitScriptKey(container->getProperty(HomogeneousPoolContainer::INITIALIZE_SCRIPT_KEY));
+ if(!zeInitScriptKey.empty())
+ pynode->executeAnotherPieceOfCode(zeInitScriptKey.c_str());
+ }
+ catch( const SALOME::SALOME_Exception& ex )
+ {
+ std::string msg="Exception on PythonNode::loadRemote python invocation of initializisation py script !";
+ msg += '\n';
+ msg += ex.details.text.in();
+ reqNode->setErrorDetails(msg);
+ throw Exception(msg);
+ }
+ DEBTRACE( "---------------End PyNode::loadRemote function---------------" );
+ }
+}
+
+std::string PythonEntry::GetContainerLog(const std::string& mode, Container *container, const Task *askingTask)
+{
+ if(mode=="local")
+ return "";
+
+ std::string msg;
+ try
+ {
+ SalomeContainer *containerCast(dynamic_cast<SalomeContainer *>(container));
+ SalomeHPContainer *objContainer2(dynamic_cast<SalomeHPContainer *>(container));
+ if(containerCast)
+ {
+ Engines::Container_var objContainer(containerCast->getContainerPtr(askingTask));
+ CORBA::String_var logname = objContainer->logfilename();
+ DEBTRACE(logname);
+ msg=logname;
+ std::string::size_type pos = msg.find(":");
+ msg=msg.substr(pos+1);
+ }
+ else if(objContainer2)
+ {
+ msg="Remote PythonNode is on HP Container : no log because no info of the location by definition of HP Container !";
+ }
+ else
+ {
+ msg="Not implemented yet for container log for that type of container !";
+ }
+ }
+ catch(...)
+ {
+ msg = "Container no longer reachable";
+ }
+ return msg;
+}
+
+void PythonEntry::commonRemoteLoad(InlineNode *reqNode)
+{
+ commonRemoteLoadPart1(reqNode);
+ bool isInitializeRequested;
+ Engines::Container_var objContainer(commonRemoteLoadPart2(reqNode,isInitializeRequested));
+ commonRemoteLoadPart3(reqNode,objContainer,isInitializeRequested);
+}
+