8000 Don't revalidate original rcParams when exiting rc_context. by anntzer · Pull Request #8962 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Don't revalidate original rcParams when exiting rc_context. #8962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 31, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
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.)
  • Loading branch information
anntzer committed Jul 30, 2017
commit 74680f619e924ca84fe31acbce2276c05fe29bf7
22 changes: 10 additions & 12 deletions doc/api/matplotlib_configuration_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,24 @@ The top level :mod:`matplotlib` module

An instance of :class:`RcParams` for handling default matplotlib values.

.. autofunction:: rc

.. autofunction::rcdefaults

.. autofunction::rc_file
.. autofunction:: rc_context

.. autofunction::rc_context

.. autofunction:: matplotlib_fname
.. autofunction:: rc

.. autofunction::rc_file_defaults
.. autofunction:: rc_file

.. autofunction::interactive
.. autofunction:: rcdefaults

.. autofunction::is_interactive
.. autofunction:: rc_file_defaults

.. autoclass:: RcParams

.. autofunction:: rc_params

.. autofunction:: rc_params_from_file

.. autoclass:: rc_context
.. autofunction:: matplotlib_fname

.. autofunction:: interactive

.. autofunction:: is_interactive
33 changes: 12 additions & 21 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,8 @@ def rc_file(fname):
rcParams.update(rc_params_from_file(fname))


class rc_context(object):
@contextlib.contextmanager
def rc_context(rc=None, fname=None):
"""
Return a context manager for managing rc settings.

Expand Down Expand Up @@ -1256,26 +1257,16 @@ class rc_context(object):

"""

def __init__(self, rc=None, fname=None):
self.rcdict = rc
self.fname = fname
self._rcparams = rcParams.copy()
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

def __exit__(self, type, value, tb):
rcParams.update(self._rcparams)
orig = rcParams.copy()
try:
if fname:
rc_file(fname)
if rc:
rcParams.update(rc)
yield
finally:
# No need to revalidate the original values.
dict.update(rcParams, orig)


_use_error_msg = """
Expand Down
0