]> SALOME platform Git repositories - modules/superv.git/blob - src/GraphExecutor/DataFlowExecutor_PyDynInvoke.cxx
Salome HOME
Merging with JR_ASV_2_1_0_deb_with_KERNEL_Head branch, which contains many bug fixes...
[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         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         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         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         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         ArgValue = Py_BuildValue( "l" , l ) ;
316         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
317         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << l
318                << " (long) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
319                << ArgValue->ob_refcnt << endl ;
320         break ;
321       }
322       case CORBA::tk_float : {
323         float f ;
324         data >>= f ;
325         ArgValue = Py_BuildValue( "f" , f ) ;
326         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
327         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << f
328                << " (float) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
329                << ArgValue->ob_refcnt << endl ;
330         break ;
331       }
332       case CORBA::tk_double : {
333         double d ;
334         data >>= d ;
335         ArgValue = Py_BuildValue( "d" , d ) ;
336         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
337         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << d
338                << " (double) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
339                << ArgValue->ob_refcnt << endl ;
340         break ;
341       }
342       case CORBA::tk_objref : {
343         MyPyObjRefList = PyTuple_New( 1 ) ;
344         PyObject * ObjValue ;
345         data >>= ObjRef ;
346         IORObjRef = ObjectToString( ObjRef ) ;
347         ObjValue = Py_BuildValue( "s" , IORObjRef ) ;
348         PyTuple_SetItem( MyPyObjRefList , 0 , ObjValue ) ;
349         cdebug << "ArgIn" << i << " : " << sname << " " << method << " " << " Value " << IORObjRef << " (objref) "
350                << MyPyObjRef->ob_refcnt << "/" << MyPyObjRefList->ob_refcnt << endl ;
351 //        ResultObj = PyEval_CallObject( MyPyObjRef , MyPyObjRefList ) ;
352         ResultObj = PyEvalCallObject( MyPyObjRef , MyPyObjRefList ) ;
353         cdebug << "ObjValue->ob_refcnt" << ObjValue->ob_refcnt << endl ;
354         ArgValue = Py_BuildValue( "O" , ResultObj ) ;
355         PyTuple_SetItem( ArgsList , i , ArgValue ) ;
356         cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value  (objref) ArgsList->ob_refcnt"
357                << ArgsList->ob_refcnt << " ArgValue->ob_refcnt" << ArgValue->ob_refcnt << endl ;
358         cdebug << "MyPyObjRefList->ob_refcnt " << MyPyObjRefList->ob_refcnt-1 << endl ;
359         Py_DECREF( MyPyObjRefList ) ;
360         if ( CORBA::is_nil( ObjRef ) ) {
361           ResultObj = NULL ;
362         }
363         else {
364           cdebug << "ResultObj->ob_refcnt " << ResultObj->ob_refcnt-1 << endl ;
365           Py_DECREF( ResultObj ) ;
366         }
367         break ;
368       }
369       default : {
370         cdebug << "ArgIn" << i << " : " << sname << " Value " << "(other ERROR)" << endl ;
371       }
372       }
373     }
374
375 //    Result = PyEval_CallObject( MyPyRunMethod , ArgsList ) ;
376     Result = PyEvalCallObject( MyPyRunMethod , ArgsList ) ;
377
378     cdebug << "ArgsList->ob_refcnt" << ArgsList->ob_refcnt << endl ;
379
380     if ( Result == NULL ) {
381       cdebug_out << "GraphExecutor::InNode::PyDynInvoke Node " << Name() << " " << method << " Error Result == NULL"
382                  << endl ;
383       RetVal = false ;
384     }
385     else {
386       for ( i = 0 ; i < n_out ; i++ ) {
387         data = outParams[i].Value ;
388         sname = outParams[i].Name.c_str() ;
389         switch ( data.type()->kind() ) {
390         case CORBA::tk_string : {
391           char * t = "" ;
392           if ( PyTuple_Check( Result ) ) {
393             ArgValue = PyTuple_GetItem( Result , i ) ;
394           }
395           else {
396             ArgValue = Result ;
397           }
398           if ( !PyString_Check( ArgValue ) ) {
399             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (string)" << endl ;
400           }
401           else {
402             t = PyString_AsString( ArgValue ) ;
403           }
404           data <<= t ;
405           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << t << " (string)"
406                  << " ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
407                  << ArgValue->ob_refcnt << endl ;
408           break ;
409         }
410         case CORBA::tk_boolean : {
411           bool b = false ;
412           if ( PyTuple_Check( Result ) ) {
413             ArgValue = PyTuple_GetItem( Result , i ) ;
414           }
415           else {
416             ArgValue = Result ;
417           }
418           if ( !PyInt_Check( ArgValue ) ) {
419             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (boolean)" << endl ;
420           }
421           else {
422             b = PyInt_AsLong( ArgValue ) ;
423           }
424           data <<= (CORBA::Any::from_boolean ) b ;
425           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << b << " (boolean)"
426                  << " ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
427                  << ArgValue->ob_refcnt << endl ;
428           break ;
429         }
430         case CORBA::tk_char : {
431           unsigned char c = 0 ;
432           if ( PyTuple_Check( Result ) ) {
433             ArgValue = PyTuple_GetItem( Result , i ) ;
434           }
435           else {
436             ArgValue = Result ;
437           }
438           if ( !PyInt_Check( ArgValue ) ) {
439             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (char)" << endl ;
440           }
441           else {
442             c = PyInt_AsLong( ArgValue ) ;
443           }
444           data <<= (CORBA::Any::from_char ) c ;
445           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << c << " (char)"
446                  << " ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
447                  << ArgValue->ob_refcnt << endl ;
448           break ;
449         }
450         case CORBA::tk_short : {
451           short s = 0 ;
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 (short)" << endl ;
460           }
461           else {
462             s = PyInt_AsLong( ArgValue ) ;
463           }
464           data <<= s ;
465           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << s << " (short)"
466                  << " ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
467                  << ArgValue->ob_refcnt << endl ;
468           break ;
469         }
470         case CORBA::tk_long : {
471           long l = 0 ;
472           if ( PyTuple_Check( Result ) ) {
473             ArgValue = PyTuple_GetItem( Result , i ) ;
474           }
475           else {
476             ArgValue = Result ;
477           }
478           if ( PyLong_Check( ArgValue ) ) {
479             l = PyLong_AsLong( ArgValue ) ;
480           }
481           else if ( PyInt_Check( ArgValue ) ) {
482             l = PyInt_AsLong( ArgValue ) ;
483           }
484           else {
485             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (CORBA::tk_long)" << endl ;
486           }
487           data <<= l ;
488           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << l << " (long)"
489                  << " ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
490                  << ArgValue->ob_refcnt << endl ;
491           break ;
492         }
493         case CORBA::tk_float : {
494           float f = 0 ;
495           if ( PyTuple_Check( Result ) ) {
496             ArgValue = PyTuple_GetItem( Result , i ) ;
497           }
498           else {
499             ArgValue = Result ;
500           }
501           if ( !PyFloat_Check( ArgValue ) ) {
502             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (float)" << endl ;
503           }
504           else {
505             f = PyFloat_AsDouble( ArgValue ) ;
506           }
507           data <<= f ;
508           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << f << " (float)"
509                  << " ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
510                  << ArgValue->ob_refcnt << endl ;
511           break ;
512         }
513         case CORBA::tk_double : {
514           double d = 0 ;
515           if ( PyTuple_Check( Result ) ) {
516             ArgValue = PyTuple_GetItem( Result , i ) ;
517           }
518           else {
519             ArgValue = Result ;
520           }
521           if ( !PyFloat_Check( ArgValue ) ) {
522             cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (double)" << endl ;
523           }
524           else {
525             d = PyFloat_AsDouble( ArgValue ) ;
526           }
527           data <<= d ;
528           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << d << " (double)"
529                  << " ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
530                  << ArgValue->ob_refcnt << endl ;
531           break ;
532         }
533         case CORBA::tk_objref : {
534           PyObject * ObjIor ;
535           MyPyObjIorList = PyTuple_New( 1 ) ;
536           if ( PyTuple_Check( Result ) ) {
537             ObjIor = PyTuple_GetItem( Result , i ) ;
538           }
539           else {
540             ObjIor = Result ;
541           }
542           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << "(object reference) "
543                  << " ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ObjIor->ob_refcnt"
544                  << ObjIor->ob_refcnt << endl ;
545           Py_INCREF( ObjIor ) ;
546 //          PyObject_Print( ObjIor , stdout , 0 ) ;
547           PyTuple_SetItem( MyPyObjIorList , 0 , ObjIor ) ;
548 //          ResultIor = PyEval_CallObject( MyPyObjIor , MyPyObjIorList ) ;
549           ResultIor = PyEvalCallObject( MyPyObjIor , MyPyObjIorList ) ;
550           cdebug << "ObjIor->ob_refcnt " << ObjIor->ob_refcnt-1 << endl ;
551           Py_DECREF( ObjIor ) ;
552           cdebug << "MyPyObjIorList->ob_refcnt " << MyPyObjIorList->ob_refcnt-1 << endl ;
553           Py_DECREF( MyPyObjIorList ) ;
554           cdebug << "MyPyObjIor->ob_refcnt " << MyPyObjIor->ob_refcnt << endl ;
555           if ( ResultIor ) {
556             char * IOR = NULL ;
557             IOR = PyString_AsString( ResultIor ) ;
558             ObjRef = StringToObject( IOR ) ;
559             data <<= ObjRef ;
560             IORObjRef = ObjectToString( ObjRef ) ;
561             cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << IORObjRef << " (objref) "
562                    << endl ;
563             if ( CORBA::is_nil( ObjRef ) ) {
564               ResultIor = NULL ;
565             }
566             else {
567               cdebug << "ResultIor->ob_refcnt " << ResultIor->ob_refcnt-1 << endl ;
568               Py_DECREF( ResultIor ) ;
569             }
570           }
571           else {
572             cdebug_out << "GraphExecutor::InNode::PyDynInvoke Error ResultIor == NULL Node "
573                        << Name() << " method " << method << " " << endl ;
574             RetVal = false ;
575           }
576           break ;
577         }
578         default : {
579           cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << "(other ERROR)" << endl ;
580         }
581         }
582         outParams[i].Value = data ;
583       }
584
585       cdebug << "Result->ob_refcnt" << Result->ob_refcnt-1 << endl ;
586       Py_DECREF( Result ) ;
587     }
588
589     cdebug << "GraphExecutor::InNode::PyDynInvoke ArgsList->ob_refcnt"
590            << ArgsList->ob_refcnt-1 << endl ;
591     Py_DECREF( ArgsList ) ;
592   }
593
594   cdebug_out << "GraphExecutor::InNode::PyDynInvoke Node " << Name() << " method " << method << " " << RetVal
595              << endl ;
596
597   return RetVal ;
598
599 }
600
601 bool GraphExecutor::InNode::PyRunSimpleString( char* thePyString )
602 {
603   const bool ErrorValue = true;
604   bool aRet;
605   try {
606     MESSAGE( pthread_self() << "Python method beginning : " << thePyString );
607     cdebug_in << pthread_self() << "Python method beginning : " << thePyString << endl ;
608     aRet = PyRun_SimpleString( thePyString );
609     // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL    
610     //aRet = _OutNode->SuperVisionContainer()->ActivatePythonExecution( thePyString ) ;
611     MESSAGE( pthread_self() << "Python method finished." );
612     cdebug_out << pthread_self() << "Python method finished." << endl ;
613   } catch( ... ) {
614     MESSAGE( pthread_self() << "ERROR: Exception caught running Python method." );
615     cdebug_out << pthread_self() << "ERROR: Exception caught running Python method."
616                << endl ;
617     MESSAGE( "       Python was reinitialized.  Previous Python definitions are lost" );
618     // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL    
619     Py_Finalize();
620     Py_Initialize();
621     Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod );
622     _OutNode->PyInitialized( false );
623     aRet = ErrorValue;
624   }
625   return aRet;
626 }
627
628 PyObject * GraphExecutor::InNode::PyEvalCallObject( PyObject * MyPyRunMethod ,
629                                                     PyObject * ArgsList ) {
630   cdebug_in << "Executor::InNode::PyEvalCallObject " << Name() << endl ;
631   PyObject * Result = NULL ;
632   try {
633     MESSAGE( pthread_self() << "PyEval_CallObject method beginning : " );
634     cdebug << pthread_self() << "PyEval_CallObject method beginning : " << Name() << endl ;
635     Result = PyEval_CallObject( MyPyRunMethod , ArgsList ) ;
636     // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL    
637     //Result = _OutNode->SuperVisionContainer()->ActivatePythonExecution( MyPyRunMethod , ArgsList ) ;
638     MESSAGE( pthread_self() << "PyEval_CallObject method finished. Result " << Result );
639     cdebug << pthread_self() << "PyEval_CallObject method finished. Result " << Result << endl ;
640     cdebug_out << "Executor::InNode::PyEvalCallObject " << Name() << endl ;
641   } catch( ... ) {
642     MESSAGE( pthread_self() << "ERROR: Exception caught PyEval_CallObject Python method. Result "  << Result );
643     cdebug << pthread_self() << "ERROR: Exception caught PyEval_CallObject Python method. Result "
644            << Result << endl ;
645     MESSAGE( "       Python was reinitialized.  Previous Python definitions are lost Py_IsInitialized " << Py_IsInitialized() );
646 //JR ===> fatal error in python : no current thread
647     // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL    
648     Py_Finalize();
649     Py_Initialize();
650     Automaton()->PyInitModule( false ) ;
651     Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod );
652     _OutNode->PyInitialized( false );
653
654     if ( Result == NULL ) {
655       Kill() ; // Reset of _ThreadId
656     }
657     cdebug_out << "Executor::InNode::PyEvalCallObject ERROR catched " << Name()
658                << " Py_IsInitialized " << Py_IsInitialized() << endl ;
659   }
660   return Result ;
661 }