1 #include "RuntimeSALOME.hxx"
2 #include "SalomePythonComponent.hxx"
3 #include "SalomePythonNode.hxx"
4 #include "PythonNode.hxx"
5 #include "PythonPorts.hxx"
6 #include "CORBANode.hxx"
7 #include "TypeCode.hxx"
12 using namespace YACS::ENGINE;
15 const char SalomePythonNode::PLACEMENT_VAR_NAME_IN_INTERP[]="__container__from__YACS__";
17 SalomePythonNode::SalomePythonNode(const SalomePythonNode& other, ComposedNode *father):ServiceInlineNode(other,father),_pyfunc(0),_context(0)
19 //Not a bug : just because port point of view this is like PythonNode.
20 _implementation = PythonNode::IMPL_NAME;
21 PyGILState_STATE gstate = PyGILState_Ensure();
22 _context=PyDict_New();
23 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
26 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
27 PyGILState_Release(gstate);
28 throw Exception(msg.str());
30 PyGILState_Release(gstate);
33 SalomePythonNode::SalomePythonNode(const std::string& name): ServiceInlineNode(name),_pyfunc(0)
36 //Not a bug : just because port point of view this is like PythonNode.
37 _implementation = PythonNode::IMPL_NAME;
38 PyGILState_STATE gstate = PyGILState_Ensure();
39 _context=PyDict_New();
40 if( PyDict_SetItemString( _context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
43 msg << "Not possible to set builtins" << __FILE__ << ":" << __LINE__;
44 PyGILState_Release(gstate);
45 throw Exception(msg.str());
47 PyGILState_Release(gstate);
50 void SalomePythonNode::load()
52 ServiceInlineNode::load();
53 cerr << "---------------SalomePythonNode::load function---------------" << endl;
54 list<OutputPort *>::iterator iter;
55 string value2Export=((SalomePythonComponent*)_component)->getStringValueToExportInInterp();
56 PyObject* ob=PyString_FromString(value2Export.c_str());
57 PyDict_SetItemString(_context,PLACEMENT_VAR_NAME_IN_INTERP,ob);
58 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
60 OutputPyPort *p=(OutputPyPort *)*iter;
61 cerr << "port name: " << p->getName() << endl;
62 cerr << "port kind: " << p->edGetType()->kind() << endl;
64 cerr << _script << endl;
65 PyGILState_STATE gstate = PyGILState_Ensure();
66 PyObject *res=PyRun_String(_script.c_str(),Py_file_input,_context,_context);
70 PyGILState_Release(gstate);
71 throw Exception("Error during execution");
75 _pyfunc=PyDict_GetItemString(_context,_method.c_str());
79 PyGILState_Release(gstate);
80 throw Exception("Error during execution");
82 cerr << "---------------End SalomePythonNode::load function---------------" << endl;
83 PyGILState_Release(gstate);
86 void SalomePythonNode::execute()
88 cerr << "++++++++++++++ SalomePythonNode::execute: " << getName() << " ++++++++++++++++++++" << endl;
91 if(!_pyfunc)throw Exception("SalomePythonNode badly loaded");
92 PyGILState_STATE gstate = PyGILState_Ensure();
94 cerr << "---------------SalomePythonNode::inputs---------------" << endl;
95 PyObject* args = PyTuple_New(getNumberOfInputPorts()) ;
96 list<InputPort *>::iterator iter2;
97 for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
99 InputPyPort *p=(InputPyPort *)*iter2;
100 cerr << "port name: " << p->getName() << endl;
101 cerr << "port kind: " << p->edGetType()->kind() << endl;
103 PyObject_Print(ob,stderr,Py_PRINT_RAW);
105 cerr << "ob refcnt: " << ob->ob_refcnt << endl;
107 PyTuple_SetItem(args,pos,ob);
108 cerr << "ob refcnt: " << ob->ob_refcnt << endl;
111 cerr << "---------------End SalomePythonNode::inputs---------------" << endl;
113 cerr << "----------------SalomePythonNode::calculation---------------" << endl;
114 PyObject_Print(_pyfunc,stderr,Py_PRINT_RAW);
116 PyObject_Print(args,stderr,Py_PRINT_RAW);
118 PyObject* result = PyObject_CallObject( _pyfunc , args ) ;
123 PyGILState_Release(gstate);
124 throw Exception("Error during execution");
126 cerr << "----------------End SalomePythonNode::calculation---------------" << endl;
128 cerr << "-----------------SalomePythonNode::outputs-----------------" << endl;
130 if(result == Py_None)
132 else if(PyTuple_Check(result))
133 nres=PyTuple_Size(result);
135 if(getNumberOfOutputPorts() != nres)
138 PyGILState_Release(gstate);
139 throw Exception("Number of output arguments : Mismatch between definition and execution");
143 PyObject_Print(result,stderr,Py_PRINT_RAW);
145 list<OutputPort *>::iterator iter;
148 for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
150 OutputPyPort *p=(OutputPyPort *)*iter;
151 cerr << "port name: " << p->getName() << endl;
152 cerr << "port kind: " << p->edGetType()->kind() << endl;
153 cerr << "port pos : " << pos << endl;
154 if(PyTuple_Check(result))ob=PyTuple_GetItem(result,pos) ;
156 cerr << "ob refcnt: " << ob->ob_refcnt << endl;
157 PyObject_Print(ob,stderr,Py_PRINT_RAW);
163 catch(ConversionException)
166 PyGILState_Release(gstate);
169 cerr << "-----------------End SalomePythonNode::outputs-----------------" << endl;
172 PyGILState_Release(gstate);
173 cerr << "++++++++++++++ End SalomePythonNode::execute: " << getName() << " ++++++++++++++++++++" << endl;
176 std::string SalomePythonNode::getKind() const
178 //This not a bug !!! Returns SalomeNode::KIND to be managed by SalomeContainer.
179 return SalomeNode::KIND;
182 Node *SalomePythonNode::simpleClone(ComposedNode *father, bool editionOnly) const
184 return new SalomePythonNode(*this,father);
187 ServiceNode *SalomePythonNode::createNode(const std::string &name)
189 ServiceNode* node=new SalomePythonNode(name);
190 node->setComponent(_component);
194 //! Create a new node of same type with a given name
195 SalomePythonNode* SalomePythonNode::cloneNode(const std::string& name)
197 SalomePythonNode* n=new SalomePythonNode(name);
198 n->setScript(_script);
199 n->setMethod(_method);
200 list<InputPort *>::iterator iter;
201 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
203 InputPyPort *p=(InputPyPort *)*iter;
204 n->edAddInputPort(p->getName(),p->edGetType());
206 list<OutputPort *>::iterator iter2;
207 for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
209 OutputPyPort *p=(OutputPyPort *)*iter2;
210 n->edAddOutputPort(p->getName(),p->edGetType());