]> SALOME platform Git repositories - modules/superv.git/blob - src/GraphExecutor/DataFlowExecutor_PyDynInvoke.cxx
Salome HOME
The final values of InPorts of EndSwitchNodes in a loop may be wrong
[modules/superv.git] / src / GraphExecutor / DataFlowExecutor_PyDynInvoke.cxx
1 //  SUPERV GraphExecutor : contains classes that permit execution of graphs and particularly the execution automaton
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : DataFlowExecutor_PyDynInvoke.cxx
8 //  Author : Jean Rahuel, CEA
9 //  Module : SUPERV
10 //  $Header:
11
12 using namespace std;
13
14 #include <stdarg.h>
15 #include <map>
16
17 #include "DataFlowExecutor_InNode.hxx"
18 #include "DataFlowExecutor_OutNode.hxx"
19
20 using namespace CORBA ;
21
22 //static PyObject * MyPyObjSignal = NULL ;
23 static PyObject * MyPyObjRef = NULL ;
24 static PyObject * MyPyObjIor = NULL ;
25 static PyObject * MyPyRunMethod = NULL ;
26
27 static PyMethodDef MethodPyRunMethod[] = {
28   { "RunMethod", PyRunMethod, METH_VARARGS },
29   { NULL,        NULL }
30 };
31
32 bool GraphExecutor::InNode::InitPython() {
33   cdebug_in << "InitPython" << endl ;
34   
35   Automaton()->PyLock() ;
36 //  PyObject * Module = Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod ) ;
37   if ( !Automaton()->PyInitModule() ) {
38 //    Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod ) ;
39     _OutNode->SuperVisionContainer()->ActivatePythonExecution( "InitPyRunMethod" , MethodPyRunMethod ) ;
40   }
41 //  PyObject * Dictionnary = PyModule_GetDict( Module ) ;
42 //  InitPyDynInvokeError = PyErr_NewException( "InitPyDynInvokeError" , NULL , NULL ) ;
43 //  PyDict_SetItemString( Dictionnary , InitPyDynInvokeError ) ;
44   cdebug << ThreadNo() << "Py_Initialized() " << endl ;
45
46 #if 0
47 //JR : Python documentation says that there is no sense to set a handler of synchronous
48 // signals as SIGSEGV and SIGFPE !!!
49   if ( Automaton()->PyFunction( "PyObjSignal" ) == NULL ) {
50     string aPyFunc ;
51     aPyFunc = "import signal\n" ;
52     aPyFunc += "import InitPyRunMethod\n" ;
53     aPyFunc += "import sys\n" ;
54     aPyFunc += "import CORBA\n" ;
55     aPyFunc += "import omniORB\n" ;
56     aPyFunc += "def PySignalHandler( aSignal , aStackFrame ) :\n" ;
57     aPyFunc += "    print 'PySignalHandler(aSignal =',aSignal,',aStackFrame= ',aStackFrame,')'\n" ;
58     aPyFunc += "InitPyRunMethod.RunMethod( PySignalHandler )\n" ;
59     aPyFunc += "PrevHandler = signal.signal( signal.SIGSEGV , PySignalHandler )\n" ;
60     aPyFunc += "print 'PyObjSignal PrevHandler of SIGSEGV :', PrevHandler\n" ;
61     aPyFunc += "print 'PyObjSignal actual handler of SIGSEGV :', signal.getsignal( signal.SIGSEGV  )\n" ;
62 //    if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
63     if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) {
64       cdebug << ThreadNo() << " " << Name() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ;
65       Automaton()->PyUnLock() ;
66       return false ;
67     }
68     MyPyObjSignal = MyPyRunMethod ;
69     Automaton()->PyFunction( "PyObjSignal" , MyPyObjSignal ) ;
70   }
71   else {
72     MyPyObjSignal = Automaton()->PyFunction( "PyObjSignal" ) ;
73   }
74 #endif
75
76 // PyObjRef convert an IOR (string) to an Python ObjectReference
77   if ( Automaton()->PyFunction( "PyObjRef" ) == NULL ) {
78     string aPyFunc ;
79     aPyFunc = "import InitPyRunMethod\n" ;
80     aPyFunc += "import sys\n" ;
81     aPyFunc += "import CORBA\n" ;
82     aPyFunc += "import omniORB\n" ;
83     aPyFunc += "def PyObjRef( IORObjStr ) :\n" ;
84     aPyFunc += "    orb = CORBA.ORB_init( sys.argv , CORBA.ORB_ID )\n" ;
85     aPyFunc += "    objref = orb.string_to_object( IORObjStr )\n" ;
86     aPyFunc += "    return objref\n" ;
87     aPyFunc += "InitPyRunMethod.RunMethod( PyObjRef )\n" ;
88 //    if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
89     if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) {
90       cdebug << ThreadNo() << " " << Name() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ;
91       Automaton()->PyUnLock() ;
92       return false ;
93     }
94     MyPyObjRef = MyPyRunMethod ;
95     Automaton()->PyFunction( "PyObjRef" , MyPyObjRef ) ;
96   }
97   else {
98     MyPyObjRef = Automaton()->PyFunction( "PyObjRef" ) ;
99   }
100
101 // PyObjRef convert an Python ObjectReference to an IOR (string)
102   if ( Automaton()->PyFunction( "PyObjIor" ) == NULL ) {
103     string aPyFunc ;
104     aPyFunc = "import InitPyRunMethod\n" ;
105     aPyFunc += "import sys\n" ;
106     aPyFunc += "import CORBA\n" ;
107     aPyFunc += "import omniORB\n" ;
108     aPyFunc += "def PyObjIor( ObjRef ) :\n" ;
109     aPyFunc += "    orb = CORBA.ORB_init( sys.argv , CORBA.ORB_ID )\n" ;
110     aPyFunc += "    objIor = orb.object_to_string( ObjRef )\n" ;
111     aPyFunc += "    return objIor\n" ;
112     aPyFunc += "InitPyRunMethod.RunMethod( PyObjIor )\n" ;
113 //    if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
114     if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) {
115       cdebug << ThreadNo() << " " << Name() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ;
116       Automaton()->PyUnLock() ;
117       return false ;
118     }
119     MyPyObjIor = MyPyRunMethod ;
120     Automaton()->PyFunction( "PyObjIor" , MyPyObjIor ) ;
121   }
122   else {
123     MyPyObjIor = Automaton()->PyFunction( "PyObjIor" ) ;
124   }
125   Automaton()->PyUnLock() ;
126
127   cdebug_out << "InitPython" << endl ;
128   
129   return true ;
130 }
131
132 void GraphExecutor::InNode::RemovePyDynInvoke( char * PyFuncName ) {
133 //  Automaton()->PyLock() ; // Already done ...
134   if ( Automaton()->ErasePyFunction( PyFuncName ) ) {
135     cdebug << "InNode::RemovePyDynInvoke( " << PyFuncName << " ) success" << endl ;
136   }
137   else {
138     cdebug << "InNode::RemovePyDynInvoke( " << PyFuncName << " ) ERROR failed" << endl ;
139   }
140 //  Automaton()->PyUnLock() ;
141 }
142
143 PyObject * GraphExecutor::InNode::InitPyDynInvoke( char * PyFuncName ,
144                                                    const SUPERV::ListOfStrings * aPythonFunction ,
145                                                    bool & Err ) {
146   bool RetVal = true ;
147   Err = false ;
148   string aPyFunc ;
149   PyObject * thePyRunMethod = NULL ;
150
151   cdebug_in << "InitPyDynInvoke '" << PyFuncName << "' length " << (*aPythonFunction).length() << endl ;
152
153   if ( strlen( PyFuncName ) ) {
154     Automaton()->PyLock() ;
155     thePyRunMethod = Automaton()->PyFunction( PyFuncName ) ;
156     if ( (*aPythonFunction).length() ) {
157       if ( thePyRunMethod == NULL ) {
158         unsigned int i ;
159         aPyFunc += "import InitPyRunMethod\n" ;
160         for ( i = 0 ; i < (*aPythonFunction).length() ; i++ ) {
161           aPyFunc += (*aPythonFunction)[ i ] ;
162           aPyFunc += "\n" ;
163         }
164         aPyFunc += "InitPyRunMethod.RunMethod( " ;
165         aPyFunc += PyFuncName ;
166         aPyFunc += " )\n" ;
167         /*cdebug *///cout<< "InitPyDynInvoke PyRun_SimpleString " << endl << aPyFunc << endl ;
168 //        if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
169         if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) {
170           cdebug << ThreadNo() << " " << Name() << " PyRunSimpleString ERROR " << endl << aPyFunc << endl ;
171           PyFuncRunned( false ) ;
172           RetVal = false ;
173           Err = true ;
174         }
175         else {
176           PyFuncRunned( true ) ;
177           thePyRunMethod = MyPyRunMethod ;
178           
179           Automaton()->PyFunction( PyFuncName , thePyRunMethod ) ;
180           cdebug << ThreadNo() << "PyRunSimpleString( " << (*aPythonFunction)[ 0 ] << " ) " << endl ;
181         }
182       }
183       if ( RetVal ) {
184         Automaton()->PyUnLock() ;
185         cdebug_out << "InitPyDynInvoke '" << PyFuncName << "' thePyRunMethod " << thePyRunMethod << " "
186                    << thePyRunMethod->ob_refcnt << endl ;
187         return thePyRunMethod ;
188       }
189     }
190     else if ( thePyRunMethod ) {
191       Automaton()->PyUnLock() ;
192       cdebug_out << "InitPyDynInvoke '" << PyFuncName << "' thePyRunMethod " << thePyRunMethod << " "
193                  << thePyRunMethod->ob_refcnt << endl ;
194       return thePyRunMethod ;
195     }
196     Automaton()->PyUnLock() ;
197   }
198
199   cdebug_out << "InitPyDynInvoke '" << PyFuncName << "' thePyRunMethod Null Err " << Err << endl ;
200   
201   return NULL ;
202 }
203
204 extern "C" PyObject * PyRunMethod( PyObject * dummy , PyObject * Args ) {
205   PyObject * Result = NULL ;
206   PyObject * Temp = NULL ;
207   if ( PyArg_ParseTuple( Args , "O:set_callback" , & Temp ) ) {
208 //    cout << "PyArg_ParsedTuple" << endl ;
209     if ( !PyCallable_Check( Temp ) ) {
210 //      PyErr_SetString( PyExc_TypeError , "PyRunMethod must be callable" ) ;
211 //      cout << "PyRunMethod must be callable" << endl ;
212       return NULL ;
213     }
214     Py_XINCREF( Temp ) ;
215     Py_XDECREF( MyPyRunMethod ) ;
216     MyPyRunMethod = Temp ;
217     Py_INCREF( Py_None ) ;
218     Result = Py_None ;
219   }
220   return Result ;
221 }
222
223 bool GraphExecutor::InNode::PyDynInvoke( PyObject * MyPyRunMethod ,
224                                          const char *method , 
225                                          ServicesAnyData * inParams , int nInParams ,
226                                          ServicesAnyData * outParams , int nOutParams ) {
227   int i ;
228
229   bool RetVal = true ;
230
231   int n_in  = nInParams ;
232   int n_out = nOutParams ;
233   const char * sname;
234
235   cdebug_in << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name() << " method " << method
236             << " " << n_in << " InArgs " << n_out << " OutArgs MyPyRunMethod " ;
237   if ( MyPyRunMethod ) {
238     cdebug << MyPyRunMethod << " " << MyPyRunMethod->ob_refcnt << endl ;
239   }
240   else {
241     cdebug << " NULL" << endl ;
242     return false ;
243   }
244
245   CORBA::Any data ;
246
247   PyObject * ArgsList = NULL ;
248   PyObject * ArgValue = NULL ;
249   PyObject * Result = NULL ;
250   PyObject * MyPyObjRefList = NULL ;
251   PyObject * ResultObj = NULL ;
252   PyObject * MyPyObjIorList = NULL ;
253   PyObject * ResultIor = NULL ;
254
255   CORBA::Object_ptr ObjRef ;
256   char * IORObjRef ;
257
258   if ( !PyCallable_Check( MyPyRunMethod ) ) {
259     Result = NULL ;
260     RetVal = false ;
261   }
262   else {
263     ArgsList = PyTuple_New( n_in ) ;
264
265     for ( i = 0 ; i < n_in ; i++ ) {
266       data = inParams[i].Value ;
267       sname = inParams[i].Name.c_str() ;
268       switch ( data.type()->kind() ) {
269       case CORBA::tk_string : {
270         char * t ;
271         data >>= t ;
272         ArgValue = Py_BuildValue( "s" , t ) ;
273         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
274         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << t << " (string) "
275                << " ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
276                << ArgValue->ob_refcnt << endl ;
277         break ;
278       }
279       case CORBA::tk_boolean : {
280         bool b ;
281         data >>= (CORBA::Any::to_boolean ) b ;
282         ArgValue = Py_BuildValue( "b" , b ) ;
283         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
284         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << b
285                << " (boolean) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
286                << ArgValue->ob_refcnt << endl ;
287         break ;
288       }
289       case CORBA::tk_char : {
290         unsigned char c ;
291         data >>= (CORBA::Any::to_char ) c ;
292         ArgValue = Py_BuildValue( "c" , c ) ;
293         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
294         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << c
295                << " (char) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
296                << ArgValue->ob_refcnt << endl ;
297         break ;
298       }
299       case CORBA::tk_short : {
300         short s ;
301         data >>= s ;
302         ArgValue = Py_BuildValue( "h" , s ) ;
303         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
304         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << s
305                << " (short) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
306                << ArgValue->ob_refcnt << endl ;
307         break ;
308       }
309       case CORBA::tk_long : {
310         long l ;
311         data >>= l ;
312         ArgValue = Py_BuildValue( "l" , l ) ;
313         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
314         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << l
315                << " (long) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
316                << ArgValue->ob_refcnt << endl ;
317         break ;
318       }
319       case CORBA::tk_float : {
320         float f ;
321         data >>= f ;
322         ArgValue = Py_BuildValue( "f" , f ) ;
323         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
324         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << f
325                << " (float) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
326                << ArgValue->ob_refcnt << endl ;
327         break ;
328       }
329       case CORBA::tk_double : {
330         double d ;
331         data >>= d ;
332         ArgValue = Py_BuildValue( "d" , d ) ;
333         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
334         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << d
335                << " (double) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
336                << ArgValue->ob_refcnt << endl ;
337         break ;
338       }
339       case CORBA::tk_objref : {
340         MyPyObjRefList = PyTuple_New( 1 ) ;
341         PyObject * ObjValue ;
342         data >>= ObjRef ;
343         IORObjRef = ObjectToString( ObjRef ) ;
344         ObjValue = Py_BuildValue( "s" , IORObjRef ) ;
345         PyTuple_SetItem( MyPyObjRefList , 0 , ObjValue ) ;
346         cdebug << "ArgIn" << i << " : " << sname << " " << method << " " << " Value " << IORObjRef << " (objref) "
347                << MyPyObjRef->ob_refcnt << "/" << MyPyObjRefList->ob_refcnt << endl ;
348 //        ResultObj = PyEval_CallObject( MyPyObjRef , MyPyObjRefList ) ;
349         ResultObj = PyEvalCallObject( MyPyObjRef , MyPyObjRefList ) ;
350         cdebug << "ObjValue->ob_refcnt" << ObjValue->ob_refcnt << endl ;
351         ArgValue = Py_BuildValue( "O" , ResultObj ) ;
352         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
353         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value  (objref) ArgsList->ob_refcnt"
354                << ArgsList->ob_refcnt << " ArgValue->ob_refcnt" << ArgValue->ob_refcnt << endl ;
355         cdebug << "MyPyObjRefList->ob_refcnt " << MyPyObjRefList->ob_refcnt-1 << endl ;
356         Py_DECREF( MyPyObjRefList ) ;
357         if ( CORBA::is_nil( ObjRef ) ) {
358           ResultObj = NULL ;
359         }
360         else {
361           cdebug << "ResultObj->ob_refcnt " << ResultObj->ob_refcnt-1 << endl ;
362           Py_DECREF( ResultObj ) ;
363         }
364         break ;
365       }
366       default : {
367         cdebug << "ArgIn" << i << " : " << sname << " Value " << "(other ERROR)" << endl ;
368       }
369       }
370     }
371
372 //    Result = PyEval_CallObject( MyPyRunMethod , ArgsList ) ;
373     Result = PyEvalCallObject( MyPyRunMethod , ArgsList ) ;
374
375     cdebug << "ArgsList->ob_refcnt" << ArgsList->ob_refcnt << endl ;
376
377     if ( Result == NULL ) {
378       cdebug_out << "GraphExecutor::InNode::PyDynInvoke Node " << Name() << " " << method << " Error Result == NULL"
379                  << endl ;
380       RetVal = false ;
381     }
382     else {
383       for ( i = 0 ; i < n_out ; i++ ) {
384         data = outParams[i].Value ;
385         sname = outParams[i].Name.c_str() ;
386         switch ( data.type()->kind() ) {
387         case CORBA::tk_string : {
388           char * t = "" ;
389           if ( PyTuple_Check( Result ) ) {
390             ArgValue = PyTuple_GetItem( Result , i ) ;
391           }
392           else {
393             ArgValue = Result ;
394           }
395           if ( !PyString_Check( ArgValue ) ) {
396             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (string)" << endl ;
397           }
398           else {
399             t = PyString_AsString( ArgValue ) ;
400           }
401           data <<= t ;
402           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << t << " (string)"
403                  << " ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
404                  << ArgValue->ob_refcnt << endl ;
405           break ;
406         }
407         case CORBA::tk_boolean : {
408           bool b = false ;
409           if ( PyTuple_Check( Result ) ) {
410             ArgValue = PyTuple_GetItem( Result , i ) ;
411           }
412           else {
413             ArgValue = Result ;
414           }
415           if ( !PyInt_Check( ArgValue ) ) {
416             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (boolean)" << endl ;
417           }
418           else {
419             b = PyInt_AsLong( ArgValue ) ;
420           }
421           data <<= (CORBA::Any::from_boolean ) b ;
422           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << b << " (boolean)"
423                  << " ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
424                  << ArgValue->ob_refcnt << endl ;
425           break ;
426         }
427         case CORBA::tk_char : {
428           unsigned char c = 0 ;
429           if ( PyTuple_Check( Result ) ) {
430             ArgValue = PyTuple_GetItem( Result , i ) ;
431           }
432           else {
433             ArgValue = Result ;
434           }
435           if ( !PyInt_Check( ArgValue ) ) {
436             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (char)" << endl ;
437           }
438           else {
439             c = PyInt_AsLong( ArgValue ) ;
440           }
441           data <<= (CORBA::Any::from_char ) c ;
442           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << c << " (char)"
443                  << " ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
444                  << ArgValue->ob_refcnt << endl ;
445           break ;
446         }
447         case CORBA::tk_short : {
448           short s = 0 ;
449           if ( PyTuple_Check( Result ) ) {
450             ArgValue = PyTuple_GetItem( Result , i ) ;
451           }
452           else {
453             ArgValue = Result ;
454           }
455           if ( !PyInt_Check( ArgValue ) ) {
456             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (short)" << endl ;
457           }
458           else {
459             s = PyInt_AsLong( ArgValue ) ;
460           }
461           data <<= s ;
462           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << s << " (short)"
463                  << " ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
464                  << ArgValue->ob_refcnt << endl ;
465           break ;
466         }
467         case CORBA::tk_long : {
468           long l = 0 ;
469           if ( PyTuple_Check( Result ) ) {
470             ArgValue = PyTuple_GetItem( Result , i ) ;
471           }
472           else {
473             ArgValue = Result ;
474           }
475           if ( PyLong_Check( ArgValue ) ) {
476             l = PyLong_AsLong( ArgValue ) ;
477           }
478           else if ( PyInt_Check( ArgValue ) ) {
479             l = PyInt_AsLong( ArgValue ) ;
480           }
481           else {
482             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (CORBA::tk_long)" << endl ;
483           }
484           data <<= l ;
485           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << l << " (long)"
486                  << " ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
487                  << ArgValue->ob_refcnt << endl ;
488           break ;
489         }
490         case CORBA::tk_float : {
491           float f = 0 ;
492           if ( PyTuple_Check( Result ) ) {
493             ArgValue = PyTuple_GetItem( Result , i ) ;
494           }
495           else {
496             ArgValue = Result ;
497           }
498           if ( !PyFloat_Check( ArgValue ) ) {
499             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (float)" << endl ;
500           }
501           else {
502             f = PyFloat_AsDouble( ArgValue ) ;
503           }
504           data <<= f ;
505           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << f << " (float)"
506                  << " ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
507                  << ArgValue->ob_refcnt << endl ;
508           break ;
509         }
510         case CORBA::tk_double : {
511           double d = 0 ;
512           if ( PyTuple_Check( Result ) ) {
513             ArgValue = PyTuple_GetItem( Result , i ) ;
514           }
515           else {
516             ArgValue = Result ;
517           }
518           if ( !PyFloat_Check( ArgValue ) ) {
519             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (double)" << endl ;
520           }
521           else {
522             d = PyFloat_AsDouble( ArgValue ) ;
523           }
524           data <<= d ;
525           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << d << " (double)"
526                  << " ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
527                  << ArgValue->ob_refcnt << endl ;
528           break ;
529         }
530         case CORBA::tk_objref : {
531           PyObject * ObjIor ;
532           MyPyObjIorList = PyTuple_New( 1 ) ;
533           if ( PyTuple_Check( Result ) ) {
534             ObjIor = PyTuple_GetItem( Result , i ) ;
535           }
536           else {
537             ObjIor = Result ;
538           }
539           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << "(object reference) "
540                  << " ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ObjIor->ob_refcnt"
541                  << ObjIor->ob_refcnt << endl ;
542           Py_INCREF( ObjIor ) ;
543 //          PyObject_Print( ObjIor , stdout , 0 ) ;
544           PyTuple_SetItem( MyPyObjIorList , 0 , ObjIor ) ;
545 //          ResultIor = PyEval_CallObject( MyPyObjIor , MyPyObjIorList ) ;
546           ResultIor = PyEvalCallObject( MyPyObjIor , MyPyObjIorList ) ;
547           cdebug << "ObjIor->ob_refcnt " << ObjIor->ob_refcnt-1 << endl ;
548           Py_DECREF( ObjIor ) ;
549           cdebug << "MyPyObjIorList->ob_refcnt " << MyPyObjIorList->ob_refcnt-1 << endl ;
550           Py_DECREF( MyPyObjIorList ) ;
551           cdebug << "MyPyObjIor->ob_refcnt " << MyPyObjIor->ob_refcnt << endl ;
552           if ( ResultIor ) {
553             char * IOR = NULL ;
554             IOR = PyString_AsString( ResultIor ) ;
555             ObjRef = StringToObject( IOR ) ;
556             data <<= ObjRef ;
557             IORObjRef = ObjectToString( ObjRef ) ;
558             cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << IORObjRef << " (objref) "
559                    << endl ;
560             if ( CORBA::is_nil( ObjRef ) ) {
561               ResultIor = NULL ;
562             }
563             else {
564               cdebug << "ResultIor->ob_refcnt " << ResultIor->ob_refcnt-1 << endl ;
565               Py_DECREF( ResultIor ) ;
566             }
567           }
568           else {
569             cdebug_out << "GraphExecutor::InNode::PyDynInvoke Error ResultIor == NULL Node "
570                        << Name() << " method " << method << " " << endl ;
571             RetVal = false ;
572           }
573           break ;
574         }
575         default : {
576           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << "(other ERROR)" << endl ;
577         }
578         }
579         outParams[i].Value = data ;
580       }
581
582       cdebug << "Result->ob_refcnt" << Result->ob_refcnt-1 << endl ;
583       Py_DECREF( Result ) ;
584     }
585
586     cdebug << "GraphExecutor::InNode::PyDynInvoke ArgsList->ob_refcnt"
587            << ArgsList->ob_refcnt-1 << endl ;
588     Py_DECREF( ArgsList ) ;
589   }
590
591   cdebug_out << "GraphExecutor::InNode::PyDynInvoke Node " << Name() << " method " << method << " " << RetVal
592              << endl ;
593
594   return RetVal ;
595
596 }
597
598 bool GraphExecutor::InNode::PyRunSimpleString( char* thePyString )
599 {
600   const bool ErrorValue = true;
601   bool aRet;
602   try {
603     MESSAGE( pthread_self() << "Python method beginning : " << thePyString );
604     cdebug_in << pthread_self() << "Python method beginning : " << thePyString << endl ;
605 //    aRet = PyRun_SimpleString( thePyString );
606     aRet = _OutNode->SuperVisionContainer()->ActivatePythonExecution( thePyString ) ;
607     MESSAGE( pthread_self() << "Python method finished." );
608     cdebug_out << pthread_self() << "Python method finished." << endl ;
609   } catch( ... ) {
610     MESSAGE( pthread_self() << "ERROR: Exception caught running Python method." );
611     cdebug_out << pthread_self() << "ERROR: Exception caught running Python method."
612                << endl ;
613     MESSAGE( "       Python was reinitialized.  Previous Python definitions are lost" );
614 //    Py_Finalize();
615 //    Py_Initialize();
616 //    Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod );
617     _OutNode->PyInitialized( false );
618     aRet = ErrorValue;
619   }
620   return aRet;
621 }
622
623 PyObject * GraphExecutor::InNode::PyEvalCallObject( PyObject * MyPyRunMethod ,
624                                                     PyObject * ArgsList ) {
625   cdebug_in << "Executor::InNode::PyEvalCallObject " << Name() << endl ;
626   PyObject * Result = NULL ;
627   try {
628     MESSAGE( pthread_self() << "PyEval_CallObject method beginning : " );
629     cdebug << pthread_self() << "PyEval_CallObject method beginning : " << Name() << endl ;
630 //    Result = PyEval_CallObject( MyPyRunMethod , ArgsList ) ;
631     Result = _OutNode->SuperVisionContainer()->ActivatePythonExecution( MyPyRunMethod , ArgsList ) ;
632     MESSAGE( pthread_self() << "PyEval_CallObject method finished. Result " << Result );
633     cdebug << pthread_self() << "PyEval_CallObject method finished. Result " << Result << endl ;
634     cdebug_out << "Executor::InNode::PyEvalCallObject " << Name() << endl ;
635   } catch( ... ) {
636     MESSAGE( pthread_self() << "ERROR: Exception caught PyEval_CallObject Python method. Result "  << Result );
637     cdebug << pthread_self() << "ERROR: Exception caught PyEval_CallObject Python method. Result "
638            << Result << endl ;
639 //    MESSAGE( "       Python was reinitialized.  Previous Python definitions are lost Py_IsInitialized " << Py_IsInitialized() );
640 //JR ===> fatal error in python : no current thread
641 //    Py_Finalize();
642 //    Py_Initialize();
643 //    Automaton()->PyInitModule( false ) ;
644 //    Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod );
645 //    _OutNode->PyInitialized( false );
646     if ( Result == NULL ) {
647       Kill() ; // Reset of _ThreadId
648     }
649     cdebug_out << "Executor::InNode::PyEvalCallObject ERROR catched " << Name()
650                << " Py_IsInitialized " << Py_IsInitialized() << endl ;
651   }
652   return Result ;
653 }