DataScopeServer::~DataScopeServer()
{
// _globals is borrowed ref -> do nothing
- Py_DECREF(_locals);
- //_pickler is borrowed ref -> do nothing
+ Py_XDECREF(_locals);
+ Py_XDECREF(_pickler);
}
/*!
}
}
-/*!
- * \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__"));
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());
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; }
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));
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()
//
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);
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);
}
/*!
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
{
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;
}
//! 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;
}
private:
static const char FAKE_VAR_NAME_FOR_WORK[];
std::string _data;
- private:
- PyCodeObject *_code_for_pickle;
};
}
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();