Salome HOME
Updated copyright comment
[modules/kernel.git] / src / SALOMESDS / SALOMESDS_PickelizedPyObjServer.cxx
index 2989150d486b79632d2f8171f22ca44c1e0f2e32..fba4c6b83ecb8a0f2fb73b5d0916b2271c20267f 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -70,26 +70,16 @@ void PickelizedPyObjServer::checkKeyPresent(PyObject *key)
   checkKeyPresence(key,true);
 }
 
-void PickelizedPyObjServer::addKeyValueHard(PyObject *key, PyObject *value)
+void PickelizedPyObjServer::addKeyValueHard(PyObject * /*key*/, PyObject * /*value*/)
 {
-  bool isOK(PyDict_SetItem(_self,key,value)==0);
-  if(!isOK)
-    throw Exception("PickelizedPyObjServer::addKeyValueHard : error when trying to add key,value to dict !");
+  std::ostringstream oss; oss << "PickelizedPyObjServer::addKeyValueHard : var \"" << getVarNameCpp() << "\" is not permitted to alter its value !";
+  throw Exception(oss.str());
 }
 
-void PickelizedPyObjServer::addKeyValueErrorIfAlreadyExisting(PyObject *key, PyObject *value)
+void PickelizedPyObjServer::removeKeyInVarErrorIfNotAlreadyExisting(PyObject * /*key*/)
 {
-  checkKeyNotAlreadyPresent(key);
-  bool isOK(PyDict_SetItem(_self,key,value)==0);
-  if(!isOK)
-    throw Exception("PickelizedPyObjServer::addKeyValueErrorIfAlreadyExisting : error when trying to add key,value to dict !");
-}
-
-void PickelizedPyObjServer::removeKeyInVarErrorIfNotAlreadyExisting(PyObject *key)
-{
-  checkKeyPresent(key);
-  if(PyDict_DelItem(_self,key)!=0)
-    throw Exception("PickelizedPyObjServer::removeKeyInVarErrorIfNotAlreadyExisting : error during deletion of key in dict !");
+  std::ostringstream oss; oss << "PickelizedPyObjServer::removeKeyInVarErrorIfNotAlreadyExisting : var \"" << getVarNameCpp() << "\" is not permitted to alter its value !";
+  throw Exception(oss.str());
 }
 
 void PickelizedPyObjServer::FromByteSeqToCpp(const SALOME::ByteVec& bsToBeConv, std::string& ret)
@@ -98,17 +88,22 @@ void PickelizedPyObjServer::FromByteSeqToCpp(const SALOME::ByteVec& bsToBeConv,
   ret.resize(sz,' ');
   char *buf(const_cast<char *>(ret.c_str()));
   for(std::size_t i=0;i<sz;i++)
-    buf[i]=bsToBeConv[i];
+    buf[i]=bsToBeConv[(CORBA::ULong)i]; //!< TODO: size_t to CORBA::ULong
 }
 
-SALOME::ByteVec *PickelizedPyObjServer::FromCppToByteSeq(const std::string& strToBeConv)
+void PickelizedPyObjServer::FromCppToByteSeq(const std::string& strToBeConv, SALOME::ByteVec& ret)
 {
-  SALOME::ByteVec *ret(new SALOME::ByteVec);
   const char *buf(strToBeConv.c_str());
   std::size_t sz(strToBeConv.size());
-  ret->length(sz);
+  ret.length((CORBA::ULong)sz); //!< TODO: size_t to CORBA::ULong
   for(std::size_t i=0;i<sz;i++)
-    (*ret)[i]=buf[i];
+    ret[(CORBA::ULong)i]=buf[i]; //!< TODO: size_t to CORBA::ULong
+}
+
+SALOME::ByteVec *PickelizedPyObjServer::FromCppToByteSeq(const std::string& strToBeConv)
+{
+  SALOME::ByteVec *ret(new SALOME::ByteVec);
+  FromCppToByteSeq(strToBeConv,*ret);
   return ret;
 }
 
@@ -116,8 +111,8 @@ SALOME::ByteVec *PickelizedPyObjServer::FromCppToByteSeq(const std::string& strT
 PyObject *PickelizedPyObjServer::GetPyObjFromPickled(const std::string& pickledData, DataScopeServerBase *dsb)
 {
   std::size_t sz(pickledData.size());
-  PyObject *pickledDataPy(PyString_FromStringAndSize(NULL,sz));// agy : do not use PyString_FromString because std::string hides a vector of byte.
-  char *buf(PyString_AsString(pickledDataPy));// this buf can be used thanks to python documentation.
+  PyObject *pickledDataPy(PyBytes_FromStringAndSize(NULL,sz));// agy : do not use PyUnicode_FromString because std::string hides a vector of byte.
+  char *buf(PyBytes_AS_STRING(pickledDataPy));// this buf can be used thanks to python documentation.
   const char *inBuf(pickledData.c_str());
   std::copy(inBuf,inBuf+sz,buf);
   PyObject *selfMeth(PyObject_GetAttrString(dsb->getPickler(),"loads"));
@@ -138,9 +133,9 @@ PyObject *PickelizedPyObjServer::getPyObjFromPickled(const std::string& pickledD
 PyObject *PickelizedPyObjServer::GetPyObjFromPickled(const std::vector<unsigned char>& pickledData, DataScopeServerBase *dsb)
 {
   std::size_t sz(pickledData.size());
-  PyObject *pickledDataPy(PyString_FromStringAndSize(NULL,sz));// agy : do not use PyString_FromString because std::string hides a vector of byte.
-  char *buf(PyString_AsString(pickledDataPy));// this buf can be used thanks to python documentation.
-  const unsigned char *inBuf(&pickledData[0]);
+  PyObject *pickledDataPy(PyBytes_FromStringAndSize(NULL,sz));// agy : do not use PyUnicode_FromString because std::string hides a vector of byte.
+  char *buf(PyBytes_AS_STRING(pickledDataPy));// this buf can be used thanks to python documentation.
+  const unsigned char *inBuf(pickledData.data());
   std::copy(inBuf,inBuf+sz,buf);
   PyObject *selfMeth(PyObject_GetAttrString(dsb->getPickler(),"loads"));
   PyObject *args(PyTuple_New(1)); PyTuple_SetItem(args,0,pickledDataPy);
@@ -161,14 +156,14 @@ std::string PickelizedPyObjServer::Pickelize(PyObject *obj, DataScopeServerBase
 {
   PyObject *args(PyTuple_New(2));
   PyTuple_SetItem(args,0,obj);
-  PyTuple_SetItem(args,1,PyInt_FromLong(2));// because "assert(cPickle.HIGHEST_PROTOCOL is 2)"
+  PyTuple_SetItem(args,1,PyLong_FromLong(3));// because "assert(pickle.HIGHEST_PROTOCOL is 3)"
   PyObject *selfMeth(PyObject_GetAttrString(dsb->getPickler(),"dumps"));
   PyObject *retPy(PyObject_CallObject(selfMeth,args));
   Py_XDECREF(selfMeth);
   Py_XDECREF(args);
-  std::size_t sz(PyString_Size(retPy));
+  std::size_t sz(PyBytes_Size(retPy));
   std::string ret(sz,'\0');
-  const char *buf(PyString_AsString(retPy));
+  const char *buf(PyBytes_AS_STRING(retPy));
   char *inBuf(const_cast<char *>(ret.c_str()));
   for(std::size_t i=0;i<sz;i++)
     inBuf[i]=buf[i];
@@ -189,8 +184,8 @@ void PickelizedPyObjServer::setNewPyObj(PyObject *obj)
     throw Exception("PickelizedPyObjServer::setNewPyObj : trying to assign a NULL pyobject in this !");
   if(obj==_self)
     return ;
-  if(PyList_Check(obj)==0 && PyDict_Check(obj)==0 && PyTuple_Check(obj)==0 && PyString_Check(obj)==0 && PyInt_Check(obj)==0 && PyBool_Check(obj)==0 && PyFloat_Check(obj)==0 && obj!=Py_None)
-    throw Exception("PickelizedPyObjServer::setNewPyObj : Supported python types are [list,tuple,dict,str,int,bool,float,None] !");
+  if(PyList_Check(obj)==0 && PyDict_Check(obj)==0 && PyTuple_Check(obj)==0 && PyBytes_Check(obj)==0 && PyLong_Check(obj)==0 && PyBool_Check(obj)==0 && PyFloat_Check(obj)==0 && obj!=Py_None)
+    throw Exception("PickelizedPyObjServer::setNewPyObj : Supported python types are [list,tuple,dict,bytes,int,bool,float,None] !");
   if(_self)
     {
       PyObject *selfType(PyObject_Type(_self));
@@ -259,3 +254,26 @@ void PickelizedPyObjServer::checkKeyPresence(PyObject *key, bool presence)
     }
   Py_XDECREF(retPy);
 }
+
+PickelizedPyObjServerModifiable::PickelizedPyObjServerModifiable(DataScopeServerBase *father, const std::string& varName, const SALOME::ByteVec& value):PickelizedPyObjServer(father,varName,value)
+{
+}
+
+PickelizedPyObjServerModifiable::PickelizedPyObjServerModifiable(DataScopeServerBase *father, const std::string& varName, PyObject *obj):PickelizedPyObjServer(father,varName,obj)
+{
+}
+
+void PickelizedPyObjServerModifiable::addKeyValueErrorIfAlreadyExisting(PyObject *key, PyObject *value)
+{
+  checkKeyNotAlreadyPresent(key);
+  bool isOK(PyDict_SetItem(_self,key,value)==0);
+  if(!isOK)
+    throw Exception("PickelizedPyObjServerModifiable::addKeyValueErrorIfAlreadyExisting : error when trying to add key,value to dict !");
+}
+
+void PickelizedPyObjServerModifiable::removeKeyInVarErrorIfNotAlreadyExisting(PyObject *key)
+{
+  checkKeyPresent(key);
+  if(PyDict_DelItem(_self,key)!=0)
+    throw Exception("PickelizedPyObjServerModifiable::removeKeyInVarErrorIfNotAlreadyExisting : error during deletion of key in dict !");
+}