Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[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 #if OMNIORB_VERSION >= 4
376         data >>= (CORBA::Any::to_object ) ObjRef ;
377 #else
378         data >>= ObjRef ;
379 #endif
380         IORObjRef = ObjectToString( ObjRef ) ;
381         ObjValue = Py_BuildValue( "s" , IORObjRef ) ;
382         PyTuple_SetItem( MyPyObjRefList , 0 , ObjValue ) ;
383 #if PyDynInvokeTrace
384         cdebug << "ArgIn" << i << " : " << sname << " " << method << " " << " Value " << IORObjRef << " (objref) "
385                << MyPyObjRef->ob_refcnt << "/" << MyPyObjRefList->ob_refcnt << endl ;
386 #endif
387 //        ResultObj = PyEval_CallObject( MyPyObjRef , MyPyObjRefList ) ;
388         ResultObj = PyEvalCallObject( "PyObjRef" , MyPyObjRef , MyPyObjRefList ) ;
389 #if PyDynInvokeTrace
390         cdebug << "ObjValue->ob_refcnt" << ObjValue->ob_refcnt << endl ;
391 #endif
392         ArgValue = Py_BuildValue( "O" , ResultObj ) ;
393         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
394 #if PyDynInvokeTrace
395         if ( ArgValue )
396           cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value  (objref) ArgsList->ob_refcnt"
397                  << ArgsList->ob_refcnt << " ArgValue->ob_refcnt" << ArgValue->ob_refcnt << endl ;
398         else
399           cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value  (objref) ArgsList->ob_refcnt"
400                  << ArgsList->ob_refcnt << " ArgValue is NULL" << endl ;
401         cdebug << "MyPyObjRefList->ob_refcnt " << MyPyObjRefList->ob_refcnt-1 << endl ;
402 #endif
403         Py_DECREF( MyPyObjRefList ) ;
404         if ( CORBA::is_nil( ObjRef ) ) {
405           ResultObj = NULL ;
406         }
407         else {
408 #if PyDynInvokeTrace
409           if ( ResultObj )
410             cdebug << "ResultObj->ob_refcnt " << ResultObj->ob_refcnt-1 << endl ;
411           else
412             cdebug << "ResultObj is NULL" << endl ;
413 #endif
414           if ( ResultObj )
415             Py_DECREF( ResultObj ) ;
416         }
417         break ;
418       }
419       default : {
420         cdebug << "ArgIn" << i << " : " << sname << " Value " << "(other ERROR)" << endl ;
421       }
422       }
423     }
424
425 //    Result = PyEval_CallObject( MyPyRunMethod , ArgsList ) ;
426     Result = PyEvalCallObject( method , MyPyRunMethod , ArgsList ) ;
427
428 #if PyDynInvokeTrace
429     cdebug << "ArgsList->ob_refcnt" << ArgsList->ob_refcnt << endl ;
430 #endif
431
432     if ( Result == NULL ) {
433       cdebug_out << "GraphExecutor::InNode::PyDynInvoke Node " << Name() << " " << method << " Error Result == NULL"
434                  << endl ;
435       RetVal = false ;
436     }
437     else {
438       for ( i = 0 ; i < nOutParams ; i++ ) {
439         data = OutParametersList[i].Value ;
440         sname = OutParametersList[i].Name.c_str() ;
441         switch ( data.type()->kind() ) {
442         case CORBA::tk_string : {
443           char * t = "" ;
444           PyObject * ArgValue ;
445           if ( PyTuple_Check( Result ) ) {
446             ArgValue = PyTuple_GetItem( Result , i ) ;
447           }
448           else {
449             ArgValue = Result ;
450           }
451           if ( !PyString_Check( ArgValue ) ) {
452             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (string)" << endl ;
453             RetVal = false ;
454           }
455           else {
456             t = PyString_AsString( ArgValue ) ;
457           }
458           data <<= t ;
459 #if PyDynInvokeTrace
460           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << t << " (string)"
461                  << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
462                  << ArgValue->ob_refcnt << endl ;
463 #endif
464           break ;
465         }
466         case CORBA::tk_boolean : {
467           bool b = false ;
468           PyObject * ArgValue ;
469           if ( PyTuple_Check( Result ) ) {
470             ArgValue = PyTuple_GetItem( Result , i ) ;
471           }
472           else {
473             ArgValue = Result ;
474           }
475           if ( !PyInt_Check( ArgValue ) ) {
476             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (boolean)" << endl ;
477             RetVal = false ;
478           }
479           else {
480             b = PyInt_AsLong( ArgValue ) ;
481           }
482           data <<= (CORBA::Any::from_boolean ) b ;
483 #if PyDynInvokeTrace
484           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << b << " (boolean)"
485                  << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
486                  << ArgValue->ob_refcnt << endl ;
487 #endif
488           break ;
489         }
490         case CORBA::tk_char : {
491           unsigned char c = 0 ;
492           PyObject * ArgValue ;
493           if ( PyTuple_Check( Result ) ) {
494             ArgValue = PyTuple_GetItem( Result , i ) ;
495           }
496           else {
497             ArgValue = Result ;
498           }
499 //JR 04.04.2005 Debug          if ( !PyInt_Check( ArgValue ) ) {
500 //Difficult to understand that behavior ... Python char type is a string of length 1 !
501 //                                          or type is int ...
502           if ( !PyString_Check( ArgValue ) && !PyInt_Check( ArgValue ) ) {
503             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (char)" << endl ;
504             RetVal = false ;
505           }
506           else {
507             c = PyInt_AsLong( ArgValue ) ;
508           }
509           data <<= (CORBA::Any::from_char ) c ;
510 #if PyDynInvokeTrace
511           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << c << " (char)"
512                  << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
513                  << ArgValue->ob_refcnt << endl ;
514 #endif
515           break ;
516         }
517         case CORBA::tk_short : {
518           short s = 0 ;
519           PyObject * ArgValue ;
520           if ( PyTuple_Check( Result ) ) {
521             ArgValue = PyTuple_GetItem( Result , i ) ;
522           }
523           else {
524             ArgValue = Result ;
525           }
526           if ( !PyInt_Check( ArgValue ) ) {
527             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (short)" << endl ;
528             RetVal = false ;
529           }
530           else {
531             s = PyInt_AsLong( ArgValue ) ;
532           }
533           data <<= s ;
534 #if PyDynInvokeTrace
535           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << s << " (short)"
536                  << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
537                  << ArgValue->ob_refcnt << endl ;
538 #endif
539           break ;
540         }
541         case CORBA::tk_long : {
542           long l = 0 ;
543           PyObject * ArgValue ;
544           if ( PyTuple_Check( Result ) ) {
545             ArgValue = PyTuple_GetItem( Result , i ) ;
546           }
547           else {
548             ArgValue = Result ;
549           }
550           if ( PyLong_Check( ArgValue ) ) {
551             l = PyLong_AsLong( ArgValue ) ;
552           }
553           else if ( PyInt_Check( ArgValue ) ) {
554             l = PyInt_AsLong( ArgValue ) ;
555           }
556           else {
557             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (CORBA::tk_long)" << endl ;
558             RetVal = false ;
559           }
560           data <<= l ;
561 #if PyDynInvokeTrace
562           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << l << " (long)"
563                  << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
564                  << ArgValue->ob_refcnt << endl ;
565 #endif
566           break ;
567         }
568         case CORBA::tk_float : {
569           float f = 0 ;
570           PyObject * ArgValue ;
571           if ( PyTuple_Check( Result ) ) {
572             ArgValue = PyTuple_GetItem( Result , i ) ;
573           }
574           else {
575             ArgValue = Result ;
576           }
577           if ( !PyFloat_Check( ArgValue ) ) {
578             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (float)" << endl ;
579             RetVal = false ;
580           }
581           else {
582             f = PyFloat_AsDouble( ArgValue ) ;
583           }
584           data.replace(CORBA::TypeCode::PR_float_tc(), (void*)(&f));
585           //data <<= f ;
586 #if PyDynInvokeTrace
587           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << f << " (float)"
588                  << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
589                  << ArgValue->ob_refcnt << endl ;
590 #endif
591           break ;
592         }
593         case CORBA::tk_double : {
594           double d = 0 ;
595           PyObject * ArgValue ;
596           if ( PyTuple_Check( Result ) ) {
597             ArgValue = PyTuple_GetItem( Result , i ) ;
598           }
599           else {
600             ArgValue = Result ;
601           }
602           if ( !PyFloat_Check( ArgValue ) ) {
603             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (double)" << endl ;
604             RetVal = false ;
605           }
606           else {
607             d = PyFloat_AsDouble( ArgValue ) ;
608           }
609           data.replace(CORBA::TypeCode::PR_double_tc(), (void*)(&d));
610           //data <<= d ;
611 #if PyDynInvokeTrace
612           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << d << " (double)"
613                  << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
614                  << ArgValue->ob_refcnt << endl ;
615 #endif
616           break ;
617         }
618         case CORBA::tk_objref : {
619           PyObject * ObjIor ;
620           MyPyObjIorList = PyTuple_New( 1 ) ;
621           if ( PyTuple_Check( Result ) ) {
622             ObjIor = PyTuple_GetItem( Result , i ) ;
623           }
624           else {
625             ObjIor = Result ;
626           }
627 #if PyDynInvokeTrace
628           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << "(object reference) "
629                  << " Result->ob_refcnt" << Result->ob_refcnt << " ObjIor->ob_refcnt"
630                  << ObjIor->ob_refcnt << endl ;
631 #endif
632           Py_INCREF( ObjIor ) ;
633 //          PyObject_Print( ObjIor , stdout , 0 ) ;
634           PyTuple_SetItem( MyPyObjIorList , 0 , ObjIor ) ;
635 //          ResultIor = PyEval_CallObject( MyPyObjIor , MyPyObjIorList ) ;
636           ResultIor = PyEvalCallObject( "PyObjIor" , MyPyObjIor , MyPyObjIorList ) ;
637 #if PyDynInvokeTrace
638           cdebug << "ObjIor->ob_refcnt " << ObjIor->ob_refcnt-1 << endl ;
639 #endif
640           Py_DECREF( ObjIor ) ;
641 #if PyDynInvokeTrace
642           cdebug << "MyPyObjIorList->ob_refcnt " << MyPyObjIorList->ob_refcnt-1 << endl ;
643 #endif
644           Py_DECREF( MyPyObjIorList ) ;
645 #if PyDynInvokeTrace
646           cdebug << "MyPyObjIor->ob_refcnt " << MyPyObjIor->ob_refcnt << endl ;
647 #endif
648           if ( ResultIor ) {
649             char * IOR = NULL ;
650             IOR = PyString_AsString( ResultIor ) ;
651             ObjRef = StringToObject( IOR ) ;
652 //JR 05.08.2005 BUG OMNIORB4 ? : that line does run with SALOME_3 (OMNIOrb4) for
653 //                               object references ... (It's Ok with OMNIOrb3)
654 //                               IOR and ObjRef and IORObjRef are Ok
655 //                               BUT data contains a nil reference !!!
656 #if !omniORB_4_0_5
657             data <<= ObjRef ;
658 #else
659             SUPERV::SuperG_var mySuperVisionComponent ;
660             mySuperVisionComponent = theAutomaton->SuperVisionComponent() ;
661             if ( CORBA::is_nil( mySuperVisionComponent ) ) {
662               Engines::Container_var myContainer ;
663               Engines::Component_var myObjComponent ;
664               _OutNode->Graph()->StartComponent( ThreadNo() , "SuperVisionContainer" ,
665                                                  "SUPERV" ,
666                                                  myContainer , myObjComponent ) ;
667               mySuperVisionComponent = SUPERV::SuperG::_narrow( myObjComponent ) ;
668               theAutomaton->SuperVisionComponent( mySuperVisionComponent ) ;
669             }
670 //JR 24.08.2005 BUG OMNIORB4 ? : That does not run also ==> Comments
671 //            CORBA::Object_ptr ObjRef1 ;
672 //            ObjRef1 = mySuperVisionComponent->ToObject( ObjRef ) ;
673 //            data <<= ObjRef1 ;
674 //JR 24.08.2005 BUG OMNIORB4 ? : BUT THAT RUNS WITH THE "IDENTITY METHOD ToObject of SUPERV.idl :
675 //                               Object ToObject( in Object anObject ) ;
676 //CODE of that method !!! :
677 //CORBA::Object_ptr SuperV_Impl::ToObject( CORBA::Object_ptr anObjectRef ) {
678 //  beginService( "SuperV_Impl::ToObject" );
679 //  CORBA::Object_ptr anObject ;
680 //  anObject = CORBA::Object::_duplicate( anObjectRef ) ;
681 //  endService( "SuperV_Impl::ToObject" );
682 //  return anObject ;
683 //}
684 //BUT THAT RUNS ONLY IF WE USE Dynamic invocation of CORBA (DynInvoke)
685             ServicesAnyData anInParametersList ;
686             anInParametersList.Name = "anObjRef" ;
687             anInParametersList.Value <<= ObjRef ;
688             DynInvoke( mySuperVisionComponent , "ToObject" ,
689                        &anInParametersList , 1 , &OutParametersList[i] , 1 ) ;
690             data = OutParametersList[i].Value ;
691 #endif
692             IORObjRef = ObjectToString( ObjRef ) ;
693 #if PyDynInvokeTrace
694             cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value IORPy "
695                    << IOR << " IORObjRef " << IORObjRef << " (objref) " << endl ;
696 #endif
697             if ( CORBA::is_nil( ObjRef ) ) {
698 #if PyDynInvokeTrace
699               cdebug << "ObjRef ERROR(nil reference) ResultIor->ob_refcnt "
700                      << ResultIor->ob_refcnt-1 << endl ;
701 #endif
702               ResultIor = NULL ;
703             }
704             else {
705 #if PyDynInvokeTrace
706               cdebug << "ObjRef NOT(nil reference)ResultIor->ob_refcnt "
707                      << ResultIor->ob_refcnt-1 << endl ;
708 #endif
709               Py_DECREF( ResultIor ) ;
710             }
711           }
712           else {
713             cdebug_out << "GraphExecutor::InNode::PyDynInvoke Error ResultIor == NULL Node "
714                        << Name() << " method " << method << " " << endl ;
715             RetVal = false ;
716           }
717           break ;
718         }
719         default : {
720           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << "(other ERROR)" << endl ;
721             RetVal = false ;
722         }
723         }
724 //JR 05.08.2005 BUG OMNIORB4 ? : that line does run with SALOME_3 (OMNIOrb4) for
725 //                               object references ...
726         OutParametersList[i].Value = data ;
727 //        if ( data.type()->kind() == CORBA::tk_objref ) {
728 //          IORObjRef = ObjectToString( ObjRef ) ;
729 //#if PyDynInvokeTrace
730 //          cdebug << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name()
731 //                 << "ArgOut" << i << " : " << sname << " " << method << " ObjRef "
732 //                 << IORObjRef << " (objref) " << endl ;
733 //          Engines::Component_var theObjComponent ;
734 //          theObjComponent = Engines::Component::_narrow( ObjRef ) ;
735 //          DynInvoke( theObjComponent , "ping" , NULL , 0 , NULL , 0 ) ;
736 //#endif
737 //          Engines::Container_var myContainer ;
738 //          Engines::Component_var myObjComponent ;
739 //          _OutNode->Graph()->StartComponent( ThreadNo() , "SuperVisionContainer" ,
740 //                                             "SUPERV" ,
741 //                                              myContainer , myObjComponent ) ;
742 //          SUPERV::SuperG_var mySuperVisionComponent ;
743 //          mySuperVisionComponent = SUPERV::SuperG::_narrow( myObjComponent ) ;
744 //          CORBA::Object_ptr ObjRef1 ;
745 //          ObjRef1 = mySuperVisionComponent->ToObject( IORObjRef ) ;
746 //#if PyDynInvokeTrace
747 //          IORObjRef = ObjectToString( ObjRef1 ) ;
748 //          cdebug << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name()
749 //                 << "ArgOut" << i << " : " << sname << " " << method << " ToObject "
750 //                 << IORObjRef << " (objref) " << endl ;
751 //#endif
752
753 //          ServicesAnyData anInParametersList ;
754 //          anInParametersList.Name = "anIORObjRef" ;
755 //          anInParametersList.Value <<= IORObjRef ;
756 //          DynInvoke( mySuperVisionComponent , "ToObject" ,
757 //                     &anInParametersList , 1 , &OutParametersList[i] , 1 ) ;
758 //#if PyDynInvokeTrace
759 //          OutParametersList[i].Value >>= ObjRef1 ;
760 //          IORObjRef = ObjectToString( ObjRef1 ) ;
761 //          cdebug << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name()
762 //                 << "ArgOut" << i << " : " << sname << " " << method
763 //                 << " DynInvoke(ToObject) " << IORObjRef << " (objref) " << endl ;
764 //#endif
765 //        }
766       }
767
768 #if PyDynInvokeTrace
769       cdebug << "InNode::PyDynInvoke Result->ob_refcnt " << Result->ob_refcnt-1 << endl ;
770 #endif
771       Py_DECREF( Result ) ;
772     }
773
774 #if PyDynInvokeTrace
775     cdebug << "InNode::PyDynInvoke ArgsList->ob_refcnt " << ArgsList->ob_refcnt-1 << endl ;
776 #endif
777     Py_DECREF( ArgsList ) ;
778   }
779
780 #if 0
781   {
782     int i ;
783     for ( i = 0 ; i < nInParams ; i++ ) { // Without Gates
784       cdebug << "InParametersList[" << i << "] : "
785              << InParametersList[i].Name << " "
786              << AnyValue( InParametersList[i].Value ) << endl ;
787     }
788     for ( i = 0 ; i < nOutParams ; i++ ) { // Without Gates
789       cdebug << "OutParametersList[" << i << "] : "
790              << OutParametersList[i].Name << " "
791              << AnyValue( OutParametersList[i].Value ) << endl ;
792     }
793   }
794 #endif
795
796 #if PyDynInvokeTrace
797   cdebug_out << "GraphExecutor::InNode::PyDynInvoke Node " << Name() << " method " << method << " " << RetVal
798              << endl ;
799 #endif
800
801   return RetVal ;
802
803 }
804
805 bool GraphExecutor::InNode::PyRunSimpleString( char* thePyString )
806 {
807   const bool ErrorValue = true;
808   bool aRet;
809   try {
810 #if PyDynInvokeTrace
811     MESSAGE( pthread_self() << "Python method beginning : " << thePyString );
812     cdebug_in << pthread_self() << "Python method beginning : " << thePyString << endl ;
813 #endif
814     aRet = PyRun_SimpleString( thePyString );
815     // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL    
816     //aRet = _OutNode->SuperVisionContainer()->ActivatePythonExecution( thePyString ) ;
817 #if PyDynInvokeTrace
818     MESSAGE( pthread_self() << "Python method finished." );
819     cdebug_out << pthread_self() << "Python method finished." << endl ;
820 #endif
821   } catch( ... ) {
822     MESSAGE( pthread_self() << "ERROR: Exception caught running Python method." );
823     cdebug_out << pthread_self() << "ERROR: Exception caught running Python method."
824                << endl ;
825     MESSAGE( "       Python was reinitialized.  Previous Python definitions are lost" );
826     // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL    
827     Py_Finalize();
828     Py_Initialize();
829     Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod );
830     _OutNode->PyInitialized( false );
831     aRet = ErrorValue;
832   }
833   return aRet;
834 }
835
836 PyObject * GraphExecutor::InNode::PyEvalCallObject( const char *method ,
837                                                     PyObject * MyPyRunMethod ,
838                                                     PyObject * ArgsList ) {
839 //  cdebug_in << "Executor::InNode::PyEvalCallObject " << Name() << endl ;
840   PyObject * Result = NULL ;
841   try {
842 //    MESSAGE( pthread_self() << "PyEval_CallObject " << Name() << " method beginning : " << method );
843 //    cdebug << pthread_self() << "PyEval_CallObject " << Name() << " method beginning : " << method << endl ;
844     Result = PyEval_CallObject( MyPyRunMethod , ArgsList ) ;
845     // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL    
846     //Result = _OutNode->SuperVisionContainer()->ActivatePythonExecution( MyPyRunMethod , ArgsList ) ;
847 //    MESSAGE( pthread_self() << "PyEval_CallObject method finished. Result " << Result );
848 //    cdebug << pthread_self() << "PyEval_CallObject method finished. Result " << Result << endl ;
849 //    cdebug_out << "Executor::InNode::PyEvalCallObject " << Name() << endl ;
850   } catch( ... ) {
851     MESSAGE( pthread_self() << "ERROR: Exception caught PyEval_CallObject " << Name()
852              << " Python method " << method << ". Result "  << Result );
853     cdebug << pthread_self() << "ERROR: Exception caught PyEval_CallObject " << Name()
854            << " Python method " << method << ". Result " << Result << endl ;
855     MESSAGE( "       Python was reinitialized.  Previous Python definitions are lost Py_IsInitialized " << Py_IsInitialized() );
856 //JR ===> fatal error in python : no current thread
857     // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL    
858     Py_Finalize();
859     Py_Initialize();
860     Automaton()->PyInitModule( false ) ;
861     Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod );
862     _OutNode->PyInitialized( false );
863
864     if ( Result == NULL ) {
865       Kill() ; // Reset of _ThreadId
866     }
867 //    cdebug << "Executor::InNode::PyEvalCallObject ERROR catched " << Name()
868 //           << " Py_IsInitialized " << Py_IsInitialized() << endl ;
869   }
870 //  cdebug_out << "Executor::InNode::PyEvalCallObject " << Name() << endl ;
871   return Result ;
872 }