1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony GEAY (EDF R&D)
21 #include "SALOMESDS_PickelizedPyObjServer.hxx"
22 #include "SALOMESDS_DataScopeServer.hxx"
23 #include "SALOMESDS_Exception.hxx"
28 using namespace SALOMESDS;
30 PickelizedPyObjServer::PickelizedPyObjServer(DataScopeServerBase *father, const std::string& varName, const SALOME::ByteVec& value):BasicDataServer(father,varName),_self(0)
32 setSerializedContentInternal(value);
36 PickelizedPyObjServer::PickelizedPyObjServer(DataScopeServerBase *father, const std::string& varName, PyObject *obj):BasicDataServer(father,varName),_self(0)
41 PickelizedPyObjServer::~PickelizedPyObjServer()
47 * Called remotely -> to protect against throw
49 SALOME::ByteVec *PickelizedPyObjServer::fetchSerializedContent()
51 Py_XINCREF(_self);//because pickelize consume _self
52 return FromCppToByteSeq(pickelize(_self));
55 bool PickelizedPyObjServer::isDict()
57 if(PyDict_Check(_self))
63 void PickelizedPyObjServer::checkKeyNotAlreadyPresent(PyObject *key)
65 checkKeyPresence(key,false);
68 void PickelizedPyObjServer::checkKeyPresent(PyObject *key)
70 checkKeyPresence(key,true);
73 void PickelizedPyObjServer::addKeyValueHard(PyObject *key, PyObject *value)
75 bool isOK(PyDict_SetItem(_self,key,value)==0);
77 throw Exception("PickelizedPyObjServer::addKeyValueHard : error when trying to add key,value to dict !");
80 void PickelizedPyObjServer::addKeyValueErrorIfAlreadyExisting(PyObject *key, PyObject *value)
82 checkKeyNotAlreadyPresent(key);
83 bool isOK(PyDict_SetItem(_self,key,value)==0);
85 throw Exception("PickelizedPyObjServer::addKeyValueErrorIfAlreadyExisting : error when trying to add key,value to dict !");
88 void PickelizedPyObjServer::removeKeyInVarErrorIfNotAlreadyExisting(PyObject *key)
91 if(PyDict_DelItem(_self,key)!=0)
92 throw Exception("PickelizedPyObjServer::removeKeyInVarErrorIfNotAlreadyExisting : error during deletion of key in dict !");
95 void PickelizedPyObjServer::FromByteSeqToCpp(const SALOME::ByteVec& bsToBeConv, std::string& ret)
97 std::size_t sz(bsToBeConv.length());
99 char *buf(const_cast<char *>(ret.c_str()));
100 for(std::size_t i=0;i<sz;i++)
101 buf[i]=bsToBeConv[i];
104 void PickelizedPyObjServer::FromCppToByteSeq(const std::string& strToBeConv, SALOME::ByteVec& ret)
106 const char *buf(strToBeConv.c_str());
107 std::size_t sz(strToBeConv.size());
109 for(std::size_t i=0;i<sz;i++)
113 SALOME::ByteVec *PickelizedPyObjServer::FromCppToByteSeq(const std::string& strToBeConv)
115 SALOME::ByteVec *ret(new SALOME::ByteVec);
116 FromCppToByteSeq(strToBeConv,*ret);
120 //! New reference returned
121 PyObject *PickelizedPyObjServer::GetPyObjFromPickled(const std::string& pickledData, DataScopeServerBase *dsb)
123 std::size_t sz(pickledData.size());
124 PyObject *pickledDataPy(PyString_FromStringAndSize(NULL,sz));// agy : do not use PyString_FromString because std::string hides a vector of byte.
125 char *buf(PyString_AsString(pickledDataPy));// this buf can be used thanks to python documentation.
126 const char *inBuf(pickledData.c_str());
127 std::copy(inBuf,inBuf+sz,buf);
128 PyObject *selfMeth(PyObject_GetAttrString(dsb->getPickler(),"loads"));
129 PyObject *args(PyTuple_New(1)); PyTuple_SetItem(args,0,pickledDataPy);
130 PyObject *ret(PyObject_CallObject(selfMeth,args));
132 Py_XDECREF(selfMeth);
136 //! New reference returned
137 PyObject *PickelizedPyObjServer::getPyObjFromPickled(const std::string& pickledData)
139 return GetPyObjFromPickled(pickledData,_father);
142 //! New reference returned
143 PyObject *PickelizedPyObjServer::GetPyObjFromPickled(const std::vector<unsigned char>& pickledData, DataScopeServerBase *dsb)
145 std::size_t sz(pickledData.size());
146 PyObject *pickledDataPy(PyString_FromStringAndSize(NULL,sz));// agy : do not use PyString_FromString because std::string hides a vector of byte.
147 char *buf(PyString_AsString(pickledDataPy));// this buf can be used thanks to python documentation.
148 const unsigned char *inBuf(&pickledData[0]);
149 std::copy(inBuf,inBuf+sz,buf);
150 PyObject *selfMeth(PyObject_GetAttrString(dsb->getPickler(),"loads"));
151 PyObject *args(PyTuple_New(1)); PyTuple_SetItem(args,0,pickledDataPy);
152 PyObject *ret(PyObject_CallObject(selfMeth,args));
154 Py_XDECREF(selfMeth);
158 //! New reference returned
159 PyObject *PickelizedPyObjServer::getPyObjFromPickled(const std::vector<unsigned char>& pickledData)
161 return GetPyObjFromPickled(pickledData,_father);
164 //! obj is consumed by this method.
165 std::string PickelizedPyObjServer::Pickelize(PyObject *obj, DataScopeServerBase *dsb)
167 PyObject *args(PyTuple_New(2));
168 PyTuple_SetItem(args,0,obj);
169 PyTuple_SetItem(args,1,PyInt_FromLong(2));// because "assert(cPickle.HIGHEST_PROTOCOL is 2)"
170 PyObject *selfMeth(PyObject_GetAttrString(dsb->getPickler(),"dumps"));
171 PyObject *retPy(PyObject_CallObject(selfMeth,args));
172 Py_XDECREF(selfMeth);
174 std::size_t sz(PyString_Size(retPy));
175 std::string ret(sz,'\0');
176 const char *buf(PyString_AsString(retPy));
177 char *inBuf(const_cast<char *>(ret.c_str()));
178 for(std::size_t i=0;i<sz;i++)
184 //! obj is consumed by this method.
185 std::string PickelizedPyObjServer::pickelize(PyObject *obj)
187 return Pickelize(obj,_father);
190 //! obj is consumed by this method.
191 void PickelizedPyObjServer::setNewPyObj(PyObject *obj)
194 throw Exception("PickelizedPyObjServer::setNewPyObj : trying to assign a NULL pyobject in this !");
197 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)
198 throw Exception("PickelizedPyObjServer::setNewPyObj : Supported python types are [list,tuple,dict,str,int,bool,float,None] !");
201 PyObject *selfType(PyObject_Type(_self));
202 if(PyObject_IsInstance(obj,selfType)!=1)
205 Py_XDECREF(selfType);
206 throw Exception("PickelizedPyObjServer::setNewPyObj : type of new object is not the same than those previously set !");
209 Py_XDECREF(selfType);
215 void PickelizedPyObjServer::setSerializedContentInternal(const SALOME::ByteVec& newValue)
218 FromByteSeqToCpp(newValue,data);
219 setNewPyObj(getPyObjFromPickled(data));
222 PyObject *PickelizedPyObjServer::CreateDftObjFromType(PyObject *globals, const std::string& typeName)
224 PyObject *builtins(PyDict_GetItemString(globals,"__builtins__"));
226 throw Exception("PickelizedPyObjServer constructor : no __builtins__ in globals !");
227 PyObject *builtins2(PyModule_GetDict(builtins));
229 throw Exception("PickelizedPyObjServer constructor : internal error fail to invoke __dict__ on __builtins__ !");
230 PyObject *tmp(PyDict_GetItemString(builtins2,typeName.c_str()));
233 std::ostringstream oss; oss << "PickelizedPyObjServer::CreateDftObjFromType : Invalid type name \"" << typeName << "\" !";
234 throw Exception(oss.str());
236 PyObject *args(PyTuple_New(0));
237 PyObject *ret(PyObject_CallObject(tmp,args));
242 void PickelizedPyObjServer::checkKeyPresence(PyObject *key, bool presence)
245 throw Exception("PickelizedPyObjServer::checkKeyPresence : not a dict !");
246 PyObject *selfMeth(PyObject_GetAttrString(_self,"__contains__"));//new ref
247 PyObject *args(PyTuple_New(1));
248 PyTuple_SetItem(args,0,key); Py_XINCREF(key);// key is stolen by PyTuple_SetItem
249 PyObject *retPy(PyObject_CallObject(selfMeth,args));
251 Py_XDECREF(selfMeth);
253 if(retPy!=Py_False && retPy!=Py_True)
254 throw Exception("PickelizedPyObjServer::checkKeyPresence : unexpected return of dict.__contains__ !");
258 throw Exception("PickelizedPyObjServer::checkKeyPresence : key is already present and it should not !");
263 throw Exception("PickelizedPyObjServer::checkKeyPresence : key is not present and it should !");