From 59518df5437a19289c52709a08706ff798c6562a Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Thu, 30 Oct 2014 10:29:32 +0100 Subject: [PATCH] Debuging done -> valgrind OK. --- src/SALOMESDS/SALOMESDS_DataScopeServer.cxx | 27 ++++++++++++------- src/SALOMESDS/SALOMESDS_DataScopeServer.hxx | 3 ++- src/SALOMESDS/SALOMESDS_DataServerManager.cxx | 6 +++-- src/SALOMESDS/SALOMESDS_StringDataServer.cxx | 27 +++++++++---------- src/SALOMESDS/SALOMESDS_StringDataServer.hxx | 2 -- src/SALOMESDS/SALOME_DataScopeServer.cxx | 3 ++- 6 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx b/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx index 152d4cc43..4ca656817 100644 --- a/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx +++ b/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx @@ -42,8 +42,8 @@ DataScopeServer::DataScopeServer(const DataScopeServer& other):_globals(0),_loca DataScopeServer::~DataScopeServer() { // _globals is borrowed ref -> do nothing - Py_DECREF(_locals); - //_pickler is borrowed ref -> do nothing + Py_XDECREF(_locals); + Py_XDECREF(_pickler); } /*! @@ -160,15 +160,8 @@ void DataScopeServer::shutdownIfNotHostedByDSM() } } -/*! - * \a ptr has been activated by the POA \a poa. - */ -void DataScopeServer::setPOAAndRegister(int argc, char *argv[], PortableServer::POA_var poa, SALOME::DataScopeServer_ptr ptr) +void DataScopeServer::initializePython(int argc, char *argv[]) { - _poa=poa; - std::string fullScopeName(SALOMESDS::DataServerManager::CreateAbsNameInNSFromScopeName(_name)); - SALOME_NamingService ns(_orb); - ns.Register(ptr,fullScopeName.c_str()); Py_Initialize(); PySys_SetArgv(argc,argv); PyObject *mainmod(PyImport_AddModule("__main__")); @@ -181,11 +174,25 @@ void DataScopeServer::setPOAAndRegister(int argc, char *argv[], PortableServer:: Py_XDECREF(bimod); } _locals=PyDict_New(); + PyObject *tmp(PyList_New(0)); + _pickler=PyImport_ImportModuleLevel(const_cast("cPickle"),_globals,_locals,tmp,-1); + //Py_XDECREF(tmp); //if(PyRun_String("import cPickle\n",Py_single_input,_globals,_locals)!=0) // throw Exception("DataScopeServer::setPOAAndRegister : cPickle is not available !"); /*_pickler=PyDict_GetItemString(_globals,"cPickle");*/ } +/*! + * \a ptr has been activated by the POA \a poa. + */ +void DataScopeServer::setPOAAndRegister(PortableServer::POA_var poa, SALOME::DataScopeServer_ptr ptr) +{ + _poa=poa; + std::string fullScopeName(SALOMESDS::DataServerManager::CreateAbsNameInNSFromScopeName(_name)); + SALOME_NamingService ns(_orb); + ns.Register(ptr,fullScopeName.c_str()); +} + std::vector< std::string > DataScopeServer::getAllVarNames() const { std::size_t sz(_vars.size()); diff --git a/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx b/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx index 5b1e830e2..ab50139d6 100644 --- a/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx +++ b/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx @@ -49,7 +49,8 @@ namespace SALOMESDS void shutdownIfNotHostedByDSM(); ~DataScopeServer(); public: - void setPOAAndRegister(int argc, char *argv[], PortableServer::POA_var poa, SALOME::DataScopeServer_ptr ptr); + void initializePython(int argc, char *argv[]); + void setPOAAndRegister(PortableServer::POA_var poa, SALOME::DataScopeServer_ptr ptr); PyObject *getGlobals() const { return _globals; } PyObject *getLocals() const { return _locals; } PyObject *getPickler() const { return _pickler; } diff --git a/src/SALOMESDS/SALOMESDS_DataServerManager.cxx b/src/SALOMESDS/SALOMESDS_DataServerManager.cxx index a908dee83..b8dac6c2a 100644 --- a/src/SALOMESDS/SALOMESDS_DataServerManager.cxx +++ b/src/SALOMESDS/SALOMESDS_DataServerManager.cxx @@ -43,6 +43,8 @@ DataServerManager::DataServerManager(int argc, char *argv[], CORBA::ORB_ptr orb, policies[0]=PortableServer::ThreadPolicy::_duplicate(threadPol); _poa=poa->create_POA("SingleThPOA4SDS",pman,policies); threadPol->destroy(); + // + dftScope->initializePython(argc,argv);// agy : Very important ! invoke this method BEFORE activation ! // activate this to be ready to be usable from NS. PortableServer::ObjectId_var id(_poa->activate_object(this)); CORBA::Object_var obj(_poa->id_to_reference(id)); @@ -54,7 +56,7 @@ DataServerManager::DataServerManager(int argc, char *argv[], CORBA::ORB_ptr orb, id=_poa->activate_object(dftScope); obj=_poa->id_to_reference(id); SALOME::DataScopeServer_var dftScopePtr(SALOME::DataScopeServer::_narrow(obj)); - dftScope->setPOAAndRegister(argc,argv,_poa,dftScopePtr); + dftScope->setPOAAndRegister(_poa,dftScopePtr);// agy : Very important ! invoke this method BEFORE activation ! Because this method initializes Python ! } SALOME::StringVec *DataServerManager::listScopes() @@ -85,7 +87,7 @@ SALOME::DataScopeServer_ptr DataServerManager::createDataScope(const char *scope // SALOME_NamingService ns(_orb); std::string fullScopeName(CreateAbsNameInNSFromScopeName(scopeName)); - std::ostringstream oss; oss << "SALOME_DataScopeServer" << " " << scopeName << " "; + std::ostringstream oss; oss << "valgrind SALOME_DataScopeServer" << " " << scopeName << " "; SALOME_ContainerManager::AddOmninamesParams(oss,&ns); std::string command(oss.str()); SALOME_ContainerManager::MakeTheCommandToBeLaunchedASync(command); diff --git a/src/SALOMESDS/SALOMESDS_StringDataServer.cxx b/src/SALOMESDS/SALOMESDS_StringDataServer.cxx index 3903096e6..202e6d2cc 100644 --- a/src/SALOMESDS/SALOMESDS_StringDataServer.cxx +++ b/src/SALOMESDS/SALOMESDS_StringDataServer.cxx @@ -26,17 +26,12 @@ using namespace SALOMESDS; -const char StringDataServer::FAKE_VAR_NAME_FOR_WORK[]="_tmp8739023zP"; - -StringDataServer::StringDataServer(DataScopeServer *father, const std::string& varName):BasicDataServer(father,varName),_code_for_pickle(0) +StringDataServer::StringDataServer(DataScopeServer *father, const std::string& varName):BasicDataServer(father,varName) { - std::ostringstream codeStr; codeStr << " ; cPickle.dumps(" << FAKE_VAR_NAME_FOR_WORK << ",cPickle.HIGHEST_PROTOCOL)"; - _code_for_pickle=(PyCodeObject*)Py_CompileString(codeStr.str().c_str(),getVarNameCpp().c_str(),Py_eval_input);//Py_file_input Py_eval_input } StringDataServer::~StringDataServer() { - Py_XDECREF(_code_for_pickle); } /*! @@ -70,7 +65,7 @@ char *StringDataServer::invokePythonMethodOn(const char *method, const char *arg std::ostringstream oss; oss << "StringDataServer::invokePythonMethodOn : Method \"" << method << "\" is not available !"; throw Exception(oss.str()); } - PyObject *res(PyObject_CallObject(self,argsPy)); + PyObject *res(PyObject_CallObject(selfMeth,argsPy)); _data=pickelize(self);// if it is a non const method ! std::string ret(pickelize(res)); // to test : res and self @@ -85,8 +80,9 @@ PyObject *StringDataServer::getPyObjFromPickled(const std::string& pickledData) { PyObject *pickledDataPy(PyString_FromString(pickledData.c_str())); PyObject *selfMeth(PyObject_GetAttrString(_father->getPickler(),"loads")); - PyObject *ret(PyObject_CallObject(selfMeth,pickledDataPy)); - Py_XDECREF(pickledDataPy); + PyObject *args(PyTuple_New(1)); PyTuple_SetItem(args,0,pickledDataPy); + PyObject *ret(PyObject_CallObject(selfMeth,args)); + Py_XDECREF(args); Py_XDECREF(selfMeth); return ret; } @@ -94,11 +90,14 @@ PyObject *StringDataServer::getPyObjFromPickled(const std::string& pickledData) //! obj is consumed by this method. std::string StringDataServer::pickelize(PyObject *obj) { - PyObject *key(PyString_FromString(FAKE_VAR_NAME_FOR_WORK)); - PyDict_SetItem(_father->getGlobals(),key,obj); - PyObject *retPy(PyEval_EvalCode(_code_for_pickle,_father->getGlobals(),_father->getLocals())); - if(!PyString_Check(retPy)) - throw Exception("StringDataServer::pickelize : internal error #0 !"); + PyObject *args(PyTuple_New(2)); + PyTuple_SetItem(args,0,obj); + PyTuple_SetItem(args,1,PyInt_FromLong(2));// because "assert(cPickle.HIGHEST_PROTOCOL is 2)" + PyObject *selfMeth(PyObject_GetAttrString(_father->getPickler(),"dumps")); + PyObject *retPy(PyObject_CallObject(selfMeth,args)); + Py_XDECREF(selfMeth); + Py_XDECREF(args); std::string ret(PyString_AsString(retPy)); + Py_XDECREF(retPy); return ret; } diff --git a/src/SALOMESDS/SALOMESDS_StringDataServer.hxx b/src/SALOMESDS/SALOMESDS_StringDataServer.hxx index 0b3f5cd3f..eb37142ae 100644 --- a/src/SALOMESDS/SALOMESDS_StringDataServer.hxx +++ b/src/SALOMESDS/SALOMESDS_StringDataServer.hxx @@ -44,8 +44,6 @@ namespace SALOMESDS private: static const char FAKE_VAR_NAME_FOR_WORK[]; std::string _data; - private: - PyCodeObject *_code_for_pickle; }; } diff --git a/src/SALOMESDS/SALOME_DataScopeServer.cxx b/src/SALOMESDS/SALOME_DataScopeServer.cxx index 92b4d731e..63e08b90d 100644 --- a/src/SALOMESDS/SALOME_DataScopeServer.cxx +++ b/src/SALOMESDS/SALOME_DataScopeServer.cxx @@ -46,10 +46,11 @@ int main(int argc, char *argv[]) policies[0]=PortableServer::ThreadPolicy::_duplicate(threadPol); PortableServer::POA_var poa2(poa->create_POA("SingleThPOA4SDS",mgr,policies)); threadPol->destroy(); + server->initializePython(argc,argv);// agy : Very important ! invoke this method BEFORE activation ! PortableServer::ObjectId_var id(poa2->activate_object(server)); obj=poa2->id_to_reference(id); SALOME::DataScopeServer_var serverPtr(SALOME::DataScopeServer::_narrow(obj)); - server->setPOAAndRegister(argc,argv,poa2,serverPtr); + server->setPOAAndRegister(poa2,serverPtr); // orb->run(); server->_remove_ref(); -- 2.39.2