From 098159c013ba8abf4bb7c67caa3e00d7bf49ed13 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sat, 29 Jul 2017 23:35:56 -0700 Subject: [PATCH 1/2] Minor reordering of imports. --- lib/matplotlib/__init__.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index 187501efcefb..f62defbfd86b 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -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 @@ -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): From 74680f619e924ca84fe31acbce2276c05fe29bf7 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 30 Jul 2017 00:18:40 -0700 Subject: [PATCH 2/2] 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.) --- doc/api/matplotlib_configuration_api.rst | 22 +++++++--------- lib/matplotlib/__init__.py | 33 +++++++++--------------- 2 files changed, 22 insertions(+), 33 deletions(-) diff --git a/doc/api/matplotlib_configuration_api.rst b/doc/api/matplotlib_configuration_api.rst index 9f0ad3678ffb..26ce3ac57edf 100644 --- a/doc/api/matplotlib_configuration_api.rst +++ b/doc/api/matplotlib_configuration_api.rst @@ -12,21 +12,15 @@ 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 @@ -34,4 +28,8 @@ The top level :mod:`matplotlib` module .. autofunction:: rc_params_from_file -.. autoclass:: rc_context +.. autofunction:: matplotlib_fname + +.. autofunction:: interactive + +.. autofunction:: is_interactive diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index f62defbfd86b..daf8e315f7cf 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -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. @@ -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 = """ pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy