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 // PAL10310: patch on omniORB
60 aPyFunc = "import omnipatch\n" ;
61 if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
62 cdebug << ThreadNo() << " " << Name() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ;
63 Automaton()->PyUnLock() ;
67 // PyObject * Dictionnary = PyModule_GetDict( Module ) ;
68 // InitPyDynInvokeError = PyErr_NewException( "InitPyDynInvokeError" , NULL , NULL ) ;
69 // PyDict_SetItemString( Dictionnary , InitPyDynInvokeError ) ;
70 cdebug << ThreadNo() << "Py_Initialized() " << endl ;
73 //JR : Python documentation says that there is no sense to set a handler of synchronous
74 // signals as SIGSEGV and SIGFPE !!!
75 if ( Automaton()->PyFunction( "PyObjSignal" ) == NULL ) {
77 aPyFunc = "import signal\n" ;
78 aPyFunc += "import InitPyRunMethod\n" ;
79 aPyFunc += "import sys\n" ;
80 aPyFunc += "import CORBA\n" ;
81 aPyFunc += "import omniORB\n" ;
82 aPyFunc += "def PySignalHandler( aSignal , aStackFrame ) :\n" ;
83 aPyFunc += " print 'PySignalHandler(aSignal =',aSignal,',aStackFrame= ',aStackFrame,')'\n" ;
84 aPyFunc += "InitPyRunMethod.RunMethod( PySignalHandler )\n" ;
85 aPyFunc += "PrevHandler = signal.signal( signal.SIGSEGV , PySignalHandler )\n" ;
86 aPyFunc += "print 'PyObjSignal PrevHandler of SIGSEGV :', PrevHandler\n" ;
87 aPyFunc += "print 'PyObjSignal actual handler of SIGSEGV :', signal.getsignal( signal.SIGSEGV )\n" ;
88 // if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
89 if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) {
90 cdebug << ThreadNo() << " " << Name() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ;
91 Automaton()->PyUnLock() ;
94 MyPyObjSignal = MyPyRunMethod ;
95 Automaton()->PyFunction( "PyObjSignal" , MyPyObjSignal ) ;
98 MyPyObjSignal = Automaton()->PyFunction( "PyObjSignal" ) ;
102 // PyObjRef convert an IOR (string) to an Python ObjectReference
103 if ( Automaton()->PyFunction( "PyObjRef" ) == NULL ) {
105 aPyFunc = "import InitPyRunMethod\n" ;
106 aPyFunc += "import sys\n" ;
107 aPyFunc += "import CORBA\n" ;
108 aPyFunc += "import omniORB\n" ;
109 aPyFunc += "orb = CORBA.ORB_init( sys.argv , CORBA.ORB_ID )\n" ;
110 aPyFunc += "def PyObjRef( IORObjStr ) :\n" ;
111 // aPyFunc += " orb = CORBA.ORB_init( sys.argv , CORBA.ORB_ID )\n" ;
112 aPyFunc += " objref = orb.string_to_object( IORObjStr )\n" ;
113 aPyFunc += " return objref\n" ;
114 aPyFunc += "InitPyRunMethod.RunMethod( PyObjRef )\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() ;
122 MyPyObjRef = MyPyRunMethod ;
123 Automaton()->PyFunction( "PyObjRef" , MyPyObjRef ) ;
126 MyPyObjRef = Automaton()->PyFunction( "PyObjRef" ) ;
129 // PyObjRef convert an Python ObjectReference to an IOR (string)
130 if ( Automaton()->PyFunction( "PyObjIor" ) == NULL ) {
132 aPyFunc = "import InitPyRunMethod\n" ;
133 aPyFunc += "import sys\n" ;
134 aPyFunc += "import CORBA\n" ;
135 aPyFunc += "import omniORB\n" ;
136 aPyFunc += "orb = CORBA.ORB_init( sys.argv , CORBA.ORB_ID )\n" ;
137 aPyFunc += "def PyObjIor( ObjRef ) :\n" ;
138 // aPyFunc += " orb = CORBA.ORB_init( sys.argv , CORBA.ORB_ID )\n" ;
139 aPyFunc += " objIor = orb.object_to_string( ObjRef )\n" ;
140 aPyFunc += " return objIor\n" ;
141 aPyFunc += "InitPyRunMethod.RunMethod( PyObjIor )\n" ;
142 if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
143 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
144 //if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) {
145 cdebug << ThreadNo() << " " << Name() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ;
146 Automaton()->PyUnLock() ;
149 MyPyObjIor = MyPyRunMethod ;
150 Automaton()->PyFunction( "PyObjIor" , MyPyObjIor ) ;
153 MyPyObjIor = Automaton()->PyFunction( "PyObjIor" ) ;
155 Automaton()->PyUnLock() ;
157 cdebug_out << "InitPython" << endl ;
162 void GraphExecutor::InNode::RemovePyDynInvoke( char * PyFuncName ) {
163 // Automaton()->PyLock() ; // Already done ...
164 if ( Automaton()->ErasePyFunction( PyFuncName ) ) {
165 cdebug << "InNode::RemovePyDynInvoke( " << PyFuncName << " ) success" << endl ;
168 cdebug << "InNode::RemovePyDynInvoke( " << PyFuncName << " ) ERROR failed" << endl ;
170 // Automaton()->PyUnLock() ;
173 PyObject * GraphExecutor::InNode::InitPyDynInvoke( char * PyFuncName ,
174 const SUPERV::ListOfStrings * aPythonFunction ,
179 PyObject * thePyRunMethod = NULL ;
181 cdebug_in << "InitPyDynInvoke '" << PyFuncName << "' length " << (*aPythonFunction).length() << endl ;
183 if ( strlen( PyFuncName ) ) {
184 Automaton()->PyLock() ;
185 thePyRunMethod = Automaton()->PyFunction( PyFuncName ) ;
187 //thePyRunMethod = NULL;
188 // asv 28.02.05 : VERY BAD fix of the following problem: after change of a function,
189 // the changes are NOT taken into account by Automation - it returns PyObject of the OLD function.
190 // so here we force re-automating the PyObject EVERY TIME, regardless if the function has changed or not.
191 // Once again - it is a very bad solution, it fully discards the whole idea of automation,
192 // here is it done as a quick fix for a bug.
193 // A better solution (to be implemented): store the PyObject NOT in Automation map, but in
194 // InLine node itself! And if the method is changed - remove the PyObject and force to regenerate it.
195 // But this means that PyObject must be stored in Editor's data model.
196 // asv 01.03.05 : the fix is not needed, the described bug is not reproduced. To investigate:
197 // WHERE PyObject is removed from Automation map on function change.
199 if ( (*aPythonFunction).length() ) {
200 if ( thePyRunMethod == NULL ) {
202 aPyFunc += "import InitPyRunMethod\n" ;
203 for ( i = 0 ; i < (*aPythonFunction).length() ; i++ ) {
204 aPyFunc += (*aPythonFunction)[ i ] ;
207 aPyFunc += "InitPyRunMethod.RunMethod( " ;
208 aPyFunc += PyFuncName ;
210 /*cdebug *///cout<< "InitPyDynInvoke PyRun_SimpleString " << endl << aPyFunc << endl ;
211 // if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
212 if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) {
213 cdebug << ThreadNo() << " " << Name() << " PyRunSimpleString ERROR " << endl << aPyFunc << endl ;
214 PyFuncRunned( false ) ;
219 PyFuncRunned( true ) ;
220 thePyRunMethod = MyPyRunMethod ;
222 Automaton()->PyFunction( PyFuncName , thePyRunMethod ) ;
223 cdebug << ThreadNo() << "PyRunSimpleString( " << (*aPythonFunction)[ 0 ] << " ) " << endl ;
227 Automaton()->PyUnLock() ;
228 cdebug_out << "InitPyDynInvoke '" << PyFuncName << "' thePyRunMethod " << thePyRunMethod << " "
229 << thePyRunMethod->ob_refcnt << endl ;
230 return thePyRunMethod ;
233 else if ( thePyRunMethod ) {
234 Automaton()->PyUnLock() ;
235 cdebug_out << "InitPyDynInvoke '" << PyFuncName << "' thePyRunMethod " << thePyRunMethod << " "
236 << thePyRunMethod->ob_refcnt << endl ;
237 return thePyRunMethod ;
239 Automaton()->PyUnLock() ;
242 cdebug_out << "InitPyDynInvoke '" << PyFuncName << "' thePyRunMethod Null Err " << Err << endl ;
247 extern "C" PyObject * PyRunMethod( PyObject * dummy , PyObject * Args ) {
248 PyObject * Result = NULL ;
249 PyObject * Temp = NULL ;
250 if ( PyArg_ParseTuple( Args , "O:set_callback" , & Temp ) ) {
251 // cout << "PyArg_ParsedTuple" << endl ;
252 if ( !PyCallable_Check( Temp ) ) {
253 // PyErr_SetString( PyExc_TypeError , "PyRunMethod must be callable" ) ;
254 // cout << "PyRunMethod must be callable" << endl ;
258 Py_XDECREF( MyPyRunMethod ) ;
259 MyPyRunMethod = Temp ;
260 Py_INCREF( Py_None ) ;
266 #define PyDynInvokeTrace 0
267 bool GraphExecutor::InNode::PyDynInvoke( PyObject * MyPyRunMethod ,
269 ServicesAnyData * InParametersList , int nInParams ,
270 ServicesAnyData * OutParametersList , int nOutParams ) {
278 cdebug_in << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name()
279 << " method " << method
280 << " InParametersList " << InParametersList
281 << " OutParametersList " << OutParametersList
282 << " " << nInParams << " InArgs " << nOutParams
283 << " OutArgs MyPyRunMethod " ;
285 if ( MyPyRunMethod ) {
287 cdebug << MyPyRunMethod << " " << MyPyRunMethod->ob_refcnt << endl ;
292 cdebug << " NULL" << endl ;
299 PyObject * ArgsList = NULL ;
300 PyObject * ArgValue = NULL ;
301 PyObject * Result = NULL ;
302 //PyObject * MyPyObjRefList = NULL ;
303 PyObject * ResultObj = NULL ;
304 //PyObject * MyPyObjIorList = NULL ;
305 PyObject * ResultIor = NULL ;
307 CORBA::Object_ptr ObjRef ;
311 if ( !PyCallable_Check( MyPyRunMethod ) ) {
316 ArgsList = PyTuple_New( nInParams ) ;
318 for ( i = 0 ; i < nInParams ; i++ ) {
319 data = InParametersList[i].Value ;
320 sname = InParametersList[i].Name.c_str() ;
321 switch ( data.type()->kind() ) {
322 case CORBA::tk_string : {
325 PyObject * ArgValue = Py_BuildValue( "s" , t ) ;
326 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
328 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << t << " (string) "
329 << " ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
330 << ArgValue->ob_refcnt << endl ;
334 case CORBA::tk_boolean : {
336 data >>= (CORBA::Any::to_boolean ) b ;
337 PyObject * ArgValue = Py_BuildValue( "b" , b ) ;
338 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
340 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << b
341 << " (boolean) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
342 << ArgValue->ob_refcnt << endl ;
346 case CORBA::tk_char : {
348 data >>= (CORBA::Any::to_char ) c ;
349 PyObject * ArgValue = Py_BuildValue( "c" , c ) ;
350 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
352 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << c
353 << " (char) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
354 << ArgValue->ob_refcnt << endl ;
358 case CORBA::tk_short : {
361 PyObject * ArgValue = Py_BuildValue( "h" , s ) ;
362 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
364 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << s
365 << " (short) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
366 << ArgValue->ob_refcnt << endl ;
370 case CORBA::tk_long : {
373 // epa - Porting to 64-bit Linux:CORBA::Long is incorrectly treated as unsigned
374 // integer by Py_BuildValue("l", val) in Python 2.4.1. Using Py_BuildValue("i",val)
376 PyObject * ArgValue = NULL;
377 if ( SIZEOF_LONG == 4 )
378 ArgValue = Py_BuildValue( "l" , l ) ;
380 ArgValue = Py_BuildValue( "i" , l ) ;
382 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << l
383 << " ArgValue->ob_refcnt" << ArgValue->ob_refcnt << endl ;
385 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
387 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << l
388 << " (CORBA::Long) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
389 << ArgValue->ob_refcnt << endl ;
393 case CORBA::tk_float : {
396 PyObject * ArgValue = Py_BuildValue( "f" , f ) ;
397 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
399 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << f
400 << " (float) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
401 << ArgValue->ob_refcnt << endl ;
405 case CORBA::tk_double : {
408 PyObject * ArgValue = Py_BuildValue( "d" , d ) ;
409 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
411 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << d
412 << " (double) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
413 << ArgValue->ob_refcnt << endl ;
417 case CORBA::tk_objref : {
418 //MyPyObjRefList = PyTuple_New( 1 ) ;
419 //PyObject * ObjValue ;
420 #if OMNIORB_VERSION >= 4
421 data >>= (CORBA::Any::to_object ) ObjRef ;
425 IORObjRef = ObjectToString( ObjRef ) ;
426 //ObjValue = Py_BuildValue( "s" , IORObjRef ) ;
427 //PyTuple_SetItem( MyPyObjRefList , 0 , ObjValue ) ;
429 cdebug << "ArgIn" << i << " : " << sname << " " << method << " " << " Value " << IORObjRef << " (objref) "<< endl ;
430 // << MyPyObjRef->ob_refcnt << "/" << MyPyObjRefList->ob_refcnt << endl ;
432 // ResultObj = PyEval_CallObject( MyPyObjRef , MyPyObjRefList ) ;
433 //ResultObj = PyEvalCallObject( "PyObjRef" , MyPyObjRef , MyPyObjRefList ) ;
435 //cdebug << "ObjValue->ob_refcnt" << ObjValue->ob_refcnt << endl ;
437 //ArgValue = Py_BuildValue( "O" , ResultObj ) ;
438 //PyTuple_SetItem( ArgsList , i , ArgValue ) ;
441 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value (objref) ArgsList->ob_refcnt"
442 << ArgsList->ob_refcnt << " ArgValue->ob_refcnt" << ArgValue->ob_refcnt << endl ;
444 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value (objref) ArgsList->ob_refcnt"
445 << ArgsList->ob_refcnt << " ArgValue is NULL" << endl ;
446 //cdebug << "MyPyObjRefList->ob_refcnt " << MyPyObjRefList->ob_refcnt-1 << endl ;
448 ResultObj = PyObject_CallFunction( MyPyObjRef , "s",IORObjRef ) ;
449 if(ResultObj == NULL){
452 Py_INCREF(ResultObj) ;
454 PyTuple_SetItem( ArgsList , i , ResultObj ) ;
455 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value (objref) ArgsList->ob_refcnt"
456 << ArgsList->ob_refcnt << " ArgValue->ob_refcnt" << ResultObj->ob_refcnt << endl ;
457 // << ArgsList->ob_refcnt << " ArgValue->ob_refcnt" << ArgValue->ob_refcnt << endl ;
458 //cdebug << "MyPyObjRefList->ob_refcnt " << MyPyObjRefList->ob_refcnt-1 << endl ;
459 //Py_DECREF( MyPyObjRefList ) ;
460 //if ( CORBA::is_nil( ObjRef ) ) {
461 // ResultObj = NULL ;
464 //#if PyDynInvokeTrace
466 //cdebug << "ResultObj->ob_refcnt " << ResultObj->ob_refcnt-1 << endl ;
468 //cdebug << "ResultObj is NULL" << endl ;
471 //Py_DECREF( ResultObj ) ;
476 cdebug << "ArgIn" << i << " : " << sname << " Value " << "(other ERROR)" << endl ;
481 // Result = PyEval_CallObject( MyPyRunMethod , ArgsList ) ;
482 Result = PyEvalCallObject( method , MyPyRunMethod , ArgsList ) ;
485 cdebug << "ArgsList->ob_refcnt" << ArgsList->ob_refcnt << endl ;
488 if ( Result == NULL ) {
489 cdebug_out << "GraphExecutor::InNode::PyDynInvoke Node " << Name() << " " << method << " Error Result == NULL"
495 for ( i = 0 ; i < nOutParams ; i++ ) {
496 data = OutParametersList[i].Value ;
497 sname = OutParametersList[i].Name.c_str() ;
498 switch ( data.type()->kind() ) {
499 case CORBA::tk_string : {
501 PyObject * ArgValue ;
502 if ( PyTuple_Check( Result ) ) {
503 ArgValue = PyTuple_GetItem( Result , i ) ;
508 if ( !ArgValue || !PyString_Check( ArgValue ) ) {
509 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (string)" << endl ;
513 t = PyString_AsString( ArgValue ) ;
518 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << t << " (string)"
519 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
520 << ArgValue->ob_refcnt << endl ;
524 case CORBA::tk_boolean : {
526 PyObject * ArgValue ;
527 if ( PyTuple_Check( Result ) ) {
528 ArgValue = PyTuple_GetItem( Result , i ) ;
533 if ( !ArgValue || !PyInt_Check( ArgValue ) ) {
534 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (boolean)" << endl ;
538 b = PyInt_AsLong( ArgValue ) ;
540 data <<= (CORBA::Any::from_boolean ) b ;
543 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << b << " (boolean)"
544 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
545 << ArgValue->ob_refcnt << endl ;
549 case CORBA::tk_char : {
550 unsigned char c = 0 ;
551 PyObject * ArgValue ;
552 if ( PyTuple_Check( Result ) ) {
553 ArgValue = PyTuple_GetItem( Result , i ) ;
558 //JR 04.04.2005 Debug if ( !PyInt_Check( ArgValue ) ) {
559 //Difficult to understand that behavior ... Python char type is a string of length 1 !
560 // or type is int ...
561 if ( !ArgValue || !PyString_Check( ArgValue ) && !PyInt_Check( ArgValue ) ) {
562 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (char)" << endl ;
566 c = PyInt_AsLong( ArgValue ) ;
568 data <<= (CORBA::Any::from_char ) c ;
571 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << c << " (char)"
572 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
573 << ArgValue->ob_refcnt << endl ;
577 case CORBA::tk_short : {
579 PyObject * ArgValue ;
580 if ( PyTuple_Check( Result ) ) {
581 ArgValue = PyTuple_GetItem( Result , i ) ;
586 if ( !ArgValue || !PyInt_Check( ArgValue ) ) {
587 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (short)" << endl ;
591 s = PyInt_AsLong( ArgValue ) ;
596 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << s << " (short)"
597 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
598 << ArgValue->ob_refcnt << endl ;
602 case CORBA::tk_long : {
604 PyObject * ArgValue ;
605 if ( PyTuple_Check( Result ) ) {
606 ArgValue = PyTuple_GetItem( Result , i ) ;
611 if ( ArgValue && PyLong_Check( ArgValue ) ) {
612 l = PyLong_AsLong( ArgValue ) ;
614 else if ( ArgValue && PyInt_Check( ArgValue ) ) {
615 l = PyInt_AsLong( ArgValue ) ;
618 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (CORBA::tk_long)" << endl ;
624 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << l << " (CORBA::Long)"
625 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
626 << ArgValue->ob_refcnt << endl ;
630 case CORBA::tk_float : {
632 PyObject * ArgValue ;
633 if ( PyTuple_Check( Result ) ) {
634 ArgValue = PyTuple_GetItem( Result , i ) ;
640 if ( !PyFloat_Check( ArgValue ) ) {
641 // mkr : bug PAL12454
642 if ( PyLong_Check( ArgValue ) ) {
643 cdebug << "ArgOut" << i << " : " << sname << " " << method << " OK (CORBA::Long)" << endl ;
644 CORBA::Long l = PyLong_AsLong( ArgValue );
647 else if ( PyInt_Check( ArgValue ) ) {
648 cdebug << "ArgOut" << i << " : " << sname << " " << method << " OK (int)" << endl ;
649 CORBA::Long l = PyInt_AsLong( ArgValue );
653 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (float)" << endl ;
658 f = PyFloat_AsDouble( ArgValue ) ;
662 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (float)" << endl ;
665 #ifdef REDHAT // mkr : debug for PAL12255
668 //JR data.replace(CORBA::TypeCode::PR_float_tc(), (void*)(&f));
669 data <<= (CORBA::Float) f ;
673 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << f << " (float)"
674 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
675 << ArgValue->ob_refcnt << endl ;
679 case CORBA::tk_double : {
681 PyObject * ArgValue ;
682 if ( PyTuple_Check( Result ) ) {
683 ArgValue = PyTuple_GetItem( Result , i ) ;
689 if ( !PyFloat_Check( ArgValue ) ) {
690 // mkr : bug PAL12454
691 if ( PyLong_Check( ArgValue ) ) {
692 cdebug << "ArgOut" << i << " : " << sname << " " << method << " OK (CORBA::Long)" << endl ;
693 CORBA::Long l = PyLong_AsLong( ArgValue );
696 else if ( PyInt_Check( ArgValue ) ) {
697 cdebug << "ArgOut" << i << " : " << sname << " " << method << " OK (int)" << endl ;
698 CORBA::Long l = PyInt_AsLong( ArgValue );
702 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (float)" << endl ;
707 d = PyFloat_AsDouble( ArgValue ) ;
711 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (float)" << endl ;
714 #ifdef REDHAT // mkr : debug for PAL12255
717 //JR data.replace(CORBA::TypeCode::PR_double_tc(), (void*)(&d));
718 data <<= (CORBA::Double) d ;
722 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << d << " (double)"
723 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
724 << ArgValue->ob_refcnt << endl ;
728 case CORBA::tk_objref : {
730 //MyPyObjIorList = PyTuple_New( 1 ) ;
731 if ( PyTuple_Check( Result ) ) {
732 ObjIor = PyTuple_GetItem( Result , i ) ;
739 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << "(object reference) "
740 << " Result->ob_refcnt" << Result->ob_refcnt << " ObjIor->ob_refcnt"
741 << ObjIor->ob_refcnt << endl ;
743 //Py_INCREF( ObjIor ) ;
744 // PyObject_Print( ObjIor , stdout , 0 ) ;
745 //PyTuple_SetItem( MyPyObjIorList , 0 , ObjIor ) ;
746 // ResultIor = PyEval_CallObject( MyPyObjIor , MyPyObjIorList ) ;
747 //ResultIor = PyEvalCallObject( "PyObjIor" , MyPyObjIor , MyPyObjIorList ) ;
748 ResultIor = PyObject_CallFunction( MyPyObjIor , "O",ObjIor ) ;
751 cdebug << "ObjIor->ob_refcnt " << ObjIor->ob_refcnt-1 << endl ;
753 //Py_DECREF( ObjIor ) ;
755 //cdebug << "MyPyObjIorList->ob_refcnt " << MyPyObjIorList->ob_refcnt-1 << endl ;
757 //Py_DECREF( MyPyObjIorList ) ;
759 //cdebug << "MyPyObjIor->ob_refcnt " << MyPyObjIor->ob_refcnt << endl ;
763 IOR = PyString_AsString( ResultIor ) ;
764 ObjRef = StringToObject( IOR ) ;
765 //JR 05.08.2005 BUG OMNIORB4 ? : that line does run with SALOME_3 (OMNIOrb4) for
766 // object references ... (It's Ok with OMNIOrb3)
767 // IOR and ObjRef and IORObjRef are Ok
768 // BUT data contains a nil reference !!!
772 SUPERV::SuperG_var mySuperVisionComponent ;
773 mySuperVisionComponent = theAutomaton->SuperVisionComponent() ;
774 if ( CORBA::is_nil( mySuperVisionComponent ) ) {
775 Engines::Container_var myContainer ;
776 Engines::Component_var myObjComponent ;
777 _OutNode->Graph()->StartComponent( ThreadNo() , "SuperVisionContainer" ,
779 myContainer , myObjComponent ) ;
780 mySuperVisionComponent = SUPERV::SuperG::_narrow( myObjComponent ) ;
781 theAutomaton->SuperVisionComponent( mySuperVisionComponent ) ;
783 //JR 24.08.2005 BUG OMNIORB4 ? : That does not run also ==> Comments
784 // CORBA::Object_ptr ObjRef1 ;
785 // ObjRef1 = mySuperVisionComponent->ToObject( ObjRef ) ;
786 // data <<= ObjRef1 ;
787 //JR 24.08.2005 BUG OMNIORB4 ? : BUT THAT RUNS WITH THE "IDENTITY METHOD ToObject of SUPERV.idl :
788 // Object ToObject( in Object anObject ) ;
789 //CODE of that method !!! :
790 //CORBA::Object_ptr SuperV_Impl::ToObject( CORBA::Object_ptr anObjectRef ) {
791 // beginService( "SuperV_Impl::ToObject" );
792 // CORBA::Object_ptr anObject ;
793 // anObject = CORBA::Object::_duplicate( anObjectRef ) ;
794 // endService( "SuperV_Impl::ToObject" );
797 //BUT THAT RUNS ONLY IF WE USE Dynamic invocation of CORBA (DynInvoke)
798 ServicesAnyData anInParametersList ;
799 anInParametersList.Name = "anObjRef" ;
800 anInParametersList.Value <<= ObjRef ;
801 DynInvoke( mySuperVisionComponent , "ToObject" ,
802 &anInParametersList , 1 , &OutParametersList[i] , 1 ) ;
803 data = OutParametersList[i].Value ;
805 IORObjRef = ObjectToString( ObjRef ) ;
807 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value IORPy "
808 << IOR << " IORObjRef " << IORObjRef << " (objref) " << endl ;
810 if ( CORBA::is_nil( ObjRef ) ) {
812 cdebug << "ObjRef ERROR(nil reference) ResultIor->ob_refcnt "
813 << ResultIor->ob_refcnt-1 << endl ;
819 cdebug << "ObjRef NOT(nil reference)ResultIor->ob_refcnt "
820 << ResultIor->ob_refcnt-1 << endl ;
822 Py_DECREF( ResultIor ) ;
827 cdebug_out << "GraphExecutor::InNode::PyDynInvoke Error ResultIor == NULL Node "
828 << Name() << " method " << method << " " << endl ;
834 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << "(other ERROR)" << endl ;
838 //JR 05.08.2005 BUG OMNIORB4 ? : that line does run with SALOME_3 (OMNIOrb4) for
839 // object references ...
840 OutParametersList[i].Value = data ;
841 // if ( data.type()->kind() == CORBA::tk_objref ) {
842 // IORObjRef = ObjectToString( ObjRef ) ;
843 //#if PyDynInvokeTrace
844 // cdebug << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name()
845 // << "ArgOut" << i << " : " << sname << " " << method << " ObjRef "
846 // << IORObjRef << " (objref) " << endl ;
847 // Engines::Component_var theObjComponent ;
848 // theObjComponent = Engines::Component::_narrow( ObjRef ) ;
849 // DynInvoke( theObjComponent , "ping" , NULL , 0 , NULL , 0 ) ;
851 // Engines::Container_var myContainer ;
852 // Engines::Component_var myObjComponent ;
853 // _OutNode->Graph()->StartComponent( ThreadNo() , "SuperVisionContainer" ,
855 // myContainer , myObjComponent ) ;
856 // SUPERV::SuperG_var mySuperVisionComponent ;
857 // mySuperVisionComponent = SUPERV::SuperG::_narrow( myObjComponent ) ;
858 // CORBA::Object_ptr ObjRef1 ;
859 // ObjRef1 = mySuperVisionComponent->ToObject( IORObjRef ) ;
860 //#if PyDynInvokeTrace
861 // IORObjRef = ObjectToString( ObjRef1 ) ;
862 // cdebug << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name()
863 // << "ArgOut" << i << " : " << sname << " " << method << " ToObject "
864 // << IORObjRef << " (objref) " << endl ;
867 // ServicesAnyData anInParametersList ;
868 // anInParametersList.Name = "anIORObjRef" ;
869 // anInParametersList.Value <<= IORObjRef ;
870 // DynInvoke( mySuperVisionComponent , "ToObject" ,
871 // &anInParametersList , 1 , &OutParametersList[i] , 1 ) ;
872 //#if PyDynInvokeTrace
873 // OutParametersList[i].Value >>= ObjRef1 ;
874 // IORObjRef = ObjectToString( ObjRef1 ) ;
875 // cdebug << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name()
876 // << "ArgOut" << i << " : " << sname << " " << method
877 // << " DynInvoke(ToObject) " << IORObjRef << " (objref) " << endl ;
883 cdebug << "InNode::PyDynInvoke Result->ob_refcnt " << Result->ob_refcnt-1 << endl ;
885 Py_DECREF( Result ) ;
889 cdebug << "InNode::PyDynInvoke ArgsList->ob_refcnt " << ArgsList->ob_refcnt-1 << endl ;
891 Py_DECREF( ArgsList ) ;
900 for ( i = 0 ; i < nInParams ; i++ ) { // Without Gates
901 cdebug << "InParametersList[" << i << "] : "
902 << InParametersList[i].Name << " "
903 << AnyValue( InParametersList[i].Value ) << endl ;
905 for ( i = 0 ; i < nOutParams ; i++ ) { // Without Gates
906 cdebug << "OutParametersList[" << i << "] : "
907 << OutParametersList[i].Name << " "
908 << AnyValue( OutParametersList[i].Value ) << endl ;
914 cdebug_out << "GraphExecutor::InNode::PyDynInvoke Node " << Name() << " method " << method << " " << RetVal
922 bool GraphExecutor::InNode::PyRunSimpleString( char* thePyString )
924 const bool ErrorValue = true;
928 MESSAGE( pthread_self() << "Python method beginning : " << thePyString );
929 cdebug_in << pthread_self() << "Python method beginning : " << thePyString << endl ;
931 aRet = PyRun_SimpleString( thePyString );
932 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
933 //aRet = _OutNode->SuperVisionContainer()->ActivatePythonExecution( thePyString ) ;
935 MESSAGE( pthread_self() << "Python method finished." );
936 cdebug_out << pthread_self() << "Python method finished." << endl ;
939 MESSAGE( pthread_self() << "ERROR: Exception caught running Python method." );
940 cdebug_out << pthread_self() << "ERROR: Exception caught running Python method."
942 MESSAGE( " Python was reinitialized. Previous Python definitions are lost" );
943 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
946 Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod );
947 _OutNode->PyInitialized( false );
953 PyObject * GraphExecutor::InNode::PyEvalCallObject( const char *method ,
954 PyObject * MyPyRunMethod ,
955 PyObject * ArgsList ) {
956 // cdebug_in << "Executor::InNode::PyEvalCallObject " << Name() << endl ;
957 PyObject * Result = NULL ;
959 // MESSAGE( pthread_self() << "PyEval_CallObject " << Name() << " method beginning : " << method );
960 // cdebug << pthread_self() << "PyEval_CallObject " << Name() << " method beginning : " << method << endl ;
961 Result = PyEval_CallObject( MyPyRunMethod , ArgsList ) ;
962 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
963 //Result = _OutNode->SuperVisionContainer()->ActivatePythonExecution( MyPyRunMethod , ArgsList ) ;
964 // MESSAGE( pthread_self() << "PyEval_CallObject method finished. Result " << Result );
965 // cdebug << pthread_self() << "PyEval_CallObject method finished. Result " << Result << endl ;
966 // cdebug_out << "Executor::InNode::PyEvalCallObject " << Name() << endl ;
968 MESSAGE( pthread_self() << "ERROR: Exception caught PyEval_CallObject " << Name()
969 << " Python method " << method << ". Result " << Result );
970 cdebug << pthread_self() << "ERROR: Exception caught PyEval_CallObject " << Name()
971 << " Python method " << method << ". Result " << Result << endl ;
972 MESSAGE( " Python was reinitialized. Previous Python definitions are lost Py_IsInitialized " << Py_IsInitialized() );
973 //JR ===> fatal error in python : no current thread
974 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
977 Automaton()->PyInitModule( false ) ;
978 Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod );
979 _OutNode->PyInitialized( false );
981 if ( Result == NULL ) {
982 Kill() ; // Reset of _ThreadId
984 // cdebug << "Executor::InNode::PyEvalCallObject ERROR catched " << Name()
985 // << " Py_IsInitialized " << Py_IsInitialized() << endl ;
987 // cdebug_out << "Executor::InNode::PyEvalCallObject " << Name() << endl ;