1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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 // SUPERV GraphExecutor : contains classes that permit execution of graphs and particularly the execution automaton
23 // File : DataFlowExecutor_PyDynInvoke.cxx
24 // Author : Jean Rahuel, CEA
32 #include "DataFlowExecutor_InNode.hxx"
33 #include "DataFlowExecutor_OutNode.hxx"
35 using namespace CORBA ;
37 extern GraphExecutor::FiniteStateMachine * theAutomaton ;
39 //static PyObject * MyPyObjSignal = NULL ;
40 static PyObject * MyPyObjRef = NULL ;
41 static PyObject * MyPyObjIor = NULL ;
42 static PyObject * MyPyRunMethod = NULL ;
44 static PyMethodDef MethodPyRunMethod[] = {
45 { "RunMethod", PyRunMethod, METH_VARARGS },
49 bool GraphExecutor::InNode::InitPython() {
50 cdebug_in << "InitPython" << endl ;
52 Automaton()->PyLock() ;
53 // PyObject * Module = Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod ) ;
54 if ( !Automaton()->PyInitModule() ) {
55 Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod ) ;
56 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
57 //_OutNode->SuperVisionContainer()->ActivatePythonExecution( "InitPyRunMethod" , MethodPyRunMethod ) ;
59 // PAL10310: patch on omniORB
61 aPyFunc = "import omnipatch\n" ;
62 if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
63 cdebug << ThreadNo() << " " << Name() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ;
64 Automaton()->PyUnLock() ;
68 // PyObject * Dictionnary = PyModule_GetDict( Module ) ;
69 // InitPyDynInvokeError = PyErr_NewException( "InitPyDynInvokeError" , NULL , NULL ) ;
70 // PyDict_SetItemString( Dictionnary , InitPyDynInvokeError ) ;
71 cdebug << ThreadNo() << "Py_Initialized() " << endl ;
74 //JR : Python documentation says that there is no sense to set a handler of synchronous
75 // signals as SIGSEGV and SIGFPE !!!
76 if ( Automaton()->PyFunction( "PyObjSignal" ) == NULL ) {
78 aPyFunc = "import signal\n" ;
79 aPyFunc += "import InitPyRunMethod\n" ;
80 aPyFunc += "import sys\n" ;
81 aPyFunc += "import CORBA\n" ;
82 aPyFunc += "import omniORB\n" ;
83 aPyFunc += "def PySignalHandler( aSignal , aStackFrame ) :\n" ;
84 aPyFunc += " print 'PySignalHandler(aSignal =',aSignal,',aStackFrame= ',aStackFrame,')'\n" ;
85 aPyFunc += "InitPyRunMethod.RunMethod( PySignalHandler )\n" ;
86 aPyFunc += "PrevHandler = signal.signal( signal.SIGSEGV , PySignalHandler )\n" ;
87 aPyFunc += "print 'PyObjSignal PrevHandler of SIGSEGV :', PrevHandler\n" ;
88 aPyFunc += "print 'PyObjSignal actual handler of SIGSEGV :', signal.getsignal( signal.SIGSEGV )\n" ;
89 // if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
90 if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) {
91 cdebug << ThreadNo() << " " << Name() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ;
92 Automaton()->PyUnLock() ;
95 MyPyObjSignal = MyPyRunMethod ;
96 Automaton()->PyFunction( "PyObjSignal" , MyPyObjSignal ) ;
99 MyPyObjSignal = Automaton()->PyFunction( "PyObjSignal" ) ;
103 // PyObjRef convert an IOR (string) to an Python ObjectReference
104 if ( Automaton()->PyFunction( "PyObjRef" ) == NULL ) {
106 aPyFunc = "import InitPyRunMethod\n" ;
107 aPyFunc += "import sys\n" ;
108 aPyFunc += "import CORBA\n" ;
109 aPyFunc += "import omniORB\n" ;
110 aPyFunc += "orb = CORBA.ORB_init( sys.argv , CORBA.ORB_ID )\n" ;
111 aPyFunc += "def PyObjRef( IORObjStr ) :\n" ;
112 // aPyFunc += " orb = CORBA.ORB_init( sys.argv , CORBA.ORB_ID )\n" ;
113 aPyFunc += " objref = orb.string_to_object( IORObjStr )\n" ;
114 aPyFunc += " return objref\n" ;
115 aPyFunc += "InitPyRunMethod.RunMethod( PyObjRef )\n" ;
116 if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
117 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
118 //if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) {
119 cdebug << ThreadNo() << " " << Name() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ;
120 Automaton()->PyUnLock() ;
123 MyPyObjRef = MyPyRunMethod ;
124 Automaton()->PyFunction( "PyObjRef" , MyPyObjRef ) ;
127 MyPyObjRef = Automaton()->PyFunction( "PyObjRef" ) ;
130 // PyObjRef convert an Python ObjectReference to an IOR (string)
131 if ( Automaton()->PyFunction( "PyObjIor" ) == NULL ) {
133 aPyFunc = "import InitPyRunMethod\n" ;
134 aPyFunc += "import sys\n" ;
135 aPyFunc += "import CORBA\n" ;
136 aPyFunc += "import omniORB\n" ;
137 aPyFunc += "orb = CORBA.ORB_init( sys.argv , CORBA.ORB_ID )\n" ;
138 aPyFunc += "def PyObjIor( ObjRef ) :\n" ;
139 // aPyFunc += " orb = CORBA.ORB_init( sys.argv , CORBA.ORB_ID )\n" ;
140 aPyFunc += " objIor = orb.object_to_string( ObjRef )\n" ;
141 aPyFunc += " return objIor\n" ;
142 aPyFunc += "InitPyRunMethod.RunMethod( PyObjIor )\n" ;
143 if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
144 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
145 //if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) {
146 cdebug << ThreadNo() << " " << Name() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ;
147 Automaton()->PyUnLock() ;
150 MyPyObjIor = MyPyRunMethod ;
151 Automaton()->PyFunction( "PyObjIor" , MyPyObjIor ) ;
154 MyPyObjIor = Automaton()->PyFunction( "PyObjIor" ) ;
156 Automaton()->PyUnLock() ;
158 cdebug_out << "InitPython" << endl ;
163 void GraphExecutor::InNode::RemovePyDynInvoke( char * PyFuncName ) {
164 // Automaton()->PyLock() ; // Already done ...
165 if ( Automaton()->ErasePyFunction( PyFuncName ) ) {
166 cdebug << "InNode::RemovePyDynInvoke( " << PyFuncName << " ) success" << endl ;
169 cdebug << "InNode::RemovePyDynInvoke( " << PyFuncName << " ) ERROR failed" << endl ;
171 // Automaton()->PyUnLock() ;
174 PyObject * GraphExecutor::InNode::InitPyDynInvoke( char * PyFuncName ,
175 const SUPERV::ListOfStrings * aPythonFunction ,
180 PyObject * thePyRunMethod = NULL ;
182 cdebug_in << "InitPyDynInvoke '" << PyFuncName << "' length " << (*aPythonFunction).length() << endl ;
184 if ( strlen( PyFuncName ) ) {
185 Automaton()->PyLock() ;
186 thePyRunMethod = Automaton()->PyFunction( PyFuncName ) ;
188 //thePyRunMethod = NULL;
189 // asv 28.02.05 : VERY BAD fix of the following problem: after change of a function,
190 // the changes are NOT taken into account by Automation - it returns PyObject of the OLD function.
191 // so here we force re-automating the PyObject EVERY TIME, regardless if the function has changed or not.
192 // Once again - it is a very bad solution, it fully discards the whole idea of automation,
193 // here is it done as a quick fix for a bug.
194 // A better solution (to be implemented): store the PyObject NOT in Automation map, but in
195 // InLine node itself! And if the method is changed - remove the PyObject and force to regenerate it.
196 // But this means that PyObject must be stored in Editor's data model.
197 // asv 01.03.05 : the fix is not needed, the described bug is not reproduced. To investigate:
198 // WHERE PyObject is removed from Automation map on function change.
200 if ( (*aPythonFunction).length() ) {
201 if ( thePyRunMethod == NULL ) {
203 aPyFunc += "import InitPyRunMethod\n" ;
204 for ( i = 0 ; i < (*aPythonFunction).length() ; i++ ) {
205 aPyFunc += (*aPythonFunction)[ i ] ;
208 aPyFunc += "InitPyRunMethod.RunMethod( " ;
209 aPyFunc += PyFuncName ;
211 /*cdebug *///cout<< "InitPyDynInvoke PyRun_SimpleString " << endl << aPyFunc << endl ;
212 // if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
213 if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) {
214 cdebug << ThreadNo() << " " << Name() << " PyRunSimpleString ERROR " << endl << aPyFunc << endl ;
215 PyFuncRunned( false ) ;
220 PyFuncRunned( true ) ;
221 thePyRunMethod = MyPyRunMethod ;
223 Automaton()->PyFunction( PyFuncName , thePyRunMethod ) ;
224 cdebug << ThreadNo() << "PyRunSimpleString( " << (*aPythonFunction)[ 0 ] << " ) " << endl ;
228 Automaton()->PyUnLock() ;
229 cdebug_out << "InitPyDynInvoke '" << PyFuncName << "' thePyRunMethod " << thePyRunMethod << " "
230 << thePyRunMethod->ob_refcnt << endl ;
231 return thePyRunMethod ;
234 else if ( thePyRunMethod ) {
235 Automaton()->PyUnLock() ;
236 cdebug_out << "InitPyDynInvoke '" << PyFuncName << "' thePyRunMethod " << thePyRunMethod << " "
237 << thePyRunMethod->ob_refcnt << endl ;
238 return thePyRunMethod ;
240 Automaton()->PyUnLock() ;
243 cdebug_out << "InitPyDynInvoke '" << PyFuncName << "' thePyRunMethod Null Err " << Err << endl ;
248 extern "C" PyObject * PyRunMethod( PyObject * dummy , PyObject * Args ) {
249 PyObject * Result = NULL ;
250 PyObject * Temp = NULL ;
251 if ( PyArg_ParseTuple( Args , "O:set_callback" , & Temp ) ) {
252 // cout << "PyArg_ParsedTuple" << endl ;
253 if ( !PyCallable_Check( Temp ) ) {
254 // PyErr_SetString( PyExc_TypeError , "PyRunMethod must be callable" ) ;
255 // cout << "PyRunMethod must be callable" << endl ;
259 Py_XDECREF( MyPyRunMethod ) ;
260 MyPyRunMethod = Temp ;
261 Py_INCREF( Py_None ) ;
267 #define PyDynInvokeTrace 0
268 bool GraphExecutor::InNode::PyDynInvoke( PyObject * MyPyRunMethod ,
270 ServicesAnyData * InParametersList , int nInParams ,
271 ServicesAnyData * OutParametersList , int nOutParams ) {
279 cdebug_in << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name()
280 << " method " << method
281 << " InParametersList " << InParametersList
282 << " OutParametersList " << OutParametersList
283 << " " << nInParams << " InArgs " << nOutParams
284 << " OutArgs MyPyRunMethod " ;
286 if ( MyPyRunMethod ) {
288 cdebug << MyPyRunMethod << " " << MyPyRunMethod->ob_refcnt << endl ;
293 cdebug << " NULL" << endl ;
300 PyObject * ArgsList = NULL ;
301 PyObject * ArgValue = NULL ;
302 PyObject * Result = NULL ;
303 //PyObject * MyPyObjRefList = NULL ;
304 PyObject * ResultObj = NULL ;
305 //PyObject * MyPyObjIorList = NULL ;
306 PyObject * ResultIor = NULL ;
308 CORBA::Object_ptr ObjRef ;
312 if ( !PyCallable_Check( MyPyRunMethod ) ) {
317 ArgsList = PyTuple_New( nInParams ) ;
319 for ( i = 0 ; i < nInParams ; i++ ) {
320 data = InParametersList[i].Value ;
321 sname = InParametersList[i].Name.c_str() ;
322 switch ( data.type()->kind() ) {
323 case CORBA::tk_string : {
326 PyObject * ArgValue = Py_BuildValue( "s" , t ) ;
327 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
329 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << t << " (string) "
330 << " ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
331 << ArgValue->ob_refcnt << endl ;
335 case CORBA::tk_boolean : {
337 data >>= (CORBA::Any::to_boolean ) b ;
338 PyObject * ArgValue = Py_BuildValue( "b" , b ) ;
339 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
341 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << b
342 << " (boolean) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
343 << ArgValue->ob_refcnt << endl ;
347 case CORBA::tk_char : {
349 data >>= (CORBA::Any::to_char ) c ;
350 PyObject * ArgValue = Py_BuildValue( "c" , c ) ;
351 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
353 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << c
354 << " (char) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
355 << ArgValue->ob_refcnt << endl ;
359 case CORBA::tk_short : {
362 PyObject * ArgValue = Py_BuildValue( "h" , s ) ;
363 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
365 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << s
366 << " (short) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
367 << ArgValue->ob_refcnt << endl ;
371 case CORBA::tk_long : {
374 // epa - Porting to 64-bit Linux:CORBA::Long is incorrectly treated as unsigned
375 // integer by Py_BuildValue("l", val) in Python 2.4.1. Using Py_BuildValue("i",val)
377 PyObject * ArgValue = NULL;
378 if ( SIZEOF_LONG == 4 )
379 ArgValue = Py_BuildValue( "l" , l ) ;
381 ArgValue = Py_BuildValue( "i" , l ) ;
383 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << l
384 << " ArgValue->ob_refcnt" << ArgValue->ob_refcnt << endl ;
386 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
388 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << l
389 << " (CORBA::Long) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
390 << ArgValue->ob_refcnt << endl ;
394 case CORBA::tk_float : {
397 PyObject * ArgValue = Py_BuildValue( "f" , f ) ;
398 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
400 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << f
401 << " (float) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
402 << ArgValue->ob_refcnt << endl ;
406 case CORBA::tk_double : {
409 PyObject * ArgValue = Py_BuildValue( "d" , d ) ;
410 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
412 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << d
413 << " (double) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << " ArgValue->ob_refcnt"
414 << ArgValue->ob_refcnt << endl ;
418 case CORBA::tk_objref : {
419 //MyPyObjRefList = PyTuple_New( 1 ) ;
420 //PyObject * ObjValue ;
421 #if OMNIORB_VERSION >= 4
422 data >>= (CORBA::Any::to_object ) ObjRef ;
426 IORObjRef = ObjectToString( ObjRef ) ;
427 //ObjValue = Py_BuildValue( "s" , IORObjRef ) ;
428 //PyTuple_SetItem( MyPyObjRefList , 0 , ObjValue ) ;
430 cdebug << "ArgIn" << i << " : " << sname << " " << method << " " << " Value " << IORObjRef << " (objref) "<< endl ;
431 // << MyPyObjRef->ob_refcnt << "/" << MyPyObjRefList->ob_refcnt << endl ;
433 // ResultObj = PyEval_CallObject( MyPyObjRef , MyPyObjRefList ) ;
434 //ResultObj = PyEvalCallObject( "PyObjRef" , MyPyObjRef , MyPyObjRefList ) ;
436 //cdebug << "ObjValue->ob_refcnt" << ObjValue->ob_refcnt << endl ;
438 //ArgValue = Py_BuildValue( "O" , ResultObj ) ;
439 //PyTuple_SetItem( ArgsList , i , ArgValue ) ;
442 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value (objref) ArgsList->ob_refcnt"
443 << ArgsList->ob_refcnt << " ArgValue->ob_refcnt" << ArgValue->ob_refcnt << endl ;
445 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value (objref) ArgsList->ob_refcnt"
446 << ArgsList->ob_refcnt << " ArgValue is NULL" << endl ;
447 //cdebug << "MyPyObjRefList->ob_refcnt " << MyPyObjRefList->ob_refcnt-1 << endl ;
449 ResultObj = PyObject_CallFunction( MyPyObjRef , "s",IORObjRef ) ;
450 if(ResultObj == NULL){
453 Py_INCREF(ResultObj) ;
455 PyTuple_SetItem( ArgsList , i , ResultObj ) ;
456 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value (objref) ArgsList->ob_refcnt"
457 << ArgsList->ob_refcnt << " ArgValue->ob_refcnt" << ResultObj->ob_refcnt << endl ;
458 // << ArgsList->ob_refcnt << " ArgValue->ob_refcnt" << ArgValue->ob_refcnt << endl ;
459 //cdebug << "MyPyObjRefList->ob_refcnt " << MyPyObjRefList->ob_refcnt-1 << endl ;
460 //Py_DECREF( MyPyObjRefList ) ;
461 //if ( CORBA::is_nil( ObjRef ) ) {
462 // ResultObj = NULL ;
465 //#if PyDynInvokeTrace
467 //cdebug << "ResultObj->ob_refcnt " << ResultObj->ob_refcnt-1 << endl ;
469 //cdebug << "ResultObj is NULL" << endl ;
472 //Py_DECREF( ResultObj ) ;
477 cdebug << "ArgIn" << i << " : " << sname << " Value " << "(other ERROR)" << endl ;
482 // Result = PyEval_CallObject( MyPyRunMethod , ArgsList ) ;
483 Result = PyEvalCallObject( method , MyPyRunMethod , ArgsList ) ;
486 cdebug << "ArgsList->ob_refcnt" << ArgsList->ob_refcnt << endl ;
489 if ( Result == NULL ) {
490 cdebug_out << "GraphExecutor::InNode::PyDynInvoke Node " << Name() << " " << method << " Error Result == NULL"
496 for ( i = 0 ; i < nOutParams ; i++ ) {
497 data = OutParametersList[i].Value ;
498 sname = OutParametersList[i].Name.c_str() ;
499 switch ( data.type()->kind() ) {
500 case CORBA::tk_string : {
502 PyObject * ArgValue ;
503 if ( PyTuple_Check( Result ) ) {
504 ArgValue = PyTuple_GetItem( Result , i ) ;
509 if ( !ArgValue || !PyString_Check( ArgValue ) ) {
510 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (string)" << endl ;
514 t = PyString_AsString( ArgValue ) ;
519 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << t << " (string)"
520 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
521 << ArgValue->ob_refcnt << endl ;
525 case CORBA::tk_boolean : {
527 PyObject * ArgValue ;
528 if ( PyTuple_Check( Result ) ) {
529 ArgValue = PyTuple_GetItem( Result , i ) ;
534 if ( !ArgValue || !PyInt_Check( ArgValue ) ) {
535 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (boolean)" << endl ;
539 b = PyInt_AsLong( ArgValue ) ;
541 data <<= (CORBA::Any::from_boolean ) b ;
544 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << b << " (boolean)"
545 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
546 << ArgValue->ob_refcnt << endl ;
550 case CORBA::tk_char : {
551 unsigned char c = 0 ;
552 PyObject * ArgValue ;
553 if ( PyTuple_Check( Result ) ) {
554 ArgValue = PyTuple_GetItem( Result , i ) ;
559 //JR 04.04.2005 Debug if ( !PyInt_Check( ArgValue ) ) {
560 //Difficult to understand that behavior ... Python char type is a string of length 1 !
561 // or type is int ...
562 if ( !ArgValue || !PyString_Check( ArgValue ) && !PyInt_Check( ArgValue ) ) {
563 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (char)" << endl ;
567 c = PyInt_AsLong( ArgValue ) ;
569 data <<= (CORBA::Any::from_char ) c ;
572 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << c << " (char)"
573 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
574 << ArgValue->ob_refcnt << endl ;
578 case CORBA::tk_short : {
580 PyObject * ArgValue ;
581 if ( PyTuple_Check( Result ) ) {
582 ArgValue = PyTuple_GetItem( Result , i ) ;
587 if ( !ArgValue || !PyInt_Check( ArgValue ) ) {
588 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (short)" << endl ;
592 s = PyInt_AsLong( ArgValue ) ;
597 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << s << " (short)"
598 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
599 << ArgValue->ob_refcnt << endl ;
603 case CORBA::tk_long : {
605 PyObject * ArgValue ;
606 if ( PyTuple_Check( Result ) ) {
607 ArgValue = PyTuple_GetItem( Result , i ) ;
612 if ( ArgValue && PyLong_Check( ArgValue ) ) {
613 l = PyLong_AsLong( ArgValue ) ;
615 else if ( ArgValue && PyInt_Check( ArgValue ) ) {
616 l = PyInt_AsLong( ArgValue ) ;
619 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (CORBA::tk_long)" << endl ;
625 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << l << " (CORBA::Long)"
626 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
627 << ArgValue->ob_refcnt << endl ;
631 case CORBA::tk_float : {
633 PyObject * ArgValue ;
634 if ( PyTuple_Check( Result ) ) {
635 ArgValue = PyTuple_GetItem( Result , i ) ;
641 if ( !PyFloat_Check( ArgValue ) ) {
642 // mkr : bug PAL12454
643 if ( PyLong_Check( ArgValue ) ) {
644 cdebug << "ArgOut" << i << " : " << sname << " " << method << " OK (CORBA::Long)" << endl ;
645 CORBA::Long l = PyLong_AsLong( ArgValue );
648 else if ( PyInt_Check( ArgValue ) ) {
649 cdebug << "ArgOut" << i << " : " << sname << " " << method << " OK (int)" << endl ;
650 CORBA::Long l = PyInt_AsLong( ArgValue );
654 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (float)" << endl ;
659 f = PyFloat_AsDouble( ArgValue ) ;
663 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (float)" << endl ;
666 #ifdef REDHAT // mkr : debug for PAL12255
669 //JR data.replace(CORBA::TypeCode::PR_float_tc(), (void*)(&f));
670 data <<= (CORBA::Float) f ;
674 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << f << " (float)"
675 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
676 << ArgValue->ob_refcnt << endl ;
680 case CORBA::tk_double : {
682 PyObject * ArgValue ;
683 if ( PyTuple_Check( Result ) ) {
684 ArgValue = PyTuple_GetItem( Result , i ) ;
690 if ( !PyFloat_Check( ArgValue ) ) {
691 // mkr : bug PAL12454
692 if ( PyLong_Check( ArgValue ) ) {
693 cdebug << "ArgOut" << i << " : " << sname << " " << method << " OK (CORBA::Long)" << endl ;
694 CORBA::Long l = PyLong_AsLong( ArgValue );
697 else if ( PyInt_Check( ArgValue ) ) {
698 cdebug << "ArgOut" << i << " : " << sname << " " << method << " OK (int)" << endl ;
699 CORBA::Long l = PyInt_AsLong( ArgValue );
703 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (float)" << endl ;
708 d = PyFloat_AsDouble( ArgValue ) ;
712 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (float)" << endl ;
715 #ifdef REDHAT // mkr : debug for PAL12255
718 //JR data.replace(CORBA::TypeCode::PR_double_tc(), (void*)(&d));
719 data <<= (CORBA::Double) d ;
723 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << d << " (double)"
724 << " Result->ob_refcnt" << Result->ob_refcnt << " ArgValue->ob_refcnt"
725 << ArgValue->ob_refcnt << endl ;
729 case CORBA::tk_objref : {
731 //MyPyObjIorList = PyTuple_New( 1 ) ;
732 if ( PyTuple_Check( Result ) ) {
733 ObjIor = PyTuple_GetItem( Result , i ) ;
740 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << "(object reference) "
741 << " Result->ob_refcnt" << Result->ob_refcnt << " ObjIor->ob_refcnt"
742 << ObjIor->ob_refcnt << endl ;
744 //Py_INCREF( ObjIor ) ;
745 // PyObject_Print( ObjIor , stdout , 0 ) ;
746 //PyTuple_SetItem( MyPyObjIorList , 0 , ObjIor ) ;
747 // ResultIor = PyEval_CallObject( MyPyObjIor , MyPyObjIorList ) ;
748 //ResultIor = PyEvalCallObject( "PyObjIor" , MyPyObjIor , MyPyObjIorList ) ;
749 ResultIor = PyObject_CallFunction( MyPyObjIor , "O",ObjIor ) ;
752 cdebug << "ObjIor->ob_refcnt " << ObjIor->ob_refcnt-1 << endl ;
754 //Py_DECREF( ObjIor ) ;
756 //cdebug << "MyPyObjIorList->ob_refcnt " << MyPyObjIorList->ob_refcnt-1 << endl ;
758 //Py_DECREF( MyPyObjIorList ) ;
760 //cdebug << "MyPyObjIor->ob_refcnt " << MyPyObjIor->ob_refcnt << endl ;
764 IOR = PyString_AsString( ResultIor ) ;
765 ObjRef = StringToObject( IOR ) ;
766 //JR 05.08.2005 BUG OMNIORB4 ? : that line does run with SALOME_3 (OMNIOrb4) for
767 // object references ... (It's Ok with OMNIOrb3)
768 // IOR and ObjRef and IORObjRef are Ok
769 // BUT data contains a nil reference !!!
773 SUPERV::SuperG_var mySuperVisionComponent ;
774 mySuperVisionComponent = theAutomaton->SuperVisionComponent() ;
775 if ( CORBA::is_nil( mySuperVisionComponent ) ) {
776 Engines::Container_var myContainer ;
777 Engines::Component_var myObjComponent ;
778 _OutNode->Graph()->StartComponent( ThreadNo() , "SuperVisionContainer" ,
780 myContainer , myObjComponent ) ;
781 mySuperVisionComponent = SUPERV::SuperG::_narrow( myObjComponent ) ;
782 theAutomaton->SuperVisionComponent( mySuperVisionComponent ) ;
784 //JR 24.08.2005 BUG OMNIORB4 ? : That does not run also ==> Comments
785 // CORBA::Object_ptr ObjRef1 ;
786 // ObjRef1 = mySuperVisionComponent->ToObject( ObjRef ) ;
787 // data <<= ObjRef1 ;
788 //JR 24.08.2005 BUG OMNIORB4 ? : BUT THAT RUNS WITH THE "IDENTITY METHOD ToObject of SUPERV.idl :
789 // Object ToObject( in Object anObject ) ;
790 //CODE of that method !!! :
791 //CORBA::Object_ptr SuperV_Impl::ToObject( CORBA::Object_ptr anObjectRef ) {
792 // beginService( "SuperV_Impl::ToObject" );
793 // CORBA::Object_ptr anObject ;
794 // anObject = CORBA::Object::_duplicate( anObjectRef ) ;
795 // endService( "SuperV_Impl::ToObject" );
798 //BUT THAT RUNS ONLY IF WE USE Dynamic invocation of CORBA (DynInvoke)
799 ServicesAnyData anInParametersList ;
800 anInParametersList.Name = "anObjRef" ;
801 anInParametersList.Value <<= ObjRef ;
802 DynInvoke( mySuperVisionComponent , "ToObject" ,
803 &anInParametersList , 1 , &OutParametersList[i] , 1 ) ;
804 data = OutParametersList[i].Value ;
806 IORObjRef = ObjectToString( ObjRef ) ;
808 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value IORPy "
809 << IOR << " IORObjRef " << IORObjRef << " (objref) " << endl ;
811 if ( CORBA::is_nil( ObjRef ) ) {
813 cdebug << "ObjRef ERROR(nil reference) ResultIor->ob_refcnt "
814 << ResultIor->ob_refcnt-1 << endl ;
820 cdebug << "ObjRef NOT(nil reference)ResultIor->ob_refcnt "
821 << ResultIor->ob_refcnt-1 << endl ;
823 Py_DECREF( ResultIor ) ;
828 cdebug_out << "GraphExecutor::InNode::PyDynInvoke Error ResultIor == NULL Node "
829 << Name() << " method " << method << " " << endl ;
835 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << "(other ERROR)" << endl ;
839 //JR 05.08.2005 BUG OMNIORB4 ? : that line does run with SALOME_3 (OMNIOrb4) for
840 // object references ...
841 OutParametersList[i].Value = data ;
842 // if ( data.type()->kind() == CORBA::tk_objref ) {
843 // IORObjRef = ObjectToString( ObjRef ) ;
844 //#if PyDynInvokeTrace
845 // cdebug << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name()
846 // << "ArgOut" << i << " : " << sname << " " << method << " ObjRef "
847 // << IORObjRef << " (objref) " << endl ;
848 // Engines::Component_var theObjComponent ;
849 // theObjComponent = Engines::Component::_narrow( ObjRef ) ;
850 // DynInvoke( theObjComponent , "ping" , NULL , 0 , NULL , 0 ) ;
852 // Engines::Container_var myContainer ;
853 // Engines::Component_var myObjComponent ;
854 // _OutNode->Graph()->StartComponent( ThreadNo() , "SuperVisionContainer" ,
856 // myContainer , myObjComponent ) ;
857 // SUPERV::SuperG_var mySuperVisionComponent ;
858 // mySuperVisionComponent = SUPERV::SuperG::_narrow( myObjComponent ) ;
859 // CORBA::Object_ptr ObjRef1 ;
860 // ObjRef1 = mySuperVisionComponent->ToObject( IORObjRef ) ;
861 //#if PyDynInvokeTrace
862 // IORObjRef = ObjectToString( ObjRef1 ) ;
863 // cdebug << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name()
864 // << "ArgOut" << i << " : " << sname << " " << method << " ToObject "
865 // << IORObjRef << " (objref) " << endl ;
868 // ServicesAnyData anInParametersList ;
869 // anInParametersList.Name = "anIORObjRef" ;
870 // anInParametersList.Value <<= IORObjRef ;
871 // DynInvoke( mySuperVisionComponent , "ToObject" ,
872 // &anInParametersList , 1 , &OutParametersList[i] , 1 ) ;
873 //#if PyDynInvokeTrace
874 // OutParametersList[i].Value >>= ObjRef1 ;
875 // IORObjRef = ObjectToString( ObjRef1 ) ;
876 // cdebug << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke Node " << Name()
877 // << "ArgOut" << i << " : " << sname << " " << method
878 // << " DynInvoke(ToObject) " << IORObjRef << " (objref) " << endl ;
884 cdebug << "InNode::PyDynInvoke Result->ob_refcnt " << Result->ob_refcnt-1 << endl ;
886 Py_DECREF( Result ) ;
890 cdebug << "InNode::PyDynInvoke ArgsList->ob_refcnt " << ArgsList->ob_refcnt-1 << endl ;
892 Py_DECREF( ArgsList ) ;
901 for ( i = 0 ; i < nInParams ; i++ ) { // Without Gates
902 cdebug << "InParametersList[" << i << "] : "
903 << InParametersList[i].Name << " "
904 << AnyValue( InParametersList[i].Value ) << endl ;
906 for ( i = 0 ; i < nOutParams ; i++ ) { // Without Gates
907 cdebug << "OutParametersList[" << i << "] : "
908 << OutParametersList[i].Name << " "
909 << AnyValue( OutParametersList[i].Value ) << endl ;
915 cdebug_out << "GraphExecutor::InNode::PyDynInvoke Node " << Name() << " method " << method << " " << RetVal
923 bool GraphExecutor::InNode::PyRunSimpleString( char* thePyString )
925 const bool ErrorValue = true;
929 MESSAGE( pthread_self() << "Python method beginning : " << thePyString );
930 cdebug_in << pthread_self() << "Python method beginning : " << thePyString << endl ;
932 aRet = PyRun_SimpleString( thePyString );
933 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
934 //aRet = _OutNode->SuperVisionContainer()->ActivatePythonExecution( thePyString ) ;
936 MESSAGE( pthread_self() << "Python method finished." );
937 cdebug_out << pthread_self() << "Python method finished." << endl ;
940 MESSAGE( pthread_self() << "ERROR: Exception caught running Python method." );
941 cdebug_out << pthread_self() << "ERROR: Exception caught running Python method."
943 MESSAGE( " Python was reinitialized. Previous Python definitions are lost" );
944 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
947 Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod );
948 _OutNode->PyInitialized( false );
954 PyObject * GraphExecutor::InNode::PyEvalCallObject( const char *method ,
955 PyObject * MyPyRunMethod ,
956 PyObject * ArgsList ) {
957 // cdebug_in << "Executor::InNode::PyEvalCallObject " << Name() << endl ;
958 PyObject * Result = NULL ;
960 // MESSAGE( pthread_self() << "PyEval_CallObject " << Name() << " method beginning : " << method );
961 // cdebug << pthread_self() << "PyEval_CallObject " << Name() << " method beginning : " << method << endl ;
962 Result = PyEval_CallObject( MyPyRunMethod , ArgsList ) ;
963 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
964 //Result = _OutNode->SuperVisionContainer()->ActivatePythonExecution( MyPyRunMethod , ArgsList ) ;
965 // MESSAGE( pthread_self() << "PyEval_CallObject method finished. Result " << Result );
966 // cdebug << pthread_self() << "PyEval_CallObject method finished. Result " << Result << endl ;
967 // cdebug_out << "Executor::InNode::PyEvalCallObject " << Name() << endl ;
969 MESSAGE( pthread_self() << "ERROR: Exception caught PyEval_CallObject " << Name()
970 << " Python method " << method << ". Result " << Result );
971 cdebug << pthread_self() << "ERROR: Exception caught PyEval_CallObject " << Name()
972 << " Python method " << method << ". Result " << Result << endl ;
973 MESSAGE( " Python was reinitialized. Previous Python definitions are lost Py_IsInitialized " << Py_IsInitialized() );
974 //JR ===> fatal error in python : no current thread
975 // asv : 20.01.05 : changes involved with switching to old (HEAD) KERNEL
978 Automaton()->PyInitModule( false ) ;
979 Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod );
980 _OutNode->PyInitialized( false );
982 if ( Result == NULL ) {
983 Kill() ; // Reset of _ThreadId
985 // cdebug << "Executor::InNode::PyEvalCallObject ERROR catched " << Name()
986 // << " Py_IsInitialized " << Py_IsInitialized() << endl ;
988 // cdebug_out << "Executor::InNode::PyEvalCallObject " << Name() << endl ;