|
103 | 103 | unicode_literals) |
104 | 104 |
|
105 | 105 | import six |
106 | | -import sys |
107 | | -import distutils.version |
108 | | -from itertools import chain |
109 | 106 |
|
110 | 107 | from collections import MutableMapping |
| 108 | +import contextlib |
| 109 | +import distutils.version |
| 110 | +import distutils.sysconfig |
| 111 | +import functools |
111 | 112 | import io |
112 | 113 | import inspect |
| 114 | +import itertools |
113 | 115 | import locale |
114 | 116 | import os |
115 | 117 | import re |
| 118 | +import sys |
116 | 119 | import tempfile |
117 | 120 | import warnings |
118 | | -import contextlib |
119 | | -import distutils.sysconfig |
120 | | -import functools |
| 121 | + |
121 | 122 | # cbook must import matplotlib only within function |
122 | 123 | # definitions, so it is safe to import from it here. |
123 | 124 | from . import cbook |
@@ -798,9 +799,8 @@ def gen_candidates(): |
798 | 799 | # The following may use a value of None to suppress the warning. |
799 | 800 | _deprecated_set = {'axes.hold'} # do NOT include in _all_deprecated |
800 | 801 |
|
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)) |
804 | 804 |
|
805 | 805 |
|
806 | 806 | class RcParams(MutableMapping, dict): |
@@ -1223,7 +1223,8 @@ def rc_file(fname): |
1223 | 1223 | rcParams.update(rc_params_from_file(fname)) |
1224 | 1224 |
|
1225 | 1225 |
|
1226 | | -class rc_context(object): |
| 1226 | +@contextlib.contextmanager |
| 1227 | +def rc_context(rc=None, fname=None): |
1227 | 1228 | """ |
1228 | 1229 | Return a context manager for managing rc settings. |
1229 | 1230 |
|
@@ -1256,26 +1257,16 @@ class rc_context(object): |
1256 | 1257 |
|
1257 | 1258 | """ |
1258 | 1259 |
|
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) |
1279 | 1270 |
|
1280 | 1271 |
|
1281 | 1272 | _use_error_msg = """ |
|
0 commit comments