2 #include "RuntimeSALOME.hxx"
3 #include "PythonNode.hxx"
4 #include "PythonPorts.hxx"
10 #include "YacsTrace.hxx"
12 using namespace YACS::ENGINE;
15 const char PythonNode::IMPL_NAME[]="Python";
16 const char PythonNode::KIND[]="Python";
18 PythonNode::PythonNode(const PythonNode& other, ComposedNode *father):InlineNode(other,father)
20 _implementation=IMPL_NAME;
21 PyGILState_STATE gstate=PyGILState_Ensure();
22 _context=PyDict_New();
23 PyGILState_Release(gstate);
26 PythonNode::PythonNode(const std::string& name):InlineNode(name)
28 _implementation=IMPL_NAME;
29 PyGILState_STATE gstate = PyGILState_Ensure();
30 _context=PyDict_New();
31 PyGILState_Release(gstate);
34 PythonNode::~PythonNode()
36 PyGILState_STATE gstate = PyGILState_Ensure();
37 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
39 PyGILState_Release(gstate);
42 void PythonNode::load()
46 void PythonNode::execute()
48 DEBTRACE( "++++++++++++++ PyNode::execute: " << getName() << " ++++++++++++++++++++" );
49 PyGILState_STATE gstate = PyGILState_Ensure();
50 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
53 msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
54 PyGILState_Release(gstate);
55 throw Exception(msg.str());
58 DEBTRACE( "---------------PyNode::inputs---------------" );
59 list<InputPort *>::iterator iter2;
60 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
62 InputPyPort *p=(InputPyPort *)*iter2;
63 DEBTRACE( "port name: " << p->getName() );
64 DEBTRACE( "port kind: " << p->edGetType()->kind() );
65 PyObject* ob=p->getPyObj();
66 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
68 PyObject_Print(ob,stderr,Py_PRINT_RAW);
71 int ier=PyDict_SetItemString(_context,p->getName().c_str(),ob);
72 DEBTRACE( "after PyDict_SetItemString:ob refcnt: " << ob->ob_refcnt );
75 DEBTRACE( "---------------End PyNode::inputs---------------" );
78 DEBTRACE( "----------------PyNode::calculation---------------" );
80 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
81 PyObject *res=PyRun_String(_script.c_str(),Py_file_input,_context,_context);
82 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
86 PyGILState_Release(gstate);
87 throw Exception("Error during execution");
91 DEBTRACE( "-----------------PyNode::outputs-----------------" );
92 list<OutputPort *>::iterator iter;
95 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
97 OutputPyPort *p=(OutputPyPort *)*iter;
98 DEBTRACE( "port name: " << p->getName() );
99 DEBTRACE( "port kind: " << p->edGetType()->kind() );
100 PyObject *ob=PyDict_GetItemString(_context,p->getName().c_str());
102 PyGILState_Release(gstate);
103 std::string msg="Error during execution: there is no variable ";
104 msg=msg+p->getName()+" in node context";
105 throw Exception(msg);
107 DEBTRACE( "PyNode::outputs::ob refcnt: " << ob->ob_refcnt );
109 PyObject_Print(ob,stderr,Py_PRINT_RAW);
115 catch(ConversionException)
117 PyGILState_Release(gstate);
121 DEBTRACE( "-----------------End PyNode::outputs-----------------" );
122 PyGILState_Release(gstate);
123 DEBTRACE( "++++++++++++++ End PyNode::execute: " << getName() << " ++++++++++++++++++++" );
126 Node *PythonNode::simpleClone(ComposedNode *father, bool editionOnly) const
128 return new PythonNode(*this,father);
131 //! Create a new node of same type with a given name
132 PythonNode* PythonNode::cloneNode(const std::string& name)
134 PythonNode* n=new PythonNode(name);
135 n->setScript(_script);
136 list<InputPort *>::iterator iter;
137 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
139 InputPyPort *p=(InputPyPort *)*iter;
140 DEBTRACE( "port name: " << p->getName() );
141 DEBTRACE( "port kind: " << p->edGetType()->kind() );
142 n->edAddInputPort(p->getName(),p->edGetType());
144 list<OutputPort *>::iterator iter2;
145 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
147 OutputPyPort *p=(OutputPyPort *)*iter2;
148 DEBTRACE( "port name: " << p->getName() );
149 DEBTRACE( "port kind: " << p->edGetType()->kind() );
150 n->edAddOutputPort(p->getName(),p->edGetType());
155 PyFuncNode::PyFuncNode(const PyFuncNode& other, ComposedNode *father):InlineFuncNode(other,father),_pyfunc(0)
157 _implementation = PythonNode::IMPL_NAME;
158 PyGILState_STATE gstate = PyGILState_Ensure();
159 _context=PyDict_New();
160 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
161 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
164 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
165 PyGILState_Release(gstate);
166 throw Exception(msg.str());
168 PyGILState_Release(gstate);
171 PyFuncNode::PyFuncNode(const std::string& name): InlineFuncNode(name),_pyfunc(0)
174 _implementation = PythonNode::IMPL_NAME;
175 DEBTRACE( "PyFuncNode::PyFuncNode " << name );
176 PyGILState_STATE gstate = PyGILState_Ensure();
177 _context=PyDict_New();
178 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
179 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
182 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
183 PyGILState_Release(gstate);
184 throw Exception(msg.str());
186 PyGILState_Release(gstate);
189 PyFuncNode::~PyFuncNode()
191 DEBTRACE( getName() );
192 PyGILState_STATE gstate = PyGILState_Ensure();
193 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
194 if(_pyfunc)DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
196 PyGILState_Release(gstate);
199 void PyFuncNode::load()
201 DEBTRACE( "---------------PyFuncNode::load function " << getName() << " ---------------" );
204 list<OutputPort *>::iterator iter;
205 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
207 OutputPyPort *p=(OutputPyPort *)*iter;
208 DEBTRACE( "port name: " << p->getName() );
209 DEBTRACE( "port kind: " << p->edGetType()->kind() );
212 PyGILState_STATE gstate = PyGILState_Ensure();
213 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
214 PyObject *res=PyRun_String(_script.c_str(),Py_file_input,_context,_context);
215 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
219 PyGILState_Release(gstate);
220 throw Exception("Error during execution");
224 _pyfunc=PyDict_GetItemString(_context,_fname.c_str());
225 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
229 PyGILState_Release(gstate);
230 throw Exception("Error during execution");
232 DEBTRACE( "---------------End PyFuncNode::load function---------------" );
233 PyGILState_Release(gstate);
236 void PyFuncNode::execute()
238 DEBTRACE( "++++++++++++++ PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
241 if(!_pyfunc)throw Exception("PyFuncNode badly loaded");
242 PyGILState_STATE gstate = PyGILState_Ensure();
244 DEBTRACE( "---------------PyFuncNode::inputs---------------" );
245 PyObject* args = PyTuple_New(getNumberOfInputPorts()) ;
246 list<InputPort *>::iterator iter2;
247 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
249 InputPyPort *p=(InputPyPort *)*iter2;
250 DEBTRACE( "port name: " << p->getName() );
251 DEBTRACE( "port kind: " << p->edGetType()->kind() );
254 PyObject_Print(ob,stderr,Py_PRINT_RAW);
257 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
259 PyTuple_SetItem(args,pos,ob);
260 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
263 DEBTRACE( "---------------End PyFuncNode::inputs---------------" );
265 DEBTRACE( "----------------PyFuncNode::calculation---------------" );
267 PyObject_Print(_pyfunc,stderr,Py_PRINT_RAW);
269 PyObject_Print(args,stderr,Py_PRINT_RAW);
272 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
273 PyObject* result = PyObject_CallObject( _pyfunc , args ) ;
274 DEBTRACE( "_pyfunc refcnt: " << _pyfunc->ob_refcnt );
279 PyGILState_Release(gstate);
280 throw Exception("Error during execution");
282 DEBTRACE( "----------------End PyFuncNode::calculation---------------" );
284 DEBTRACE( "-----------------PyFuncNode::outputs-----------------" );
286 if(result == Py_None)
288 else if(PyTuple_Check(result))
289 nres=PyTuple_Size(result);
291 if(getNumberOfOutputPorts() != nres)
294 PyGILState_Release(gstate);
295 throw Exception("Number of output arguments : Mismatch between definition and execution");
300 PyObject_Print(result,stderr,Py_PRINT_RAW);
303 list<OutputPort *>::iterator iter;
306 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
308 OutputPyPort *p=(OutputPyPort *)*iter;
309 DEBTRACE( "port name: " << p->getName() );
310 DEBTRACE( "port kind: " << p->edGetType()->kind() );
311 DEBTRACE( "port pos : " << pos );
312 if(PyTuple_Check(result))ob=PyTuple_GetItem(result,pos) ;
314 DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
316 PyObject_Print(ob,stderr,Py_PRINT_RAW);
323 catch(ConversionException)
326 PyGILState_Release(gstate);
329 DEBTRACE( "-----------------End PyFuncNode::outputs-----------------" );
332 PyGILState_Release(gstate);
333 DEBTRACE( "++++++++++++++ End PyFuncNode::execute: " << getName() << " ++++++++++++++++++++" );
336 Node *PyFuncNode::simpleClone(ComposedNode *father, bool editionOnly) const
338 return new PyFuncNode(*this,father);
341 //! Create a new node of same type with a given name
342 PyFuncNode* PyFuncNode::cloneNode(const std::string& name)
344 PyFuncNode* n=new PyFuncNode(name);
345 n->setScript(_script);
347 list<InputPort *>::iterator iter;
348 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
350 InputPyPort *p=(InputPyPort *)*iter;
351 n->edAddInputPort(p->getName(),p->edGetType());
353 list<OutputPort *>::iterator iter2;
354 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
356 OutputPyPort *p=(OutputPyPort *)*iter2;
357 n->edAddOutputPort(p->getName(),p->edGetType());