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