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 all commits
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
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
51 changes: 21 additions & 30 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,22 @@
unicode_literals)

import six
import sys
import distutils.version
from itertools import chain

from collections import MutableMapping
import contextlib
import distutils.version
import distutils.sysconfig
import functools
import io
import inspect
import itertools
import locale
import os
import re
import sys
import tempfile
import warnings
import contextlib
import distutils.sysconfig
import functools

# cbook must import matplotlib only within function
# definitions, so it is safe to import from it here.
from . import cbook
Expand Down Expand Up @@ -798,9 +799,8 @@ def gen_candidates():
# The following may use a value of None to suppress the warning.
_deprecated_set = {'axes.hold'} # do NOT include in _all_deprecated

_all_deprecated = set(chain(_deprecated_ignore_map,
_deprecated_map,
_obsolete_set))
_all_deprecated = set(itertools.chain(
_deprecated_ignore_map, _deprecated_map, _obsolete_set))


class RcParams(MutableMapping, dict):
Expand Down 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