1 // Copyright (C) 2006-2015 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 bool InputPyPort::isEmpty()
195 return _data == Py_None;
198 //! Save the current data value for further reinitialization of the port
202 void InputPyPort::exSaveInit()
204 // Interpreter lock seems necessary when deleting lists in Python 2.7
205 PyGILState_STATE gstate = PyGILState_Ensure();
206 Py_XDECREF(_initData);
207 PyGILState_Release(gstate);
209 Py_INCREF(_initData);
210 DEBTRACE( "_initData.ob refcnt: " << _initData->ob_refcnt );
211 DEBTRACE( "_data.ob refcnt: " << _data->ob_refcnt );
214 //! Restore the saved data value to current data value
216 * If no data has been saved (_initData == 0) don't restore
218 void InputPyPort::exRestoreInit()
220 if(!_initData)return;
224 DEBTRACE( "_initData.ob refcnt: " << _initData->ob_refcnt );
225 DEBTRACE( "_data.ob refcnt: " << _data->ob_refcnt );
228 std::string InputPyPort::dump()
230 if( _data == Py_None)
231 return "<value>None</value>";
233 InterpreterUnlocker l;
234 if (edGetType()->kind() != YACS::ENGINE::Objref)
235 return convertPyObjectXml(edGetType(), _data);
236 if (! _stringRef.empty())
239 return convertPyObjectXml(edGetType(), _data);
242 // msg << "Cannot retreive init reference string for port " << _name
243 // << " on node " << _node->getName();
244 // throw Exception(msg.str());
248 std::string InputPyPort::valToStr()
250 int isString = PyString_Check(getPyObj());
251 //DEBTRACE("isString=" << isString);
252 PyObject *strPyObj = PyObject_Str(getPyObj());
253 //DEBTRACE(PyString_Size(strPyObj));
254 string val = PyString_AsString(strPyObj);
256 val = "\"" + val + "\"";
262 void InputPyPort::valFromStr(std::string valstr)
267 OutputPyPort::OutputPyPort(const std::string& name, Node *node, TypeCode * type)
268 : OutputPort(name, node, type), DataPort(name, node, type), Port(node)
273 OutputPyPort::~OutputPyPort()
275 PyGILState_STATE gstate = PyGILState_Ensure();
276 DEBTRACE( "_data refcnt: " << _data->ob_refcnt );
277 // Release or not release : all GenericObj are deleted when the output port is deleted
280 PyGILState_Release(gstate);
283 OutputPyPort::OutputPyPort(const OutputPyPort& other, Node *newHelder):OutputPort(other,newHelder),DataPort(other,newHelder),Port(other,newHelder),
289 void OutputPyPort::put(const void *data) throw(ConversionException)
291 put((PyObject *)data);
294 void OutputPyPort::put(PyObject *data) throw(ConversionException)
297 DEBTRACE( "OutputPyPort::put.ob refcnt: " << data->ob_refcnt );
299 PyObject_Print(data,stderr,Py_PRINT_RAW);
306 //no registerPyObj : we steal the output reference of the node
307 DEBTRACE( "OutputPyPort::put.ob refcnt: " << data->ob_refcnt );
308 OutputPort::put(data);
311 OutputPort *OutputPyPort::clone(Node *newHelder) const
313 return new OutputPyPort(*this,newHelder);
316 PyObject * OutputPyPort::get() const
321 PyObject * OutputPyPort::getPyObj() const
326 std::string OutputPyPort::getAsString()
329 //protect _data against modification or delete in another thread
330 PyObject* data=_data;
332 ret = convertPyObjectToString(data);
337 std::string OutputPyPort::dump()
339 if( _data == Py_None)
340 return "<value>None</value>";
341 InterpreterUnlocker l;
342 string xmldump = convertPyObjectXml(edGetType(), _data);
346 std::string OutputPyPort::valToStr()
348 PyObject *strPyObj = PyObject_Str(getPyObj());
349 string val = PyString_AsString(strPyObj);
354 void OutputPyPort::valFromStr(std::string valstr)