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