2 #include "RuntimeSALOME.hxx"
3 #include "PythonNode.hxx"
4 #include "PythonPorts.hxx"
6 #include "PyStdout.hxx"
11 #include "YacsTrace.hxx"
13 using namespace YACS::ENGINE;
16 const char PythonNode::IMPL_NAME[]="Python";
17 const char PythonNode::KIND[]="Python";
19 PythonNode::PythonNode(const PythonNode& other, ComposedNode *father):InlineNode(other,father)
21 _implementation=IMPL_NAME;
22 PyGILState_STATE gstate=PyGILState_Ensure();
23 _context=PyDict_New();
24 PyGILState_Release(gstate);
27 PythonNode::PythonNode(const std::string& name):InlineNode(name)
29 _implementation=IMPL_NAME;
30 PyGILState_STATE gstate = PyGILState_Ensure();
31 _context=PyDict_New();
32 PyGILState_Release(gstate);
35 PythonNode::~PythonNode()
37 PyGILState_STATE gstate = PyGILState_Ensure();
38 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
40 PyGILState_Release(gstate);
43 void PythonNode::load()
47 void PythonNode::execute()
49 DEBTRACE( "++++++++++++++ PyNode::execute: " << getName() << " ++++++++++++++++++++" );
50 PyGILState_STATE gstate = PyGILState_Ensure();
51 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
54 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
55 PyGILState_Release(gstate);
56 _errorDetails=msg.str();
57 throw Exception(msg.str());
60 DEBTRACE( "---------------PyNode::inputs---------------" );
61 list<InputPort *>::iterator iter2;
62 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
64 InputPyPort *p=(InputPyPort *)*iter2;
65 DEBTRACE( "port name: " << p->getName() );
66 DEBTRACE( "port kind: " << p->edGetType()->kind() );
67 PyObject* ob=p->getPyObj();
68 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
70 PyObject_Print(ob,stderr,Py_PRINT_RAW);
73 int ier=PyDict_SetItemString(_context,p->getName().c_str(),ob);
74 DEBTRACE( "after PyDict_SetItemString:ob refcnt: " << ob->ob_refcnt );
77 DEBTRACE( "---------------End PyNode::inputs---------------" );
80 DEBTRACE( "----------------PyNode::calculation---------------" );
82 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
83 PyObject *res=PyRun_String(_script.c_str(),Py_file_input,_context,_context);
84 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
90 PyObject* new_stderr = newPyStdOut(_errorDetails);
91 PySys_SetObject("stderr", new_stderr);
93 PySys_SetObject("stderr", PySys_GetObject("__stderr__"));
94 Py_DECREF(new_stderr);
96 PyGILState_Release(gstate);
97 throw Exception("Error during execution");
101 DEBTRACE( "-----------------PyNode::outputs-----------------" );
102 list<OutputPort *>::iterator iter;
105 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
107 OutputPyPort *p=(OutputPyPort *)*iter;
108 DEBTRACE( "port name: " << p->getName() );
109 DEBTRACE( "port kind: " << p->edGetType()->kind() );
110 PyObject *ob=PyDict_GetItemString(_context,p->getName().c_str());
112 PyGILState_Release(gstate);
113 std::string msg="Error during execution: there is no variable ";
114 msg=msg+p->getName()+" in node context";
116 throw Exception(msg);
118 DEBTRACE( "PyNode::outputs::ob refcnt: " << ob->ob_refcnt );
120 PyObject_Print(ob,stderr,Py_PRINT_RAW);
126 catch(ConversionException& ex)
128 PyGILState_Release(gstate);
129 _errorDetails=ex.what();
133 DEBTRACE( "-----------------End PyNode::outputs-----------------" );
134 PyGILState_Release(gstate);
135 DEBTRACE( "++++++++++++++ End PyNode::execute: " << getName() << " ++++++++++++++++++++" );
138 Node *PythonNode::simpleClone(ComposedNode *father, bool editionOnly) const
140 return new PythonNode(*this,father);
143 //! Create a new node of same type with a given name
144 PythonNode* PythonNode::cloneNode(const std::string& name)
146 PythonNode* n=new PythonNode(name);
147 n->setScript(_script);
148 list<InputPort *>::iterator iter;
149 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
151 InputPyPort *p=(InputPyPort *)*iter;
152 DEBTRACE( "port name: " << p->getName() );
153 DEBTRACE( "port kind: " << p->edGetType()->kind() );
154 n->edAddInputPort(p->getName(),p->edGetType());
156 list<OutputPort *>::iterator iter2;
157 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
159 OutputPyPort *p=(OutputPyPort *)*iter2;
160 DEBTRACE( "port name: " << p->getName() );
161 DEBTRACE( "port kind: " << p->edGetType()->kind() );
162 n->edAddOutputPort(p->getName(),p->edGetType());
167 PyFuncNode::PyFuncNode(const PyFuncNode& other, ComposedNode *father):InlineFuncNode(other,father),_pyfunc(0)
169 _implementation = PythonNode::IMPL_NAME;
170 PyGILState_STATE gstate = PyGILState_Ensure();
171 _context=PyDict_New();
172 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
173 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
176 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
177 _errorDetails=msg.str();
178 PyGILState_Release(gstate);
179 throw Exception(msg.str());
181 PyGILState_Release(gstate);
184 PyFuncNode::PyFuncNode(const std::string& name): InlineFuncNode(name),_pyfunc(0)
187 _implementation = PythonNode::IMPL_NAME;
188 DEBTRACE( "PyFuncNode::PyFuncNode " << name );
189 PyGILState_STATE gstate = PyGILState_Ensure();
190 _context=PyDict_New();
191 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
192 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
195 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
196 _errorDetails=msg.str();
197 PyGILState_Release(gstate);
198 throw Exception(msg.str());
200 PyGILState_Release(gstate);
203 PyFuncNode::~PyFuncNode()
205 DEBTRACE( getName() );
206 PyGILState_STATE gstate = PyGILState_Ensure();
207 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
208 if(_pyfunc)DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
210 PyGILState_Release(gstate);
213 void PyFuncNode::load()
215 DEBTRACE( "---------------PyFuncNode::load function " << getName() << " ---------------" );
218 list<OutputPort *>::iterator iter;
219 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
221 OutputPyPort *p=(OutputPyPort *)*iter;
222 DEBTRACE( "port name: " << p->getName() );
223 DEBTRACE( "port kind: " << p->edGetType()->kind() );
226 PyGILState_STATE gstate = PyGILState_Ensure();
227 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
228 PyObject *res=PyRun_String(_script.c_str(),Py_file_input,_context,_context);
229 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
233 PyObject* new_stderr = newPyStdOut(_errorDetails);
234 PySys_SetObject("stderr", new_stderr);
236 PySys_SetObject("stderr", PySys_GetObject("__stderr__"));
237 Py_DECREF(new_stderr);
239 PyGILState_Release(gstate);
240 throw Exception("Error during execution");
244 _pyfunc=PyDict_GetItemString(_context,_fname.c_str());
245 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
249 PyObject* new_stderr = newPyStdOut(_errorDetails);
250 PySys_SetObject("stderr", new_stderr);
252 PySys_SetObject("stderr", PySys_GetObject("__stderr__"));
253 Py_DECREF(new_stderr);
255 PyGILState_Release(gstate);
256 throw Exception("Error during execution");
258 DEBTRACE( "---------------End PyFuncNode::load function---------------" );
259 PyGILState_Release(gstate);
262 void PyFuncNode::execute()
264 DEBTRACE( "++++++++++++++ PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
267 if(!_pyfunc)throw Exception("PyFuncNode badly loaded");
268 PyGILState_STATE gstate = PyGILState_Ensure();
270 DEBTRACE( "---------------PyFuncNode::inputs---------------" );
271 PyObject* args = PyTuple_New(getNumberOfInputPorts()) ;
272 list<InputPort *>::iterator iter2;
273 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
275 InputPyPort *p=(InputPyPort *)*iter2;
276 DEBTRACE( "port name: " << p->getName() );
277 DEBTRACE( "port kind: " << p->edGetType()->kind() );
280 PyObject_Print(ob,stderr,Py_PRINT_RAW);
283 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
285 PyTuple_SetItem(args,pos,ob);
286 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
289 DEBTRACE( "---------------End PyFuncNode::inputs---------------" );
291 DEBTRACE( "----------------PyFuncNode::calculation---------------" );
293 PyObject_Print(_pyfunc,stderr,Py_PRINT_RAW);
295 PyObject_Print(args,stderr,Py_PRINT_RAW);
298 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
299 PyObject* result = PyObject_CallObject( _pyfunc , args ) ;
300 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
307 PyObject* new_stderr = newPyStdOut(_errorDetails);
308 PySys_SetObject("stderr", new_stderr);
310 PySys_SetObject("stderr", PySys_GetObject("__stderr__"));
311 Py_DECREF(new_stderr);
313 PyGILState_Release(gstate);
314 throw Exception("Error during execution");
316 DEBTRACE( "----------------End PyFuncNode::calculation---------------" );
318 DEBTRACE( "-----------------PyFuncNode::outputs-----------------" );
320 if(result == Py_None)
322 else if(PyTuple_Check(result))
323 nres=PyTuple_Size(result);
325 if(getNumberOfOutputPorts() != nres)
327 std::string msg="Number of output arguments : Mismatch between definition and execution";
329 PyGILState_Release(gstate);
331 throw Exception(msg);
336 PyObject_Print(result,stderr,Py_PRINT_RAW);
339 list<OutputPort *>::iterator iter;
342 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
344 OutputPyPort *p=(OutputPyPort *)*iter;
345 DEBTRACE( "port name: " << p->getName() );
346 DEBTRACE( "port kind: " << p->edGetType()->kind() );
347 DEBTRACE( "port pos : " << pos );
348 if(PyTuple_Check(result))ob=PyTuple_GetItem(result,pos) ;
350 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
352 PyObject_Print(ob,stderr,Py_PRINT_RAW);
359 catch(ConversionException& ex)
362 PyGILState_Release(gstate);
363 _errorDetails=ex.what();
366 DEBTRACE( "-----------------End PyFuncNode::outputs-----------------" );
369 PyGILState_Release(gstate);
370 DEBTRACE( "++++++++++++++ End PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
373 Node *PyFuncNode::simpleClone(ComposedNode *father, bool editionOnly) const
375 return new PyFuncNode(*this,father);
378 //! Create a new node of same type with a given name
379 PyFuncNode* PyFuncNode::cloneNode(const std::string& name)
381 PyFuncNode* n=new PyFuncNode(name);
382 n->setScript(_script);
384 list<InputPort *>::iterator iter;
385 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
387 InputPyPort *p=(InputPyPort *)*iter;
388 n->edAddInputPort(p->getName(),p->edGetType());
390 list<OutputPort *>::iterator iter2;
391 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
393 OutputPyPort *p=(OutputPyPort *)*iter2;
394 n->edAddOutputPort(p->getName(),p->edGetType());