1 // SUPERV GraphExecutor : contains classes that permit execution of graphs and particularly the execution automaton
3 // Copyright (C) 2003 CEA/DEN, EDF R&D
7 // File : DataFlowExecutor_PyDynInvoke.cxx
8 // Author : Jean Rahuel, CEA
17 #include "DataFlowExecutor_InNode.hxx"
18 #include "DataFlowExecutor_OutNode.hxx"
20 using namespace CORBA ;
22 extern GraphExecutor::FiniteStateMachine * theAutomaton ;
24 //static PyObject * MyPyObjSignal = NULL ;
25 static PyObject * MyPyObjRef = NULL ;
26 static PyObject * MyPyObjIor = NULL ;
27 static PyObject * MyPyRunMethod = NULL ;
29 static PyMethodDef MethodPyRunMethod[] = {
30 { "RunMethod", PyRunMethod, METH_VARARGS },
34 bool GraphExecutor::InNode::InitPython() {
35 cdebug_in << "InitPython" << endl ;
37 Automaton()->PyLock() ;
38 // PyObject * Module = Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod ) ;
39 if ( !Automaton()->PyInitModule() ) {
40 Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod ) ;
41 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
42 //_OutNode->SuperVisionContainer()->ActivatePythonExecution( "InitPyRunMethod" , MethodPyRunMethod ) ;
44 // PAL10310: patch on omniORB
46 aPyFunc = "import omnipatch\n" ;
47 if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
48 cdebug << ThreadNo() << " " << Name() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ;
49 Automaton()->PyUnLock() ;
53 // PyObject * Dictionnary = PyModule_GetDict( Module ) ;
54 // InitPyDynInvokeError = PyErr_NewException( "InitPyDynInvokeError" , NULL , NULL ) ;
55 // PyDict_SetItemString( Dictionnary , InitPyDynInvokeError ) ;
56 cdebug << ThreadNo() << "Py_Initialized() " << endl ;
59 //JR : Python documentation says that there is no sense to set a handler of synchronous
60 // signals as SIGSEGV and SIGFPE !!!
61 if ( Automaton()->PyFunction( "PyObjSignal" ) == NULL ) {
63 aPyFunc = "import signal\n" ;
64 aPyFunc += "import InitPyRunMethod\n" ;
65 aPyFunc += "import sys\n" ;
66 aPyFunc += "import CORBA\n" ;
67 aPyFunc += "import omniORB\n" ;
68 aPyFunc += "def PySignalHandler( aSignal , aStackFrame ) :\n" ;
69 aPyFunc += " print 'PySignalHandler(aSignal =',aSignal,',aStackFrame= ',aStackFrame,')'\n" ;
70 aPyFunc += "InitPyRunMethod.RunMethod( PySignalHandler )\n" ;
71 aPyFunc += "PrevHandler = signal.signal( signal.SIGSEGV , PySignalHandler )\n" ;
72 aPyFunc += "print 'PyObjSignal PrevHandler of SIGSEGV :', PrevHandler\n" ;
73 aPyFunc += "print 'PyObjSignal actual handler of SIGSEGV :', signal.getsignal( signal.SIGSEGV )\n" ;
74 // if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
75 if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) {
76 cdebug << ThreadNo() << " " << Name() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ;
77 Automaton()->PyUnLock() ;
80 MyPyObjSignal = MyPyRunMethod ;
81 Automaton()->PyFunction( "PyObjSignal" , MyPyObjSignal ) ;
84 MyPyObjSignal = Automaton()->PyFunction( "PyObjSignal" ) ;
88 // PyObjRef convert an IOR (string) to an Python ObjectReference
89 if ( Automaton()->PyFunction( "PyObjRef" ) == NULL ) {
91 aPyFunc = "import InitPyRunMethod\n" ;
92 aPyFunc += "import sys\n" ;
93 aPyFunc += "import CORBA\n" ;
94 aPyFunc += "import omniORB\n" ;
95 aPyFunc += "orb = CORBA.ORB_init( sys.argv , CORBA.ORB_ID )\n" ;
96 aPyFunc += "def PyObjRef( IORObjStr ) :\n" ;
97 // aPyFunc += " orb = CORBA.ORB_init( sys.argv , CORBA.ORB_ID )\n" ;
98 aPyFunc += " objref = orb.string_to_object( IORObjStr )\n" ;
99 aPyFunc += " return objref\n" ;
100 aPyFunc += "InitPyRunMethod.RunMethod( PyObjRef )\n" ;
101 if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
102 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
103 //if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) {
104 cdebug << ThreadNo() << " " << Name() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ;
105 Automaton()->PyUnLock() ;
108 MyPyObjRef = MyPyRunMethod ;
109 Automaton()->PyFunction( "PyObjRef" , MyPyObjRef ) ;
112 MyPyObjRef = Automaton()->PyFunction( "PyObjRef" ) ;
115 // PyObjRef convert an Python ObjectReference to an IOR (string)
116 if ( Automaton()->PyFunction( "PyObjIor" ) == NULL ) {
118 aPyFunc = "import InitPyRunMethod\n" ;
119 aPyFunc += "import sys\n" ;
120 aPyFunc += "import CORBA\n" ;
121 aPyFunc += "import omniORB\n" ;
122 aPyFunc += "orb = CORBA.ORB_init( sys.argv , CORBA.ORB_ID )\n" ;
123 aPyFunc += "def PyObjIor( ObjRef ) :\n" ;
124 // aPyFunc += " orb = CORBA.ORB_init( sys.argv , CORBA.ORB_ID )\n" ;
125 aPyFunc += " objIor = orb.object_to_string( ObjRef )\n" ;
126 aPyFunc += " return objIor\n" ;
127 aPyFunc += "InitPyRunMethod.RunMethod( PyObjIor )\n" ;
128 if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
129 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
130 //if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) {
131 cdebug << ThreadNo() << " " << Name() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ;
132 Automaton()->PyUnLock() ;
135 MyPyObjIor = MyPyRunMethod ;
136 Automaton()->PyFunction( "PyObjIor" , MyPyObjIor ) ;
139 MyPyObjIor = Automaton()->PyFunction( "PyObjIor" ) ;
141 Automaton()->PyUnLock() ;
143 cdebug_out << "InitPython" << endl ;
148 void GraphExecutor::InNode::RemovePyDynInvoke( char * PyFuncName ) {
149 // Automaton()->PyLock() ; // Already done ...
150 if ( Automaton()->ErasePyFunction( PyFuncName ) ) {
151 cdebug << "InNode::RemovePyDynInvoke( " << PyFuncName << " ) success" << endl ;
154 cdebug << "InNode::RemovePyDynInvoke( " << PyFuncName << " ) ERROR failed" << endl ;
156 // Automaton()->PyUnLock() ;
159 PyObject * GraphExecutor::InNode::InitPyDynInvoke( char * PyFuncName ,
160 const SUPERV::ListOfStrings * aPythonFunction ,
165 PyObject * thePyRunMethod = NULL ;
167 cdebug_in << "InitPyDynInvoke '" << PyFuncName << "' length " << (*aPythonFunction).length() << endl ;
169 if ( strlen( PyFuncName ) ) {
170 Automaton()->PyLock() ;
172 thePyRunMethod = Automaton()->PyFunction( PyFuncName ) ;
174 //thePyRunMethod = NULL;
175 // asv 28.02.05 : VERY BAD fix of the following problem: after change of a function,
176 // the changes are NOT taken into account by Automation - it returns PyObject of the OLD function.
177 // so here we force re-automating the PyObject EVERY TIME, regardless if the function has changed or not.
178 // Once again - it is a very bad solution, it fully discards the whole idea of automation,
179 // here is it done as a quick fix for a bug.
180 // A better solution (to be implemented): store the PyObject NOT in Automation map, but in
181 // InLine node itself! And if the method is changed - remove the PyObject and force to regenerate it.
182 // But this means that PyObject must be stored in Editor's data model.
183 // asv 01.03.05 : the fix is not needed, the described bug is not reproduced. To investigate:
184 // WHERE PyObject is removed from Automation map on function change.
186 if ( (*aPythonFunction).length() ) {
187 if ( thePyRunMethod == NULL ) {
189 aPyFunc += "import InitPyRunMethod\n" ;
190 for ( i = 0 ; i < (*aPythonFunction).length() ; i++ ) {
191 aPyFunc += (*aPythonFunction)[ i ] ;
194 aPyFunc += "InitPyRunMethod.RunMethod( " ;
195 aPyFunc += PyFuncName ;
197 /*cdebug *///cout<< "InitPyDynInvoke PyRun_SimpleString " << endl << aPyFunc << endl ;
198 // if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
199 if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) {
200 cdebug << ThreadNo() << " " << Name() << " PyRunSimpleString ERROR " << endl << aPyFunc << endl ;
201 PyFuncRunned( false ) ;
206 PyFuncRunned( true ) ;
207 thePyRunMethod = MyPyRunMethod ;
209 Automaton()->PyFunction( PyFuncName , thePyRunMethod ) ;
210 cdebug << ThreadNo() << "PyRunSimpleString( " << (*aPythonFunction)[ 0 ] << " ) " << endl ;
214 Automaton()->PyUnLock() ;
215 cdebug_out << "InitPyDynInvoke '" << PyFuncName << "' thePyRunMethod " << thePyRunMethod << " "
216 << thePyRunMethod->ob_refcnt << endl ;
217 return thePyRunMethod ;
220 else if ( thePyRunMethod ) {
221 Automaton()->PyUnLock() ;
222 cdebug_out << "InitPyDynInvoke '" << PyFuncName << "' thePyRunMethod " << thePyRunMethod << " "
223 << thePyRunMethod->ob_refcnt << endl ;
224 return thePyRunMethod ;
226 Automaton()->PyUnLock() ;
229 cdebug_out << "InitPyDynInvoke '" << PyFuncName << "' thePyRunMethod Null Err " << Err << endl ;
234 extern "C" PyObject * PyRunMethod( PyObject * dummy , PyObject * Args ) {
235 PyObject * Result = NULL ;
236 PyObject * Temp = NULL ;
237 if ( PyArg_ParseTuple( Args , "O:set_callback" , & Temp ) ) {
238 // cout << "PyArg_ParsedTuple" << endl ;
239 if ( !PyCallable_Check( Temp ) ) {
240 // PyErr_SetString( PyExc_TypeError , "PyRunMethod must be callable" ) ;
241 // cout << "PyRunMethod must be callable" << endl ;
245 Py_XDECREF( MyPyRunMethod ) ;
246 MyPyRunMethod = Temp ;
247 Py_INCREF( Py_None ) ;
253 #define PyDynInvokeTrace 1
254 bool GraphExecutor::InNode::PyDynInvoke( PyObject * MyPyRunMethod ,
256 ServicesAnyData * InParametersList , int nInParams ,
257 ServicesAnyData * OutParametersList , int nOutParams ) {
265 cdebug_in << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name()
266 << " method " << method
267 << " InParametersList " << InParametersList
268 << " OutParametersList " << OutParametersList
269 << " " << nInParams << " InArgs " << nOutParams
270 << " OutArgs MyPyRunMethod " ;
272 if ( MyPyRunMethod ) {
274 cdebug << MyPyRunMethod << " " << MyPyRunMethod->ob_refcnt << endl ;
279 cdebug << " NULL" << endl ;
286 PyObject * ArgsList = NULL ;
287 PyObject * ArgValue = NULL ;
288 PyObject * Result = NULL ;
289 //PyObject * MyPyObjRefList = NULL ;
290 PyObject * ResultObj = NULL ;
291 //PyObject * MyPyObjIorList = NULL ;
292 PyObject * ResultIor = NULL ;
294 CORBA::Object_ptr ObjRef ;
298 if ( !PyCallable_Check( MyPyRunMethod ) ) {
303 ArgsList = PyTuple_New( nInParams ) ;
305 for ( i = 0 ; i < nInParams ; i++ ) {
306 data = InParametersList[i].Value ;
307 sname = InParametersList[i].Name.c_str() ;
308 switch ( data.type()->kind() ) {
309 case CORBA::tk_string : {
312 PyObject * ArgValue = Py_BuildValue( "s" , t ) ;
313 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
315 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << t << " (string) "
316 << " ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
317 << ArgValue->ob_refcnt << endl ;
321 case CORBA::tk_boolean : {
323 data >>= (CORBA::Any::to_boolean ) b ;
324 PyObject * ArgValue = Py_BuildValue( "b" , b ) ;
325 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
327 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << b
328 << " (boolean) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
329 << ArgValue->ob_refcnt << endl ;
333 case CORBA::tk_char : {
335 data >>= (CORBA::Any::to_char ) c ;
336 PyObject * ArgValue = Py_BuildValue( "c" , c ) ;
337 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
339 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << c
340 << " (char) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
341 << ArgValue->ob_refcnt << endl ;
345 case CORBA::tk_short : {
348 PyObject * ArgValue = Py_BuildValue( "h" , s ) ;
349 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
351 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << s
352 << " (short) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
353 << ArgValue->ob_refcnt << endl ;
357 case CORBA::tk_long : {
360 PyObject * ArgValue = Py_BuildValue( "l" , l ) ;
362 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << l
363 << " ArgValue->ob_refcnt" << ArgValue->ob_refcnt << endl ;
365 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
367 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << l
368 << " (long) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
369 << ArgValue->ob_refcnt << endl ;
373 case CORBA::tk_float : {
376 PyObject * ArgValue = Py_BuildValue( "f" , f ) ;
377 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
379 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << f
380 << " (float) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
381 << ArgValue->ob_refcnt << endl ;
385 case CORBA::tk_double : {
388 PyObject * ArgValue = Py_BuildValue( "d" , d ) ;
389 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
391 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << d
392 << " (double) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
393 << ArgValue->ob_refcnt << endl ;
397 case CORBA::tk_objref : {
398 //MyPyObjRefList = PyTuple_New( 1 ) ;
399 //PyObject * ObjValue ;
400 #if OMNIORB_VERSION >= 4
401 data >>= (CORBA::Any::to_object ) ObjRef ;
405 IORObjRef = ObjectToString( ObjRef ) ;
406 //ObjValue = Py_BuildValue( "s" , IORObjRef ) ;
407 //PyTuple_SetItem( MyPyObjRefList , 0 , ObjValue ) ;
409 cdebug << "ArgIn" << i << " : " << sname << " " << method << " " << " Value " << IORObjRef << " (objref) "<< endl ;
410 // << MyPyObjRef->ob_refcnt << "/" << MyPyObjRefList->ob_refcnt << endl ;
412 // ResultObj = PyEval_CallObject( MyPyObjRef , MyPyObjRefList ) ;
413 //ResultObj = PyEvalCallObject( "PyObjRef" , MyPyObjRef , MyPyObjRefList ) ;
415 //cdebug << "ObjValue->ob_refcnt" << ObjValue->ob_refcnt << endl ;
417 //ArgValue = Py_BuildValue( "O" , ResultObj ) ;
418 //PyTuple_SetItem( ArgsList , i , ArgValue ) ;
421 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value (objref) ArgsList->ob_refcnt"
422 << ArgsList->ob_refcnt << " ArgValue->ob_refcnt" << ArgValue->ob_refcnt << endl ;
424 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value (objref) ArgsList->ob_refcnt"
425 << ArgsList->ob_refcnt << " ArgValue is NULL" << endl ;
426 //cdebug << "MyPyObjRefList->ob_refcnt " << MyPyObjRefList->ob_refcnt-1 << endl ;
428 ResultObj = PyObject_CallFunction( MyPyObjRef , "s",IORObjRef ) ;
429 if(ResultObj == NULL){
432 Py_INCREF(ResultObj) ;
434 PyTuple_SetItem( ArgsList , i , ResultObj ) ;
435 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value (objref) ArgsList->ob_refcnt"
436 << ArgsList->ob_refcnt << " ArgValue->ob_refcnt" << ResultObj->ob_refcnt << endl ;
437 // << ArgsList->ob_refcnt << " ArgValue->ob_refcnt" << ArgValue->ob_refcnt << endl ;
438 //cdebug << "MyPyObjRefList->ob_refcnt " << MyPyObjRefList->ob_refcnt-1 << endl ;
439 //Py_DECREF( MyPyObjRefList ) ;
440 //if ( CORBA::is_nil( ObjRef ) ) {
441 // ResultObj = NULL ;
444 //#if PyDynInvokeTrace
446 //cdebug << "ResultObj->ob_refcnt " << ResultObj->ob_refcnt-1 << endl ;
448 //cdebug << "ResultObj is NULL" << endl ;
451 //Py_DECREF( ResultObj ) ;
456 cdebug << "ArgIn" << i << " : " << sname << " Value " << "(other ERROR)" << endl ;
461 // Result = PyEval_CallObject( MyPyRunMethod , ArgsList ) ;
462 Result = PyEvalCallObject( method , MyPyRunMethod , ArgsList ) ;
465 cdebug << "ArgsList->ob_refcnt" << ArgsList->ob_refcnt << endl ;
468 if ( Result == NULL ) {
469 cdebug_out << "GraphExecutor::InNode::PyDynInvoke Node " << Name() << " " << method << " Error Result == NULL"
475 for ( i = 0 ; i < nOutParams ; i++ ) {
476 data = OutParametersList[i].Value ;
477 sname = OutParametersList[i].Name.c_str() ;
478 switch ( data.type()->kind() ) {
479 case CORBA::tk_string : {
481 PyObject * ArgValue ;
482 if ( PyTuple_Check( Result ) ) {
483 ArgValue = PyTuple_GetItem( Result , i ) ;
488 if ( !PyString_Check( ArgValue ) ) {
489 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (string)" << endl ;
493 t = PyString_AsString( ArgValue ) ;
497 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << t << " (string)"
498 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
499 << ArgValue->ob_refcnt << endl ;
503 case CORBA::tk_boolean : {
505 PyObject * ArgValue ;
506 if ( PyTuple_Check( Result ) ) {
507 ArgValue = PyTuple_GetItem( Result , i ) ;
512 if ( !PyInt_Check( ArgValue ) ) {
513 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (boolean)" << endl ;
517 b = PyInt_AsLong( ArgValue ) ;
519 data <<= (CORBA::Any::from_boolean ) b ;
521 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << b << " (boolean)"
522 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
523 << ArgValue->ob_refcnt << endl ;
527 case CORBA::tk_char : {
528 unsigned char c = 0 ;
529 PyObject * ArgValue ;
530 if ( PyTuple_Check( Result ) ) {
531 ArgValue = PyTuple_GetItem( Result , i ) ;
536 //JR 04.04.2005 Debug if ( !PyInt_Check( ArgValue ) ) {
537 //Difficult to understand that behavior ... Python char type is a string of length 1 !
538 // or type is int ...
539 if ( !PyString_Check( ArgValue ) && !PyInt_Check( ArgValue ) ) {
540 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (char)" << endl ;
544 c = PyInt_AsLong( ArgValue ) ;
546 data <<= (CORBA::Any::from_char ) c ;
548 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << c << " (char)"
549 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
550 << ArgValue->ob_refcnt << endl ;
554 case CORBA::tk_short : {
556 PyObject * ArgValue ;
557 if ( PyTuple_Check( Result ) ) {
558 ArgValue = PyTuple_GetItem( Result , i ) ;
563 if ( !PyInt_Check( ArgValue ) ) {
564 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (short)" << endl ;
568 s = PyInt_AsLong( ArgValue ) ;
572 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << s << " (short)"
573 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
574 << ArgValue->ob_refcnt << endl ;
578 case CORBA::tk_long : {
580 PyObject * ArgValue ;
581 if ( PyTuple_Check( Result ) ) {
582 ArgValue = PyTuple_GetItem( Result , i ) ;
587 if ( PyLong_Check( ArgValue ) ) {
588 l = PyLong_AsLong( ArgValue ) ;
590 else if ( PyInt_Check( ArgValue ) ) {
591 l = PyInt_AsLong( ArgValue ) ;
594 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (CORBA::tk_long)" << endl ;
599 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << l << " (long)"
600 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
601 << ArgValue->ob_refcnt << endl ;
605 case CORBA::tk_float : {
607 PyObject * ArgValue ;
608 if ( PyTuple_Check( Result ) ) {
609 ArgValue = PyTuple_GetItem( Result , i ) ;
614 if ( !PyFloat_Check( ArgValue ) ) {
615 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (float)" << endl ;
619 f = PyFloat_AsDouble( ArgValue ) ;
621 data.replace(CORBA::TypeCode::PR_float_tc(), (void*)(&f));
624 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << f << " (float)"
625 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
626 << ArgValue->ob_refcnt << endl ;
630 case CORBA::tk_double : {
632 PyObject * ArgValue ;
633 if ( PyTuple_Check( Result ) ) {
634 ArgValue = PyTuple_GetItem( Result , i ) ;
639 if ( !PyFloat_Check( ArgValue ) ) {
640 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (double)" << endl ;
644 d = PyFloat_AsDouble( ArgValue ) ;
646 data.replace(CORBA::TypeCode::PR_double_tc(), (void*)(&d));
649 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << d << " (double)"
650 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
651 << ArgValue->ob_refcnt << endl ;
655 case CORBA::tk_objref : {
657 //MyPyObjIorList = PyTuple_New( 1 ) ;
658 if ( PyTuple_Check( Result ) ) {
659 ObjIor = PyTuple_GetItem( Result , i ) ;
665 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << "(object reference) "
666 << " Result->ob_refcnt" << Result->ob_refcnt << " ObjIor->ob_refcnt"
667 << ObjIor->ob_refcnt << endl ;
669 //Py_INCREF( ObjIor ) ;
670 // PyObject_Print( ObjIor , stdout , 0 ) ;
671 //PyTuple_SetItem( MyPyObjIorList , 0 , ObjIor ) ;
672 // ResultIor = PyEval_CallObject( MyPyObjIor , MyPyObjIorList ) ;
673 //ResultIor = PyEvalCallObject( "PyObjIor" , MyPyObjIor , MyPyObjIorList ) ;
674 ResultIor = PyObject_CallFunction( MyPyObjIor , "O",ObjIor ) ;
676 cdebug << "ObjIor->ob_refcnt " << ObjIor->ob_refcnt-1 << endl ;
678 //Py_DECREF( ObjIor ) ;
680 //cdebug << "MyPyObjIorList->ob_refcnt " << MyPyObjIorList->ob_refcnt-1 << endl ;
682 //Py_DECREF( MyPyObjIorList ) ;
684 //cdebug << "MyPyObjIor->ob_refcnt " << MyPyObjIor->ob_refcnt << endl ;
688 IOR = PyString_AsString( ResultIor ) ;
689 ObjRef = StringToObject( IOR ) ;
690 //JR 05.08.2005 BUG OMNIORB4 ? : that line does run with SALOME_3 (OMNIOrb4) for
691 // object references ... (It's Ok with OMNIOrb3)
692 // IOR and ObjRef and IORObjRef are Ok
693 // BUT data contains a nil reference !!!
697 SUPERV::SuperG_var mySuperVisionComponent ;
698 mySuperVisionComponent = theAutomaton->SuperVisionComponent() ;
699 if ( CORBA::is_nil( mySuperVisionComponent ) ) {
700 Engines::Container_var myContainer ;
701 Engines::Component_var myObjComponent ;
702 _OutNode->Graph()->StartComponent( ThreadNo() , "SuperVisionContainer" ,
704 myContainer , myObjComponent ) ;
705 mySuperVisionComponent = SUPERV::SuperG::_narrow( myObjComponent ) ;
706 theAutomaton->SuperVisionComponent( mySuperVisionComponent ) ;
708 //JR 24.08.2005 BUG OMNIORB4 ? : That does not run also ==> Comments
709 // CORBA::Object_ptr ObjRef1 ;
710 // ObjRef1 = mySuperVisionComponent->ToObject( ObjRef ) ;
711 // data <<= ObjRef1 ;
712 //JR 24.08.2005 BUG OMNIORB4 ? : BUT THAT RUNS WITH THE "IDENTITY METHOD ToObject of SUPERV.idl :
713 // Object ToObject( in Object anObject ) ;
714 //CODE of that method !!! :
715 //CORBA::Object_ptr SuperV_Impl::ToObject( CORBA::Object_ptr anObjectRef ) {
716 // beginService( "SuperV_Impl::ToObject" );
717 // CORBA::Object_ptr anObject ;
718 // anObject = CORBA::Object::_duplicate( anObjectRef ) ;
719 // endService( "SuperV_Impl::ToObject" );
722 //BUT THAT RUNS ONLY IF WE USE Dynamic invocation of CORBA (DynInvoke)
723 ServicesAnyData anInParametersList ;
724 anInParametersList.Name = "anObjRef" ;
725 anInParametersList.Value <<= ObjRef ;
726 DynInvoke( mySuperVisionComponent , "ToObject" ,
727 &anInParametersList , 1 , &OutParametersList[i] , 1 ) ;
728 data = OutParametersList[i].Value ;
730 IORObjRef = ObjectToString( ObjRef ) ;
732 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value IORPy "
733 << IOR << " IORObjRef " << IORObjRef << " (objref) " << endl ;
735 if ( CORBA::is_nil( ObjRef ) ) {
737 cdebug << "ObjRef ERROR(nil reference) ResultIor->ob_refcnt "
738 << ResultIor->ob_refcnt-1 << endl ;
744 cdebug << "ObjRef NOT(nil reference)ResultIor->ob_refcnt "
745 << ResultIor->ob_refcnt-1 << endl ;
747 Py_DECREF( ResultIor ) ;
752 cdebug_out << "GraphExecutor::InNode::PyDynInvoke Error ResultIor == NULL Node "
753 << Name() << " method " << method << " " << endl ;
759 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << "(other ERROR)" << endl ;
763 //JR 05.08.2005 BUG OMNIORB4 ? : that line does run with SALOME_3 (OMNIOrb4) for
764 // object references ...
765 OutParametersList[i].Value = data ;
766 // if ( data.type()->kind() == CORBA::tk_objref ) {
767 // IORObjRef = ObjectToString( ObjRef ) ;
768 //#if PyDynInvokeTrace
769 // cdebug << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name()
770 // << "ArgOut" << i << " : " << sname << " " << method << " ObjRef "
771 // << IORObjRef << " (objref) " << endl ;
772 // Engines::Component_var theObjComponent ;
773 // theObjComponent = Engines::Component::_narrow( ObjRef ) ;
774 // DynInvoke( theObjComponent , "ping" , NULL , 0 , NULL , 0 ) ;
776 // Engines::Container_var myContainer ;
777 // Engines::Component_var myObjComponent ;
778 // _OutNode->Graph()->StartComponent( ThreadNo() , "SuperVisionContainer" ,
780 // myContainer , myObjComponent ) ;
781 // SUPERV::SuperG_var mySuperVisionComponent ;
782 // mySuperVisionComponent = SUPERV::SuperG::_narrow( myObjComponent ) ;
783 // CORBA::Object_ptr ObjRef1 ;
784 // ObjRef1 = mySuperVisionComponent->ToObject( IORObjRef ) ;
785 //#if PyDynInvokeTrace
786 // IORObjRef = ObjectToString( ObjRef1 ) ;
787 // cdebug << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name()
788 // << "ArgOut" << i << " : " << sname << " " << method << " ToObject "
789 // << IORObjRef << " (objref) " << endl ;
792 // ServicesAnyData anInParametersList ;
793 // anInParametersList.Name = "anIORObjRef" ;
794 // anInParametersList.Value <<= IORObjRef ;
795 // DynInvoke( mySuperVisionComponent , "ToObject" ,
796 // &anInParametersList , 1 , &OutParametersList[i] , 1 ) ;
797 //#if PyDynInvokeTrace
798 // OutParametersList[i].Value >>= ObjRef1 ;
799 // IORObjRef = ObjectToString( ObjRef1 ) ;
800 // cdebug << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name()
801 // << "ArgOut" << i << " : " << sname << " " << method
802 // << " DynInvoke(ToObject) " << IORObjRef << " (objref) " << endl ;
808 cdebug << "InNode::PyDynInvoke Result->ob_refcnt " << Result->ob_refcnt-1 << endl ;
810 Py_DECREF( Result ) ;
814 cdebug << "InNode::PyDynInvoke ArgsList->ob_refcnt " << ArgsList->ob_refcnt-1 << endl ;
816 Py_DECREF( ArgsList ) ;
825 for ( i = 0 ; i < nInParams ; i++ ) { // Without Gates
826 cdebug << "InParametersList[" << i << "] : "
827 << InParametersList[i].Name << " "
828 << AnyValue( InParametersList[i].Value ) << endl ;
830 for ( i = 0 ; i < nOutParams ; i++ ) { // Without Gates
831 cdebug << "OutParametersList[" << i << "] : "
832 << OutParametersList[i].Name << " "
833 << AnyValue( OutParametersList[i].Value ) << endl ;
839 cdebug_out << "GraphExecutor::InNode::PyDynInvoke Node " << Name() << " method " << method << " " << RetVal
847 bool GraphExecutor::InNode::PyRunSimpleString( char* thePyString )
849 const bool ErrorValue = true;
853 MESSAGE( pthread_self() << "Python method beginning : " << thePyString );
854 cdebug_in << pthread_self() << "Python method beginning : " << thePyString << endl ;
856 aRet = PyRun_SimpleString( thePyString );
857 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
858 //aRet = _OutNode->SuperVisionContainer()->ActivatePythonExecution( thePyString ) ;
860 MESSAGE( pthread_self() << "Python method finished." );
861 cdebug_out << pthread_self() << "Python method finished." << endl ;
864 MESSAGE( pthread_self() << "ERROR: Exception caught running Python method." );
865 cdebug_out << pthread_self() << "ERROR: Exception caught running Python method."
867 MESSAGE( " Python was reinitialized. Previous Python definitions are lost" );
868 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
871 Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod );
872 _OutNode->PyInitialized( false );
878 PyObject * GraphExecutor::InNode::PyEvalCallObject( const char *method ,
879 PyObject * MyPyRunMethod ,
880 PyObject * ArgsList ) {
881 // cdebug_in << "Executor::InNode::PyEvalCallObject " << Name() << endl ;
882 PyObject * Result = NULL ;
884 // MESSAGE( pthread_self() << "PyEval_CallObject " << Name() << " method beginning : " << method );
885 // cdebug << pthread_self() << "PyEval_CallObject " << Name() << " method beginning : " << method << endl ;
886 Result = PyEval_CallObject( MyPyRunMethod , ArgsList ) ;
887 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
888 //Result = _OutNode->SuperVisionContainer()->ActivatePythonExecution( MyPyRunMethod , ArgsList ) ;
889 // MESSAGE( pthread_self() << "PyEval_CallObject method finished. Result " << Result );
890 // cdebug << pthread_self() << "PyEval_CallObject method finished. Result " << Result << endl ;
891 // cdebug_out << "Executor::InNode::PyEvalCallObject " << Name() << endl ;
893 MESSAGE( pthread_self() << "ERROR: Exception caught PyEval_CallObject " << Name()
894 << " Python method " << method << ". Result " << Result );
895 cdebug << pthread_self() << "ERROR: Exception caught PyEval_CallObject " << Name()
896 << " Python method " << method << ". Result " << Result << endl ;
897 MESSAGE( " Python was reinitialized. Previous Python definitions are lost Py_IsInitialized " << Py_IsInitialized() );
898 //JR ===> fatal error in python : no current thread
899 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
902 Automaton()->PyInitModule( false ) ;
903 Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod );
904 _OutNode->PyInitialized( false );
906 if ( Result == NULL ) {
907 Kill() ; // Reset of _ThreadId
909 // cdebug << "Executor::InNode::PyEvalCallObject ERROR catched " << Name()
910 // << " Py_IsInitialized " << Py_IsInitialized() << endl ;
912 // cdebug_out << "Executor::InNode::PyEvalCallObject " << Name() << endl ;