File tree 2 files changed +30
-5
lines changed 2 files changed +30
-5
lines changed Original file line number Diff line number Diff line change @@ -77,11 +77,11 @@ def use(style):
77
77
try :
78
78
rc = rc_params_from_file (style , use_default_template = False )
79
79
mpl .rcParams .update (rc )
80
- except :
80
+ except IOError :
81
81
msg = ("'%s' not found in the style library and input is "
82
82
"not a valid URL or path. See `style.available` for "
83
83
"list of available styles." )
84
- raise ValueError (msg % style )
84
+ raise IOError (msg % style )
85
85
86
86
87
87
@contextlib .contextmanager
@@ -111,9 +111,16 @@ def context(style, after_reset=False):
111
111
initial_settings = mpl .rcParams .copy ()
112
112
if after_reset :
113
113
mpl .rcdefaults ()
114
- use (style )
115
- yield
116
- mpl .rcParams .update (initial_settings )
114
+ try :
115
+ use (style )
116
+ except :
117
+ # Restore original settings before raising any errors during the update.
118
+ mpl .rcParams .update (initial_settings )
119
+ raise
120
+ else :
121
+ yield
122
+ finally :
123
+ mpl .rcParams .update (initial_settings )
117
124
118
125
119
126
def load_base_library ():
Original file line number Diff line number Diff line change 2
2
unicode_literals )
3
3
4
4
import os
5
+ import sys
5
6
import shutil
6
7
import tempfile
7
8
from contextlib import contextmanager
12
13
13
14
import six
14
15
16
+ from nose .tools import assert_raises
15
17
16
18
PARAM = 'image.cmap'
17
19
VALUE = 'pink'
@@ -115,6 +117,22 @@ def test_context_with_union_of_dict_and_namedstyle():
115
117
assert mpl .rcParams [other_param ] == (not other_value )
116
118
117
119
120
+ def test_context_with_badparam ():
121
+ if sys .version_info [:2 ] >= (2 , 7 ):
122
+ from collections import OrderedDict
123
+ else :
124
+ msg = "Test can only be run in Python >= 2.7 as it requires OrderedDict"
125
+ raise SkipTest (msg )
126
+
127
+ original_value = 'gray'
128
+ other_value = 'blue'
129
+ d = OrderedDict ([(PARAM , original_value ), ('badparam' , None )])
130
+ with style .context ({PARAM : other_value }):
131
+ assert mpl .rcParams [PARAM ] == other_value
132
+ x = style .context ([d ])
133
+ assert_raises (KeyError , x .__enter__ )
134
+ assert mpl .rcParams [PARAM ] == other_value
135
+
118
136
if __name__ == '__main__' :
119
137
from numpy import testing
120
138
testing .run_module_suite ()
You can’t perform that action at this time.
0 commit comments