1 // SUPERV GraphExecutor : contains classes that permit execution of graphs and particularly the execution automaton
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : DataFlowExecutor_PyDynInvoke.cxx
23 // Author : Jean Rahuel, CEA
31 #include "DataFlowExecutor_InNode.hxx"
32 #include "DataFlowExecutor_OutNode.hxx"
34 using namespace CORBA ;
36 extern GraphExecutor::FiniteStateMachine * theAutomaton ;
38 //static PyObject * MyPyObjSignal = NULL ;
39 static PyObject * MyPyObjRef = NULL ;
40 static PyObject * MyPyObjIor = NULL ;
41 static PyObject * MyPyRunMethod = NULL ;
43 static PyMethodDef MethodPyRunMethod[] = {
44 { "RunMethod", PyRunMethod, METH_VARARGS },
48 bool GraphExecutor::InNode::InitPython() {
49 cdebug_in << "InitPython" << endl ;
51 Automaton()->PyLock() ;
52 // PyObject * Module = Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod ) ;
53 if ( !Automaton()->PyInitModule() ) {
54 Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod ) ;
55 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
56 //_OutNode->SuperVisionContainer()->ActivatePythonExecution( "InitPyRunMethod" , MethodPyRunMethod ) ;
58 // PyObject * Dictionnary = PyModule_GetDict( Module ) ;
59 // InitPyDynInvokeError = PyErr_NewException( "InitPyDynInvokeError" , NULL , NULL ) ;
60 // PyDict_SetItemString( Dictionnary , InitPyDynInvokeError ) ;
61 cdebug << ThreadNo() << "Py_Initialized() " << endl ;
64 //JR : Python documentation says that there is no sense to set a handler of synchronous
65 // signals as SIGSEGV and SIGFPE !!!
66 if ( Automaton()->PyFunction( "PyObjSignal" ) == NULL ) {
68 aPyFunc = "import signal\n" ;
69 aPyFunc += "import InitPyRunMethod\n" ;
70 aPyFunc += "import sys\n" ;
71 aPyFunc += "import CORBA\n" ;
72 aPyFunc += "import omniORB\n" ;
73 aPyFunc += "def PySignalHandler( aSignal , aStackFrame ) :\n" ;
74 aPyFunc += " print 'PySignalHandler(aSignal =',aSignal,',aStackFrame= ',aStackFrame,')'\n" ;
75 aPyFunc += "InitPyRunMethod.RunMethod( PySignalHandler )\n" ;
76 aPyFunc += "PrevHandler = signal.signal( signal.SIGSEGV , PySignalHandler )\n" ;
77 aPyFunc += "print 'PyObjSignal PrevHandler of SIGSEGV :', PrevHandler\n" ;
78 aPyFunc += "print 'PyObjSignal actual handler of SIGSEGV :', signal.getsignal( signal.SIGSEGV )\n" ;
79 // if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
80 if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) {
81 cdebug << ThreadNo() << " " << Name() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ;
82 Automaton()->PyUnLock() ;
85 MyPyObjSignal = MyPyRunMethod ;
86 Automaton()->PyFunction( "PyObjSignal" , MyPyObjSignal ) ;
89 MyPyObjSignal = Automaton()->PyFunction( "PyObjSignal" ) ;
93 // PyObjRef convert an IOR (string) to an Python ObjectReference
94 if ( Automaton()->PyFunction( "PyObjRef" ) == NULL ) {
96 aPyFunc = "import InitPyRunMethod\n" ;
97 aPyFunc += "import sys\n" ;
98 aPyFunc += "import CORBA\n" ;
99 aPyFunc += "import omniORB\n" ;
100 aPyFunc += "def PyObjRef( IORObjStr ) :\n" ;
101 aPyFunc += " orb = CORBA.ORB_init( sys.argv , CORBA.ORB_ID )\n" ;
102 aPyFunc += " objref = orb.string_to_object( IORObjStr )\n" ;
103 aPyFunc += " return objref\n" ;
104 aPyFunc += "InitPyRunMethod.RunMethod( PyObjRef )\n" ;
105 if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
106 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
107 //if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) {
108 cdebug << ThreadNo() << " " << Name() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ;
109 Automaton()->PyUnLock() ;
112 MyPyObjRef = MyPyRunMethod ;
113 Automaton()->PyFunction( "PyObjRef" , MyPyObjRef ) ;
116 MyPyObjRef = Automaton()->PyFunction( "PyObjRef" ) ;
119 // PyObjRef convert an Python ObjectReference to an IOR (string)
120 if ( Automaton()->PyFunction( "PyObjIor" ) == NULL ) {
122 aPyFunc = "import InitPyRunMethod\n" ;
123 aPyFunc += "import sys\n" ;
124 aPyFunc += "import CORBA\n" ;
125 aPyFunc += "import omniORB\n" ;
126 aPyFunc += "def PyObjIor( ObjRef ) :\n" ;
127 aPyFunc += " orb = CORBA.ORB_init( sys.argv , CORBA.ORB_ID )\n" ;
128 aPyFunc += " objIor = orb.object_to_string( ObjRef )\n" ;
129 aPyFunc += " return objIor\n" ;
130 aPyFunc += "InitPyRunMethod.RunMethod( PyObjIor )\n" ;
131 if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
132 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
133 //if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) {
134 cdebug << ThreadNo() << " " << Name() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ;
135 Automaton()->PyUnLock() ;
138 MyPyObjIor = MyPyRunMethod ;
139 Automaton()->PyFunction( "PyObjIor" , MyPyObjIor ) ;
142 MyPyObjIor = Automaton()->PyFunction( "PyObjIor" ) ;
144 Automaton()->PyUnLock() ;
146 cdebug_out << "InitPython" << endl ;
151 void GraphExecutor::InNode::RemovePyDynInvoke( char * PyFuncName ) {
152 // Automaton()->PyLock() ; // Already done ...
153 if ( Automaton()->ErasePyFunction( PyFuncName ) ) {
154 cdebug << "InNode::RemovePyDynInvoke( " << PyFuncName << " ) success" << endl ;
157 cdebug << "InNode::RemovePyDynInvoke( " << PyFuncName << " ) ERROR failed" << endl ;
159 // Automaton()->PyUnLock() ;
162 PyObject * GraphExecutor::InNode::InitPyDynInvoke( char * PyFuncName ,
163 const SUPERV::ListOfStrings * aPythonFunction ,
168 PyObject * thePyRunMethod = NULL ;
170 cdebug_in << "InitPyDynInvoke '" << PyFuncName << "' length " << (*aPythonFunction).length() << endl ;
172 if ( strlen( PyFuncName ) ) {
173 Automaton()->PyLock() ;
174 thePyRunMethod = Automaton()->PyFunction( PyFuncName ) ;
175 if ( (*aPythonFunction).length() ) {
176 if ( thePyRunMethod == NULL ) {
178 aPyFunc += "import InitPyRunMethod\n" ;
179 for ( i = 0 ; i < (*aPythonFunction).length() ; i++ ) {
180 aPyFunc += (*aPythonFunction)[ i ] ;
183 aPyFunc += "InitPyRunMethod.RunMethod( " ;
184 aPyFunc += PyFuncName ;
186 /*cdebug *///cout<< "InitPyDynInvoke PyRun_SimpleString " << endl << aPyFunc << endl ;
187 // if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
188 if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) {
189 cdebug << ThreadNo() << " " << Name() << " PyRunSimpleString ERROR " << endl << aPyFunc << endl ;
190 PyFuncRunned( false ) ;
195 PyFuncRunned( true ) ;
196 thePyRunMethod = MyPyRunMethod ;
198 Automaton()->PyFunction( PyFuncName , thePyRunMethod ) ;
199 cdebug << ThreadNo() << "PyRunSimpleString( " << (*aPythonFunction)[ 0 ] << " ) " << endl ;
203 Automaton()->PyUnLock() ;
204 cdebug_out << "InitPyDynInvoke '" << PyFuncName << "' thePyRunMethod " << thePyRunMethod << " "
205 << thePyRunMethod->ob_refcnt << endl ;
206 return thePyRunMethod ;
209 else if ( thePyRunMethod ) {
210 Automaton()->PyUnLock() ;
211 cdebug_out << "InitPyDynInvoke '" << PyFuncName << "' thePyRunMethod " << thePyRunMethod << " "
212 << thePyRunMethod->ob_refcnt << endl ;
213 return thePyRunMethod ;
215 Automaton()->PyUnLock() ;
218 cdebug_out << "InitPyDynInvoke '" << PyFuncName << "' thePyRunMethod Null Err " << Err << endl ;
223 extern "C" PyObject * PyRunMethod( PyObject * dummy , PyObject * Args ) {
224 PyObject * Result = NULL ;
225 PyObject * Temp = NULL ;
226 if ( PyArg_ParseTuple( Args , "O:set_callback" , & Temp ) ) {
227 // cout << "PyArg_ParsedTuple" << endl ;
228 if ( !PyCallable_Check( Temp ) ) {
229 // PyErr_SetString( PyExc_TypeError , "PyRunMethod must be callable" ) ;
230 // cout << "PyRunMethod must be callable" << endl ;
234 Py_XDECREF( MyPyRunMethod ) ;
235 MyPyRunMethod = Temp ;
236 Py_INCREF( Py_None ) ;
242 #define PyDynInvokeTrace 1
243 bool GraphExecutor::InNode::PyDynInvoke( PyObject * MyPyRunMethod ,
245 ServicesAnyData * InParametersList , int nInParams ,
246 ServicesAnyData * OutParametersList , int nOutParams ) {
254 cdebug_in << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name()
255 << " method " << method
256 << " InParametersList " << InParametersList
257 << " OutParametersList " << OutParametersList
258 << " " << nInParams << " InArgs " << nOutParams
259 << " OutArgs MyPyRunMethod " ;
261 if ( MyPyRunMethod ) {
263 cdebug << MyPyRunMethod << " " << MyPyRunMethod->ob_refcnt << endl ;
268 cdebug << " NULL" << endl ;
275 PyObject * ArgsList = NULL ;
276 PyObject * ArgValue = NULL ;
277 PyObject * Result = NULL ;
278 PyObject * MyPyObjRefList = NULL ;
279 PyObject * ResultObj = NULL ;
280 PyObject * MyPyObjIorList = NULL ;
281 PyObject * ResultIor = NULL ;
283 CORBA::Object_ptr ObjRef ;
287 if ( !PyCallable_Check( MyPyRunMethod ) ) {
292 ArgsList = PyTuple_New( nInParams ) ;
294 for ( i = 0 ; i < nInParams ; i++ ) {
295 data = InParametersList[i].Value ;
296 sname = InParametersList[i].Name.c_str() ;
297 switch ( data.type()->kind() ) {
298 case CORBA::tk_string : {
301 PyObject * ArgValue = Py_BuildValue( "s" , t ) ;
302 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
304 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << t << " (string) "
305 << " ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
306 << ArgValue->ob_refcnt << endl ;
310 case CORBA::tk_boolean : {
312 data >>= (CORBA::Any::to_boolean ) b ;
313 PyObject * ArgValue = Py_BuildValue( "b" , b ) ;
314 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
316 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << b
317 << " (boolean) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
318 << ArgValue->ob_refcnt << endl ;
322 case CORBA::tk_char : {
324 data >>= (CORBA::Any::to_char ) c ;
325 PyObject * ArgValue = Py_BuildValue( "c" , c ) ;
326 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
328 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << c
329 << " (char) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
330 << ArgValue->ob_refcnt << endl ;
334 case CORBA::tk_short : {
337 PyObject * ArgValue = Py_BuildValue( "h" , s ) ;
338 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
340 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << s
341 << " (short) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
342 << ArgValue->ob_refcnt << endl ;
346 case CORBA::tk_long : {
349 PyObject * ArgValue = Py_BuildValue( "l" , l ) ;
351 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << l
352 << " ArgValue->ob_refcnt" << ArgValue->ob_refcnt << endl ;
354 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
356 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << l
357 << " (long) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
358 << ArgValue->ob_refcnt << endl ;
362 case CORBA::tk_float : {
365 PyObject * ArgValue = Py_BuildValue( "f" , f ) ;
366 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
368 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << f
369 << " (float) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
370 << ArgValue->ob_refcnt << endl ;
374 case CORBA::tk_double : {
377 PyObject * ArgValue = Py_BuildValue( "d" , d ) ;
378 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
380 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << d
381 << " (double) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
382 << ArgValue->ob_refcnt << endl ;
386 case CORBA::tk_objref : {
387 MyPyObjRefList = PyTuple_New( 1 ) ;
388 PyObject * ObjValue ;
389 #if OMNIORB_VERSION >= 4
390 data >>= (CORBA::Any::to_object ) ObjRef ;
394 IORObjRef = ObjectToString( ObjRef ) ;
395 ObjValue = Py_BuildValue( "s" , IORObjRef ) ;
396 PyTuple_SetItem( MyPyObjRefList , 0 , ObjValue ) ;
398 cdebug << "ArgIn" << i << " : " << sname << " " << method << " " << " Value " << IORObjRef << " (objref) "
399 << MyPyObjRef->ob_refcnt << "/" << MyPyObjRefList->ob_refcnt << endl ;
401 // ResultObj = PyEval_CallObject( MyPyObjRef , MyPyObjRefList ) ;
402 ResultObj = PyEvalCallObject( "PyObjRef" , MyPyObjRef , MyPyObjRefList ) ;
404 cdebug << "ObjValue->ob_refcnt" << ObjValue->ob_refcnt << endl ;
406 ArgValue = Py_BuildValue( "O" , ResultObj ) ;
407 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
410 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value (objref) ArgsList->ob_refcnt"
411 << ArgsList->ob_refcnt << " ArgValue->ob_refcnt" << ArgValue->ob_refcnt << endl ;
413 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value (objref) ArgsList->ob_refcnt"
414 << ArgsList->ob_refcnt << " ArgValue is NULL" << endl ;
415 cdebug << "MyPyObjRefList->ob_refcnt " << MyPyObjRefList->ob_refcnt-1 << endl ;
417 Py_DECREF( MyPyObjRefList ) ;
418 if ( CORBA::is_nil( ObjRef ) ) {
424 cdebug << "ResultObj->ob_refcnt " << ResultObj->ob_refcnt-1 << endl ;
426 cdebug << "ResultObj is NULL" << endl ;
429 Py_DECREF( ResultObj ) ;
434 cdebug << "ArgIn" << i << " : " << sname << " Value " << "(other ERROR)" << endl ;
439 // Result = PyEval_CallObject( MyPyRunMethod , ArgsList ) ;
440 Result = PyEvalCallObject( method , MyPyRunMethod , ArgsList ) ;
443 cdebug << "ArgsList->ob_refcnt" << ArgsList->ob_refcnt << endl ;
446 if ( Result == NULL ) {
447 cdebug_out << "GraphExecutor::InNode::PyDynInvoke Node " << Name() << " " << method << " Error Result == NULL"
452 for ( i = 0 ; i < nOutParams ; i++ ) {
453 data = OutParametersList[i].Value ;
454 sname = OutParametersList[i].Name.c_str() ;
455 switch ( data.type()->kind() ) {
456 case CORBA::tk_string : {
458 PyObject * ArgValue ;
459 if ( PyTuple_Check( Result ) ) {
460 ArgValue = PyTuple_GetItem( Result , i ) ;
465 if ( !PyString_Check( ArgValue ) ) {
466 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (string)" << endl ;
470 t = PyString_AsString( ArgValue ) ;
474 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << t << " (string)"
475 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
476 << ArgValue->ob_refcnt << endl ;
480 case CORBA::tk_boolean : {
482 PyObject * ArgValue ;
483 if ( PyTuple_Check( Result ) ) {
484 ArgValue = PyTuple_GetItem( Result , i ) ;
489 if ( !PyInt_Check( ArgValue ) ) {
490 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (boolean)" << endl ;
494 b = PyInt_AsLong( ArgValue ) ;
496 data <<= (CORBA::Any::from_boolean ) b ;
498 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << b << " (boolean)"
499 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
500 << ArgValue->ob_refcnt << endl ;
504 case CORBA::tk_char : {
505 unsigned char c = 0 ;
506 PyObject * ArgValue ;
507 if ( PyTuple_Check( Result ) ) {
508 ArgValue = PyTuple_GetItem( Result , i ) ;
513 //JR 04.04.2005 Debug if ( !PyInt_Check( ArgValue ) ) {
514 //Difficult to understand that behavior ... Python char type is a string of length 1 !
515 // or type is int ...
516 if ( !PyString_Check( ArgValue ) && !PyInt_Check( ArgValue ) ) {
517 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (char)" << endl ;
521 c = PyInt_AsLong( ArgValue ) ;
523 data <<= (CORBA::Any::from_char ) c ;
525 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << c << " (char)"
526 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
527 << ArgValue->ob_refcnt << endl ;
531 case CORBA::tk_short : {
533 PyObject * ArgValue ;
534 if ( PyTuple_Check( Result ) ) {
535 ArgValue = PyTuple_GetItem( Result , i ) ;
540 if ( !PyInt_Check( ArgValue ) ) {
541 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (short)" << endl ;
545 s = PyInt_AsLong( ArgValue ) ;
549 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << s << " (short)"
550 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
551 << ArgValue->ob_refcnt << endl ;
555 case CORBA::tk_long : {
557 PyObject * ArgValue ;
558 if ( PyTuple_Check( Result ) ) {
559 ArgValue = PyTuple_GetItem( Result , i ) ;
564 if ( PyLong_Check( ArgValue ) ) {
565 l = PyLong_AsLong( ArgValue ) ;
567 else if ( PyInt_Check( ArgValue ) ) {
568 l = PyInt_AsLong( ArgValue ) ;
571 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (CORBA::tk_long)" << endl ;
576 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << l << " (long)"
577 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
578 << ArgValue->ob_refcnt << endl ;
582 case CORBA::tk_float : {
584 PyObject * ArgValue ;
585 if ( PyTuple_Check( Result ) ) {
586 ArgValue = PyTuple_GetItem( Result , i ) ;
591 if ( !PyFloat_Check( ArgValue ) ) {
592 // mkr : bug PAL12454
593 if ( PyLong_Check( ArgValue ) ) {
594 cdebug << "ArgOut" << i << " : " << sname << " " << method << " OK (long)" << endl ;
595 long l = PyLong_AsLong( ArgValue );
598 else if ( PyInt_Check( ArgValue ) ) {
599 cdebug << "ArgOut" << i << " : " << sname << " " << method << " OK (int)" << endl ;
600 long l = PyInt_AsLong( ArgValue );
604 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (float)" << endl ;
609 f = PyFloat_AsDouble( ArgValue ) ;
611 #ifdef REDHAT // mkr : debug for PAL12255
614 data.replace(CORBA::TypeCode::PR_float_tc(), (void*)(&f));
617 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << f << " (float)"
618 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
619 << ArgValue->ob_refcnt << endl ;
623 case CORBA::tk_double : {
625 PyObject * ArgValue ;
626 if ( PyTuple_Check( Result ) ) {
627 ArgValue = PyTuple_GetItem( Result , i ) ;
632 if ( !PyFloat_Check( ArgValue ) ) {
633 // mkr : bug PAL12454
634 if ( PyLong_Check( ArgValue ) ) {
635 cdebug << "ArgOut" << i << " : " << sname << " " << method << " OK (long)" << endl ;
636 long l = PyLong_AsLong( ArgValue );
639 else if ( PyInt_Check( ArgValue ) ) {
640 cdebug << "ArgOut" << i << " : " << sname << " " << method << " OK (int)" << endl ;
641 long l = PyInt_AsLong( ArgValue );
645 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (float)" << endl ;
650 d = PyFloat_AsDouble( ArgValue ) ;
652 #ifdef REDHAT // mkr : debug for PAL12255
655 data.replace(CORBA::TypeCode::PR_double_tc(), (void*)(&d));
658 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << d << " (double)"
659 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
660 << ArgValue->ob_refcnt << endl ;
664 case CORBA::tk_objref : {
666 MyPyObjIorList = PyTuple_New( 1 ) ;
667 if ( PyTuple_Check( Result ) ) {
668 ObjIor = PyTuple_GetItem( Result , i ) ;
674 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << "(object reference) "
675 << " Result->ob_refcnt" << Result->ob_refcnt << " ObjIor->ob_refcnt"
676 << ObjIor->ob_refcnt << endl ;
678 Py_INCREF( ObjIor ) ;
679 // PyObject_Print( ObjIor , stdout , 0 ) ;
680 PyTuple_SetItem( MyPyObjIorList , 0 , ObjIor ) ;
681 // ResultIor = PyEval_CallObject( MyPyObjIor , MyPyObjIorList ) ;
682 ResultIor = PyEvalCallObject( "PyObjIor" , MyPyObjIor , MyPyObjIorList ) ;
684 cdebug << "ObjIor->ob_refcnt " << ObjIor->ob_refcnt-1 << endl ;
686 Py_DECREF( ObjIor ) ;
688 cdebug << "MyPyObjIorList->ob_refcnt " << MyPyObjIorList->ob_refcnt-1 << endl ;
690 Py_DECREF( MyPyObjIorList ) ;
692 cdebug << "MyPyObjIor->ob_refcnt " << MyPyObjIor->ob_refcnt << endl ;
696 IOR = PyString_AsString( ResultIor ) ;
697 ObjRef = StringToObject( IOR ) ;
698 //JR 05.08.2005 BUG OMNIORB4 ? : that line does run with SALOME_3 (OMNIOrb4) for
699 // object references ... (It's Ok with OMNIOrb3)
700 // IOR and ObjRef and IORObjRef are Ok
701 // BUT data contains a nil reference !!!
705 SUPERV::SuperG_var mySuperVisionComponent ;
706 mySuperVisionComponent = theAutomaton->SuperVisionComponent() ;
707 if ( CORBA::is_nil( mySuperVisionComponent ) ) {
708 Engines::Container_var myContainer ;
709 Engines::Component_var myObjComponent ;
710 _OutNode->Graph()->StartComponent( ThreadNo() , "SuperVisionContainer" ,
712 myContainer , myObjComponent ) ;
713 mySuperVisionComponent = SUPERV::SuperG::_narrow( myObjComponent ) ;
714 theAutomaton->SuperVisionComponent( mySuperVisionComponent ) ;
716 //JR 24.08.2005 BUG OMNIORB4 ? : That does not run also ==> Comments
717 // CORBA::Object_ptr ObjRef1 ;
718 // ObjRef1 = mySuperVisionComponent->ToObject( ObjRef ) ;
719 // data <<= ObjRef1 ;
720 //JR 24.08.2005 BUG OMNIORB4 ? : BUT THAT RUNS WITH THE "IDENTITY METHOD ToObject of SUPERV.idl :
721 // Object ToObject( in Object anObject ) ;
722 //CODE of that method !!! :
723 //CORBA::Object_ptr SuperV_Impl::ToObject( CORBA::Object_ptr anObjectRef ) {
724 // beginService( "SuperV_Impl::ToObject" );
725 // CORBA::Object_ptr anObject ;
726 // anObject = CORBA::Object::_duplicate( anObjectRef ) ;
727 // endService( "SuperV_Impl::ToObject" );
730 //BUT THAT RUNS ONLY IF WE USE Dynamic invocation of CORBA (DynInvoke)
731 ServicesAnyData anInParametersList ;
732 anInParametersList.Name = "anObjRef" ;
733 anInParametersList.Value <<= ObjRef ;
734 DynInvoke( mySuperVisionComponent , "ToObject" ,
735 &anInParametersList , 1 , &OutParametersList[i] , 1 ) ;
736 data = OutParametersList[i].Value ;
738 IORObjRef = ObjectToString( ObjRef ) ;
740 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value IORPy "
741 << IOR << " IORObjRef " << IORObjRef << " (objref) " << endl ;
743 if ( CORBA::is_nil( ObjRef ) ) {
745 cdebug << "ObjRef ERROR(nil reference) ResultIor->ob_refcnt "
746 << ResultIor->ob_refcnt-1 << endl ;
752 cdebug << "ObjRef NOT(nil reference)ResultIor->ob_refcnt "
753 << ResultIor->ob_refcnt-1 << endl ;
755 Py_DECREF( ResultIor ) ;
759 cdebug_out << "GraphExecutor::InNode::PyDynInvoke Error ResultIor == NULL Node "
760 << Name() << " method " << method << " " << endl ;
766 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << "(other ERROR)" << endl ;
770 //JR 05.08.2005 BUG OMNIORB4 ? : that line does run with SALOME_3 (OMNIOrb4) for
771 // object references ...
772 OutParametersList[i].Value = data ;
773 // if ( data.type()->kind() == CORBA::tk_objref ) {
774 // IORObjRef = ObjectToString( ObjRef ) ;
775 //#if PyDynInvokeTrace
776 // cdebug << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name()
777 // << "ArgOut" << i << " : " << sname << " " << method << " ObjRef "
778 // << IORObjRef << " (objref) " << endl ;
779 // Engines::Component_var theObjComponent ;
780 // theObjComponent = Engines::Component::_narrow( ObjRef ) ;
781 // DynInvoke( theObjComponent , "ping" , NULL , 0 , NULL , 0 ) ;
783 // Engines::Container_var myContainer ;
784 // Engines::Component_var myObjComponent ;
785 // _OutNode->Graph()->StartComponent( ThreadNo() , "SuperVisionContainer" ,
787 // myContainer , myObjComponent ) ;
788 // SUPERV::SuperG_var mySuperVisionComponent ;
789 // mySuperVisionComponent = SUPERV::SuperG::_narrow( myObjComponent ) ;
790 // CORBA::Object_ptr ObjRef1 ;
791 // ObjRef1 = mySuperVisionComponent->ToObject( IORObjRef ) ;
792 //#if PyDynInvokeTrace
793 // IORObjRef = ObjectToString( ObjRef1 ) ;
794 // cdebug << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name()
795 // << "ArgOut" << i << " : " << sname << " " << method << " ToObject "
796 // << IORObjRef << " (objref) " << endl ;
799 // ServicesAnyData anInParametersList ;
800 // anInParametersList.Name = "anIORObjRef" ;
801 // anInParametersList.Value <<= IORObjRef ;
802 // DynInvoke( mySuperVisionComponent , "ToObject" ,
803 // &anInParametersList , 1 , &OutParametersList[i] , 1 ) ;
804 //#if PyDynInvokeTrace
805 // OutParametersList[i].Value >>= ObjRef1 ;
806 // IORObjRef = ObjectToString( ObjRef1 ) ;
807 // cdebug << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name()
808 // << "ArgOut" << i << " : " << sname << " " << method
809 // << " DynInvoke(ToObject) " << IORObjRef << " (objref) " << endl ;
815 cdebug << "InNode::PyDynInvoke Result->ob_refcnt " << Result->ob_refcnt-1 << endl ;
817 Py_DECREF( Result ) ;
821 cdebug << "InNode::PyDynInvoke ArgsList->ob_refcnt " << ArgsList->ob_refcnt-1 << endl ;
823 Py_DECREF( ArgsList ) ;
829 for ( i = 0 ; i < nInParams ; i++ ) { // Without Gates
830 cdebug << "InParametersList[" << i << "] : "
831 << InParametersList[i].Name << " "
832 << AnyValue( InParametersList[i].Value ) << endl ;
834 for ( i = 0 ; i < nOutParams ; i++ ) { // Without Gates
835 cdebug << "OutParametersList[" << i << "] : "
836 << OutParametersList[i].Name << " "
837 << AnyValue( OutParametersList[i].Value ) << endl ;
843 cdebug_out << "GraphExecutor::InNode::PyDynInvoke Node " << Name() << " method " << method << " " << RetVal
851 bool GraphExecutor::InNode::PyRunSimpleString( char* thePyString )
853 const bool ErrorValue = true;
857 MESSAGE( pthread_self() << "Python method beginning : " << thePyString );
858 cdebug_in << pthread_self() << "Python method beginning : " << thePyString << endl ;
860 aRet = PyRun_SimpleString( thePyString );
861 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
862 //aRet = _OutNode->SuperVisionContainer()->ActivatePythonExecution( thePyString ) ;
864 MESSAGE( pthread_self() << "Python method finished." );
865 cdebug_out << pthread_self() << "Python method finished." << endl ;
868 MESSAGE( pthread_self() << "ERROR: Exception caught running Python method." );
869 cdebug_out << pthread_self() << "ERROR: Exception caught running Python method."
871 MESSAGE( " Python was reinitialized. Previous Python definitions are lost" );
872 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
875 Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod );
876 _OutNode->PyInitialized( false );
882 PyObject * GraphExecutor::InNode::PyEvalCallObject( const char *method ,
883 PyObject * MyPyRunMethod ,
884 PyObject * ArgsList ) {
885 // cdebug_in << "Executor::InNode::PyEvalCallObject " << Name() << endl ;
886 PyObject * Result = NULL ;
888 // MESSAGE( pthread_self() << "PyEval_CallObject " << Name() << " method beginning : " << method );
889 // cdebug << pthread_self() << "PyEval_CallObject " << Name() << " method beginning : " << method << endl ;
890 Result = PyEval_CallObject( MyPyRunMethod , ArgsList ) ;
891 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
892 //Result = _OutNode->SuperVisionContainer()->ActivatePythonExecution( MyPyRunMethod , ArgsList ) ;
893 // MESSAGE( pthread_self() << "PyEval_CallObject method finished. Result " << Result );
894 // cdebug << pthread_self() << "PyEval_CallObject method finished. Result " << Result << endl ;
895 // cdebug_out << "Executor::InNode::PyEvalCallObject " << Name() << endl ;
897 MESSAGE( pthread_self() << "ERROR: Exception caught PyEval_CallObject " << Name()
898 << " Python method " << method << ". Result " << Result );
899 cdebug << pthread_self() << "ERROR: Exception caught PyEval_CallObject " << Name()
900 << " Python method " << method << ". Result " << Result << endl ;
901 MESSAGE( " Python was reinitialized. Previous Python definitions are lost Py_IsInitialized " << Py_IsInitialized() );
902 //JR ===> fatal error in python : no current thread
903 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
906 Automaton()->PyInitModule( false ) ;
907 Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod );
908 _OutNode->PyInitialized( false );
910 if ( Result == NULL ) {
911 Kill() ; // Reset of _ThreadId
913 // cdebug << "Executor::InNode::PyEvalCallObject ERROR catched " << Name()
914 // << " Py_IsInitialized " << Py_IsInitialized() << endl ;
916 // cdebug_out << "Executor::InNode::PyEvalCallObject " << Name() << endl ;