|
From: Jochen V. <vo...@se...> - 2004-11-18 18:06:00
|
Hello,
I tried to think a little bit more systematically about the way
error conditions and messages are passed around in matplotlib.
1) There see to be several kinds of object:
- informational messages like the output of --verbose-debug-annoying
- error messages which the matlab interface passes to the
backends to have them displayed to the user
- error conditions reported by the backends to the caller
2) Typical scenarios:
a) A user tries to debug some installation problem by using
the --verbose-debug-annoying option or similar. This generates
potentially a lot of messages which should shown to the user.
Dumping them all to stdout or stderr seems fine here.
b) A script calls the matlab interface functions with invalid arguments.
The user should get an error message. In a GUI backend this error
message might pop up in a message window or similar.
c) A backend encounters an error condition, e.g. if it cannot open
the file, cannot load a font etc. If a GUI backend is active
the error message should be displayed by the GUI backend,
otherwise it should be printed to stderr or similar.
Is this so far correct?
3) Required or almost required properties of the implementation:
- the user should be able to customise how much information he
sees, e.g. by using the --verbose-* options
- The GUI backends should not be terminated if there is an error
while saving a file etc.
4) some ideas:
- The backends could return errors via python standard exceptions
without printing messages and such. This leads to simple code,
feels quite Pythonic, makes the backends more independent of the
error reporting policy and at the moment seems to be the only sane
solution to me.
- The backends should have a function which the matlab interface can
call to report errors to the user. This should pop up a dialog
etc. on GUI backends and just print the message to stderr for
non-GUI backends. It should not terminate the program.
Rationale for not calling verbose.report_error in this function:
the GUI backend reports the error on its own (dialog, status line,
etc), so it does not need to use verbose.report_error
additionally. When the PostScript backend is called from the GUI
backend, then the GUI backends error report function will be used,
what PostScript does does not matter. In all other cases
displaying the error message to stderr seems the only useful thing
to do (e.g. disabling it is not useful).
5) the verbose.report mechanism:
At the moment I am not sure how this fits in here. Maybe it should
only be used for non-error messages?
What is configurable here? The user can choose the amount of
messages he wants to see. Anything else?
6) Analysis of the three scenarios above:
a) --verbose-* output:
Here the flow of control is straightforward. At random places
there are calls to verbose.report. These are either discarded
or printed to stdout. This is currently done by the Verbose class.
b) invalid arguments
Again, this is simple: the matlab interface calls error_msg,
the GUI backend does something useful with the message.
c) backend error conditions
This seems to be the most complex scenario. I think this should
work as follows: the backend raises the standard Python exception,
e.g IOError. If the GUI backend calls the PostScript backend,
it should catch any exception from the PostScript backend call,
and tell the user about it.
If the user in a script calls e.g. "savefig('/fig1.eps')" and has
no write permission for the root directory, then he will get the
Python exception and can do with it whatever he likes.
7) Conclusions:
I suggest the following.
- Change backend_template.py and all the other non-GUI backends to
def error_msg_ps(msg, *args):
"""
Signal an error condition.
"""
sys.stderr.write('Error: %s' % msg)
Maybe even make this function a class method of the
FigureManagerBase class? (the "error_msg =3D error_msg_..."
assignments seem hackish.)
- Change all callers of error_msg to terminate the program after the
call when appropriate
- Remove the verbose.report_error function and replace it with Python
exceptions etc.
What do you think?
Jochen
--=20
http://seehuhn.de/
|
|
From: Steve C. <ste...@ya...> - 2004-11-19 09:24:08
|
On Thu, 2004-11-18 at 18:05 +0000, Jochen Voss wrote:
> 4) some ideas:
>
> - The backends could return errors via python standard exceptions
> without printing messages and such. This leads to simple code,
> feels quite Pythonic, makes the backends more independent of the
> error reporting policy and at the moment seems to be the only sane
> solution to me.
>
> - The backends should have a function which the matlab interface can
> call to report errors to the user. This should pop up a dialog
> etc. on GUI backends and just print the message to stderr for
> non-GUI backends. It should not terminate the program.
I agree with these ideas.
> 7) Conclusions:
>
> I suggest the following.
>
> - Change backend_template.py and all the other non-GUI backends to
>
> def error_msg_ps(msg, *args):
> """
> Signal an error condition.
> """
> sys.stderr.write('Error: %s' % msg)
I'd prefer to merge error_msg into the Verbose class, or just delete it.
> - Change all callers of error_msg to terminate the program after the
> call when appropriate
Agree
> - Remove the verbose.report_error function and replace it with Python
> exceptions etc.
Agree
> What do you think?
At the moment we have error_msg(), the Verbose class and exceptions all
working in the same area and a bit of confusion as to which one does
what. I don't think we need all three.
I suggest
- using exceptions to handle errors that may terminate the program, and
allowing the matlab interface, GUI backends and user scripts to catch
these exceptions.
- using verbose for all reporting
- merging error_msg() into the Verbose class (with the GUI backends
possibly subclassing Verbose to provide a popup error dialog)
Steve
|
|
From: John H. <jdh...@ac...> - 2004-11-19 15:23:57
|
>>>>> "Steve" == Steve Chaplin <ste...@ya...> writes:
Steve> At the moment we have error_msg(), the Verbose class and
Steve> exceptions all working in the same area and a bit of
Steve> confusion as to which one does what. I don't think we need
Steve> all three.
Steve> I suggest - using exceptions to handle errors that may
Steve> terminate the program, and allowing the matlab interface,
Steve> GUI backends and user scripts to catch these exceptions. -
Steve> using verbose for all reporting - merging error_msg() into
Steve> the Verbose class (with the GUI backends possibly
Steve> subclassing Verbose to provide a popup error dialog)
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
|
|
From: Jochen V. <vo...@se...> - 2004-11-19 16:30:40
|
Hello,
On Fri, Nov 19, 2004 at 09:23:36AM -0600, John Hunter wrote:
> In summary, the thought is
>=20
> * use verbose only for non-error reporting
>=20
> * use exceptions for all error reporting
I agree with this.
> * work some GUI magic to transparently get the errors forwarded to a
> dialog box w/o using special functions
Here I have no opinion until I understand the special kind
of magic which is going to be used here.
> For another, it would also require some
> caching of messages because if 30 messages generate 30 popups it will
> get annoying quick.
Yes, we have to be careful here.
As nobody objected to this part of the plan, I changed the PostScript
backend in CVS as follows:
diff -u -r1.13 backend_ps.py
--- backend_ps.py 13 Nov 2004 11:41:54 -0000 1.13
+++ backend_ps.py 19 Nov 2004 16:23:13 -0000
@@ -529,10 +529,7 @@
basename, ext =3D os.path.splitext(outfile)
if not ext: outfile +=3D '.ps'
isEPSF =3D ext.lower().startswith('.ep')
- try:
- fh =3D file(outfile, 'w')
- except IOError:
- error_msg_ps('Could not open %s for writing' % outfile)
+ fh =3D file(outfile, 'w')
needsClose =3D True
title =3D outfile
Slight problem: it might now be a little bit more difficult to include
the name of the file which could not be opened in the error message.
The IOError exception will probably only have "permission denied" associated
with it.
All the best,
Jochen
--=20
http://seehuhn.de/
|
|
From: John H. <jdh...@ac...> - 2004-11-19 16:34:07
|
>>>>> "Jochen" == Jochen Voss <vo...@se...> writes:
Jochen> Slight problem: it might now be a little bit more
Jochen> difficult to include the name of the file which could not
Jochen> be opened in the error message. The IOError exception
Jochen> will probably only have "permission denied" associated
Jochen> with it.
Looks OK, at least on linux
>>> file('/sbin/ldconfig', 'w')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: [Errno 13] Permission denied: '/sbin/ldconfig'
|
|
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
|
|
From: Jochen V. <vo...@se...> - 2004-11-20 14:10:18
|
Hello,
On Sat, Nov 20, 2004 at 11:53:01AM +0800, Steve Chaplin wrote:
> I was thinking of something like:
>=20
> class VerboseGTK(Verbose):
> def report_error(self, s):
> dialog =3D gtk.MessageDialog(
> parent =3D None,
> type =3D gtk.MESSAGE_ERROR,
> buttons =3D gtk.BUTTONS_OK,
> message_format =3D msg)
> dialog.run()
> dialog.destroy()
Alternatively we could make report_error a figure_manager method.
If could default to
class FigureManagerBase:
def report_error(self, s):
sys.stderr.write("error: %s\n"%s)
And FigureManagerGTK could overload it with the above code to generate
an error box.
Reasons why I would prefer this:
1) I do not like these global variables which are set on module import
at all. Using the VerboseGTK idea we would get another instance of this,
namely something like "currentVerboseClass=3DVerboseBackend" or such.
We already have something like this for figure managers, so no new instan=
ce
of this would be created with my suggestion.
Reporting errors would then work like this:
manager =3D get_current_fig_manager()
manager.canvas.report_error(message)
which could be wrapped into a function.
2) The main functionality of the Verbose class seems to be,
that the user can select how many messages he wants to see.
Error messages (at least fatal ones) should be presented to
the user in any case, so for me reporting errors does not
look like an application of the Verbose class.
What do you think?
Jochen
--=20
http://seehuhn.de/
|
|
From: John H. <jdh...@ac...> - 2004-11-20 23:27:28
|
>>>>> "Steve" == Steve Chaplin <ste...@ya...> writes:
Steve> I was thinking of something like:
Steve> class VerboseGTK(Verbose): def report_error(self, s):
Steve> dialog = gtk.MessageDialog( parent = None, type =
Steve> gtk.MESSAGE_ERROR, buttons = gtk.BUTTONS_OK, message_format
Steve> = msg) dialog.run() dialog.destroy()
Steve> So that the matlab interface can call
Steve> verbose.report_error() and for image backends it writes to
Steve> stdout and for GUI backends it pops up a message dialog.
Steve> You can hook a GTK dialog into unhandled Python exceptions
Steve> with: import sys
Steve> def exception_handler(type, value, tb): """Handle uncaught
Steve> exceptions""" error_msg_gtk(value)
Steve> sys.excepthook = exception_handler
Steve> (I've added this to backend_gtk.py in cvs if you want to
Steve> try it out)
As for report_error, subclassing Verbose, or using figure manager for
this as Jochen has suggested, are both workable solutions, but what
does it ultimately buy us? I am inclined to the logically cleaner
solution of doing all error handling with exceptions, using a hook
like you've provided for GUIs.
The only lingering advantage I see for a report_error call w/o an
exception being raised is it presents a cleaner error message, which
is nice for newbies. I have a python 3000-esque design philosophy for
matplotlib -- I want it to be accessible to newbies. And a simple
message "function blah expects 1 or 2 arguments" is much more likely
to be read and parsed by a newbie, who in my experience will disregard
a traceback simply because it often appears unreadable, until you are
trained to read from the bottom up, which is counter intuitive to
some.
Are there other advantages to report_error that I'm missing, and if
not, does the readability issue justify circumventing the default
exception handling mechanism? My inclination is that it doesn't.
Steve> But you still need to decide how to handle the exceptions -
Steve> with some you need to terminate the program, with others
Steve> its safe to continue. It may mean you end up writing a
Steve> complicated generic exception handler that tries to handle
Steve> every possible exception. In that case handling exceptions
Steve> individually, the usual way might be better, possibly using
Steve> the sys.excepthook to handle the remaining uncaught
Steve> exceptions, or using it when you want to terminate the
Steve> program and want to popup a message saying "Fatal error..."
I grepped for all the current uses of report error (included below) --
on quick inspection none of these appear fatal for a GUI. I think
simply informing the user of the error may suffice. Can you provide
an example of where we may need to exit (and would it suffice for the
raiser to simply raise a SystemExit for this case?)
JDH
'Error: %s'%msg
'Unable to allocate color %1.3f, %1.3f, %1.3f; using nearest neighbor' % rgb
'Error: %s'% msg
'Error: %s' % msg
'Could not load font file "%s"'%fname
'Error: %s'% msg
'Could not load filename for text "%s"'%fname
msg
'Could not find bitmap file "%s"; dying'%bmpFilename
'backend_gtk could not import mathtext (build with ft2font')
'Error: %s' % exc
'The GTK backend cannot draw text at a %i degree angle, try GtkAgg instead' % angle
'mathtext not supported: %s' % exc
"Could not renderer vertical text", s
"cairo.numpy module required for draw_image(")
'Mathtext not implemented yet'
'Unrecognized cap style. Found %s' % cs
'Unrecognized join style. Found %s' % js
"%s: %s" % (exc.filename, exc.strerror)
'Format "%s" is not supported.\nSupported formats: %s.' %
./__init__.py: def report_error(self, s:
'Could not find .matplotlibrc; using defaults'
message
'Illegal line #%d\n\t%s\n\tin file "%s"' % (cnt, line, fname)
'%s is deprecated in .matplotlibrc - use %s instead.' % (key, alt)
'Bad key "%s" on line %d in %s' % (key, cnt, fname)
'Bad val "%s" on line #%d\n\t"%s"\n\tin file "%s"\n\t%s' % (val, cnt, line, fname, msg)
'unrecognized backend %s.\n' % arg +\
./backend_bases.py: verbose.report_error('Error: %s'% msg
"ColormapJet deprecated, please use cm.jet instead"
"Grayscale deprecated, please use cm.gray instead"
'urlopen( failure\n' + url + '\n' + exc.strerror[1])
"Could not open font file %s"%fpath
"Could not open font file %s"%fpath
msg % name
'Could not match %s, %s, %s. Returning %s' % (name, style, variant, self.defaultFont)
'Unrecognized location %s. Falling back on upper right; valid locations are\n%s\t' %(loc, '\n\t'.join(self.codes.keys()))
'Unrecognized line style %s' %( linestyle, type(linestyle))
'Unrecognized marker style %s'%( marker, type(marker))
'unrecognized symbol "%s"' % sym
'unrecognized symbol "%s, %d"' % (sym, num)
'Coherence is calculated by averaging over NFFT length segments. Your signal is too short for your choice of NFFT'
'Dimension error'
'Second argument not permitted for matrices'
__doc__
'Unrecognized location %s. Falling back on bottom; valid locations are\n%s\t' %(loc, '\n\t'.join(self.codes.keys()))
'AutoLocator illegal dataInterval range %s; returning NullLocator'%d
'Unrecognized location %s. Falling back on upper right; valid locations are\n%s\t' %(loc, '\n\t'.join(self.codes.keys()))
Steve> Steve
Steve> -------------------------------------------------------
Steve> This SF.Net email is sponsored by: InterSystems CACHE FREE
Steve> OODBMS DOWNLOAD - A multidimensional database that combines
Steve> robust object and relational technologies, making it a
Steve> perfect match for Java, C++,COM, XML, ODBC and
Steve> JDBC. www.intersystems.com/match8
Steve> _______________________________________________
Steve> Matplotlib-devel mailing list
Steve> Mat...@li...
Steve> https://lists.sourceforge.net/lists/listinfo/matplotlib-devel
|
|
From: Steve C. <ste...@ya...> - 2004-11-22 09:08:10
|
On Sat, 2004-11-20 at 17:26 -0600, John Hunter wrote: > As for report_error, subclassing Verbose, or using figure manager for > this as Jochen has suggested, are both workable solutions, but what > does it ultimately buy us? I am inclined to the logically cleaner > solution of doing all error handling with exceptions, using a hook > like you've provided for GUIs. By subclassing Verbose you group all the functions that display messages to the user together into one class. I may have misunderstood some of the discussion - I agree with doing all error handling with exceptions, but when you catch the exception in a GUI I'm assuming you still want to popup a message to inform the user. Are you describing using exceptions and tracebacks without any error messages for GUI backends? I don't think you can assume a GUI backend user will see a traceback since the terminal window may be obscured, iconified or even closed. > I grepped for all the current uses of report error (included below) -- > on quick inspection none of these appear fatal for a GUI. I think > simply informing the user of the error may suffice. Can you provide > an example of where we may need to exit (and would it suffice for the > raiser to simply raise a SystemExit for this case?) It does look like they are all non-fatal. I guess the fatal ones are the ones that matplotlib does not anticipate or catch, thinks like faulty installations, missing libraries etc. At the moment these would cause matplotlib to terminate but if we add a default exception handler we will start catching these also. We could have a policy for matplotlib to catch all exceptions and always attempt to continue, and if it becomes unusable its up to the user to close the window. Also we could recognise some situations (if there are any) where we need to terminate, so we raise SystemExit and set the default exception handler to terminate on SystemExit and continue on all other cases. Steve |
|
From: John H. <jdh...@ac...> - 2004-11-22 21:58:15
|
>>>>> "Steve" == Steve Chaplin <ste...@ya...> writes:
Steve> By subclassing Verbose you group all the functions that
Steve> display messages to the user together into one class. I
Steve> may have misunderstood some of the discussion - I agree
Steve> with doing all error handling with exceptions, but when you
Steve> catch the exception in a GUI I'm assuming you still want to
Steve> popup a message to inform the user. Are you describing
Steve> using exceptions and tracebacks without any error messages
Steve> for GUI backends? I don't think you can assume a GUI
Steve> backend user will see a traceback since the terminal window
Steve> may be obscured, iconified or even closed.
For the GUI error handling, I was assuming we would use the
except_hook, and do away with error reporting in the verbose class.
For the regular verbose reporting, I'm not averse to plugging it into
a GUI dialog, but I'm not sure this is useful. As I wrote before,
this will mainly be used in debug situations when we can probably
assume a user has access to a shell output. They can always capture
it to a file using the regular rc mechanism of setting verbose.fileo.
If we did want to do it in a GUI, we would have to be fairly careful
about the implementation, so that the GUI cached sequential methods
and only displayed them if some time interval (eg 100ms) had lapsed
with no new messages. This would be used to prevent the curse of 20
popups. This could presumably be done in GTK with an idle handler and
a changed timestamp.
Alternatively, it could be done in the verbose base class itself,
implemented using threads, but I'm a little wary of the extra
complexity here.
That said, I think that having the figure manager define a
popup_dialog method would be generally useful.
Steve> It does look like they are all non-fatal. I guess the fatal
Steve> ones are the ones that matplotlib does not anticipate or
Steve> catch, thinks like faulty installations, missing libraries
Steve> etc. At the moment these would cause matplotlib to
Steve> terminate but if we add a default exception handler we will
Steve> start catching these also. We could have a policy for
Steve> matplotlib to catch all exceptions and always attempt to
Steve> continue, and if it becomes unusable its up to the user to
Steve> close the window. Also we could recognise some situations
Steve> (if there are any) where we need to terminate, so we raise
Steve> SystemExit and set the default exception handler to
Steve> terminate on SystemExit and continue on all other cases.
Sounds right to me.... So in summary, if all agree and we've covered
all the bases, Verbose.report_error and error_msg disappear and are
replaced by regular exceptions. matplotlib code can raise a
SystemExit for the relatively rare fatal errors. GUIs define
sys.excepthook = exception_handler following the lead of Steve's
implementation in backend_gtk (in CVS).
verbose.report is left untouched for now but may be hooked into a GUI
reporting functionality if we can resolve the issues of whether this
is desirable and how to handle caching of many independent sequential
messages.
JDH
|
|
From: Fernando P. <Fer...@co...> - 2004-11-22 22:43:21
|
John Hunter schrieb: >>>>>>"Steve" == Steve Chaplin <ste...@ya...> writes: > > > Steve> By subclassing Verbose you group all the functions that > Steve> display messages to the user together into one class. I > Steve> may have misunderstood some of the discussion - I agree > Steve> with doing all error handling with exceptions, but when you > Steve> catch the exception in a GUI I'm assuming you still want to > Steve> popup a message to inform the user. Are you describing > Steve> using exceptions and tracebacks without any error messages > Steve> for GUI backends? I don't think you can assume a GUI > Steve> backend user will see a traceback since the terminal window > Steve> may be obscured, iconified or even closed. > > For the GUI error handling, I was assuming we would use the > except_hook, and do away with error reporting in the verbose class. I'd like to strongly plead that you stay away from sys.excepthook. That is the 'last resort' tool to manipulate exceptions, and it's typically used by frameworks which need to completely control the python process. For example, ipython puts its internal crash handler in sys.excepthook, so that if all else fails, the crash handler generates a very detailed crash report. But all 'normal' exception handling is done by the internal user loop, with manual control. I haven't had time to look in detail, but I even think that ipython pretty aggressively reclaims sys.excepthook if user code messes with it. I wouldn't be surprised if other frameworks (like envisage) also used sys.excepthook themselves. Matplotlib is 'only' a plotting library :), and it should IMHO play nicely with other code running along with it. If it gets into a foodfight over who owns sys.excepthook, or if it crashes because sys.excepthook is not what it thinks it is, I expect serious interoperability problems to pop up down the road. I realize that excepthook is a tempting tool to use, but I hope you guys reconsider this. I really think it would cause many more headaches down the road than those it initially appears to solve. Regards, f |
|
From: John H. <jdh...@ac...> - 2004-11-22 22:50:48
|
>>>>> "Fernando" == Fernando Perez <Fer...@co...> writes:
Fernando> I realize that excepthook is a tempting tool to use, but
Fernando> I hope you guys reconsider this. I really think it
Fernando> would cause many more headaches down the road than those
Fernando> it initially appears to solve.
OK, good to know. That was news to me. Now why is it that ipython
and envisage get to mess around with it and we don't :-) ?
So what is the canonical way to funnel exceptions into GUI dialog
boxes? Isn't this what sys.except_hook is for?
Actually, it would be fine if matplotlib overrode sys.except_hook and
ipython later came along and overrode that. Basically, ipython would
be saying, "I know I've got a shell to display errors in, so we don't
need to GUI method". I don't think matplotlib would have a problem
with that. Ditto for envisage. Basically, we would be providing a
default method to get the message to the GUI which could be overriden
by other applications that want to (ipython, envisage, what-have-you).
So I don't really see a danger here, but please educate me!
JDH
|
|
From: Fernando P. <Fer...@co...> - 2004-11-22 22:58:54
|
John Hunter schrieb: >>>>>>"Fernando" == Fernando Perez <Fer...@co...> writes: > > > > Fernando> I realize that excepthook is a tempting tool to use, but > Fernando> I hope you guys reconsider this. I really think it > Fernando> would cause many more headaches down the road than those > Fernando> it initially appears to solve. > > OK, good to know. That was news to me. Now why is it that ipython > and envisage get to mess around with it and we don't :-) ? > > So what is the canonical way to funnel exceptions into GUI dialog > boxes? Isn't this what sys.except_hook is for? Mmh, I don't really know for sure. But here's a quick test: planck[mayavi]> egrep -r sys.except * planck[mayavi]> This is on the mayavi sources. Mayavi DOES show all VTK exceptions into a GUI (such as you get when you try to open the volume rendering module with floating point data, for example). I imagine it just traps them locally, but I'm not sure. It is possible that mayavi has an easier job because it's done in Tk, with no threading issues to worry about. Threads make this problem MUCH worse, since python has no sensible way for an exception raised in one thread to be handled by another: all go to the sys.excepthook bucket. There have been long, complicated threads recently on c.l.py on the cousin topics of signals and exceptions in threads, and it doesn't look pretty. > Actually, it would be fine if matplotlib overrode sys.except_hook and > ipython later came along and overrode that. Basically, ipython would > be saying, "I know I've got a shell to display errors in, so we don't > need to GUI method". I don't think matplotlib would have a problem > with that. Ditto for envisage. Basically, we would be providing a > default method to get the message to the GUI which could be overriden > by other applications that want to (ipython, envisage, what-have-you). I guess this is a good heads-up for me. I know ipython does some of this, I'll just add a bit more such control. That way, if during the running of user code they need sys.ehook for something, they'll get it. But ipython will keep it for when it needs it. I'm pretty sure the embeddable ipython has such control in it, I may just need to put it in the general case as well. Ultimately I'm not 100% sure what a good solution for matplotlib is, I just wanted to make you aware of these issues, so that at least they are on your radar. Best, f |
|
From: John H. <jdh...@ac...> - 2004-11-22 23:05:34
|
>>>>> "Fernando" == Fernando Perez <Fer...@co...> writes:
>> Actually, it would be fine if matplotlib overrode
>> sys.except_hook and ipython later came along and overrode that.
>> Basically, ipython would be saying, "I know I've got a shell to
>> display errors in, so we don't need to GUI method". I don't
>> think matplotlib would have a problem with that. Ditto for
>> envisage. Basically, we would be providing a default method to
>> get the message to the GUI which could be overriden by other
>> applications that want to (ipython, envisage, what-have-you).
Fernando> I guess this is a good heads-up for me. I know ipython
Fernando> does some of this, I'll just add a bit more such
Fernando> control. That way, if during the running of user code
Fernando> they need sys.ehook for something, they'll get it. But
Fernando> ipython will keep it for when it needs it. I'm pretty
Fernando> sure the embeddable ipython has such control in it, I
Fernando> may just need to put it in the general case as well.
Fernando> Ultimately I'm not 100% sure what a good solution for
Fernando> matplotlib is, I just wanted to make you aware of these
Fernando> issues, so that at least they are on your radar.
We do have some more options. For one, we could use the excepthook
only in the matlab interface -- in this case matlab is being used more
as an application rather than a library. Folks using matplotlib as a
library, eg embedding in a GUI, would be advised to do their own
trapping.
The only exceptional case I see is basically the ipython (and friends)
case. Ie, someone wants to write a shell or otherwise that embeds
matplotlib.matlab. In this case it would be fine to override
matplotlib.matlab's excepthook, as discussed.
To play really nicely, matplotlib.matlab would like to be able to
override excepthook only if it hadn't been otherwise overridden. I
don't see any elegant way to do this. Any ideas?
JDH
|
|
From: Fernando P. <Fer...@co...> - 2004-11-22 23:08:45
|
John Hunter schrieb: >>>>>>"Fernando" == Fernando Perez <Fer...@co...> writes: > > > >> Actually, it would be fine if matplotlib overrode > >> sys.except_hook and ipython later came along and overrode that. > >> Basically, ipython would be saying, "I know I've got a shell to > >> display errors in, so we don't need to GUI method". I don't > >> think matplotlib would have a problem with that. Ditto for > >> envisage. Basically, we would be providing a default method to > >> get the message to the GUI which could be overriden by other > >> applications that want to (ipython, envisage, what-have-you). > > Fernando> I guess this is a good heads-up for me. I know ipython > Fernando> does some of this, I'll just add a bit more such > Fernando> control. That way, if during the running of user code > Fernando> they need sys.ehook for something, they'll get it. But > Fernando> ipython will keep it for when it needs it. I'm pretty > Fernando> sure the embeddable ipython has such control in it, I > Fernando> may just need to put it in the general case as well. > > Fernando> Ultimately I'm not 100% sure what a good solution for > Fernando> matplotlib is, I just wanted to make you aware of these > Fernando> issues, so that at least they are on your radar. > > > We do have some more options. For one, we could use the excepthook > only in the matlab interface -- in this case matlab is being used more > as an application rather than a library. Folks using matplotlib as a > library, eg embedding in a GUI, would be advised to do their own > trapping. > > The only exceptional case I see is basically the ipython (and friends) > case. Ie, someone wants to write a shell or otherwise that embeds > matplotlib.matlab. In this case it would be fine to override > matplotlib.matlab's excepthook, as discussed. > > To play really nicely, matplotlib.matlab would like to be able to > override excepthook only if it hadn't been otherwise overridden. I > don't see any elegant way to do this. Any ideas? Yup: planck[mayavi]> ip In [1]: import sys In [2]: sys.__excepthook__ is sys.excepthook Out[2]: False planck[mayavi]> python >>> import sys >>> sys.__excepthook__ is sys.excepthook True Cheers, f |
|
From: John H. <jdh...@ac...> - 2004-11-22 23:12:50
|
>>>>> "Fernando" == Fernando Perez <Fer...@co...> writes:
>> overridden. I don't see any elegant way to do this. Any
>> ideas?
Fernando> In [2]: sys.__excepthook__ is sys.excepthook Out[2]:
Excellent. So if the backends define a function excepthook_backend
and matplotlib.matlab does
if sys.__excepthook__ is sys.excepthook:
sys.excepthook = excepthook_backend
would we be in reasonably good standing with the python gods and
others?
JDH
|
|
From: Fernando P. <Fer...@co...> - 2004-11-22 23:14:56
|
John Hunter schrieb: >>>>>>"Fernando" == Fernando Perez <Fer...@co...> writes: > > > >> overridden. I don't see any elegant way to do this. Any > >> ideas? > > Fernando> In [2]: sys.__excepthook__ is sys.excepthook Out[2]: > > Excellent. So if the backends define a function excepthook_backend > and matplotlib.matlab does > > if sys.__excepthook__ is sys.excepthook: > sys.excepthook = excepthook_backend > > would we be in reasonably good standing with the python gods and > others? You'd have to ask the gods directly, but it looks pretty good to me, a mere mortal :) Best, f |
|
From: Steve C. <ste...@ya...> - 2004-11-23 01:29:59
|
I've updated backend_gtk.py in cvs to use a default exception handler, and noticed a few things in the process: - sys.excepthook does not catch SystemExit, which is what we wanted anyway. - for some errors I needed to display a matplotlib message rather than the default exception message, or to raise an exception where error_msg () was used with no exception. I added an 'MPLError' exception, its probably best to move it into a central file if other people need to use it also. - changing from error_msg() to raise exception means the rest of the method will not execute, which I hadn't thought about. This is no good for 'get_filename_from_user()' where I want to loop until a file (or Cancel) is selected. So I think GTK still has a need to use of a popup message dialog occasionally. And for print_figure() it means the section of code to restore figure settings will not get executed after an error. Steve |
|
From: Jochen V. <vo...@se...> - 2004-11-23 10:24:57
|
Hello John, On Mon, Nov 22, 2004 at 04:50:10PM -0600, John Hunter wrote: > So what is the canonical way to funnel exceptions into GUI dialog > boxes? Isn't this what sys.except_hook is for? I think we should somehow explicitely catch all exceptions we are expecting (maybe near the main loop) and the exception handler could feed them to the backend. Reasons: 1) This looks like the most Pythonic solution to me: exceptions are ment to by caught by "try: ... except: ..." statements 2) This might open a way to differentiate between expected exections (opening a file with a user-supplied file name might fail, if the user mistyped the name -> User should be told and asked for another file name) and unexpected ones which indicate programming errors. 3) Easier to understand flow of control. If you follow a chain of callers through the source code you can see which exections are caught at which place, whereas the except_hook mechanism is more "magical". What do you think? Jochen --=20 http://seehuhn.de/ |
|
From: John H. <jdh...@ac...> - 2004-11-23 21:39:35
|
>>>>> "Jochen" == Jochen Voss <vo...@se...> writes:
Jochen> I think we should somehow explicitely catch all exceptions
Jochen> we are expecting (maybe near the main loop) and the
Jochen> exception handler could feed them to the backend.
Jochen> Reasons:
Jochen> 1) This looks like the most Pythonic solution to me:
Jochen> exceptions are ment to by caught by "try: ... except: ..."
Jochen> statements
Jochen> 2) This might open a way to differentiate between expected
Jochen> exections (opening a file with a user-supplied file name
Jochen> might fail, if the user mistyped the name -> User should
Jochen> be told and asked for another file name) and unexpected
Jochen> ones which indicate programming errors.
Jochen> 3) Easier to understand flow of control. If you follow a
Jochen> chain of callers through the source code you can see which
Jochen> exections are caught at which place, whereas the
Jochen> except_hook mechanism is more "magical".
Jochen> What do you think?
I think in the backend and other matplotlib code where we know how to
handle the exception, this makes sense. Eg, if backend_gtk gets a
IOError on some file, it can catch it and request a new filename. We
should catch and handle exceptions where we can.
Buy I'm thinking about the matlab interface. There are two problems
in matlab.py: 1) There are practically no exceptions that we can
handle at that level so the best we can do it forward it on the user
and 2) there is no single point to plug in a master exception handler.
Consider the canonical matlab interface function
def plot(*args, **kwargs):
ret = gca().plot(*args, **kwargs)
draw_if_interactive()
gca().plot makes a series of deeply nested calls, as does
draw_if_interactive. With these two calls, a substantial number of
the total methods defined in matplotlib are actually called (format
string parsers, line instantiation, transformations, clipping, gcs,
renderers, font finding, etc, etc....) There are a lot of potential
exceptions to be thrown, and tracking down all of them would be a big
task. And then once we catch them what could we do with them?
The problem is compounded by the fact that the solution would have to
repeated for *every* matlab interface function separately, which looks
like it would lead to a big tangle of unmanageable code across the
whole module.
Perhaps I'm missing the obvious thing and not understanding your
suggestion. But form my end at the level of the matlab interface, the
best we can do is get a helpful, descriptive error message to the
user. Did you have another approach in mind?
JDH
|
|
From: Jochen V. <vo...@se...> - 2004-11-23 21:52:03
|
Hello, On Tue, Nov 23, 2004 at 03:38:57PM -0600, John Hunter wrote: > Perhaps I'm missing the obvious thing and not understanding your > suggestion. But form my end at the level of the matlab interface, the > best we can do is get a helpful, descriptive error message to the > user. Did you have another approach in mind? No, I am not very clear about anything here and was just throwing ideas around. Actually I am happy with the plans as far as the PostScript backend is concerned (reporting everyting as exceptions etc.) So let's just try what you suggested. All the best, Jochen --=20 http://seehuhn.de/ |
|
From: John H. <jdh...@ac...> - 2004-11-23 21:29:03
|
>>>>> "Steve" == Steve Chaplin <ste...@ya...> writes:
Steve> I've updated backend_gtk.py in cvs to use a default
Steve> exception handler, and noticed a few things in the process:
Steve> - sys.excepthook does not catch SystemExit, which is what
Steve> we wanted anyway.
I think the full exception should be printed. It makes debugging very
hard, otherwise. In fact, while recently debugging some code, I
commented out the exception handler for this reason. matplotlib.cbook
provides a method exception_to_str to convert a traceback to a string.
Steve> - for some errors I needed to display a matplotlib message
Steve> rather than the default exception message, or to raise an
Steve> exception where error_msg () was used with no exception. I
Steve> added an 'MPLError' exception, its probably best to move it
Steve> into a central file if other people need to use it also.
Perhaps in matplotlib.__init__ ?
Steve> - changing from error_msg() to raise exception means the
Steve> rest of the method will not execute, which I hadn't thought
Steve> about. This is no good for 'get_filename_from_user()' where
Steve> I want to loop until a file (or Cancel) is selected. So I
Steve> think GTK still has a need to use of a popup message dialog
Steve> occasionally. And for print_figure() it means the section
Steve> of code to restore figure settings will not get executed
Steve> after an error.
I agree that a general facility to popup a message is useful How
abouts we follow Jochen's suggestion and make is a FigureManager
method?
As for your get_filename / restore figure problems, can't these be
addressed by try/catching the exceptions locally, and restoring the
figure state before forwarding the exception onwards?
JDH
|
|
From: Steve C. <ste...@ya...> - 2004-11-24 02:13:42
|
On Tue, 2004-11-23 at 15:28 -0600, John Hunter wrote: > Steve> - for some errors I needed to display a matplotlib message > Steve> rather than the default exception message, or to raise an > Steve> exception where error_msg () was used with no exception. I > Steve> added an 'MPLError' exception, its probably best to move it > Steve> into a central file if other people need to use it also. > > Perhaps in matplotlib.__init__ ? OK, I've added "MPLError' a subclass of Exception to matplotlib.__init__ Steve |
|
From: John H. <jdh...@ac...> - 2004-11-23 22:39:29
|
>>>>> "Steve" == Steve Chaplin <ste...@ya...> writes:
Steve> I've updated backend_gtk.py in cvs to use a default
Steve> exception handler, and noticed a few things in the process:
Steve> - sys.excepthook does not catch SystemExit, which is what
Steve> we wanted anyway.
For some reason with matplotlib CVS using backend GTK on linux, I no
longer recover the linux shell when I close the figure by clicking on
the 'x' in the figure window
> python somefile.py
I have to use CTRL-C.
|
|
From: Steve C. <ste...@ya...> - 2004-11-24 12:50:50
|
On Tue, 2004-11-23 at 16:38 -0600, John Hunter wrote:
> >>>>> "Steve" == Steve Chaplin <ste...@ya...> writes:
>
> Steve> I've updated backend_gtk.py in cvs to use a default
> Steve> exception handler, and noticed a few things in the process:
> Steve> - sys.excepthook does not catch SystemExit, which is what
> Steve> we wanted anyway.
>
> For some reason with matplotlib CVS using backend GTK on linux, I no
> longer recover the linux shell when I close the figure by clicking on
> the 'x' in the figure window
>
> > python somefile.py
>
> I have to use CTRL-C.
>
This situation happens when the main window is destroyed but
gtk.main_quit() is not called - the gtk.main loop is still running.
I'm not seeing this problem at the moment.
In FigureManagerGTK the 'destroy' signal is connected to Gcf.destroy
(num), which should eventually call
class FigureManagerGTK
def destroy(self, *args):
self.window.destroy()
if Gcf.get_num_fig_managers()==0 and not
matplotlib.is_interactive():
gtk.main_quit()
I set DEBUG = True and ran a few examples and it showed
FigureManagerGTK.destroy() was being called as expected.
Perhaps Gcf.get_num_fig_managers() is sometimes not 0 when it should be
0 and gtk.main_quit() is not being called.
Steve
|