Skip to content

Commit

Permalink
Merge pull request #3752 from astrofrog/rc_context_exceptions
Browse files Browse the repository at this point in the history
BUG : Make sure that initial state gets reset if anything goes wrong in ``rc_context``
  • Loading branch information
tacaswell committed Nov 3, 2014
2 parents e0020d3 + 39c3194 commit 112f199
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/matplotlib/__init__.py
Expand Up @@ -1249,10 +1249,16 @@ 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.clear()
rcParams.update(self._rcparams)
raise

def __enter__(self):
return self
Expand Down
23 changes: 23 additions & 0 deletions lib/matplotlib/tests/test_rcparams.py
Expand Up @@ -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
Expand Down Expand Up @@ -262,3 +263,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.

if sys.version_info[:2] >= (2, 7):
from collections import OrderedDict
else:
raise SkipTest("Test can only be run in Python >= 2.7 as it requires OrderedDict")

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

0 comments on commit 112f199

Please sign in to comment.