[Contribution] Debugging helper

Henning

21-10-2006 14:29:26

Since in pyogre python code calls c++ code which in turn calls other python code, you loose your stack trace on exceptions.
Imagine you have some Problem in your self written frame listener in the frameStarted method, your app will just crash with "unhandled exception" and you dont know what was wrong.

Catching them at a high level ("main"-)Function does not help, because the stack information is already lost there. Wrapping functions in try-except blocks by hand is enerving, so I decided to make a little module called utils.py which includes (beside other things)this:

def catch(f):
def new_f(*args, **kws):
try:
return f(*args, **kws)
except SystemExit, e:
raise e
except Exception, e:
s = ("=" * 40) + "\n"
sys.stderr.write(s)
sys.excepthook(*(sys.exc_info()))
sys.exit(1)
return new_f


If you now want to catch errors that occur in frameStarted you can do:


from pyogre import ogre
from utils import catch

class MyFrameListener(ogre.CombinedListener):
# ...
def frameStarted(self, event):
#...
frameStarted = catch(frameStarted)


Or, if you use Python 2.4 or above:

from pyogre import ogre
from utils import catch

class MyFrameListener(ogre.CombinedListener):
# ...
@catch
def frameStarted(self, event):
#...


Then, whenever there is a (Python) exception in frameStarted, your program will terminate, but you will get a stack trace and the row where the exception occured as well is a description of it.
Also very simple to remove later for speed (deleteting the @catch 'es or redefining catch).

Hope that helps someone.

Henning

dermont

25-10-2006 00:26:39

Nice, you should also take a look at the exception handling that Istari introduced for pyOgre1.2.0.
http://svn.berlios.de/wsvn/pyogre/branc ... rev=0&sc=0

svn checkout http://svn.berlios.de/svnroot/repos/pyo ... /dev-1.2.0
http://prdownload.berlios.de/pyogre/dem ... _1.2.0.zip