1 // Copyright (C) 2006-2008 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.
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 #include "RuntimeSALOME.hxx"
20 #include "PythonNode.hxx"
21 #include "PythonPorts.hxx"
23 #include "PyStdout.hxx"
28 #include "YacsTrace.hxx"
30 using namespace YACS::ENGINE;
33 const char PythonNode::IMPL_NAME[]="Python";
34 const char PythonNode::KIND[]="Python";
36 PythonNode::PythonNode(const PythonNode& other, ComposedNode *father):InlineNode(other,father)
38 _implementation=IMPL_NAME;
39 PyGILState_STATE gstate=PyGILState_Ensure();
40 _context=PyDict_New();
41 PyGILState_Release(gstate);
44 PythonNode::PythonNode(const std::string& name):InlineNode(name)
46 _implementation=IMPL_NAME;
47 PyGILState_STATE gstate = PyGILState_Ensure();
48 _context=PyDict_New();
49 PyGILState_Release(gstate);
52 PythonNode::~PythonNode()
54 PyGILState_STATE gstate = PyGILState_Ensure();
55 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
57 PyGILState_Release(gstate);
60 void PythonNode::load()
64 void PythonNode::execute()
66 DEBTRACE( "++++++++++++++ PyNode::execute: " << getName() << " ++++++++++++++++++++" );
67 PyGILState_STATE gstate = PyGILState_Ensure();
68 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
71 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
72 PyGILState_Release(gstate);
73 _errorDetails=msg.str();
74 throw Exception(msg.str());
77 DEBTRACE( "---------------PyNode::inputs---------------" );
78 list<InputPort *>::iterator iter2;
79 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
81 InputPyPort *p=(InputPyPort *)*iter2;
82 DEBTRACE( "port name: " << p->getName() );
83 DEBTRACE( "port kind: " << p->edGetType()->kind() );
84 PyObject* ob=p->getPyObj();
85 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
87 PyObject_Print(ob,stderr,Py_PRINT_RAW);
90 int ier=PyDict_SetItemString(_context,p->getName().c_str(),ob);
91 DEBTRACE( "after PyDict_SetItemString:ob refcnt: " << ob->ob_refcnt );
94 DEBTRACE( "---------------End PyNode::inputs---------------" );
97 DEBTRACE( "----------------PyNode::calculation---------------" );
99 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
100 PyObject *res=PyRun_String(_script.c_str(),Py_file_input,_context,_context);
101 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
107 PyObject* new_stderr = newPyStdOut(_errorDetails);
108 PySys_SetObject("stderr", new_stderr);
110 PySys_SetObject("stderr", PySys_GetObject("__stderr__"));
111 Py_DECREF(new_stderr);
113 PyGILState_Release(gstate);
114 throw Exception("Error during execution");
118 DEBTRACE( "-----------------PyNode::outputs-----------------" );
119 list<OutputPort *>::iterator iter;
122 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
124 OutputPyPort *p=(OutputPyPort *)*iter;
125 DEBTRACE( "port name: " << p->getName() );
126 DEBTRACE( "port kind: " << p->edGetType()->kind() );
127 PyObject *ob=PyDict_GetItemString(_context,p->getName().c_str());
129 PyGILState_Release(gstate);
130 std::string msg="Error during execution: there is no variable ";
131 msg=msg+p->getName()+" in node context";
133 throw Exception(msg);
135 DEBTRACE( "PyNode::outputs::ob refcnt: " << ob->ob_refcnt );
137 PyObject_Print(ob,stderr,Py_PRINT_RAW);
143 catch(ConversionException& ex)
145 PyGILState_Release(gstate);
146 _errorDetails=ex.what();
150 DEBTRACE( "-----------------End PyNode::outputs-----------------" );
151 PyGILState_Release(gstate);
152 DEBTRACE( "++++++++++++++ End PyNode::execute: " << getName() << " ++++++++++++++++++++" );
155 Node *PythonNode::simpleClone(ComposedNode *father, bool editionOnly) const
157 return new PythonNode(*this,father);
160 //! Create a new node of same type with a given name
161 PythonNode* PythonNode::cloneNode(const std::string& name)
163 PythonNode* n=new PythonNode(name);
164 n->setScript(_script);
165 list<InputPort *>::iterator iter;
166 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
168 InputPyPort *p=(InputPyPort *)*iter;
169 DEBTRACE( "port name: " << p->getName() );
170 DEBTRACE( "port kind: " << p->edGetType()->kind() );
171 n->edAddInputPort(p->getName(),p->edGetType());
173 list<OutputPort *>::iterator iter2;
174 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
176 OutputPyPort *p=(OutputPyPort *)*iter2;
177 DEBTRACE( "port name: " << p->getName() );
178 DEBTRACE( "port kind: " << p->edGetType()->kind() );
179 n->edAddOutputPort(p->getName(),p->edGetType());
184 PyFuncNode::PyFuncNode(const PyFuncNode& other, ComposedNode *father):InlineFuncNode(other,father),_pyfunc(0)
186 _implementation = PythonNode::IMPL_NAME;
187 PyGILState_STATE gstate = PyGILState_Ensure();
188 _context=PyDict_New();
189 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
190 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
193 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
194 _errorDetails=msg.str();
195 PyGILState_Release(gstate);
196 throw Exception(msg.str());
198 PyGILState_Release(gstate);
201 PyFuncNode::PyFuncNode(const std::string& name): InlineFuncNode(name),_pyfunc(0)
204 _implementation = PythonNode::IMPL_NAME;
205 DEBTRACE( "PyFuncNode::PyFuncNode " << name );
206 PyGILState_STATE gstate = PyGILState_Ensure();
207 _context=PyDict_New();
208 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
209 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
212 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
213 _errorDetails=msg.str();
214 PyGILState_Release(gstate);
215 throw Exception(msg.str());
217 PyGILState_Release(gstate);
220 PyFuncNode::~PyFuncNode()
222 DEBTRACE( getName() );
223 PyGILState_STATE gstate = PyGILState_Ensure();
224 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
225 if(_pyfunc)DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
227 PyGILState_Release(gstate);
230 void PyFuncNode::load()
232 DEBTRACE( "---------------PyFuncNode::load function " << getName() << " ---------------" );
235 list<OutputPort *>::iterator iter;
236 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
238 OutputPyPort *p=(OutputPyPort *)*iter;
239 DEBTRACE( "port name: " << p->getName() );
240 DEBTRACE( "port kind: " << p->edGetType()->kind() );
243 PyGILState_STATE gstate = PyGILState_Ensure();
244 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
245 PyObject *res=PyRun_String(_script.c_str(),Py_file_input,_context,_context);
246 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
250 PyObject* new_stderr = newPyStdOut(_errorDetails);
251 PySys_SetObject("stderr", new_stderr);
253 PySys_SetObject("stderr", PySys_GetObject("__stderr__"));
254 Py_DECREF(new_stderr);
256 PyGILState_Release(gstate);
257 throw Exception("Error during execution");
261 _pyfunc=PyDict_GetItemString(_context,_fname.c_str());
262 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
266 PyObject* new_stderr = newPyStdOut(_errorDetails);
267 PySys_SetObject("stderr", new_stderr);
269 PySys_SetObject("stderr", PySys_GetObject("__stderr__"));
270 Py_DECREF(new_stderr);
272 PyGILState_Release(gstate);
273 throw Exception("Error during execution");
275 DEBTRACE( "---------------End PyFuncNode::load function---------------" );
276 PyGILState_Release(gstate);
279 void PyFuncNode::execute()
281 DEBTRACE( "++++++++++++++ PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
284 if(!_pyfunc)throw Exception("PyFuncNode badly loaded");
285 PyGILState_STATE gstate = PyGILState_Ensure();
287 DEBTRACE( "---------------PyFuncNode::inputs---------------" );
288 PyObject* args = PyTuple_New(getNumberOfInputPorts()) ;
289 list<InputPort *>::iterator iter2;
290 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
292 InputPyPort *p=(InputPyPort *)*iter2;
293 DEBTRACE( "port name: " << p->getName() );
294 DEBTRACE( "port kind: " << p->edGetType()->kind() );
297 PyObject_Print(ob,stderr,Py_PRINT_RAW);
300 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
302 PyTuple_SetItem(args,pos,ob);
303 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
306 DEBTRACE( "---------------End PyFuncNode::inputs---------------" );
308 DEBTRACE( "----------------PyFuncNode::calculation---------------" );
310 PyObject_Print(_pyfunc,stderr,Py_PRINT_RAW);
312 PyObject_Print(args,stderr,Py_PRINT_RAW);
315 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
316 PyObject* result = PyObject_CallObject( _pyfunc , args ) ;
317 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
324 PyObject* new_stderr = newPyStdOut(_errorDetails);
325 PySys_SetObject("stderr", new_stderr);
327 PySys_SetObject("stderr", PySys_GetObject("__stderr__"));
328 Py_DECREF(new_stderr);
330 PyGILState_Release(gstate);
331 throw Exception("Error during execution");
333 DEBTRACE( "----------------End PyFuncNode::calculation---------------" );
335 DEBTRACE( "-----------------PyFuncNode::outputs-----------------" );
337 if(result == Py_None)
339 else if(PyTuple_Check(result))
340 nres=PyTuple_Size(result);
342 if(getNumberOfOutputPorts() != nres)
344 std::string msg="Number of output arguments : Mismatch between definition and execution";
346 PyGILState_Release(gstate);
348 throw Exception(msg);
353 PyObject_Print(result,stderr,Py_PRINT_RAW);
356 list<OutputPort *>::iterator iter;
359 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
361 OutputPyPort *p=(OutputPyPort *)*iter;
362 DEBTRACE( "port name: " << p->getName() );
363 DEBTRACE( "port kind: " << p->edGetType()->kind() );
364 DEBTRACE( "port pos : " << pos );
365 if(PyTuple_Check(result))ob=PyTuple_GetItem(result,pos) ;
367 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
369 PyObject_Print(ob,stderr,Py_PRINT_RAW);
376 catch(ConversionException& ex)
379 PyGILState_Release(gstate);
380 _errorDetails=ex.what();
383 DEBTRACE( "-----------------End PyFuncNode::outputs-----------------" );
386 PyGILState_Release(gstate);
387 DEBTRACE( "++++++++++++++ End PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
390 Node *PyFuncNode::simpleClone(ComposedNode *father, bool editionOnly) const
392 return new PyFuncNode(*this,father);
395 //! Create a new node of same type with a given name
396 PyFuncNode* PyFuncNode::cloneNode(const std::string& name)
398 PyFuncNode* n=new PyFuncNode(name);
399 n->setScript(_script);
401 list<InputPort *>::iterator iter;
402 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
404 InputPyPort *p=(InputPyPort *)*iter;
405 n->edAddInputPort(p->getName(),p->edGetType());
407 list<OutputPort *>::iterator iter2;
408 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
410 OutputPyPort *p=(OutputPyPort *)*iter2;
411 n->edAddOutputPort(p->getName(),p->edGetType());