Salome HOME
The order of deletion of Ports (during the deletion of nodes) was 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 extern GraphExecutor::FiniteStateMachine * theAutomaton ;
23
24 //static PyObject * MyPyObjSignal = NULL ;
25 static PyObject * MyPyObjRef = NULL ;
26 static PyObject * MyPyObjIor = NULL ;
27 static PyObject * MyPyRunMethod = NULL ;
28
29 static PyMethodDef MethodPyRunMethod[] = {
30   { "RunMethod", PyRunMethod, METH_VARARGS },
31   { NULL,        NULL }
32 };
33
34 bool GraphExecutor::InNode::InitPython() {
35   cdebug_in << "InitPython" << endl ;
36   
37   Automaton()->PyLock() ;
38 //  PyObject * Module = Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod ) ;
39   if ( !Automaton()->PyInitModule() ) {
40     Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod ) ;
41     // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL    
42     //_OutNode->SuperVisionContainer()->ActivatePythonExecution( "InitPyRunMethod" , MethodPyRunMethod ) ;
43   }
44 //  PyObject * Dictionnary = PyModule_GetDict( Module ) ;
45 //  InitPyDynInvokeError = PyErr_NewException( "InitPyDynInvokeError" , NULL , NULL ) ;
46 //  PyDict_SetItemString( Dictionnary , InitPyDynInvokeError ) ;
47   cdebug << ThreadNo() << "Py_Initialized() " << endl ;
48
49 #if 0
50 //JR : Python documentation says that there is no sense to set a handler of synchronous
51 // signals as SIGSEGV and SIGFPE !!!
52   if ( Automaton()->PyFunction( "PyObjSignal" ) == NULL ) {
53     string aPyFunc ;
54     aPyFunc = "import signal\n" ;
55     aPyFunc += "import InitPyRunMethod\n" ;
56     aPyFunc += "import sys\n" ;
57     aPyFunc += "import CORBA\n" ;
58     aPyFunc += "import omniORB\n" ;
59     aPyFunc += "def PySignalHandler( aSignal , aStackFrame ) :\n" ;
60     aPyFunc += "    print 'PySignalHandler(aSignal =',aSignal,',aStackFrame= ',aStackFrame,')'\n" ;
61     aPyFunc += "InitPyRunMethod.RunMethod( PySignalHandler )\n" ;
62     aPyFunc += "PrevHandler = signal.signal( signal.SIGSEGV , PySignalHandler )\n" ;
63     aPyFunc += "print 'PyObjSignal PrevHandler of SIGSEGV :', PrevHandler\n" ;
64     aPyFunc += "print 'PyObjSignal actual handler of SIGSEGV :', signal.getsignal( signal.SIGSEGV  )\n" ;
65 //    if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
66     if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) {
67       cdebug << ThreadNo() << " " << Name() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ;
68       Automaton()->PyUnLock() ;
69       return false ;
70     }
71     MyPyObjSignal = MyPyRunMethod ;
72     Automaton()->PyFunction( "PyObjSignal" , MyPyObjSignal ) ;
73   }
74   else {
75     MyPyObjSignal = Automaton()->PyFunction( "PyObjSignal" ) ;
76   }
77 #endif
78
79 // PyObjRef convert an IOR (string) to an Python ObjectReference
80   if ( Automaton()->PyFunction( "PyObjRef" ) == NULL ) {
81     string aPyFunc ;
82     aPyFunc = "import InitPyRunMethod\n" ;
83     aPyFunc += "import sys\n" ;
84     aPyFunc += "import CORBA\n" ;
85     aPyFunc += "import omniORB\n" ;
86     aPyFunc += "def PyObjRef( IORObjStr ) :\n" ;
87     aPyFunc += "    orb = CORBA.ORB_init( sys.argv , CORBA.ORB_ID )\n" ;
88     aPyFunc += "    objref = orb.string_to_object( IORObjStr )\n" ;
89     aPyFunc += "    return objref\n" ;
90     aPyFunc += "InitPyRunMethod.RunMethod( PyObjRef )\n" ;
91     if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
92     // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
93     //if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) {
94       cdebug << ThreadNo() << " " << Name() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ;
95       Automaton()->PyUnLock() ;
96       return false ;
97     }
98     MyPyObjRef = MyPyRunMethod ;
99     Automaton()->PyFunction( "PyObjRef" , MyPyObjRef ) ;
100   }
101   else {
102     MyPyObjRef = Automaton()->PyFunction( "PyObjRef" ) ;
103   }
104
105 // PyObjRef convert an Python ObjectReference to an IOR (string)
106   if ( Automaton()->PyFunction( "PyObjIor" ) == NULL ) {
107     string aPyFunc ;
108     aPyFunc = "import InitPyRunMethod\n" ;
109     aPyFunc += "import sys\n" ;
110     aPyFunc += "import CORBA\n" ;
111     aPyFunc += "import omniORB\n" ;
112     aPyFunc += "def PyObjIor( ObjRef ) :\n" ;
113     aPyFunc += "    orb = CORBA.ORB_init( sys.argv , CORBA.ORB_ID )\n" ;
114     aPyFunc += "    objIor = orb.object_to_string( ObjRef )\n" ;
115     aPyFunc += "    return objIor\n" ;
116     aPyFunc += "InitPyRunMethod.RunMethod( PyObjIor )\n" ;
117     if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
118     // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
119     //if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) {
120       cdebug << ThreadNo() << " " << Name() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ;
121       Automaton()->PyUnLock() ;
122       return false ;
123     }
124     MyPyObjIor = MyPyRunMethod ;
125     Automaton()->PyFunction( "PyObjIor" , MyPyObjIor ) ;
126   }
127   else {
128     MyPyObjIor = Automaton()->PyFunction( "PyObjIor" ) ;
129   }
130   Automaton()->PyUnLock() ;
131
132   cdebug_out << "InitPython" << endl ;
133   
134   return true ;
135 }
136
137 void GraphExecutor::InNode::RemovePyDynInvoke( char * PyFuncName ) {
138 //  Automaton()->PyLock() ; // Already done ...
139   if ( Automaton()->ErasePyFunction( PyFuncName ) ) {
140     cdebug << "InNode::RemovePyDynInvoke( " << PyFuncName << " ) success" << endl ;
141   }
142   else {
143     cdebug << "InNode::RemovePyDynInvoke( " << PyFuncName << " ) ERROR failed" << endl ;
144   }
145 //  Automaton()->PyUnLock() ;
146 }
147
148 PyObject * GraphExecutor::InNode::InitPyDynInvoke( char * PyFuncName ,
149                                                    const SUPERV::ListOfStrings * aPythonFunction ,
150                                                    bool & Err ) {
151   bool RetVal = true ;
152   Err = false ;
153   string aPyFunc ;
154   PyObject * thePyRunMethod = NULL ;
155
156   cdebug_in << "InitPyDynInvoke '" << PyFuncName << "' length " << (*aPythonFunction).length() << endl ;
157
158   if ( strlen( PyFuncName ) ) {
159     Automaton()->PyLock() ;
160     thePyRunMethod = Automaton()->PyFunction( PyFuncName ) ;
161     if ( (*aPythonFunction).length() ) {
162       if ( thePyRunMethod == NULL ) {
163         unsigned int i ;
164         aPyFunc += "import InitPyRunMethod\n" ;
165         for ( i = 0 ; i < (*aPythonFunction).length() ; i++ ) {
166           aPyFunc += (*aPythonFunction)[ i ] ;
167           aPyFunc += "\n" ;
168         }
169         aPyFunc += "InitPyRunMethod.RunMethod( " ;
170         aPyFunc += PyFuncName ;
171         aPyFunc += " )\n" ;
172         /*cdebug *///cout<< "InitPyDynInvoke PyRun_SimpleString " << endl << aPyFunc << endl ;
173 //        if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
174         if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) {
175           cdebug << ThreadNo() << " " << Name() << " PyRunSimpleString ERROR " << endl << aPyFunc << endl ;
176           PyFuncRunned( false ) ;
177           RetVal = false ;
178           Err = true ;
179         }
180         else {
181           PyFuncRunned( true ) ;
182           thePyRunMethod = MyPyRunMethod ;
183           
184           Automaton()->PyFunction( PyFuncName , thePyRunMethod ) ;
185           cdebug << ThreadNo() << "PyRunSimpleString( " << (*aPythonFunction)[ 0 ] << " ) " << endl ;
186         }
187       }
188       if ( RetVal ) {
189         Automaton()->PyUnLock() ;
190         cdebug_out << "InitPyDynInvoke '" << PyFuncName << "' thePyRunMethod " << thePyRunMethod << " "
191                    << thePyRunMethod->ob_refcnt << endl ;
192         return thePyRunMethod ;
193       }
194     }
195     else if ( thePyRunMethod ) {
196       Automaton()->PyUnLock() ;
197       cdebug_out << "InitPyDynInvoke '" << PyFuncName << "' thePyRunMethod " << thePyRunMethod << " "
198                  << thePyRunMethod->ob_refcnt << endl ;
199       return thePyRunMethod ;
200     }
201     Automaton()->PyUnLock() ;
202   }
203
204   cdebug_out << "InitPyDynInvoke '" << PyFuncName << "' thePyRunMethod Null Err " << Err << endl ;
205   
206   return NULL ;
207 }
208
209 extern "C" PyObject * PyRunMethod( PyObject * dummy , PyObject * Args ) {
210   PyObject * Result = NULL ;
211   PyObject * Temp = NULL ;
212   if ( PyArg_ParseTuple( Args , "O:set_callback" , & Temp ) ) {
213 //    cout << "PyArg_ParsedTuple" << endl ;
214     if ( !PyCallable_Check( Temp ) ) {
215 //      PyErr_SetString( PyExc_TypeError , "PyRunMethod must be callable" ) ;
216 //      cout << "PyRunMethod must be callable" << endl ;
217       return NULL ;
218     }
219     Py_XINCREF( Temp ) ;
220     Py_XDECREF( MyPyRunMethod ) ;
221     MyPyRunMethod = Temp ;
222     Py_INCREF( Py_None ) ;
223     Result = Py_None ;
224   }
225   return Result ;
226 }
227
228 #define PyDynInvokeTrace 1
229 bool GraphExecutor::InNode::PyDynInvoke( PyObject * MyPyRunMethod ,
230                                          const char *method , 
231                                          ServicesAnyData * InParametersList , int nInParams ,
232                                          ServicesAnyData * OutParametersList , int nOutParams ) {
233   int i ;
234
235   bool RetVal = true ;
236
237   const char * sname;
238
239 #if PyDynInvokeTrace
240   cdebug_in << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name()
241             << " method " << method
242             << " InParametersList " << InParametersList
243             << " OutParametersList " << OutParametersList 
244             << " " << nInParams << " InArgs " << nOutParams
245             << " OutArgs MyPyRunMethod " ;
246 #endif
247   if ( MyPyRunMethod ) {
248 #if PyDynInvokeTrace
249     cdebug << MyPyRunMethod << " " << MyPyRunMethod->ob_refcnt << endl ;
250 #endif
251   }
252   else {
253 #if PyDynInvokeTrace
254     cdebug << " NULL" << endl ;
255 #endif
256     return false ;
257   }
258
259   CORBA::Any data ;
260
261   PyObject * ArgsList = NULL ;
262   PyObject * ArgValue = NULL ;
263   PyObject * Result = NULL ;
264   PyObject * MyPyObjRefList = NULL ;
265   PyObject * ResultObj = NULL ;
266   PyObject * MyPyObjIorList = NULL ;
267   PyObject * ResultIor = NULL ;
268
269   CORBA::Object_ptr ObjRef ;
270
271   char * IORObjRef ;
272
273   if ( !PyCallable_Check( MyPyRunMethod ) ) {
274     Result = NULL ;
275     RetVal = false ;
276   }
277   else {
278     ArgsList = PyTuple_New( nInParams ) ;
279
280     for ( i = 0 ; i < nInParams ; i++ ) {
281       data = InParametersList[i].Value ;
282       sname = InParametersList[i].Name.c_str() ;
283       switch ( data.type()->kind() ) {
284       case CORBA::tk_string : {
285         char * t ;
286         data >>= t ;
287         PyObject * ArgValue = Py_BuildValue( "s" , t ) ;
288         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
289 #if PyDynInvokeTrace
290         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << t << " (string) "
291                << " ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
292                << ArgValue->ob_refcnt << endl ;
293 #endif
294         break ;
295       }
296       case CORBA::tk_boolean : {
297         bool b ;
298         data >>= (CORBA::Any::to_boolean ) b ;
299         PyObject * ArgValue = Py_BuildValue( "b" , b ) ;
300         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
301 #if PyDynInvokeTrace
302         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << b
303                << " (boolean) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
304                << ArgValue->ob_refcnt << endl ;
305 #endif
306         break ;
307       }
308       case CORBA::tk_char : {
309         unsigned char c ;
310         data >>= (CORBA::Any::to_char ) c ;
311         PyObject * ArgValue = Py_BuildValue( "c" , c ) ;
312         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
313 #if PyDynInvokeTrace
314         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << c
315                << " (char) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
316                << ArgValue->ob_refcnt << endl ;
317 #endif
318         break ;
319       }
320       case CORBA::tk_short : {
321         short s ;
322         data >>= s ;
323         PyObject * ArgValue = Py_BuildValue( "h" , s ) ;
324         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
325 #if PyDynInvokeTrace
326         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << s
327                << " (short) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
328                << ArgValue->ob_refcnt << endl ;
329 #endif
330         break ;
331       }
332       case CORBA::tk_long : {
333         long l ;
334         data >>= l ;
335         PyObject * ArgValue = Py_BuildValue( "l" , l ) ;
336 #if PyDynInvokeTrace
337         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << l
338                << " ArgValue->ob_refcnt" << ArgValue->ob_refcnt << endl ;
339 #endif
340         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
341 #if PyDynInvokeTrace
342         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << l
343                << " (long) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
344                << ArgValue->ob_refcnt << endl ;
345 #endif
346         break ;
347       }
348       case CORBA::tk_float : {
349         float f ;
350         data >>= f ;
351         PyObject * ArgValue = Py_BuildValue( "f" , f ) ;
352         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
353 #if PyDynInvokeTrace
354         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << f
355                << " (float) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
356                << ArgValue->ob_refcnt << endl ;
357 #endif
358         break ;
359       }
360       case CORBA::tk_double : {
361         double d ;
362         data >>= d ;
363         PyObject * ArgValue = Py_BuildValue( "d" , d ) ;
364         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
365 #if PyDynInvokeTrace
366         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << d
367                << " (double) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
368                << ArgValue->ob_refcnt << endl ;
369 #endif
370         break ;
371       }
372       case CORBA::tk_objref : {
373         MyPyObjRefList = PyTuple_New( 1 ) ;
374         PyObject * ObjValue ;
375         data >>= ObjRef ;
376         IORObjRef = ObjectToString( ObjRef ) ;
377         ObjValue = Py_BuildValue( "s" , IORObjRef ) ;
378         PyTuple_SetItem( MyPyObjRefList , 0 , ObjValue ) ;
379 #if PyDynInvokeTrace
380         cdebug << "ArgIn" << i << " : " << sname << " " << method << " " << " Value " << IORObjRef << " (objref) "
381                << MyPyObjRef->ob_refcnt << "/" << MyPyObjRefList->ob_refcnt << endl ;
382 #endif
383 //        ResultObj = PyEval_CallObject( MyPyObjRef , MyPyObjRefList ) ;
384         ResultObj = PyEvalCallObject( "PyObjRef" , MyPyObjRef , MyPyObjRefList ) ;
385 #if PyDynInvokeTrace
386         cdebug << "ObjValue->ob_refcnt" << ObjValue->ob_refcnt << endl ;
387 #endif
388         ArgValue = Py_BuildValue( "O" , ResultObj ) ;
389         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
390 #if PyDynInvokeTrace
391         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value  (objref) ArgsList->ob_refcnt"
392                << ArgsList->ob_refcnt << " ArgValue->ob_refcnt" << ArgValue->ob_refcnt << endl ;
393         cdebug << "MyPyObjRefList->ob_refcnt " << MyPyObjRefList->ob_refcnt-1 << endl ;
394 #endif
395         Py_DECREF( MyPyObjRefList ) ;
396         if ( CORBA::is_nil( ObjRef ) ) {
397           ResultObj = NULL ;
398         }
399         else {
400 #if PyDynInvokeTrace
401           cdebug << "ResultObj->ob_refcnt " << ResultObj->ob_refcnt-1 << endl ;
402 #endif
403           Py_DECREF( ResultObj ) ;
404         }
405         break ;
406       }
407       default : {
408         cdebug << "ArgIn" << i << " : " << sname << " Value " << "(other ERROR)" << endl ;
409       }
410       }
411     }
412
413 //    Result = PyEval_CallObject( MyPyRunMethod , ArgsList ) ;
414     Result = PyEvalCallObject( method , MyPyRunMethod , ArgsList ) ;
415
416 #if PyDynInvokeTrace
417     cdebug << "ArgsList->ob_refcnt" << ArgsList->ob_refcnt << endl ;
418 #endif
419
420     if ( Result == NULL ) {
421       cdebug_out << "GraphExecutor::InNode::PyDynInvoke Node " << Name() << " " << method << " Error Result == NULL"
422                  << endl ;
423       RetVal = false ;
424     }
425     else {
426       for ( i = 0 ; i < nOutParams ; i++ ) {
427         data = OutParametersList[i].Value ;
428         sname = OutParametersList[i].Name.c_str() ;
429         switch ( data.type()->kind() ) {
430         case CORBA::tk_string : {
431           char * t = "" ;
432           PyObject * ArgValue ;
433           if ( PyTuple_Check( Result ) ) {
434             ArgValue = PyTuple_GetItem( Result , i ) ;
435           }
436           else {
437             ArgValue = Result ;
438           }
439           if ( !PyString_Check( ArgValue ) ) {
440             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (string)" << endl ;
441             RetVal = false ;
442           }
443           else {
444             t = PyString_AsString( ArgValue ) ;
445           }
446           data <<= t ;
447 #if PyDynInvokeTrace
448           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << t << " (string)"
449                  << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
450                  << ArgValue->ob_refcnt << endl ;
451 #endif
452           break ;
453         }
454         case CORBA::tk_boolean : {
455           bool b = false ;
456           PyObject * ArgValue ;
457           if ( PyTuple_Check( Result ) ) {
458             ArgValue = PyTuple_GetItem( Result , i ) ;
459           }
460           else {
461             ArgValue = Result ;
462           }
463           if ( !PyInt_Check( ArgValue ) ) {
464             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (boolean)" << endl ;
465             RetVal = false ;
466           }
467           else {
468             b = PyInt_AsLong( ArgValue ) ;
469           }
470           data <<= (CORBA::Any::from_boolean ) b ;
471 #if PyDynInvokeTrace
472           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << b << " (boolean)"
473                  << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
474                  << ArgValue->ob_refcnt << endl ;
475 #endif
476           break ;
477         }
478         case CORBA::tk_char : {
479           unsigned char c = 0 ;
480           PyObject * ArgValue ;
481           if ( PyTuple_Check( Result ) ) {
482             ArgValue = PyTuple_GetItem( Result , i ) ;
483           }
484           else {
485             ArgValue = Result ;
486           }
487 //JR 04.04.2005 Debug          if ( !PyInt_Check( ArgValue ) ) {
488 //Difficult to understand that behavior ... Python char type is a string of length 1 !
489 //                                          or type is int ...
490           if ( !PyString_Check( ArgValue ) && !PyInt_Check( ArgValue ) ) {
491             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (char)" << endl ;
492             RetVal = false ;
493           }
494           else {
495             c = PyInt_AsLong( ArgValue ) ;
496           }
497           data <<= (CORBA::Any::from_char ) c ;
498 #if PyDynInvokeTrace
499           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << c << " (char)"
500                  << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
501                  << ArgValue->ob_refcnt << endl ;
502 #endif
503           break ;
504         }
505         case CORBA::tk_short : {
506           short s = 0 ;
507           PyObject * ArgValue ;
508           if ( PyTuple_Check( Result ) ) {
509             ArgValue = PyTuple_GetItem( Result , i ) ;
510           }
511           else {
512             ArgValue = Result ;
513           }
514           if ( !PyInt_Check( ArgValue ) ) {
515             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (short)" << endl ;
516             RetVal = false ;
517           }
518           else {
519             s = PyInt_AsLong( ArgValue ) ;
520           }
521           data <<= s ;
522 #if PyDynInvokeTrace
523           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << s << " (short)"
524                  << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
525                  << ArgValue->ob_refcnt << endl ;
526 #endif
527           break ;
528         }
529         case CORBA::tk_long : {
530           long l = 0 ;
531           PyObject * ArgValue ;
532           if ( PyTuple_Check( Result ) ) {
533             ArgValue = PyTuple_GetItem( Result , i ) ;
534           }
535           else {
536             ArgValue = Result ;
537           }
538           if ( PyLong_Check( ArgValue ) ) {
539             l = PyLong_AsLong( ArgValue ) ;
540           }
541           else if ( PyInt_Check( ArgValue ) ) {
542             l = PyInt_AsLong( ArgValue ) ;
543           }
544           else {
545             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (CORBA::tk_long)" << endl ;
546             RetVal = false ;
547           }
548           data <<= l ;
549 #if PyDynInvokeTrace
550           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << l << " (long)"
551                  << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
552                  << ArgValue->ob_refcnt << endl ;
553 #endif
554           break ;
555         }
556         case CORBA::tk_float : {
557           float f = 0 ;
558           PyObject * ArgValue ;
559           if ( PyTuple_Check( Result ) ) {
560             ArgValue = PyTuple_GetItem( Result , i ) ;
561           }
562           else {
563             ArgValue = Result ;
564           }
565           if ( !PyFloat_Check( ArgValue ) ) {
566             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (float)" << endl ;
567             RetVal = false ;
568           }
569           else {
570             f = PyFloat_AsDouble( ArgValue ) ;
571           }
572           data <<= f ;
573 #if PyDynInvokeTrace
574           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << f << " (float)"
575                  << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
576                  << ArgValue->ob_refcnt << endl ;
577 #endif
578           break ;
579         }
580         case CORBA::tk_double : {
581           double d = 0 ;
582           PyObject * ArgValue ;
583           if ( PyTuple_Check( Result ) ) {
584             ArgValue = PyTuple_GetItem( Result , i ) ;
585           }
586           else {
587             ArgValue = Result ;
588           }
589           if ( !PyFloat_Check( ArgValue ) ) {
590             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (double)" << endl ;
591             RetVal = false ;
592           }
593           else {
594             d = PyFloat_AsDouble( ArgValue ) ;
595           }
596           data <<= d ;
597 #if PyDynInvokeTrace
598           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << d << " (double)"
599                  << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
600                  << ArgValue->ob_refcnt << endl ;
601 #endif
602           break ;
603         }
604         case CORBA::tk_objref : {
605           PyObject * ObjIor ;
606           MyPyObjIorList = PyTuple_New( 1 ) ;
607           if ( PyTuple_Check( Result ) ) {
608             ObjIor = PyTuple_GetItem( Result , i ) ;
609           }
610           else {
611             ObjIor = Result ;
612           }
613 #if PyDynInvokeTrace
614           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << "(object reference) "
615                  << " Result->ob_refcnt" << Result->ob_refcnt << " ObjIor->ob_refcnt"
616                  << ObjIor->ob_refcnt << endl ;
617 #endif
618           Py_INCREF( ObjIor ) ;
619 //          PyObject_Print( ObjIor , stdout , 0 ) ;
620           PyTuple_SetItem( MyPyObjIorList , 0 , ObjIor ) ;
621 //          ResultIor = PyEval_CallObject( MyPyObjIor , MyPyObjIorList ) ;
622           ResultIor = PyEvalCallObject( "PyObjIor" , MyPyObjIor , MyPyObjIorList ) ;
623 #if PyDynInvokeTrace
624           cdebug << "ObjIor->ob_refcnt " << ObjIor->ob_refcnt-1 << endl ;
625 #endif
626           Py_DECREF( ObjIor ) ;
627 #if PyDynInvokeTrace
628           cdebug << "MyPyObjIorList->ob_refcnt " << MyPyObjIorList->ob_refcnt-1 << endl ;
629 #endif
630           Py_DECREF( MyPyObjIorList ) ;
631 #if PyDynInvokeTrace
632           cdebug << "MyPyObjIor->ob_refcnt " << MyPyObjIor->ob_refcnt << endl ;
633 #endif
634           if ( ResultIor ) {
635             char * IOR = NULL ;
636             IOR = PyString_AsString( ResultIor ) ;
637             ObjRef = StringToObject( IOR ) ;
638 //JR 05.08.2005 BUG OMNIORB4 ? : that line does run with SALOME_3 (OMNIOrb4) for
639 //                               object references ... (It's Ok with OMNIOrb3)
640 //                               IOR and ObjRef and IORObjRef are Ok
641 //                               BUT data contains a nil reference !!!
642 #if !omniORB_4_0_5
643             data <<= ObjRef ;
644 #else
645             SUPERV::SuperG_var mySuperVisionComponent ;
646             mySuperVisionComponent = theAutomaton->SuperVisionComponent() ;
647             if ( CORBA::is_nil( mySuperVisionComponent ) ) {
648               Engines::Container_var myContainer ;
649               Engines::Component_var myObjComponent ;
650               _OutNode->Graph()->StartComponent( ThreadNo() , "SuperVisionContainer" ,
651                                                  "SUPERV" ,
652                                                  myContainer , myObjComponent ) ;
653               mySuperVisionComponent = SUPERV::SuperG::_narrow( myObjComponent ) ;
654               theAutomaton->SuperVisionComponent( mySuperVisionComponent ) ;
655             }
656 //JR 24.08.2005 BUG OMNIORB4 ? : That does not run also ==> Comments
657 //            CORBA::Object_ptr ObjRef1 ;
658 //            ObjRef1 = mySuperVisionComponent->ToObject( ObjRef ) ;
659 //            data <<= ObjRef1 ;
660 //JR 24.08.2005 BUG OMNIORB4 ? : BUT THAT RUNS WITH THE "IDENTITY METHOD ToObject of SUPERV.idl :
661 //                               Object ToObject( in Object anObject ) ;
662 //CODE of that method !!! :
663 //CORBA::Object_ptr SuperV_Impl::ToObject( CORBA::Object_ptr anObjectRef ) {
664 //  beginService( "SuperV_Impl::ToObject" );
665 //  CORBA::Object_ptr anObject ;
666 //  anObject = CORBA::Object::_duplicate( anObjectRef ) ;
667 //  endService( "SuperV_Impl::ToObject" );
668 //  return anObject ;
669 //}
670 //BUT THAT RUNS ONLY IF WE USE Dynamic invocation of CORBA (DynInvoke)
671             ServicesAnyData anInParametersList ;
672             anInParametersList.Name = "anObjRef" ;
673             anInParametersList.Value <<= ObjRef ;
674             DynInvoke( mySuperVisionComponent , "ToObject" ,
675                        &anInParametersList , 1 , &OutParametersList[i] , 1 ) ;
676             data = OutParametersList[i].Value ;
677 #endif
678             IORObjRef = ObjectToString( ObjRef ) ;
679 #if PyDynInvokeTrace
680             cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value IORPy "
681                    << IOR << " IORObjRef " << IORObjRef << " (objref) " << endl ;
682 #endif
683             if ( CORBA::is_nil( ObjRef ) ) {
684 #if PyDynInvokeTrace
685               cdebug << "ObjRef ERROR(nil reference) ResultIor->ob_refcnt "
686                      << ResultIor->ob_refcnt-1 << endl ;
687 #endif
688               ResultIor = NULL ;
689             }
690             else {
691 #if PyDynInvokeTrace
692               cdebug << "ObjRef NOT(nil reference)ResultIor->ob_refcnt "
693                      << ResultIor->ob_refcnt-1 << endl ;
694 #endif
695               Py_DECREF( ResultIor ) ;
696             }
697           }
698           else {
699             cdebug_out << "GraphExecutor::InNode::PyDynInvoke Error ResultIor == NULL Node "
700                        << Name() << " method " << method << " " << endl ;
701             RetVal = false ;
702           }
703           break ;
704         }
705         default : {
706           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << "(other ERROR)" << endl ;
707             RetVal = false ;
708         }
709         }
710 //JR 05.08.2005 BUG OMNIORB4 ? : that line does run with SALOME_3 (OMNIOrb4) for
711 //                               object references ...
712         OutParametersList[i].Value = data ;
713 //        if ( data.type()->kind() == CORBA::tk_objref ) {
714 //          IORObjRef = ObjectToString( ObjRef ) ;
715 //#if PyDynInvokeTrace
716 //          cdebug << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name()
717 //                 << "ArgOut" << i << " : " << sname << " " << method << " ObjRef "
718 //                 << IORObjRef << " (objref) " << endl ;
719 //          Engines::Component_var theObjComponent ;
720 //          theObjComponent = Engines::Component::_narrow( ObjRef ) ;
721 //          DynInvoke( theObjComponent , "ping" , NULL , 0 , NULL , 0 ) ;
722 //#endif
723 //          Engines::Container_var myContainer ;
724 //          Engines::Component_var myObjComponent ;
725 //          _OutNode->Graph()->StartComponent( ThreadNo() , "SuperVisionContainer" ,
726 //                                             "SUPERV" ,
727 //                                              myContainer , myObjComponent ) ;
728 //          SUPERV::SuperG_var mySuperVisionComponent ;
729 //          mySuperVisionComponent = SUPERV::SuperG::_narrow( myObjComponent ) ;
730 //          CORBA::Object_ptr ObjRef1 ;
731 //          ObjRef1 = mySuperVisionComponent->ToObject( IORObjRef ) ;
732 //#if PyDynInvokeTrace
733 //          IORObjRef = ObjectToString( ObjRef1 ) ;
734 //          cdebug << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name()
735 //                 << "ArgOut" << i << " : " << sname << " " << method << " ToObject "
736 //                 << IORObjRef << " (objref) " << endl ;
737 //#endif
738
739 //          ServicesAnyData anInParametersList ;
740 //          anInParametersList.Name = "anIORObjRef" ;
741 //          anInParametersList.Value <<= IORObjRef ;
742 //          DynInvoke( mySuperVisionComponent , "ToObject" ,
743 //                     &anInParametersList , 1 , &OutParametersList[i] , 1 ) ;
744 //#if PyDynInvokeTrace
745 //          OutParametersList[i].Value >>= ObjRef1 ;
746 //          IORObjRef = ObjectToString( ObjRef1 ) ;
747 //          cdebug << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name()
748 //                 << "ArgOut" << i << " : " << sname << " " << method
749 //                 << " DynInvoke(ToObject) " << IORObjRef << " (objref) " << endl ;
750 //#endif
751 //        }
752       }
753
754 #if PyDynInvokeTrace
755       cdebug << "InNode::PyDynInvoke Result->ob_refcnt " << Result->ob_refcnt-1 << endl ;
756 #endif
757       Py_DECREF( Result ) ;
758     }
759
760 #if PyDynInvokeTrace
761     cdebug << "InNode::PyDynInvoke ArgsList->ob_refcnt " << ArgsList->ob_refcnt-1 << endl ;
762 #endif
763     Py_DECREF( ArgsList ) ;
764   }
765
766 #if 0
767   {
768     int i ;
769     for ( i = 0 ; i < nInParams ; i++ ) { // Without Gates
770       cdebug << "InParametersList[" << i << "] : "
771              << InParametersList[i].Name << " "
772              << AnyValue( InParametersList[i].Value ) << endl ;
773     }
774     for ( i = 0 ; i < nOutParams ; i++ ) { // Without Gates
775       cdebug << "OutParametersList[" << i << "] : "
776              << OutParametersList[i].Name << " "
777              << AnyValue( OutParametersList[i].Value ) << endl ;
778     }
779   }
780 #endif
781
782 #if PyDynInvokeTrace
783   cdebug_out << "GraphExecutor::InNode::PyDynInvoke Node " << Name() << " method " << method << " " << RetVal
784              << endl ;
785 #endif
786
787   return RetVal ;
788
789 }
790
791 bool GraphExecutor::InNode::PyRunSimpleString( char* thePyString )
792 {
793   const bool ErrorValue = true;
794   bool aRet;
795   try {
796 #if PyDynInvokeTrace
797     MESSAGE( pthread_self() << "Python method beginning : " << thePyString );
798     cdebug_in << pthread_self() << "Python method beginning : " << thePyString << endl ;
799 #endif
800     aRet = PyRun_SimpleString( thePyString );
801     // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL    
802     //aRet = _OutNode->SuperVisionContainer()->ActivatePythonExecution( thePyString ) ;
803 #if PyDynInvokeTrace
804     MESSAGE( pthread_self() << "Python method finished." );
805     cdebug_out << pthread_self() << "Python method finished." << endl ;
806 #endif
807   } catch( ... ) {
808     MESSAGE( pthread_self() << "ERROR: Exception caught running Python method." );
809     cdebug_out << pthread_self() << "ERROR: Exception caught running Python method."
810                << endl ;
811     MESSAGE( "       Python was reinitialized.  Previous Python definitions are lost" );
812     // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL    
813     Py_Finalize();
814     Py_Initialize();
815     Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod );
816     _OutNode->PyInitialized( false );
817     aRet = ErrorValue;
818   }
819   return aRet;
820 }
821
822 PyObject * GraphExecutor::InNode::PyEvalCallObject( const char *method ,
823                                                     PyObject * MyPyRunMethod ,
824                                                     PyObject * ArgsList ) {
825 //  cdebug_in << "Executor::InNode::PyEvalCallObject " << Name() << endl ;
826   PyObject * Result = NULL ;
827   try {
828 //    MESSAGE( pthread_self() << "PyEval_CallObject " << Name() << " method beginning : " << method );
829 //    cdebug << pthread_self() << "PyEval_CallObject " << Name() << " method beginning : " << method << endl ;
830     Result = PyEval_CallObject( MyPyRunMethod , ArgsList ) ;
831     // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL    
832     //Result = _OutNode->SuperVisionContainer()->ActivatePythonExecution( MyPyRunMethod , ArgsList ) ;
833 //    MESSAGE( pthread_self() << "PyEval_CallObject method finished. Result " << Result );
834 //    cdebug << pthread_self() << "PyEval_CallObject method finished. Result " << Result << endl ;
835 //    cdebug_out << "Executor::InNode::PyEvalCallObject " << Name() << endl ;
836   } catch( ... ) {
837     MESSAGE( pthread_self() << "ERROR: Exception caught PyEval_CallObject " << Name()
838              << " Python method " << method << ". Result "  << Result );
839     cdebug << pthread_self() << "ERROR: Exception caught PyEval_CallObject " << Name()
840            << " Python method " << method << ". Result " << Result << endl ;
841     MESSAGE( "       Python was reinitialized.  Previous Python definitions are lost Py_IsInitialized " << Py_IsInitialized() );
842 //JR ===> fatal error in python : no current thread
843     // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL    
844     Py_Finalize();
845     Py_Initialize();
846     Automaton()->PyInitModule( false ) ;
847     Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod );
848     _OutNode->PyInitialized( false );
849
850     if ( Result == NULL ) {
851       Kill() ; // Reset of _ThreadId
852     }
853 //    cdebug << "Executor::InNode::PyEvalCallObject ERROR catched " << Name()
854 //           << " Py_IsInitialized " << Py_IsInitialized() << endl ;
855   }
856 //  cdebug_out << "Executor::InNode::PyEvalCallObject " << Name() << endl ;
857   return Result ;
858 }