From: W. Trevor King Date: Tue, 6 Dec 2011 03:36:28 +0000 (-0500) Subject: Also catch RuntimeError when importing matplotlib in slow_bend.py. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=c24a79ac91b51a4bd03cf927143b932c061baca9;p=blog.git Also catch RuntimeError when importing matplotlib in slow_bend.py. When it can't initialize the backend, the matplotlib crashes with a runtime error. For example, I've used `~/.matplotlib/matplotlibrc` to select the GTKAgg backend as my default. When I log in without X, running the previous version of slow_bend.py would produce: $ slow_bend.py --help /usr/lib/python2.7/site-packages/gtk-2.0/gtk/__init__.py:57: GtkWarning: could not open display warnings.warn(str(e), _gtk.Warning) /usr/lib/python2.7/site-packages/matplotlib/backends/backend_gtk.py:48: GtkWarning: IA__gdk_cursor_new_for_display: assertion `GDK_IS_DISPLAY (display)' failed cursors.MOVE : gdk.Cursor(gdk.FLEUR), Traceback (most recent call last): File "/home/wking/bin/slow_bend.py", line 39, in from matplotlib import pyplot as _pyplot ... RuntimeError: could not create GdkCursor object --- diff --git a/posts/slow_bend/slow_bend.py b/posts/slow_bend/slow_bend.py index 63cd2e0..b62c3e1 100755 --- a/posts/slow_bend/slow_bend.py +++ b/posts/slow_bend/slow_bend.py @@ -37,7 +37,7 @@ import time as _time try: import numpy as _numpy from matplotlib import pyplot as _pyplot -except ImportError, _matplotlib_import_error: +except (ImportError, RuntimeError), _matplotlib_import_error: _pyplot = None import pycomedi.device as _pycomedi_device