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...
Subscribe to:
Post Comments (Atom)
2 comments:
You were probably looking for sys.excepthook.
http://docs.python.org/lib/module-sys.html
This has been done already :)
http://faq.pygtk.org/index.py?req=show&file=faq20.010.htp
Post a Comment