]> SALOME platform Git repositories - modules/superv.git/blob - src/GraphExecutor/DataFlowExecutor_PyDynInvoke.cxx
Salome HOME
*** empty log message ***
[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 bool GraphExecutor::InNode::PyDynInvoke( PyObject * MyPyRunMethod ,
227                                          const char *method , 
228                                          ServicesAnyData * inParams , int nInParams ,
229                                          ServicesAnyData * outParams , int nOutParams ) {
230   int i ;
231
232   bool RetVal = true ;
233
234   int n_in  = nInParams ;
235   int n_out = nOutParams ;
236   const char * sname;
237
238   cdebug_in << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name() << " method " << method
239             << " " << n_in << " InArgs " << n_out << " OutArgs MyPyRunMethod " ;
240   if ( MyPyRunMethod ) {
241     cdebug << MyPyRunMethod << " " << MyPyRunMethod->ob_refcnt << endl ;
242   }
243   else {
244     cdebug << " NULL" << endl ;
245     return false ;
246   }
247
248   CORBA::Any data ;
249
250   PyObject * ArgsList = NULL ;
251   PyObject * ArgValue = NULL ;
252   PyObject * Result = NULL ;
253   PyObject * MyPyObjRefList = NULL ;
254   PyObject * ResultObj = NULL ;
255   PyObject * MyPyObjIorList = NULL ;
256   PyObject * ResultIor = NULL ;
257
258   CORBA::Object_ptr ObjRef ;
259   char * IORObjRef ;
260
261   if ( !PyCallable_Check( MyPyRunMethod ) ) {
262     Result = NULL ;
263     RetVal = false ;
264   }
265   else {
266     ArgsList = PyTuple_New( n_in ) ;
267
268     for ( i = 0 ; i < n_in ; i++ ) {
269       data = inParams[i].Value ;
270       sname = inParams[i].Name.c_str() ;
271       switch ( data.type()->kind() ) {
272       case CORBA::tk_string : {
273         char * t ;
274         data >>= t ;
275         PyObject * ArgValue = Py_BuildValue( "s" , t ) ;
276         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
277         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << t << " (string) "
278                << " ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
279                << ArgValue->ob_refcnt << endl ;
280         break ;
281       }
282       case CORBA::tk_boolean : {
283         bool b ;
284         data >>= (CORBA::Any::to_boolean ) b ;
285         PyObject * ArgValue = Py_BuildValue( "b" , b ) ;
286         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
287         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << b
288                << " (boolean) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
289                << ArgValue->ob_refcnt << endl ;
290         break ;
291       }
292       case CORBA::tk_char : {
293         unsigned char c ;
294         data >>= (CORBA::Any::to_char ) c ;
295         PyObject * ArgValue = Py_BuildValue( "c" , c ) ;
296         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
297         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << c
298                << " (char) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
299                << ArgValue->ob_refcnt << endl ;
300         break ;
301       }
302       case CORBA::tk_short : {
303         short s ;
304         data >>= s ;
305         PyObject * ArgValue = Py_BuildValue( "h" , s ) ;
306         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
307         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << s
308                << " (short) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
309                << ArgValue->ob_refcnt << endl ;
310         break ;
311       }
312       case CORBA::tk_long : {
313         long l ;
314         data >>= l ;
315         PyObject * ArgValue = Py_BuildValue( "l" , l ) ;
316         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << l
317                << " ArgValue->ob_refcnt" << ArgValue->ob_refcnt << endl ;
318         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
319         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << l
320                << " (long) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
321                << ArgValue->ob_refcnt << endl ;
322         break ;
323       }
324       case CORBA::tk_float : {
325         float f ;
326         data >>= f ;
327         PyObject * ArgValue = Py_BuildValue( "f" , f ) ;
328         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
329         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << f
330                << " (float) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
331                << ArgValue->ob_refcnt << endl ;
332         break ;
333       }
334       case CORBA::tk_double : {
335         double d ;
336         data >>= d ;
337         PyObject * ArgValue = Py_BuildValue( "d" , d ) ;
338         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
339         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << d
340                << " (double) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
341                << ArgValue->ob_refcnt << endl ;
342         break ;
343       }
344       case CORBA::tk_objref : {
345         MyPyObjRefList = PyTuple_New( 1 ) ;
346         PyObject * ObjValue ;
347         data >>= ObjRef ;
348         IORObjRef = ObjectToString( ObjRef ) ;
349         ObjValue = Py_BuildValue( "s" , IORObjRef ) ;
350         PyTuple_SetItem( MyPyObjRefList , 0 , ObjValue ) ;
351         cdebug << "ArgIn" << i << " : " << sname << " " << method << " " << " Value " << IORObjRef << " (objref) "
352                << MyPyObjRef->ob_refcnt << "/" << MyPyObjRefList->ob_refcnt << endl ;
353 //        ResultObj = PyEval_CallObject( MyPyObjRef , MyPyObjRefList ) ;
354         ResultObj = PyEvalCallObject( "PyObjRef" , MyPyObjRef , MyPyObjRefList ) ;
355         cdebug << "ObjValue->ob_refcnt" << ObjValue->ob_refcnt << endl ;
356         ArgValue = Py_BuildValue( "O" , ResultObj ) ;
357         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
358         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value  (objref) ArgsList->ob_refcnt"
359                << ArgsList->ob_refcnt << " ArgValue->ob_refcnt" << ArgValue->ob_refcnt << endl ;
360         cdebug << "MyPyObjRefList->ob_refcnt " << MyPyObjRefList->ob_refcnt-1 << endl ;
361         Py_DECREF( MyPyObjRefList ) ;
362         if ( CORBA::is_nil( ObjRef ) ) {
363           ResultObj = NULL ;
364         }
365         else {
366           cdebug << "ResultObj->ob_refcnt " << ResultObj->ob_refcnt-1 << endl ;
367           Py_DECREF( ResultObj ) ;
368         }
369         break ;
370       }
371       default : {
372         cdebug << "ArgIn" << i << " : " << sname << " Value " << "(other ERROR)" << endl ;
373       }
374       }
375     }
376
377 //    Result = PyEval_CallObject( MyPyRunMethod , ArgsList ) ;
378     Result = PyEvalCallObject( method , MyPyRunMethod , ArgsList ) ;
379
380     cdebug << "ArgsList->ob_refcnt" << ArgsList->ob_refcnt << endl ;
381
382     if ( Result == NULL ) {
383       cdebug_out << "GraphExecutor::InNode::PyDynInvoke Node " << Name() << " " << method << " Error Result == NULL"
384                  << endl ;
385       RetVal = false ;
386     }
387     else {
388       for ( i = 0 ; i < n_out ; i++ ) {
389         data = outParams[i].Value ;
390         sname = outParams[i].Name.c_str() ;
391         switch ( data.type()->kind() ) {
392         case CORBA::tk_string : {
393           char * t = "" ;
394           PyObject * ArgValue ;
395           if ( PyTuple_Check( Result ) ) {
396             ArgValue = PyTuple_GetItem( Result , i ) ;
397           }
398           else {
399             ArgValue = Result ;
400           }
401           if ( !PyString_Check( ArgValue ) ) {
402             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (string)" << endl ;
403           }
404           else {
405             t = PyString_AsString( ArgValue ) ;
406           }
407           data <<= t ;
408           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << t << " (string)"
409                  << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
410                  << ArgValue->ob_refcnt << endl ;
411           break ;
412         }
413         case CORBA::tk_boolean : {
414           bool b = false ;
415           PyObject * ArgValue ;
416           if ( PyTuple_Check( Result ) ) {
417             ArgValue = PyTuple_GetItem( Result , i ) ;
418           }
419           else {
420             ArgValue = Result ;
421           }
422           if ( !PyInt_Check( ArgValue ) ) {
423             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (boolean)" << endl ;
424           }
425           else {
426             b = PyInt_AsLong( ArgValue ) ;
427           }
428           data <<= (CORBA::Any::from_boolean ) b ;
429           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << b << " (boolean)"
430                  << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
431                  << ArgValue->ob_refcnt << endl ;
432           break ;
433         }
434         case CORBA::tk_char : {
435           unsigned char c = 0 ;
436           PyObject * ArgValue ;
437           if ( PyTuple_Check( Result ) ) {
438             ArgValue = PyTuple_GetItem( Result , i ) ;
439           }
440           else {
441             ArgValue = Result ;
442           }
443           if ( !PyInt_Check( ArgValue ) ) {
444             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (char)" << endl ;
445           }
446           else {
447             c = PyInt_AsLong( ArgValue ) ;
448           }
449           data <<= (CORBA::Any::from_char ) c ;
450           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << c << " (char)"
451                  << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
452                  << ArgValue->ob_refcnt << endl ;
453           break ;
454         }
455         case CORBA::tk_short : {
456           short s = 0 ;
457           PyObject * ArgValue ;
458           if ( PyTuple_Check( Result ) ) {
459             ArgValue = PyTuple_GetItem( Result , i ) ;
460           }
461           else {
462             ArgValue = Result ;
463           }
464           if ( !PyInt_Check( ArgValue ) ) {
465             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (short)" << endl ;
466           }
467           else {
468             s = PyInt_AsLong( ArgValue ) ;
469           }
470           data <<= s ;
471           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << s << " (short)"
472                  << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
473                  << ArgValue->ob_refcnt << endl ;
474           break ;
475         }
476         case CORBA::tk_long : {
477           long l = 0 ;
478           PyObject * ArgValue ;
479           if ( PyTuple_Check( Result ) ) {
480             ArgValue = PyTuple_GetItem( Result , i ) ;
481           }
482           else {
483             ArgValue = Result ;
484           }
485           if ( PyLong_Check( ArgValue ) ) {
486             l = PyLong_AsLong( ArgValue ) ;
487           }
488           else if ( PyInt_Check( ArgValue ) ) {
489             l = PyInt_AsLong( ArgValue ) ;
490           }
491           else {
492             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (CORBA::tk_long)" << endl ;
493           }
494           data <<= l ;
495           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << l << " (long)"
496                  << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
497                  << ArgValue->ob_refcnt << endl ;
498           break ;
499         }
500         case CORBA::tk_float : {
501           float f = 0 ;
502           PyObject * ArgValue ;
503           if ( PyTuple_Check( Result ) ) {
504             ArgValue = PyTuple_GetItem( Result , i ) ;
505           }
506           else {
507             ArgValue = Result ;
508           }
509           if ( !PyFloat_Check( ArgValue ) ) {
510             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (float)" << endl ;
511           }
512           else {
513             f = PyFloat_AsDouble( ArgValue ) ;
514           }
515           data <<= f ;
516           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << f << " (float)"
517                  << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
518                  << ArgValue->ob_refcnt << endl ;
519           break ;
520         }
521         case CORBA::tk_double : {
522           double d = 0 ;
523           PyObject * ArgValue ;
524           if ( PyTuple_Check( Result ) ) {
525             ArgValue = PyTuple_GetItem( Result , i ) ;
526           }
527           else {
528             ArgValue = Result ;
529           }
530           if ( !PyFloat_Check( ArgValue ) ) {
531             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (double)" << endl ;
532           }
533           else {
534             d = PyFloat_AsDouble( ArgValue ) ;
535           }
536           data <<= d ;
537           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << d << " (double)"
538                  << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
539                  << ArgValue->ob_refcnt << endl ;
540           break ;
541         }
542         case CORBA::tk_objref : {
543           PyObject * ObjIor ;
544           MyPyObjIorList = PyTuple_New( 1 ) ;
545           if ( PyTuple_Check( Result ) ) {
546             ObjIor = PyTuple_GetItem( Result , i ) ;
547           }
548           else {
549             ObjIor = Result ;
550           }
551           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << "(object reference) "
552                  << " Result->ob_refcnt" << Result->ob_refcnt << " ObjIor->ob_refcnt"
553                  << ObjIor->ob_refcnt << endl ;
554           Py_INCREF( ObjIor ) ;
555 //          PyObject_Print( ObjIor , stdout , 0 ) ;
556           PyTuple_SetItem( MyPyObjIorList , 0 , ObjIor ) ;
557 //          ResultIor = PyEval_CallObject( MyPyObjIor , MyPyObjIorList ) ;
558           ResultIor = PyEvalCallObject( "PyObjIor" , MyPyObjIor , MyPyObjIorList ) ;
559           cdebug << "ObjIor->ob_refcnt " << ObjIor->ob_refcnt-1 << endl ;
560           Py_DECREF( ObjIor ) ;
561           cdebug << "MyPyObjIorList->ob_refcnt " << MyPyObjIorList->ob_refcnt-1 << endl ;
562           Py_DECREF( MyPyObjIorList ) ;
563           cdebug << "MyPyObjIor->ob_refcnt " << MyPyObjIor->ob_refcnt << endl ;
564           if ( ResultIor ) {
565             char * IOR = NULL ;
566             IOR = PyString_AsString( ResultIor ) ;
567             ObjRef = StringToObject( IOR ) ;
568             data <<= ObjRef ;
569             IORObjRef = ObjectToString( ObjRef ) ;
570             cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << IORObjRef << " (objref) "
571                    << endl ;
572             if ( CORBA::is_nil( ObjRef ) ) {
573               ResultIor = NULL ;
574             }
575             else {
576               cdebug << "ResultIor->ob_refcnt " << ResultIor->ob_refcnt-1 << endl ;
577               Py_DECREF( ResultIor ) ;
578             }
579           }
580           else {
581             cdebug_out << "GraphExecutor::InNode::PyDynInvoke Error ResultIor == NULL Node "
582                        << Name() << " method " << method << " " << endl ;
583             RetVal = false ;
584           }
585           break ;
586         }
587         default : {
588           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << "(other ERROR)" << endl ;
589         }
590         }
591         outParams[i].Value = data ;
592       }
593
594 //      int k ;
595 //      for ( k = 0 ; k < n_out ; k++ ) {
596 //        cdebug << "Result" << k << "->ob_refcnt " << PyTuple_GetItem( Result , k )->ob_refcnt << endl ;
597 //        Py_DECREF( PyTuple_GetItem( Result , k ) ) ;
598 //      }
599
600       cdebug << "InNode::PyDynInvoke Result->ob_refcnt " << Result->ob_refcnt-1 << endl ;
601       Py_DECREF( Result ) ;
602     }
603
604 //    int k ;
605 //    for ( k = 0 ; k < n_in ; k++ ) {
606 //      cdebug << "ArgsList" << k << "->ob_refcnt " << PyTuple_GetItem( ArgsList , k )->ob_refcnt << endl ;
607 //      Py_DECREF( PyTuple_GetItem( ArgsList , k ) ) ;
608 //    }
609
610     cdebug << "InNode::PyDynInvoke ArgsList->ob_refcnt " << ArgsList->ob_refcnt-1 << endl ;
611     Py_DECREF( ArgsList ) ;
612   }
613
614   cdebug_out << "GraphExecutor::InNode::PyDynInvoke Node " << Name() << " method " << method << " " << RetVal
615              << endl ;
616
617   return RetVal ;
618
619 }
620
621 bool GraphExecutor::InNode::PyRunSimpleString( char* thePyString )
622 {
623   const bool ErrorValue = true;
624   bool aRet;
625   try {
626     MESSAGE( pthread_self() << "Python method beginning : " << thePyString );
627     cdebug_in << pthread_self() << "Python method beginning : " << thePyString << endl ;
628     aRet = PyRun_SimpleString( thePyString );
629     // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL    
630     //aRet = _OutNode->SuperVisionContainer()->ActivatePythonExecution( thePyString ) ;
631     MESSAGE( pthread_self() << "Python method finished." );
632     cdebug_out << pthread_self() << "Python method finished." << endl ;
633   } catch( ... ) {
634     MESSAGE( pthread_self() << "ERROR: Exception caught running Python method." );
635     cdebug_out << pthread_self() << "ERROR: Exception caught running Python method."
636                << endl ;
637     MESSAGE( "       Python was reinitialized.  Previous Python definitions are lost" );
638     // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL    
639     Py_Finalize();
640     Py_Initialize();
641     Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod );
642     _OutNode->PyInitialized( false );
643     aRet = ErrorValue;
644   }
645   return aRet;
646 }
647
648 PyObject * GraphExecutor::InNode::PyEvalCallObject( const char *method ,
649                                                     PyObject * MyPyRunMethod ,
650                                                     PyObject * ArgsList ) {
651 //  cdebug_in << "Executor::InNode::PyEvalCallObject " << Name() << endl ;
652   PyObject * Result = NULL ;
653   try {
654 //    MESSAGE( pthread_self() << "PyEval_CallObject " << Name() << " method beginning : " << method );
655 //    cdebug << pthread_self() << "PyEval_CallObject " << Name() << " method beginning : " << method << endl ;
656     Result = PyEval_CallObject( MyPyRunMethod , ArgsList ) ;
657     // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL    
658     //Result = _OutNode->SuperVisionContainer()->ActivatePythonExecution( MyPyRunMethod , ArgsList ) ;
659 //    MESSAGE( pthread_self() << "PyEval_CallObject method finished. Result " << Result );
660 //    cdebug << pthread_self() << "PyEval_CallObject method finished. Result " << Result << endl ;
661 //    cdebug_out << "Executor::InNode::PyEvalCallObject " << Name() << endl ;
662   } catch( ... ) {
663     MESSAGE( pthread_self() << "ERROR: Exception caught PyEval_CallObject " << Name()
664              << " Python method " << method << ". Result "  << Result );
665     cdebug << pthread_self() << "ERROR: Exception caught PyEval_CallObject " << Name()
666            << " Python method " << method << ". Result " << Result << endl ;
667     MESSAGE( "       Python was reinitialized.  Previous Python definitions are lost Py_IsInitialized " << Py_IsInitialized() );
668 //JR ===> fatal error in python : no current thread
669     // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL    
670     Py_Finalize();
671     Py_Initialize();
672     Automaton()->PyInitModule( false ) ;
673     Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod );
674     _OutNode->PyInitialized( false );
675
676     if ( Result == NULL ) {
677       Kill() ; // Reset of _ThreadId
678     }
679 //    cdebug << "Executor::InNode::PyEvalCallObject ERROR catched " << Name()
680 //           << " Py_IsInitialized " << Py_IsInitialized() << endl ;
681   }
682 //  cdebug_out << "Executor::InNode::PyEvalCallObject " << Name() << endl ;
683   return Result ;
684 }