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(DataScopeServer *father, const std::string& varName, const SALOME::ByteVec& value):BasicDataServer(father,varName),_self(0)
32 setSerializedContentInternal(value);
36 PickelizedPyObjServer::PickelizedPyObjServer(DataScopeServer *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 void PickelizedPyObjServer::FromByteSeqToCpp(const SALOME::ByteVec& bsToBeConv, std::string& ret)
57 std::size_t sz(bsToBeConv.length());
59 char *buf(const_cast<char *>(ret.c_str()));
60 for(std::size_t i=0;i<sz;i++)
64 SALOME::ByteVec *PickelizedPyObjServer::FromCppToByteSeq(const std::string& strToBeConv)
66 SALOME::ByteVec *ret(new SALOME::ByteVec);
67 const char *buf(strToBeConv.c_str());
68 std::size_t sz(strToBeConv.size());
70 for(std::size_t i=0;i<sz;i++)
75 //! New reference returned
76 PyObject *PickelizedPyObjServer::getPyObjFromPickled(const std::string& pickledData)
78 std::size_t sz(pickledData.size());
79 PyObject *pickledDataPy(PyString_FromStringAndSize(NULL,sz));// agy : do not use PyString_FromString because std::string hides a vector of byte.
80 char *buf(PyString_AsString(pickledDataPy));// this buf can be used thanks to python documentation.
81 const char *inBuf(pickledData.c_str());
82 std::copy(inBuf,inBuf+sz,buf);
83 PyObject *selfMeth(PyObject_GetAttrString(_father->getPickler(),"loads"));
84 PyObject *args(PyTuple_New(1)); PyTuple_SetItem(args,0,pickledDataPy);
85 PyObject *ret(PyObject_CallObject(selfMeth,args));
91 //! obj is consumed by this method.
92 std::string PickelizedPyObjServer::pickelize(PyObject *obj)
94 PyObject *args(PyTuple_New(2));
95 PyTuple_SetItem(args,0,obj);
96 PyTuple_SetItem(args,1,PyInt_FromLong(2));// because "assert(cPickle.HIGHEST_PROTOCOL is 2)"
97 PyObject *selfMeth(PyObject_GetAttrString(_father->getPickler(),"dumps"));
98 PyObject *retPy(PyObject_CallObject(selfMeth,args));
101 std::size_t sz(PyString_Size(retPy));
102 std::string ret(sz,'\0');
103 const char *buf(PyString_AsString(retPy));
104 char *inBuf(const_cast<char *>(ret.c_str()));
105 for(std::size_t i=0;i<sz;i++)
111 //! obj is consumed by this method.
112 void PickelizedPyObjServer::setNewPyObj(PyObject *obj)
115 throw Exception("PickelizedPyObjServer::setNewPyObj : trying to assign a NULL pyobject in this !");
118 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)
119 throw Exception("PickelizedPyObjServer::setNewPyObj : Supported python types are [list,tuple,dict,str,int,bool,float,None] !");
122 PyObject *selfType(PyObject_Type(_self));
123 if(PyObject_IsInstance(obj,selfType)!=1)
126 Py_XDECREF(selfType);
127 throw Exception("PickelizedPyObjServer::setNewPyObj : type of new object is not the same than those previously set !");
130 Py_XDECREF(selfType);
136 void PickelizedPyObjServer::setSerializedContentInternal(const SALOME::ByteVec& newValue)
139 FromByteSeqToCpp(newValue,data);
140 setNewPyObj(getPyObjFromPickled(data));
143 PyObject *PickelizedPyObjServer::CreateDftObjFromType(PyObject *globals, const std::string& typeName)
145 PyObject *builtins(PyDict_GetItemString(globals,"__builtins__"));
147 throw Exception("PickelizedPyObjServer constructor : no __builtins__ in globals !");
148 PyObject *builtins2(PyModule_GetDict(builtins));
150 throw Exception("PickelizedPyObjServer constructor : internal error fail to invoke __dict__ on __builtins__ !");
151 PyObject *tmp(PyDict_GetItemString(builtins2,typeName.c_str()));
154 std::ostringstream oss; oss << "PickelizedPyObjServer::CreateDftObjFromType : Invalid type name \"" << typeName << "\" !";
155 throw Exception(oss.str());
157 PyObject *args(PyTuple_New(0));
158 PyObject *ret(PyObject_CallObject(tmp,args));