From: rahuel Date: Wed, 8 Dec 2004 13:39:51 +0000 (+0000) Subject: Remove of CasCatch handler and execution of python function in the main thread in... X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=8c39fa98ce66c575f90c9e3d234b3eee8fa6da96;p=modules%2Fsuperv.git Remove of CasCatch handler and execution of python function in the main thread in the class Container. --- diff --git a/src/GraphExecutor/DataFlowExecutor_PyDynInvoke.cxx b/src/GraphExecutor/DataFlowExecutor_PyDynInvoke.cxx index 0d39581..c22221d 100644 --- a/src/GraphExecutor/DataFlowExecutor_PyDynInvoke.cxx +++ b/src/GraphExecutor/DataFlowExecutor_PyDynInvoke.cxx @@ -17,10 +17,9 @@ using namespace std; #include "DataFlowExecutor_InNode.hxx" #include "DataFlowExecutor_OutNode.hxx" -#include "CASCatch_SignalsHandler.h" // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC - using namespace CORBA ; +//static PyObject * MyPyObjSignal = NULL ; static PyObject * MyPyObjRef = NULL ; static PyObject * MyPyObjIor = NULL ; static PyObject * MyPyRunMethod = NULL ; @@ -36,33 +35,57 @@ bool GraphExecutor::InNode::InitPython() { Automaton()->PyLock() ; // PyObject * Module = Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod ) ; if ( !Automaton()->PyInitModule() ) { - Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod ) ; +// Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod ) ; + _OutNode->SuperVisionContainer()->ActivatePythonExecution( "InitPyRunMethod" , MethodPyRunMethod ) ; } // PyObject * Dictionnary = PyModule_GetDict( Module ) ; // InitPyDynInvokeError = PyErr_NewException( "InitPyDynInvokeError" , NULL , NULL ) ; // PyDict_SetItemString( Dictionnary , InitPyDynInvokeError ) ; cdebug << ThreadNo() << "Py_Initialized() " << endl ; +#if 0 +//JR : Python documentation says that there is no sense to set a handler of synchronous +// signals as SIGSEGV and SIGFPE !!! + if ( Automaton()->PyFunction( "PyObjSignal" ) == NULL ) { + string aPyFunc ; + aPyFunc = "import signal\n" ; + aPyFunc += "import InitPyRunMethod\n" ; + aPyFunc += "import sys\n" ; + aPyFunc += "import CORBA\n" ; + aPyFunc += "import omniORB\n" ; + aPyFunc += "def PySignalHandler( aSignal , aStackFrame ) :\n" ; + aPyFunc += " print 'PySignalHandler(aSignal =',aSignal,',aStackFrame= ',aStackFrame,')'\n" ; + aPyFunc += "InitPyRunMethod.RunMethod( PySignalHandler )\n" ; + aPyFunc += "PrevHandler = signal.signal( signal.SIGSEGV , PySignalHandler )\n" ; + aPyFunc += "print 'PyObjSignal PrevHandler of SIGSEGV :', PrevHandler\n" ; + aPyFunc += "print 'PyObjSignal actual handler of SIGSEGV :', signal.getsignal( signal.SIGSEGV )\n" ; +// if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) { + if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) { + cdebug << ThreadNo() << " " << Name() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ; + Automaton()->PyUnLock() ; + return false ; + } + MyPyObjSignal = MyPyRunMethod ; + Automaton()->PyFunction( "PyObjSignal" , MyPyObjSignal ) ; + } + else { + MyPyObjSignal = Automaton()->PyFunction( "PyObjSignal" ) ; + } +#endif + if ( Automaton()->PyFunction( "PyObjRef" ) == NULL ) { string aPyFunc ; -// aPyFunc = "print 'InitPyRunMethod'\n" ; aPyFunc = "import InitPyRunMethod\n" ; -// aPyFunc += "print 'sys'\n" ; aPyFunc += "import sys\n" ; -// aPyFunc += "print 'CORBA'\n" ; aPyFunc += "import CORBA\n" ; -// aPyFunc += "print 'omniORB'\n" ; aPyFunc += "import omniORB\n" ; -// aPyFunc += "print 'PyObjRef'\n" ; aPyFunc += "def PyObjRef( IORObjStr ) :\n" ; -// aPyFunc += " print 'PyObjRef',IORObjStr\n" ; aPyFunc += " orb = CORBA.ORB_init( sys.argv , CORBA.ORB_ID )\n" ; -// aPyFunc += " print 'PyObjRef orb',orb\n" ; aPyFunc += " objref = orb.string_to_object( IORObjStr )\n" ; -// aPyFunc += " print 'PyObjRef IORObjStr objref',IORObjStr,objref\n" ; aPyFunc += " return objref\n" ; aPyFunc += "InitPyRunMethod.RunMethod( PyObjRef )\n" ; - if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) { +// if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) { + if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) { cdebug << ThreadNo() << " " << Name() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ; Automaton()->PyUnLock() ; return false ; @@ -81,14 +104,12 @@ bool GraphExecutor::InNode::InitPython() { aPyFunc += "import CORBA\n" ; aPyFunc += "import omniORB\n" ; aPyFunc += "def PyObjIor( ObjRef ) :\n" ; -// aPyFunc += " print 'PyObjIor',ObjRef\n" ; aPyFunc += " orb = CORBA.ORB_init( sys.argv , CORBA.ORB_ID )\n" ; -// aPyFunc += " print 'PyObjIor orb',orb\n" ; aPyFunc += " objIor = orb.object_to_string( ObjRef )\n" ; -// aPyFunc += " print 'PyObjIor ObjRef objIor',ObjRef,objIor\n" ; aPyFunc += " return objIor\n" ; aPyFunc += "InitPyRunMethod.RunMethod( PyObjIor )\n" ; - if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) { +// if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) { + if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) { cdebug << ThreadNo() << " " << Name() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ; Automaton()->PyUnLock() ; return false ; @@ -131,6 +152,7 @@ PyObject * GraphExecutor::InNode::InitPyDynInvoke( char * PyFuncName , aPyFunc += PyFuncName ; aPyFunc += " )\n" ; /*cdebug *///cout<< "InitPyDynInvoke PyRun_SimpleString " << endl << aPyFunc << endl ; +// if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) { if ( PyRunSimpleString( (char *) aPyFunc.c_str() ) ) { cdebug << ThreadNo() << " " << Name() << " PyRunSimpleString ERROR " << endl << aPyFunc << endl ; RetVal = false ; @@ -309,7 +331,8 @@ bool GraphExecutor::InNode::PyDynInvoke( PyObject * MyPyRunMethod , PyTuple_SetItem( MyPyObjRefList , 0 , ObjValue ) ; cdebug << "ArgIn" << i << " : " << sname << " " << method << " " << " Value " << IORObjRef << " (objref) " << MyPyObjRef->ob_refcnt << "/" << MyPyObjRefList->ob_refcnt << endl ; - ResultObj = PyEval_CallObject( MyPyObjRef , MyPyObjRefList ) ; +// ResultObj = PyEval_CallObject( MyPyObjRef , MyPyObjRefList ) ; + ResultObj = PyEvalCallObject( MyPyObjRef , MyPyObjRefList ) ; cdebug << "ObjValue->ob_refcnt" << ObjValue->ob_refcnt << endl ; ArgValue = Py_BuildValue( "O" , ResultObj ) ; PyTuple_SetItem( ArgsList , i , ArgValue ) ; @@ -332,7 +355,8 @@ bool GraphExecutor::InNode::PyDynInvoke( PyObject * MyPyRunMethod , } } - Result = PyEval_CallObject( MyPyRunMethod , ArgsList ) ; +// Result = PyEval_CallObject( MyPyRunMethod , ArgsList ) ; + Result = PyEvalCallObject( MyPyRunMethod , ArgsList ) ; cdebug << "ArgsList->ob_refcnt" << ArgsList->ob_refcnt << endl ; @@ -504,7 +528,8 @@ bool GraphExecutor::InNode::PyDynInvoke( PyObject * MyPyRunMethod , Py_INCREF( ObjIor ) ; // PyObject_Print( ObjIor , stdout , 0 ) ; PyTuple_SetItem( MyPyObjIorList , 0 , ObjIor ) ; - ResultIor = PyEval_CallObject( MyPyObjIor , MyPyObjIorList ) ; +// ResultIor = PyEval_CallObject( MyPyObjIor , MyPyObjIorList ) ; + ResultIor = PyEvalCallObject( MyPyObjIor , MyPyObjIorList ) ; cdebug << "ObjIor->ob_refcnt " << ObjIor->ob_refcnt-1 << endl ; Py_DECREF( ObjIor ) ; cdebug << "MyPyObjIorList->ob_refcnt " << MyPyObjIorList->ob_refcnt-1 << endl ; @@ -556,30 +581,56 @@ bool GraphExecutor::InNode::PyDynInvoke( PyObject * MyPyRunMethod , } -// This method was created in order to wrap PyRun_SimpleString() with exception handling wrapper. -// Python does not throw usual C++ style exceptions, so try-catch construct does not work for this -// purpose. Instead Python raises signals. If these signals are not caught by the application -// it will be caught by the OS and the application will be terminated : BUG PAL6869 -// Declaring CASCatch_SignalsHandler object redefines (!) standard C++ reserved words "try" and "catch". -// Standard_Failure "exception" is a signal in fact, and it will be caught in the catch section. -// Why do we finalize and re-initialize Python after catching the signal? Because the Python stack -// is corrupted after that. -// Declaration of CASCatch_SignalsHandler forces us to add linkage with CASCADE libraries.. See Makefile.in. bool GraphExecutor::InNode::PyRunSimpleString( char* thePyString ) { - CASCatch_SignalsHandler aSignalsHandler; const bool ErrorValue = true; bool aRet; try { - aRet = PyRun_SimpleString( thePyString ); - } catch( Standard_Failure ) { - MESSAGE( "ERROR: Exception caught running Python method." ); + MESSAGE( pthread_self() << "Python method beginning : " << thePyString ); + cdebug_in << pthread_self() << "Python method beginning : " << thePyString << endl ; +// aRet = PyRun_SimpleString( thePyString ); + aRet = _OutNode->SuperVisionContainer()->ActivatePythonExecution( thePyString ) ; + MESSAGE( pthread_self() << "Python method finished." ); + cdebug_out << pthread_self() << "Python method finished." << endl ; + } catch( ... ) { + MESSAGE( pthread_self() << "ERROR: Exception caught running Python method." ); + cdebug_out << pthread_self() << "ERROR: Exception caught running Python method." + << endl ; MESSAGE( " Python was reinitialized. Previous Python definitions are lost" ); - Py_Finalize(); - Py_Initialize(); - Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod ); +// Py_Finalize(); +// Py_Initialize(); +// Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod ); _OutNode->PyInitialized( false ); aRet = ErrorValue; } return aRet; } + +PyObject * GraphExecutor::InNode::PyEvalCallObject( PyObject * MyPyRunMethod , + PyObject * ArgsList ) { + cdebug_in << "Executor::InNode::PyEvalCallObject " << Name() << endl ; + PyObject * Result = NULL ; + try { + MESSAGE( pthread_self() << "PyEval_CallObject method beginning : " ); + cdebug << pthread_self() << "PyEval_CallObject method beginning : " << Name() << endl ; +// Result = PyEval_CallObject( MyPyRunMethod , ArgsList ) ; + Result = _OutNode->SuperVisionContainer()->ActivatePythonExecution( MyPyRunMethod , ArgsList ) ; + MESSAGE( pthread_self() << "PyEval_CallObject method finished." ); + cdebug << pthread_self() << "PyEval_CallObject method finished." << endl ; + cdebug_out << "Executor::InNode::PyEvalCallObject " << Name() << endl ; + } catch( ... ) { + MESSAGE( pthread_self() << "ERROR: Exception caught PyEval_CallObject Python method." ); + cdebug << pthread_self() << "ERROR: Exception caught PyEval_CallObject Python method." + << endl ; + MESSAGE( " Python was reinitialized. Previous Python definitions are lost Py_IsInitialized " << Py_IsInitialized() ); +//JR ===> fatal error in python : no current thread +// Py_Finalize(); +// Py_Initialize(); +// Automaton()->PyInitModule( false ) ; +// Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod ); +// _OutNode->PyInitialized( false ); + cdebug_out << "Executor::InNode::PyEvalCallObject ERROR catched " << Name() + << " Py_IsInitialized " << Py_IsInitialized() << endl ; + } + return Result ; +}