8000 Merge pull request #8962 from anntzer/rc_context · choldgraf/matplotlib@c4d7ce4 · GitHub
[go: up one dir, main page]

Skip to content

Commit c4d7ce4

Browse files
authored
Merge pull request matplotlib#8962 from anntzer/rc_context
Don't revalidate original rcParams when exiting rc_context.
2 parents 80a5e99 + 74680f6 commit c4d7ce4

File tree

2 files changed

+31
-42
lines changed

2 files changed

+31
-42
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+
.. autofunction:: 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: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,22 @@
103103
unicode_literals)
104104

105105
import six
106-
import sys
107-
import distutils.version
108-
from itertools import chain
109106

110107
from collections import MutableMapping
108+
import contextlib
109+
import distutils.version
110+
import distutils.sysconfig
111+
import functools
111112
import io
112113
import inspect
114+
import itertools
113115
import locale
114116
import os
115117
import re
118+
import sys
116119
import tempfile
117120
import warnings
118-
import contextlib
119-
import distutils.sysconfig
120-
import functools
121+
121122
# cbook must import matplotlib only within function
122123
# definitions, so it is safe to import from it here.
123124
from . import cbook
@@ -798,9 +799,8 @@ def gen_candidates():
798799
# The following may use a value of None to suppress the warning.
799800
_deprecated_set = {'axes.hold'} # do NOT include in _all_deprecated
800801

801-
_all_deprecated = set(chain(_deprecated_ignore_map,
802-
_deprecated_map,
803-
_obsolete_set))
802+
_all_deprecated = set(itertools.chain(
803+
_deprecated_ignore_map, _deprecated_map, _obsolete_set))
804804

805805

806806
class RcParams(MutableMapping, dict):
@@ -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)
0