From: Steve C. <ste...@ya...> - 2004-11-18 15:36:50
|
On Wed, 2004-11-17 at 10:54 -0700, Fernando Perez wrote: > Jochen Voss schrieb: > > Hello Steve, > > > > On Wed, Nov 17, 2004 at 11:00:32PM +0800, Steve Chaplin wrote: > > > >>I don't think failure to save a file should be a fatal error. > >>Perhaps PS/SVG print_figure() could raise an exception for the GUI > >>backend to catch so it can popup an error message and continue. > > > > Even now you can probably (not tried) catch the SystemExit > > exception and prevent the program from aborting. > > Do you think raising SystemExit like > > > > raise SystemExit("error while writing file: permission denied") > > > > would be good enough. The GUI frontend could catch SystemExit, > > check whether the associated value is a string, and then display > > this string in an error message box. > > > > What do you think? > > Bad design. Doing exception analysis based on string matching for the message > is very brittle. A single change in capitalization of the message can break > things down the road. > > It's _far_ better to either: > > 1. have the ps/svg backends do whatever cleanup they want, and then reraise > the original exception unchanged > > 2. or simply make a matplotlib.SaveError exception which can be explicitly > caught based on class matching, which is the preferred python way of doing this. > > Best, > > f At the moment the PS backend does try: fh = file(outfile, 'w') except IOError: error_msg_ps('Could not open %s for writing' % outfile) which translates into try: fh = file(outfile, 'w') except IOError: verbose.report_error('Error: Could not open %s for writing' % outfile) sys.exit() The backend does not do any cleanup after the exception, so I think it could be changed to just fh = file(outfile, 'w') allowing the exception terminate the program with the error message appearing at the end of the traceback. Then the GUI backends can then do from backend_svg import FigureCanvasSVG as FigureCanvas try: fc = self.switch_backends(FigureCanvas) fc.print_figure(filename, dpi, facecolor, edgecolor, orientation) except IOError, exc: error_msg("%s: %s" % (exc.filename, exc.strerror), parent=self) I tried this calling the SVG backend directly and by switching to the SVG backend from the GTK backend and it works OK. Steve |