Update of /cvsroot/wpdev/wolfpack/python
In directory sc8-pr-cvs1:/tmp/cvs-serv25898/python
Modified Files:
engine.cpp engine.h utilities.h
Log Message:
Fixed Memory Leaking in ai.cpp (hopefull).
Implemented advanced logging of Python Errors to the Console and the standard logfile. (Makes errorhandler.py obsolete).
Index: engine.cpp
===================================================================
RCS file: /cvsroot/wpdev/wolfpack/python/engine.cpp,v
retrieving revision 1.16
retrieving revision 1.17
diff -C2 -d -r1.16 -r1.17
*** engine.cpp 8 Sep 2003 10:58:47 -0000 1.16
--- engine.cpp 20 Sep 2003 01:05:10 -0000 1.17
***************
*** 162,163 ****
--- 162,271 ----
PyImport_ReloadModule( PyList_GetItem( mList, i ) );
}
+
+ void reportPythonError( QString moduleName )
+ {
+ // Print the Error
+ if( PyErr_Occurred() )
+ {
+ PyObject *exception, *value, *traceback;
+
+ PyErr_Fetch( &exception, &value, &traceback );
+ PyErr_NormalizeException( &exception, &value, &traceback );
+
+ // Set sys. variables for exception tracking
+ PySys_SetObject( "last_type", exception );
+ PySys_SetObject( "last_value", value );
+ PySys_SetObject( "last_traceback", traceback );
+
+ PyObject *exceptionName = PyObject_GetAttrString( exception, "__name__" );
+
+ // Do we have a detailed description of the error ?
+ PyObject *error = value != 0 ? PyObject_Str( value ) : 0;
+
+ if( !error )
+ {
+ if( !moduleName.isNull() )
+ {
+ Console::instance()->log( LOG_ERROR, QString( "An error occured while compiling \"%1\": %2" ).arg( moduleName ).arg( PyString_AsString( exceptionName ) ) );
+ }
+ else
+ {
+ Console::instance()->log( LOG_ERROR, QString( "An error occured: %1" ).arg( PyString_AsString( exceptionName ) ) );
+ }
+ }
+ else
+ {
+ if( !moduleName.isNull() )
+ {
+ Console::instance()->log( LOG_ERROR, QString( "An error occured in \"%1\": %2" ).arg( moduleName ).arg( PyString_AsString( exceptionName ) ) );
+ }
+ else
+ {
+ Console::instance()->log( LOG_ERROR, QString( "An error occured: %1" ).arg( PyString_AsString( exceptionName ) ) );
+ }
+
+ Console::instance()->log( LOG_PYTHON, QString( "%1: %2" ).arg( PyString_AsString( exceptionName ) ).arg( PyString_AsString( error ) ), false );
+ Py_XDECREF( error );
+ }
+
+ // Don't print a traceback for syntax errors
+ if( PyErr_GivenExceptionMatches( exception, PyExc_SyntaxError ) )
+ {
+ Py_XDECREF( traceback );
+ traceback = 0;
+ }
+
+ // Dump a traceback
+ while( traceback )
+ {
+ if( !PyObject_HasAttrString( traceback, "tb_frame" ) )
+ break;
+
+ PyObject *frame = PyObject_GetAttrString( traceback, "tb_frame" );
+
+ if( !PyObject_HasAttrString( frame, "f_code" ) )
+ {
+ Py_XDECREF( frame );
+ break;
+ }
+
+ PyObject *code = PyObject_GetAttrString( frame, "f_code" );
+
+ if( !PyObject_HasAttrString( code, "co_filename" ) || !PyObject_HasAttrString( code, "co_name" ) )
+ {
+ Py_XDECREF( frame );
+ Py_XDECREF( code );
+ break;
+ }
+
+ PyObject *pyFilename = PyObject_GetAttrString( code, "co_filename" );
+ PyObject *pyFunction = PyObject_GetAttrString( code, "co_name" );
+
+ QString filename = PyString_AsString( pyFilename );
+ QString function = PyString_AsString( pyFunction );
+
+ Py_XDECREF( pyFilename );
+ Py_XDECREF( pyFunction );
+
+ Py_XDECREF( code );
+ Py_XDECREF( frame );
+
+ PyObject *pyLine = PyObject_GetAttrString( traceback, "tb_lineno" );
+ unsigned int line = PyInt_AsLong( pyLine );
+ Py_XDECREF( pyLine );
+
+ // Print it
+ Console::instance()->log( LOG_PYTHON, QString( "File '%1',%2 in '%3'" ).arg( filename ).arg( line ).arg( function ), false );
+
+ // Switch Frames
+ PyObject *newtb = PyObject_GetAttrString( traceback, "tb_next" );
+ Py_XDECREF( traceback );
+ traceback = newtb;
+ }
+
+ Py_XDECREF( exceptionName );
+ Py_XDECREF( exception );
+ Py_XDECREF( value );
+ Py_XDECREF( traceback );
+ }
+ }
Index: engine.h
===================================================================
RCS file: /cvsroot/wpdev/wolfpack/python/engine.h,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** engine.h 4 Aug 2003 07:28:18 -0000 1.7
--- engine.h 20 Sep 2003 01:05:10 -0000 1.8
***************
*** 38,41 ****
--- 38,42 ----
#include <qglobal.h>
+ #include <qstring.h>
//#define DEBUG_PYTHON
***************
*** 53,56 ****
--- 54,59 ----
void stopPython( void );
void startPython( int argc, char* argv[], bool silent = false );
+ void reportPythonError( QString moduleName = QString::null );
+
#endif // __PYTHON_ENGINE_H__
Index: utilities.h
===================================================================
RCS file: /cvsroot/wpdev/wolfpack/python/utilities.h,v
retrieving revision 1.21
retrieving revision 1.22
diff -C2 -d -r1.21 -r1.22
*** utilities.h 9 Sep 2003 23:09:31 -0000 1.21
--- utilities.h 20 Sep 2003 01:05:10 -0000 1.22
***************
*** 34,37 ****
--- 34,38 ----
#include "engine.h"
#include "pyerrors.h"
+ #include <qstring.h>
#include "../typedefs.h"
***************
*** 63,75 ****
#define getIntProperty( identifier, property ) if( !strcmp( name, identifier ) ) return PyInt_FromLong( self->property );
#define getStrProperty( identifier, property ) if( !strcmp( name, identifier ) ) return PyString_FromString( self->property );
-
- /*!
- This function checks if there has been an error and tries to print it out.
- */
- inline void PyReportError( void )
- {
- if( PyErr_Occurred() )
- PyErr_Print();
- }
inline void wpDealloc( PyObject* self )
--- 64,67 ----
|