From 0da8354bf35965651a753eae8081fe5ce2195d7c Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Wed, 7 Jan 2015 09:13:15 +0100 Subject: [PATCH] Exceptions management + clean DataScopes destruction. --- idl/SALOME_SDS.idl | 27 ++++++++++---------- src/SALOMESDS/SALOMESDS_DataScopeServer.cxx | 25 ++++++++++++++++++ src/SALOMESDS/SALOMESDS_DataScopeServer.hxx | 1 + src/SALOMESDS/SALOMESDS_Exception.cxx | 14 +++++----- src/SALOMESDS/SALOMESDS_Exception.hxx | 10 +++----- src/SALOMESDS/SALOMESDS_StringDataServer.cxx | 12 +++++++++ src/SALOMESDS/SALOME_DataScopeServer.cxx | 1 + src/SALOMESDS/TestSalomeSDS1.py | 6 ++--- 8 files changed, 66 insertions(+), 30 deletions(-) diff --git a/idl/SALOME_SDS.idl b/idl/SALOME_SDS.idl index 76f45c893..9fcc3ec85 100644 --- a/idl/SALOME_SDS.idl +++ b/idl/SALOME_SDS.idl @@ -19,6 +19,7 @@ // Author : Anthony GEAY (EDF R&D) #include "SALOME_GenericObj.idl" +#include "SALOME_Exception.idl" module SALOME { @@ -35,9 +36,9 @@ module SALOME interface StringDataServer : BasicDataServer { - void setSerializedContent(in ByteVec newValue); - ByteVec fetchSerializedContent(); - StringDataServer invokePythonMethodOn(in string method, in ByteVec args); + void setSerializedContent(in ByteVec newValue) raises (SALOME::SALOME_Exception); + ByteVec fetchSerializedContent() raises (SALOME::SALOME_Exception); + StringDataServer invokePythonMethodOn(in string method, in ByteVec args) raises (SALOME::SALOME_Exception); }; interface DataServerManager; @@ -47,23 +48,23 @@ module SALOME void ping(); string getScopeName(); StringVec listVars(); - BasicDataServer retrieveVar(in string varName); - StringDataServer createGlobalStringVar(in string typeName, in string varName); - StringDataServer createGlobalTmpVar(in ByteVec newValue); + BasicDataServer retrieveVar(in string varName) raises (SALOME::SALOME_Exception); + StringDataServer createGlobalStringVar(in string typeName, in string varName) raises (SALOME::SALOME_Exception); + StringDataServer createGlobalTmpVar(in ByteVec newValue) raises (SALOME::SALOME_Exception); void shutdownIfNotHostedByDSM(); }; interface DataServerManager { StringVec listScopes(); - StringVec listAliveAndKickingScopes(); - DataScopeServer getDefaultScope(); - boolean isAliveAndKicking(in string scopeName); - DataScopeServer createDataScope(in string scopeName); - DataScopeServer retriveDataScope(in string scopeName); + StringVec listAliveAndKickingScopes() raises (SALOME::SALOME_Exception); + DataScopeServer getDefaultScope() raises (SALOME::SALOME_Exception); + boolean isAliveAndKicking(in string scopeName) raises (SALOME::SALOME_Exception); + DataScopeServer createDataScope(in string scopeName) raises (SALOME::SALOME_Exception); + DataScopeServer retriveDataScope(in string scopeName) raises (SALOME::SALOME_Exception); DataScopeServer giveADataScopeCalled(in string scopeName); - void removeDataScope(in string scopeName); + void removeDataScope(in string scopeName) raises (SALOME::SALOME_Exception); void cleanScopesInNS(); - void shutdownScopes(); + void shutdownScopes() raises (SALOME::SALOME_Exception); }; }; diff --git a/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx b/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx index 5b3cfc1c2..a1a109496 100644 --- a/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx +++ b/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx @@ -28,6 +28,14 @@ #include #include +// agy : awful, to be factorized with ContainerManager. +#ifndef WIN32 +#include +#else +#include +#define getpid _getpid +#endif + using namespace SALOMESDS; std::size_t DataScopeServer::COUNTER=0; @@ -181,6 +189,23 @@ void DataScopeServer::initializePython(int argc, char *argv[]) _pickler=PyImport_ImportModuleLevel(const_cast("cPickle"),_globals,_locals,tmp,-1); } +void DataScopeServer::registerToSalomePiDict() const +{ + PyObject *mod(PyImport_ImportModule("addToKillList")); + if(!mod) + return; + PyObject *meth(PyObject_GetAttrString(mod,"addToKillList")); + if(!meth) + { Py_XDECREF(mod); return ; } + PyObject *args(PyTuple_New(2)); + PyTuple_SetItem(args,0,PyInt_FromLong(getpid())); + PyTuple_SetItem(args,1,PyString_FromString("SALOME_DataScopeServer")); + PyObject *res(PyObject_CallObject(meth,args)); + Py_XDECREF(args); + Py_XDECREF(res); + Py_XDECREF(mod); +} + /*! * \a ptr has been activated by the POA \a poa. */ diff --git a/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx b/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx index 6f50f68c0..cf80fee60 100644 --- a/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx +++ b/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx @@ -51,6 +51,7 @@ namespace SALOMESDS ~DataScopeServer(); public: void initializePython(int argc, char *argv[]); + void registerToSalomePiDict() const; void setPOAAndRegister(PortableServer::POA_var poa, SALOME::DataScopeServer_ptr ptr); PyObject *getGlobals() const { return _globals; } PyObject *getLocals() const { return _locals; } diff --git a/src/SALOMESDS/SALOMESDS_Exception.cxx b/src/SALOMESDS/SALOMESDS_Exception.cxx index fd3d8e199..5493bbf2d 100644 --- a/src/SALOMESDS/SALOMESDS_Exception.cxx +++ b/src/SALOMESDS/SALOMESDS_Exception.cxx @@ -20,15 +20,13 @@ #include "SALOMESDS_Exception.hxx" -SALOMESDS::Exception::Exception(const std::string& reason):_reason(reason) +SALOMESDS::Exception::Exception(const std::string& reason) { + SALOME::ExceptionStruct es; + es.type=SALOME::INTERNAL_ERROR; + es.text=CORBA::string_dup(reason.c_str()); + es.lineNumber=0; + (*this).details=es; } -SALOMESDS::Exception::~Exception() throw () -{ -} -const char *SALOMESDS::Exception::what() const throw() -{ - return _reason.c_str(); -} diff --git a/src/SALOMESDS/SALOMESDS_Exception.hxx b/src/SALOMESDS/SALOMESDS_Exception.hxx index 2299c2233..c5943ae8a 100644 --- a/src/SALOMESDS/SALOMESDS_Exception.hxx +++ b/src/SALOMESDS/SALOMESDS_Exception.hxx @@ -21,19 +21,17 @@ #ifndef __SALOMESDS_EXCEPTION_HXX__ #define __SALOMESDS_EXCEPTION_HXX__ +#include "SALOMEconfig.h" +#include CORBA_SERVER_HEADER(SALOME_Exception) + #include -#include namespace SALOMESDS { - class Exception : public std::exception + class Exception : public SALOME::SALOME_Exception { public: Exception(const std::string& reason); - ~Exception() throw (); - const char *what() const throw(); - protected: - std::string _reason; }; } diff --git a/src/SALOMESDS/SALOMESDS_StringDataServer.cxx b/src/SALOMESDS/SALOMESDS_StringDataServer.cxx index 87b155a67..8bece7749 100644 --- a/src/SALOMESDS/SALOMESDS_StringDataServer.cxx +++ b/src/SALOMESDS/SALOMESDS_StringDataServer.cxx @@ -166,6 +166,18 @@ void StringDataServer::setNewPyObj(PyObject *obj) throw Exception("StringDataServer::setNewPyObj : trying to assign a NULL pyobject in this !"); if(obj==_self) return ; + if(_self) + { + PyObject *selfType(PyObject_Type(_self)); + if(PyObject_IsInstance(obj,selfType)!=1) + { + Py_XDECREF(obj); + Py_XDECREF(selfType); + throw Exception("StringDataServer::setNewPyObj : type of new object is not the same than those previously set !"); + } + else + Py_XDECREF(selfType); + } Py_XDECREF(_self); _self=obj; } diff --git a/src/SALOMESDS/SALOME_DataScopeServer.cxx b/src/SALOMESDS/SALOME_DataScopeServer.cxx index 63e08b90d..a5061c384 100644 --- a/src/SALOMESDS/SALOME_DataScopeServer.cxx +++ b/src/SALOMESDS/SALOME_DataScopeServer.cxx @@ -47,6 +47,7 @@ int main(int argc, char *argv[]) PortableServer::POA_var poa2(poa->create_POA("SingleThPOA4SDS",mgr,policies)); threadPol->destroy(); server->initializePython(argc,argv);// agy : Very important ! invoke this method BEFORE activation ! + server->registerToSalomePiDict(); PortableServer::ObjectId_var id(poa2->activate_object(server)); obj=poa2->id_to_reference(id); SALOME::DataScopeServer_var serverPtr(SALOME::DataScopeServer::_narrow(obj)); diff --git a/src/SALOMESDS/TestSalomeSDS1.py b/src/SALOMESDS/TestSalomeSDS1.py index 4951c9832..44ab8edb3 100644 --- a/src/SALOMESDS/TestSalomeSDS1.py +++ b/src/SALOMESDS/TestSalomeSDS1.py @@ -32,7 +32,7 @@ assert(isinstance(dsm,SALOME._objref_DataServerManager)) assert(isinstance(dsm.getDefaultScope(),SALOME._objref_DataScopeServer)) d2s=dsm.createDataScope("tonyy") assert(isinstance(d2s,SALOME._objref_DataScopeServer)) -a=d2s.createGlobalStringVar("int","c") +a=d2s.createGlobalStringVar("str","c") assert(a.getVarName()=="c") # a.setSerializedContent(cPickle.dumps(st,cPickle.HIGHEST_PROTOCOL)) @@ -55,8 +55,8 @@ dsm=salome.naming_service.Resolve("/DataServerManager") st=cPickle.dumps([],cPickle.HIGHEST_PROTOCOL) a=dsm.giveADataScopeCalled(sname).createGlobalStringVar("list","a") dsm.giveADataScopeCalled(sname) -a.setSerializedContent(cPickle.dumps((0,),cPickle.HIGHEST_PROTOCOL)) -assert(cPickle.loads(a.fetchSerializedContent())==(0,)) +a.setSerializedContent(cPickle.dumps([0,],cPickle.HIGHEST_PROTOCOL)) +assert(cPickle.loads(a.fetchSerializedContent())==[0,]) a.setSerializedContent(st) assert(cPickle.loads(a.fetchSerializedContent())==[]) tmp=a.invokePythonMethodOn("append",cPickle.dumps((0,),cPickle.HIGHEST_PROTOCOL)) -- 2.39.2