Salome HOME
PR: merge from BR_DATACONV_PR tag "mergeto_trunk_25oct06"
[modules/yacs.git] / src / runtime / PythonNode.cxx
1
2 #include "PythonNode.hxx"
3 #include "RuntimeSALOME.hxx"
4
5 #include <iostream>
6 #include <sstream>
7
8 using namespace YACS::ENGINE;
9 using namespace std;
10
11 PythonNode::PythonNode(const string& name): ElementaryNode(name)
12 {
13   _implementation = "Python";
14   cerr << "PythonNode::PythonNode " << name << endl;
15 }
16
17 void PythonNode::set_script(const string& script)
18 {
19   _script = script;
20 }
21
22 void PythonNode::execute()
23 {
24   cerr << "PyNode::run" << endl;
25   PyObject* context=PyDict_New();
26   if( PyDict_SetItemString( context, "__builtins__", getSALOMERuntime()->getBuiltins() ))
27     {
28       stringstream msg;
29       msg << "Impossible to set builtins" << __FILE__ << ":" << __LINE__;
30       throw Exception(msg.str());
31     }
32
33   cerr << "---------------PyNode::inputs---------------" << endl;
34   set<InputPort *>::iterator iter2;
35   for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
36     {
37       InputPyPort *p=(InputPyPort *)*iter2;
38       cerr << "port name: " << p->getName() << endl;
39       cerr << "port kind: " << p->type()->kind() << endl;
40       PyObject* ob=p->getPyObj();
41       cerr << "ob refcnt: " << ob->ob_refcnt << endl;
42       PyObject_Print(ob,stdout,Py_PRINT_RAW);
43       cerr << endl;
44       int ier=PyDict_SetItemString(context,p->getName().c_str(),ob);
45       cerr << "ob refcnt: " << ob->ob_refcnt << endl;
46     }
47   
48   cerr << "---------------End PyNode::inputs---------------" << endl;
49   
50   //calculation
51   cerr << "PyNode::calculation " << _script << endl;
52   PyObject *res=PyRun_String(_script.c_str(),Py_file_input,context,context);
53   if(res == NULL)
54     {
55       PyErr_Print();
56       return;
57     }
58   Py_DECREF(res);
59   
60   cerr << "-----------------PyNode::outputs-----------------" << endl;
61   set<OutputPort *>::iterator iter;
62   for(iter = _setOfOutputPort.begin(); iter != _setOfOutputPort.end(); iter++)
63     {
64       OutputPyPort *p=(OutputPyPort *)*iter;
65       cerr << "port name: " << p->getName() << endl;
66       cerr << "port kind: " << p->type()->kind() << endl;
67       PyObject *ob=PyDict_GetItemString(context,p->getName().c_str());
68       cerr << "ob refcnt: " << ob->ob_refcnt << endl;
69       PyObject_Print(ob,stdout,Py_PRINT_RAW);
70       cerr << endl;
71       //Faut-il incrémenter ici ?
72       Py_INCREF(ob);
73       cerr << "ob refcnt: " << ob->ob_refcnt << endl;
74       p->put(ob);
75     }
76
77   cerr << "-----------------End PyNode::outputs-----------------" << endl;
78   Py_DECREF(context);
79 }