From bc00dc9f8ee8a5a68cd64c015834926a9511bf1d Mon Sep 17 00:00:00 2001 From: Thomas Robitaille Date: Sun, 2 Nov 2014 15:16:10 +0100 Subject: [PATCH 1/3] Fixed a bug that caused rc parameters to not be reset to initial state if an exception happened while setting new rc parameters inside rc_context. --- lib/matplotlib/__init__.py | 13 +++++++++---- lib/matplotlib/tests/test_rcparams.py | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index e07a6fd13e87..a7034c47f952 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -1249,10 +1249,15 @@ def __init__(self, rc=None, fname=None): self.rcdict = rc self.fname = fname self._rcparams = rcParams.copy() - if self.fname: - rc_file(self.fname) - if self.rcdict: - rcParams.update(self.rcdict) + try: + if self.fname: + rc_file(self.fname) + if self.rcdict: + rcParams.update(self.rcdict) + except: + # if anything goes wrong, revert rc parameters and re-raise + rcParams.update(self._rcparams) + raise def __enter__(self): return self diff --git a/lib/matplotlib/tests/test_rcparams.py b/lib/matplotlib/tests/test_rcparams.py index 74341c6ba26c..3d0e6f3295e0 100644 --- a/lib/matplotlib/tests/test_rcparams.py +++ b/lib/matplotlib/tests/test_rcparams.py @@ -262,3 +262,25 @@ def test_keymaps(): key_list = [k for k in mpl.rcParams if 'keymap' in k] for k in key_list: assert(isinstance(mpl.rcParams[k], list)) + + +def test_rcparams_reset_after_fail(): + + # There was previously a bug that meant that if rc_context failed and + # raised an exception due to issues in the supplied rc parameters, the + # global rc parameters were left in a modified state. + + try: + from collections import OrderedDict + except: + return # can't run this test on Python 2.6 + + with mpl.rc_context(rc={'text.usetex': False}): + + assert mpl.rcParams['text.usetex'] is False + + with assert_raises(KeyError): + with mpl.rc_context(rc=OrderedDict([('text.usetex', True),('test.blah', True)])): + pass + + assert mpl.rcParams['text.usetex'] is False From 8d3cd0c4b6e52e484c5b77ddccd8bc9424c54996 Mon Sep 17 00:00:00 2001 From: Thomas Robitaille Date: Sun, 2 Nov 2014 17:03:48 +0100 Subject: [PATCH 2/3] Minor fixes --- lib/matplotlib/__init__.py | 1 + lib/matplotlib/tests/test_rcparams.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index a7034c47f952..6ec9edac84e0 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -1256,6 +1256,7 @@ def __init__(self, rc=None, fname=None): rcParams.update(self.rcdict) except: # if anything goes wrong, revert rc parameters and re-raise + rcParams.clear() rcParams.update(self._rcparams) raise diff --git a/lib/matplotlib/tests/test_rcparams.py b/lib/matplotlib/tests/test_rcparams.py index 3d0e6f3295e0..4ba6b1b5c227 100644 --- a/lib/matplotlib/tests/test_rcparams.py +++ b/lib/matplotlib/tests/test_rcparams.py @@ -272,7 +272,7 @@ def test_rcparams_reset_after_fail(): try: from collections import OrderedDict - except: + except ImportError: return # can't run this test on Python 2.6 with mpl.rc_context(rc={'text.usetex': False}): From 39c3194847b6683c06066e666a896ee0edb51896 Mon Sep 17 00:00:00 2001 From: Thomas Robitaille Date: Mon, 3 Nov 2014 10:25:49 +0100 Subject: [PATCH 3/3] raise SkipTest if not using Python >= 2.7 --- lib/matplotlib/tests/test_rcparams.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/tests/test_rcparams.py b/lib/matplotlib/tests/test_rcparams.py index 4ba6b1b5c227..ec92da9ee246 100644 --- a/lib/matplotlib/tests/test_rcparams.py +++ b/lib/matplotlib/tests/test_rcparams.py @@ -12,6 +12,7 @@ from matplotlib.tests import assert_str_equal from matplotlib.testing.decorators import cleanup, knownfailureif from nose.tools import assert_true, assert_raises, assert_equal +from nose.plugins.skip import SkipTest import nose from itertools import chain import numpy as np @@ -270,10 +271,10 @@ def test_rcparams_reset_after_fail(): # raised an exception due to issues in the supplied rc parameters, the # global rc parameters were left in a modified state. - try: + if sys.version_info[:2] >= (2, 7): from collections import OrderedDict - except ImportError: - return # can't run this test on Python 2.6 + else: + raise SkipTest("Test can only be run in Python >= 2.7 as it requires OrderedDict") with mpl.rc_context(rc={'text.usetex': False}):