Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cf42412

Browse files
committed
Don't revalidate rcParams when exiting rc_context.
- Rewrite rc_context using contextmanager, which removes the need for saving state. - When `__exit__`ing rc_context, skip rcParam validation, as the items have already been validated originally. The rationale for not revalidating (beyond a minor gain in speed) is that it may make sense to smuggle seemingly "invalid" values into the rcParams (by calling `dict.__setitem__`), and we do not want them to cause an error when exiting a rc_context (of course, they may or may not cause an error at the backend level, but that is the responsibility of whoever set this "invalid" value). For example, the rcparam `lines.antialiased` is declared as a boolean, but the mpl_cairo backend actually also recognizes any of cairo's internal antialiasing enum values (NONE, FAST, GOOD, BEST, etc.). (The rst docs needed to be slightly updated because .. autoclass:: rc_context won't work anymore, so I also took advantage of that to reorder all the rc-related functions slightly more logically.)
1 parent e7c9530 commit cf42412

File tree

2 files changed

+22
-33
lines changed

2 files changed

+22
-33
lines changed

doc/api/matplotlib_configuration_api.rst

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,24 @@ The top level :mod:`matplotlib` module
1212
1313
An instance of :class:`RcParams` for handling default matplotlib values.
1414

15-
.. autofunction:: rc
16-
17-
.. autofunction::rcdefaults
18-
19-
.. autofunction::rc_file
15+
.. autodata:: rc_context
2016

21-
.. autofunction::rc_context
22-
23-
.. autofunction:: matplotlib_fname
17+
.. autofunction:: rc
2418

25-
.. autofunction::rc_file_defaults
19+
.. autofunction:: rc_file
2620

27-
.. autofunction::interactive
21+
.. autofunction:: rcdefaults
2822

29-
.. autofunction::is_interactive
23+
.. autofunction:: rc_file_defaults
3024

3125
.. autoclass:: RcParams
3226

3327
.. autofunction:: rc_params
3428

3529
.. autofunction:: rc_params_from_file
3630

37-
.. autoclass:: rc_context
31+
.. autofunction:: matplotlib_fname
32+
33+
.. autofunction:: interactive
34+
35+
.. autofunction:: is_interactive

lib/matplotlib/__init__.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,8 @@ def rc_file(fname):
12221222
rcParams.update(rc_params_from_file(fname))
12231223

12241224

1225-
class rc_context(object):
1225+
@contextlib.contextmanager
1226+
def rc_context(rc=None, fname=None):
12261227
"""
12271228
Return a context manager for managing rc settings.
12281229
@@ -1255,26 +1256,16 @@ class rc_context(object):
12551256
12561257
"""
12571258

1258-
def __init__(self, rc=None, fname=None):
1259-
self.rcdict = rc
1260-
self.fname = fname
1261-
self._rcparams = rcParams.copy()
1262-
try:
1263-
if self.fname:
1264-
rc_file(self.fname)
1265-
if self.rcdict:
1266-
rcParams.update(self.rcdict)
1267-
except:
1268-
# if anything goes wrong, revert rc parameters and re-raise
1269-
rcParams.clear()
1270-
rcParams.update(self._rcparams)
1271-
raise
1272-
1273-
def __enter__(self):
1274-
return self
1275-
1276-
def __exit__(self, type, value, tb):
1277-
rcParams.update(self._rcparams)
1259+
orig = rcParams.copy()
1260+
try:
1261+
if fname:
1262+
rc_file(fname)
1263+
if rc:
1264+
rcParams.update(rc)
1265+
yield
1266+
finally:
1267+
# No need to revalidate the original values.
1268+
dict.update(rcParams, orig)
12781269

12791270

12801271
_use_error_msg = """

0 commit comments

Comments
 (0)