Salome HOME
Implementation of working glob var. Remove memory leak on TestSalomeSDS.py.
[modules/kernel.git] / src / SALOMESDS / SALOMESDS_PickelizedPyObjRdWrServer.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony GEAY (EDF R&D)
20
21 #include "SALOMESDS_PickelizedPyObjRdWrServer.hxx"
22 #include "SALOMESDS_DataScopeServer.hxx"
23 #include "SALOMESDS_Exception.hxx"
24
25 #include <iostream>
26 #include <sstream>
27
28 using namespace SALOMESDS;
29
30 const char PickelizedPyObjRdWrServer::ACCESS_REPR[]="RdWr";
31
32 PickelizedPyObjRdWrServer::PickelizedPyObjRdWrServer(DataScopeServerBase *father, const std::string& typeName, const std::string& varName):PickelizedPyObjServerModifiable(father,varName,CreateDftObjFromType(father->getGlobals(),typeName))
33 {
34 }
35
36 PickelizedPyObjRdWrServer::PickelizedPyObjRdWrServer(DataScopeServerBase *father, const std::string& varName, const SALOME::ByteVec& value):PickelizedPyObjServerModifiable(father,varName,value)
37 {
38 }
39
40 //! obj is consumed
41 PickelizedPyObjRdWrServer::PickelizedPyObjRdWrServer(DataScopeServerBase *father, const std::string& varName, PyObject *obj):PickelizedPyObjServerModifiable(father,varName,obj)
42 {
43 }
44
45 PickelizedPyObjRdWrServer::~PickelizedPyObjRdWrServer()
46 {
47 }
48
49 /*!
50  * Called remotely -> to protect against throw
51  */
52 void PickelizedPyObjRdWrServer::setSerializedContent(const SALOME::ByteVec& newValue)
53 {
54   setSerializedContentInternal(newValue);
55 }
56
57 SALOME::PickelizedPyObjRdWrServer_ptr PickelizedPyObjRdWrServer::invokePythonMethodOn(const char *method, const SALOME::ByteVec& args)
58 {
59   if(!_self)
60     throw Exception("PickelizedPyObjRdWrServer::invokePythonMethodOn : self is NULL !");
61   std::string argsCpp;
62   FromByteSeqToCpp(args,argsCpp);
63   PyObject *argsPy(getPyObjFromPickled(argsCpp));
64   //
65   PyObject *selfMeth(PyObject_GetAttrString(_self,method));
66   if(!selfMeth)
67     {
68       std::ostringstream oss; oss << "PickelizedPyObjRdWrServer::invokePythonMethodOn : Method \"" << method << "\" is not available !";
69       throw Exception(oss.str());
70     }
71   PyObject *res(PyObject_CallObject(selfMeth,argsPy));// self can have been modified by this call !
72   Py_XDECREF(selfMeth);
73   Py_XDECREF(argsPy);
74   if(!res)
75     {
76       std::ostringstream oss,oss2,oss3;
77       PyObject *errTyp(0),*errValue(0),*errTB(0);
78       PyErr_Fetch(&errTyp,&errValue,&errTB);
79       oss2 << "(";
80       if(errTyp)
81         {
82           PyObject *ob(PyObject_Str(errTyp));
83           oss2 << " type : \"" << (const char *)PyString_AsString(ob) << "\"";
84           Py_XDECREF(ob); Py_XDECREF(errTyp);
85         }
86       if(errValue)
87         {
88           PyObject *ob(PyObject_Str(errValue));
89           oss2 << " value : \"" << (const char *)PyString_AsString(ob) << "\"";
90           Py_XDECREF(ob); Py_XDECREF(errValue);
91         }
92       oss2 << " )";
93       if(errTB)
94         {
95           PyObject *ob(PyObject_Str(errTB));
96           oss2 << "( traceback : \"" << (const char *)PyString_AsString(ob) << "\"";
97           Py_XDECREF(ob); Py_XDECREF(errTB);
98         }
99       oss2 << " )";
100       PyErr_Clear();
101       oss << "PickelizedPyObjRdWrServer::invokePythonMethodOn : Problem during invokation serverside of Method \"" << method << "\" ! Details are : " << oss2.str() << "\n\n" << "TraceBack is : " << oss3.str(); 
102       throw Exception(oss.str());
103     }
104   PickelizedPyObjRdWrServer *ret(new PickelizedPyObjRdWrServer(_father,DataScopeServer::BuildTmpVarNameFrom(getVarNameCpp()),res));
105   PortableServer::POA_var poa(_father->getPOA());
106   PortableServer::ObjectId_var id(poa->activate_object(ret));
107   CORBA::Object_var obj(poa->id_to_reference(id));
108   return SALOME::PickelizedPyObjRdWrServer::_narrow(obj);
109 }
110
111 void PickelizedPyObjRdWrServer::addKeyValueHard(PyObject *key, PyObject *value)
112 {
113   bool isOK(PyDict_SetItem(_self,key,value)==0);
114   if(!isOK)
115     throw Exception("PickelizedPyObjRdWrServer::addKeyValueHard : error when trying to add key,value to dict !");
116 }
117
118 void PickelizedPyObjRdWrServer::removeKeyInVarErrorIfNotAlreadyExisting(PyObject *key)
119 {
120   checkKeyPresent(key);
121   if(PyDict_DelItem(_self,key)!=0)
122     throw Exception("PickelizedPyObjRdWrServer::removeKeyInVarErrorIfNotAlreadyExisting : error during deletion of key in dict !");
123 }
124
125 std::string PickelizedPyObjRdWrServer::getAccessStr() const
126 {
127   return std::string(ACCESS_REPR);
128 }