From: Steve C. <ste...@ya...> - 2004-11-20 03:51:35
|
On Fri, 2004-11-19 at 09:23 -0600, John Hunter wrote: > I think you are right that the plethora of error reporting strategies > is causing confusion, especially for me! I like the idea of the GUI > backends overriding placing a hook into the python exception handling > process. One possibility would be to do away with > verbose.report_error and error_msg. The GUIs hook into the exception > message, and anywhere we want to report an error we raise a python > exception. And we continue to use verbose.report as before. > > I just checked backend_ps and the only place is uses error_msg is > > error_msg_ps('Could not open %s for writing' % outfile) > > which would be more naturally handled as an exception anyway. > > Steve, could you look into hooking a GTK dialog into the python > exception reporting mechanism to see if this is viable? In summary, > the thought is > > * use verbose only for non-error reporting > > * use exceptions for all error reporting > > * work some GUI magic to transparently get the errors forwarded to a > dialog box w/o using special functions > > As for verbose.report, I'm not convinced it is a good idea to hook > this into the GUI. For one thing, some reporting occurs before the > backend is determined. For another, it would also require some > caching of messages because if 30 messages generate 30 popups it will > get annoying quick. These things are manageable, but I think the main > use for verbose.report is debugging a problem, in which case simply > having the messages go to a stdout or a file may be the best place for > them. > > JDH I was thinking of something like: class VerboseGTK(Verbose): def report_error(self, s): dialog = gtk.MessageDialog( parent = None, type = gtk.MESSAGE_ERROR, buttons = gtk.BUTTONS_OK, message_format = msg) dialog.run() dialog.destroy() So that the matlab interface can call verbose.report_error() and for image backends it writes to stdout and for GUI backends it pops up a message dialog. You can hook a GTK dialog into unhandled Python exceptions with: import sys def exception_handler(type, value, tb): """Handle uncaught exceptions""" error_msg_gtk(value) sys.excepthook = exception_handler (I've added this to backend_gtk.py in cvs if you want to try it out) But you still need to decide how to handle the exceptions - with some you need to terminate the program, with others its safe to continue. It may mean you end up writing a complicated generic exception handler that tries to handle every possible exception. In that case handling exceptions individually, the usual way might be better, possibly using the sys.excepthook to handle the remaining uncaught exceptions, or using it when you want to terminate the program and want to popup a message saying "Fatal error..." Steve |