1 // Copyright (C) 2006-2016 CEA/DEN, EDF R&D
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
20 #include "PythonPorts.hxx"
21 #include "TypeConversions.hxx"
22 #include "TypeCode.hxx"
24 #include "ConversionException.hxx"
30 #include "YacsTrace.hxx"
32 using namespace YACS::ENGINE;
35 void releasePyObj(PyObject* data)
37 DEBTRACE( "data refcnt: " << data->ob_refcnt );
38 if (PyObject_HasAttrString(data, (char*)"_is_a"))
40 PyObject *result = PyObject_CallMethod(data, (char*)"_is_a", (char*)"s",(char*)"IDL:SALOME/GenericObj:1.0");
41 if(result && PyInt_Check(result))
43 if(PyInt_AS_LONG(result))
45 PyObject* o=PyObject_CallMethod(data, (char*)"Destroy", (char*)"");
55 throw ConversionException("Corba object does not exist: you have perhaps forgotten to call Register on a GenericObj");
67 throw ConversionException("Corba object does not exist: you have perhaps forgotten to call Register on a GenericObj");
72 void registerPyObj(PyObject* data)
74 if (PyObject_HasAttrString(data, (char*)"_is_a"))
76 PyObject *result = PyObject_CallMethod(data, (char*)"_is_a", (char*)"s",(char*)"IDL:SALOME/GenericObj:1.0");
77 if(result && PyInt_Check(result))
79 if(PyInt_AS_LONG(result))
81 PyObject* o= PyObject_CallMethod(data, (char*)"Register", (char*)"") ;
91 throw ConversionException("Corba object does not exist: you have perhaps forgotten to call Register on a GenericObj");
103 throw ConversionException("Corba object does not exist: you have perhaps forgotten to call Register on a GenericObj");
108 InputPyPort::InputPyPort(const std::string& name, Node *node, TypeCode * type)
109 : InputPort(name, node, type), DataPort(name, node, type), Port(node), _data(Py_None),_initData(Py_None)
112 Py_INCREF(_initData);
114 InputPyPort::~InputPyPort()
116 PyGILState_STATE gstate = PyGILState_Ensure();
117 DEBTRACE( "_data refcnt: " << _data->ob_refcnt );
118 DEBTRACE( "_initData refcnt: " << _initData->ob_refcnt );
119 // Release or not release : all GenericObj are deleted when the input port is deleted
122 Py_XDECREF(_initData);
123 PyGILState_Release(gstate);
126 InputPyPort::InputPyPort(const InputPyPort& other, Node *newHelder):InputPort(other,newHelder),DataPort(other,newHelder),Port(other,newHelder)
128 _initData=other._initData;
129 Py_INCREF(_initData);
134 bool InputPyPort::edIsManuallyInitialized() const
136 return _initData!=Py_None;
139 void InputPyPort::edRemoveManInit()
141 Py_XDECREF(_initData);
143 Py_INCREF(_initData);
147 InputPort::edRemoveManInit();
150 void InputPyPort::put(const void *data) throw(ConversionException)
152 put((PyObject *)data);
155 void InputPyPort::put(PyObject *data) throw(ConversionException)
157 InterpreterUnlocker l;
163 registerPyObj(_data);
164 DEBTRACE( "_data refcnt: " << _data->ob_refcnt );
167 InputPort *InputPyPort::clone(Node *newHelder) const
169 return new InputPyPort(*this,newHelder);
172 PyObject * InputPyPort::getPyObj() const
177 void *InputPyPort::get() const throw(YACS::Exception)
179 return (void*) _data;
182 std::string InputPyPort::getAsString()
185 //protect _data against modification or delete in another thread
186 PyObject* data=_data;
188 ret = convertPyObjectToString(data);
193 std::string InputPyPort::getHumanRepr()
197 PyObject *ret(PyObject_Str(_data));
201 char *val(PyString_AsString(ret));
208 bool InputPyPort::isEmpty()
210 return _data == Py_None;
213 //! Save the current data value for further reinitialization of the port
217 void InputPyPort::exSaveInit()
219 // Interpreter lock seems necessary when deleting lists in Python 2.7
220 PyGILState_STATE gstate = PyGILState_Ensure();
221 Py_XDECREF(_initData);
222 PyGILState_Release(gstate);
224 Py_INCREF(_initData);
225 DEBTRACE( "_initData.ob refcnt: " << _initData->ob_refcnt );
226 DEBTRACE( "_data.ob refcnt: " << _data->ob_refcnt );
229 //! Restore the saved data value to current data value
231 * If no data has been saved (_initData == 0) don't restore
233 void InputPyPort::exRestoreInit()
235 if(!_initData)return;
239 DEBTRACE( "_initData.ob refcnt: " << _initData->ob_refcnt );
240 DEBTRACE( "_data.ob refcnt: " << _data->ob_refcnt );
243 std::string InputPyPort::dump()
245 if( _data == Py_None)
246 return "<value>None</value>";
248 InterpreterUnlocker l;
249 if (edGetType()->kind() != YACS::ENGINE::Objref)
250 return convertPyObjectXml(edGetType(), _data);
251 if (! _stringRef.empty())
254 return convertPyObjectXml(edGetType(), _data);
257 // msg << "Cannot retreive init reference string for port " << _name
258 // << " on node " << _node->getName();
259 // throw Exception(msg.str());
263 std::string InputPyPort::valToStr()
265 int isString = PyString_Check(getPyObj());
266 //DEBTRACE("isString=" << isString);
267 PyObject *strPyObj = PyObject_Str(getPyObj());
268 //DEBTRACE(PyString_Size(strPyObj));
269 string val = PyString_AsString(strPyObj);
271 val = "\"" + val + "\"";
277 void InputPyPort::valFromStr(std::string valstr)
282 OutputPyPort::OutputPyPort(const std::string& name, Node *node, TypeCode * type)
283 : OutputPort(name, node, type), DataPort(name, node, type), Port(node)
288 OutputPyPort::~OutputPyPort()
290 PyGILState_STATE gstate = PyGILState_Ensure();
291 DEBTRACE( "_data refcnt: " << _data->ob_refcnt );
292 // Release or not release : all GenericObj are deleted when the output port is deleted
295 PyGILState_Release(gstate);
298 OutputPyPort::OutputPyPort(const OutputPyPort& other, Node *newHelder):OutputPort(other,newHelder),DataPort(other,newHelder),Port(other,newHelder),
304 void OutputPyPort::put(const void *data) throw(ConversionException)
306 put((PyObject *)data);
309 void OutputPyPort::put(PyObject *data) throw(ConversionException)
312 DEBTRACE( "OutputPyPort::put.ob refcnt: " << data->ob_refcnt );
314 PyObject_Print(data,stderr,Py_PRINT_RAW);
321 //no registerPyObj : we steal the output reference of the node
322 DEBTRACE( "OutputPyPort::put.ob refcnt: " << data->ob_refcnt );
323 OutputPort::put(data);
326 OutputPort *OutputPyPort::clone(Node *newHelder) const
328 return new OutputPyPort(*this,newHelder);
331 PyObject * OutputPyPort::get() const
336 PyObject * OutputPyPort::getPyObj() const
341 std::string OutputPyPort::getAsString()
344 //protect _data against modification or delete in another thread
345 PyObject* data=_data;
347 ret = convertPyObjectToString(data);
352 std::string OutputPyPort::dump()
354 if( _data == Py_None)
355 return "<value>None</value>";
356 InterpreterUnlocker l;
357 string xmldump = convertPyObjectXml(edGetType(), _data);
361 std::string OutputPyPort::valToStr()
363 PyObject *strPyObj = PyObject_Str(getPyObj());
364 string val = PyString_AsString(strPyObj);
369 void OutputPyPort::valFromStr(std::string valstr)