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

Skip to content

Commit f7134f2

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 d0ae324 commit f7134f2

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
@@ -1223,7 +1223,8 @@ def rc_file(fname):
12231223
rcParams.update(rc_params_from_file(fname))
12241224

12251225

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

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

12801271

12811272
_use_error_msg = """

0 commit comments

Comments
 (0)