Salome HOME
Little cleanup for global vars.
[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 PickelizedPyObjRdWrServer::PickelizedPyObjRdWrServer(DataScopeServer *father, const std::string& typeName, const std::string& varName):PickelizedPyObjServer(father,varName,CreateDftObjFromType(father->getGlobals(),typeName))
31 {
32 }
33
34 PickelizedPyObjRdWrServer::PickelizedPyObjRdWrServer(DataScopeServer *father, const std::string& varName, const SALOME::ByteVec& value):PickelizedPyObjServer(father,varName,value)
35 {
36 }
37
38 //! obj is consumed
39 PickelizedPyObjRdWrServer::PickelizedPyObjRdWrServer(DataScopeServer *father, const std::string& varName, PyObject *obj):PickelizedPyObjServer(father,varName,obj)
40 {
41 }
42
43 PickelizedPyObjRdWrServer::~PickelizedPyObjRdWrServer()
44 {
45 }
46
47 /*!
48  * Called remotely -> to protect against throw
49  */
50 void PickelizedPyObjRdWrServer::setSerializedContent(const SALOME::ByteVec& newValue)
51 {
52   setSerializedContentInternal(newValue);
53 }
54
55 /*!
56  * Called remotely -> to protect against throw
57  */
58 SALOME::PickelizedPyObjRdWrServer_ptr PickelizedPyObjRdWrServer::invokePythonMethodOn(const char *method, const SALOME::ByteVec& args)
59 {
60   if(!_self)
61     throw Exception("PickelizedPyObjRdWrServer::invokePythonMethodOn : self is NULL !");
62   std::string argsCpp;
63   FromByteSeqToCpp(args,argsCpp);
64   PyObject *argsPy(getPyObjFromPickled(argsCpp));
65   //
66   PyObject *selfMeth(PyObject_GetAttrString(_self,method));
67   if(!selfMeth)
68     {
69       std::ostringstream oss; oss << "PickelizedPyObjRdWrServer::invokePythonMethodOn : Method \"" << method << "\" is not available !";
70       throw Exception(oss.str());
71     }
72   PyObject *res(PyObject_CallObject(selfMeth,argsPy));// self can have been modified by this call !
73   Py_XDECREF(selfMeth);
74   Py_XDECREF(argsPy);
75   if(!res)
76     {
77       std::ostringstream oss,oss2,oss3;
78       PyObject *errTyp(0),*errValue(0),*errTB(0);
79       PyErr_Fetch(&errTyp,&errValue,&errTB);
80       oss2 << "(";
81       if(errTyp)
82         {
83           PyObject *ob(PyObject_Str(errTyp));
84           oss2 << " type : \"" << (const char *)PyString_AsString(ob) << "\"";
85           Py_XDECREF(ob); Py_XDECREF(errTyp);
86         }
87       if(errValue)
88         {
89           PyObject *ob(PyObject_Str(errValue));
90           oss2 << " value : \"" << (const char *)PyString_AsString(ob) << "\"";
91           Py_XDECREF(ob); Py_XDECREF(errValue);
92         }
93       oss2 << " )";
94       if(errTB)
95         {
96           PyObject *ob(PyObject_Str(errTB));
97           oss2 << "( traceback : \"" << (const char *)PyString_AsString(ob) << "\"";
98           Py_XDECREF(ob); Py_XDECREF(errTB);
99         }
100       oss2 << " )";
101       PyErr_Clear();
102       oss << "PickelizedPyObjRdWrServer::invokePythonMethodOn : Problem during invokation serverside of Method \"" << method << "\" ! Details are : " << oss2.str() << "\n\n" << "TraceBack is : " << oss3.str(); 
103       throw Exception(oss.str());
104     }
105   PickelizedPyObjRdWrServer *ret(new PickelizedPyObjRdWrServer(_father,DataScopeServer::BuildTmpVarNameFrom(getVarNameCpp()),res));
106   PortableServer::POA_var poa(_father->getPOA());
107   PortableServer::ObjectId_var id(poa->activate_object(ret));
108   CORBA::Object_var obj(poa->id_to_reference(id));
109   return SALOME::PickelizedPyObjRdWrServer::_narrow(obj);
110 }