Welcome to my blog. This blog was created mainly to help track my progress in Google's Summer of Code 2007, on which I've started implementing the Bug Triage and Forward Tool for the Debian Project. Currently I use it to write what I'm doing on free software projects or whatever is in my mind when I get some time/spirit to write.
Sunday, 1 July 2007
Exception handling on PyGTK applications
A feature I've been wanting to implement on bug-triage for some time now is to have unhandled exceptions shown in a gtk message dialog instead of getting lost on the terminal. And I've finally got it working now.
At first, I had some vague impression of having read about changing the default exception handler on Python. Maybe I've just mixed things up, but I couldn't find anything about that today (maybe it was Java?). Instead, I found out some information about Python decorators.
This is the first time I've seen anything like the decorators, and I must say this is a concept a bit hard to get used to, at least for me. Basically, if is a function that gets a callable (ie, a function or anything else that can be called) and returns another callable to be used in its place -- usually the original one with some nice extra.
I've created a python decorator to wrap the function call it decorates inside a try statement, whose except clause show some info about any exception it gets. The code can be seen on the bug-triage git repository.
On the linked file, errorhandler is the decorator and msg_exception takes an exception as argument and shows a message dialog. Applying the decorator to a function is simple, import it and add an @errorhandler just before its declaration.
Some references on decorators:
PS: I just got really happy to discover that this blogger/blogspot draft autosaving stuff works...
You were probably looking for sys.excepthook.
ReplyDeletehttp://docs.python.org/lib/module-sys.html
This has been done already :)
ReplyDeletehttp://faq.pygtk.org/index.py?req=show&file=faq20.010.htp