Skip to content

Commit 15996be

Browse files
author
Steve Chaplin
committed
SC 23/11/2004
svn path=/trunk/matplotlib/; revision=706
1 parent 6dc7cfa commit 15996be

File tree

2 files changed

+76
-60
lines changed

2 files changed

+76
-60
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
New entries should be added at the top
22

3+
2004-11-23 backend_gtk.py: added a default exception handler
4+
35
2004-11-18 backend_gtk.py: change so that the backend knows about all image
46
formats and does not need to use IMAGE_FORMAT in other backends - SC
57

lib/matplotlib/backends/backend_gtk.py

Lines changed: 74 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import sys
55
def fn_name(): return sys._getframe(1).f_code.co_name
66

7-
87
import matplotlib
98
from matplotlib import verbose
109
from matplotlib.numerix import asarray, fromstring, UInt8, zeros, \
@@ -25,7 +24,6 @@ def fn_name(): return sys._getframe(1).f_code.co_name
2524
useMathText = False
2625
else: useMathText = True
2726

28-
2927
try:
3028
import pygtk
3129
if not matplotlib.FROZEN:
@@ -547,24 +545,6 @@ def set_linewidth(self, w):
547545
self.gdkGC.line_width = max(1, int(round(pixels)))
548546

549547

550-
def error_msg_gtk(msg, parent=None):
551-
if parent: # find the toplevel gtk.Window
552-
parent = parent.get_toplevel()
553-
if not parent.flags() & gtk.TOPLEVEL:
554-
parent = None
555-
556-
if not is_string_like(msg):
557-
msg = ','.join(map(str,msg))
558-
559-
dialog = gtk.MessageDialog(
560-
parent = parent,
561-
type = gtk.MESSAGE_ERROR,
562-
buttons = gtk.BUTTONS_OK,
563-
message_format = msg)
564-
dialog.run()
565-
dialog.destroy()
566-
567-
568548
def draw_if_interactive():
569549
"""
570550
Is called after every matplotlib.matlab drawing command
@@ -701,8 +681,9 @@ def key_release_event(self, widget, event):
701681
def mpl_connect(self, s, func):
702682

703683
if s not in self.events:
704-
error_msg('Can only connect events of type "%s"\nDo not know how to handle "%s"' %(', '.join(self.events), s),
705-
parent=self)
684+
raise MPLError('Can only connect events of type "%s"\nDo not know how to handle "%s"' %(', '.join(self.events), s))
685+
#error_msg('Can only connect events of type "%s"\nDo not know how to handle "%s"' %(', '.join(self.events), s),
686+
# parent=self)
706687

707688
def wrapper(widget, event):
708689
thisEvent = MplEvent(s, self)
@@ -851,20 +832,22 @@ def print_figure(self, filename, dpi=150, facecolor='w', edgecolor='w',
851832

852833
# pixbuf.save() recognises 'jpeg' not 'jpg'
853834
if ext == 'jpg': ext = 'jpeg'
854-
try: pixbuf.save(filename, ext)
855-
except gobject.GError, exc:
856-
error_msg('Save figure failure:\n%s' % (exc,), parent=self)
835+
pixbuf.save(filename, ext)
836+
#try: pixbuf.save(filename, ext)
837+
#except gobject.GError, exc:
838+
# error_msg('Save figure failure:\n%s' % (exc,), parent=self)
857839

858840
elif ext in ('eps', 'ps', 'svg',):
859841
if ext == 'svg':
860842
from backend_svg import FigureCanvasSVG as FigureCanvas
861843
else:
862844
from backend_ps import FigureCanvasPS as FigureCanvas
863845

864-
#try:
865-
# Testing - use uncaught exception handler
866846
fc = self.switch_backends(FigureCanvas)
867847
fc.print_figure(filename, dpi, facecolor, edgecolor, orientation)
848+
#try:
849+
# fc = self.switch_backends(FigureCanvas)
850+
# fc.print_figure(filename, dpi, facecolor, edgecolor, orientation)
868851
#except IOError, exc:
869852
# error_msg("Save figure failure:\n%s: %s" %
870853
# (exc.filename, exc.strerror), parent=self)
@@ -875,17 +858,21 @@ def print_figure(self, filename, dpi=150, facecolor='w', edgecolor='w',
875858
try:
876859
from backend_agg import FigureCanvasAgg as FigureCanvas
877860
except:
878-
error_msg('Save figure failure:\n'
879-
'Agg must be loaded to save as bmp, raw and rgb',
880-
parent=self)
861+
raise MPLError('Save figure failure:\n'
862+
'Agg must be loaded to save as bmp, raw and rgb')
863+
#error_msg('Save figure failure:\n'
864+
# 'Agg must be loaded to save as bmp, raw and rgb',
865+
# parent=self)
881866
else:
882867
fc = self.switch_backends(FigureCanvas)
883868
fc.print_figure(filename, dpi, facecolor, edgecolor, orientation)
884869

885870
else:
886-
error_msg('Format "%s" is not supported.\nSupported formats are %s.' %
887-
(ext, ', '.join(IMAGE_FORMAT)),
888-
parent=self)
871+
raise MPLError('Format "%s" is not supported.\nSupported formats are %s.' %
872+
(ext, ', '.join(IMAGE_FORMAT)))
873+
#error_msg('Format "%s" is not supported.\nSupported formats are %s.' %
874+
# (ext, ', '.join(IMAGE_FORMAT)),
875+
# parent=self)
889876

890877
# restore figure settings
891878
self.figure.dpi.set(origDPI)
@@ -1455,37 +1442,30 @@ def __init__ (self,
14551442
if path: self.path = path
14561443
else: self.path = os.getcwd() + os.sep
14571444

1458-
#self.IMAGE_FORMAT = matplotlib.backends.backend_mod.IMAGE_FORMAT
1459-
#self.IMAGE_FORMAT_DEFAULT = matplotlib.backends.backend_mod.IMAGE_FORMAT_DEFAULT
1460-
#self.IMAGE_FORMAT.sort()
1461-
1462-
# later: change self.IMAGE_FORMAT to IMAGE_FORMAT
1463-
self.IMAGE_FORMAT = IMAGE_FORMAT
1464-
self.IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_DEFAULT
1465-
self.IMAGE_FORMAT.sort()
1445+
IMAGE_FORMAT.sort()
14661446

14671447
# create an extra widget to list supported image formats
14681448
self.set_current_folder (self.path)
1469-
self.set_current_name ('image.' + self.IMAGE_FORMAT_DEFAULT)
1449+
self.set_current_name ('image.' + IMAGE_FORMAT_DEFAULT)
14701450

14711451
hbox = gtk.HBox (spacing=10)
14721452
hbox.pack_start (gtk.Label ("Image Format:"), expand=False)
14731453

14741454
self.cbox = gtk.combo_box_new_text()
14751455
hbox.pack_start (self.cbox)
14761456

1477-
for item in self.IMAGE_FORMAT:
1457+
for item in IMAGE_FORMAT:
14781458
self.cbox.append_text (item)
1479-
self.cbox.set_active (self.IMAGE_FORMAT.index (self.IMAGE_FORMAT_DEFAULT))
1459+
self.cbox.set_active (IMAGE_FORMAT.index (IMAGE_FORMAT_DEFAULT))
14801460

14811461
def cb_cbox_changed (cbox, data=None):
14821462
"""File extension changed"""
14831463
head, filename = os.path.split(self.get_filename())
14841464
root, ext = os.path.splitext(filename)
14851465
ext = ext[1:]
1486-
new_ext = self.IMAGE_FORMAT[cbox.get_active()]
1466+
new_ext = IMAGE_FORMAT[cbox.get_active()]
14871467

1488-
if ext in self.IMAGE_FORMAT:
1468+
if ext in IMAGE_FORMAT:
14891469
filename = filename.replace(ext, new_ext)
14901470
elif ext == '':
14911471
filename = filename.rstrip('.') + '.' + new_ext
@@ -1504,17 +1484,22 @@ def get_filename_from_user (self):
15041484
filename = None
15051485
break
15061486
filename = self.get_filename()
1507-
menu_ext = self.IMAGE_FORMAT[self.cbox.get_active()]
1487+
menu_ext = IMAGE_FORMAT[self.cbox.get_active()]
15081488
root, ext = os.path.splitext(filename)
15091489
ext = ext[1:]
15101490
if ext == '':
15111491
ext = menu_ext
15121492
filename += '.' + ext
15131493

1514-
if ext in self.IMAGE_FORMAT:
1494+
if ext in IMAGE_FORMAT:
15151495
self.path = filename
15161496
break
15171497
else:
1498+
# XXX exception causes method to terminate w/o closing window
1499+
# but want to repeat loop until selection is made
1500+
# so a popup IS more appropriate?
1501+
#raise MPLError('Format "%s" is not supported.\nSupported formats are %s.' %
1502+
# (ext, ', '.join(IMAGE_FORMAT)))
15181503
error_msg('Image format "%s" is not supported' % ext,
15191504
parent=self)
15201505
self.set_current_name(os.path.split(root)[1] + '.' + menu_ext)
@@ -1523,9 +1508,6 @@ def get_filename_from_user (self):
15231508
return filename
15241509

15251510

1526-
FigureManager = FigureManagerGTK
1527-
error_msg = error_msg_gtk
1528-
15291511
# set icon used when windows are minimized, it requires
15301512
# gtk.pygtk_version >= (2,2,0) with a GDK pixbuf loader for SVG installed
15311513
try:
@@ -1535,22 +1517,54 @@ def get_filename_from_user (self):
15351517
verbose.report('Could not load matplotlib icon: %s' % sys.exc_info()[1])
15361518

15371519

1538-
# TESTING
1539-
import sys
1520+
def error_msg_gtk(msg, parent=None):
1521+
if parent: # find the toplevel gtk.Window
1522+
parent = parent.get_toplevel()
1523+
if not parent.flags() & gtk.TOPLEVEL:
1524+
parent = None
1525+
1526+
if not is_string_like(msg):
1527+
msg = ','.join(map(str,msg))
1528+
1529+
dialog = gtk.MessageDialog(
1530+
parent = parent,
1531+
type = gtk.MESSAGE_ERROR,
1532+
buttons = gtk.BUTTONS_OK,
1533+
message_format = msg)
1534+
dialog.run()
1535+
dialog.destroy()
1536+
1537+
1538+
class MPLError (Exception):
1539+
"""Exception for Matplotlib errors
1540+
Attributes:
1541+
message -- explanation of the error
1542+
"""
1543+
def __init__ (self, message):
1544+
self.message = message
1545+
1546+
def __str__ (self):
1547+
return self.message
1548+
15401549

15411550
def exception_handler(type, value, tb):
1542-
"""Handle uncaught exceptions"""
1543-
# type is of format - exceptions.IOError
1551+
"""Handle uncaught exceptions
1552+
It does not catch SystemExit
1553+
"""
1554+
msg = ''
15441555
# get the filename attribute if available (for IOError)
15451556
if hasattr(value, 'filename') and value.filename != None:
15461557
msg = value.filename + ': '
1547-
else:
1548-
msg = ''
15491558
if hasattr(value, 'strerror') and value.strerror != None:
15501559
msg += value.strerror
15511560
else:
1552-
msg = value
1561+
msg += str(value)
15531562

1554-
if len(msg): error_msg_gtk(msg)
1563+
if len(msg) :error_msg_gtk(msg)
15551564

1556-
sys.excepthook = exception_handler
1565+
# override excepthook only if it has not already been overridden
1566+
if sys.__excepthook__ is sys.excepthook:
1567+
sys.excepthook = exception_handler
1568+
1569+
FigureManager = FigureManagerGTK
1570+
error_msg = error_msg_gtk

0 commit comments

Comments
 (0)