]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
Debuging done -> valgrind OK.
authorAnthony Geay <anthony.geay@edf.fr>
Thu, 30 Oct 2014 09:29:32 +0000 (10:29 +0100)
committerAnthony Geay <anthony.geay@edf.fr>
Thu, 30 Oct 2014 09:29:32 +0000 (10:29 +0100)
src/SALOMESDS/SALOMESDS_DataScopeServer.cxx
src/SALOMESDS/SALOMESDS_DataScopeServer.hxx
src/SALOMESDS/SALOMESDS_DataServerManager.cxx
src/SALOMESDS/SALOMESDS_StringDataServer.cxx
src/SALOMESDS/SALOMESDS_StringDataServer.hxx
src/SALOMESDS/SALOME_DataScopeServer.cxx

index 152d4cc434c9120129492b2c69705c39fc41d064..4ca656817d3d6a668e33c1c01ae340b427645a2e 100644 (file)
@@ -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<char *>("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());
index 5b1e830e230087d090344925f5c294e50b736e24..ab50139d6f395f22055efa51347d46194db6e388 100644 (file)
@@ -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; }
index a908dee83dcb2061d4ac5b98f88ae93416c0500a..b8dac6c2ac8f0cc47b009d476df8326e1864b51f 100644 (file)
@@ -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);
index 3903096e61f7c1b19306c91e2980190ab7644fbb..202e6d2cc70470e9b6d8130cf5caed7dceb34e94 100644 (file)
 
 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;
 }
index 0b3f5cd3fde82cf34f63e9939c2c780ceaffc62b..eb37142ae60ecdabf6821dc29ed7da8795d7436b 100644 (file)
@@ -44,8 +44,6 @@ namespace SALOMESDS
   private:
     static const char FAKE_VAR_NAME_FOR_WORK[];
     std::string _data;
-  private:
-    PyCodeObject *_code_for_pickle;
   };
 }
 
index 92b4d731e67cef73a760d02018da4eabc592f24a..63e08b90d7c8b3b5e09d19a10ca9369516755250 100644 (file)
@@ -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();