Fix #4855: Blacklist rcParams that aren't style #5440
Merged
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c90bcfb
Fix #4855: Blacklist rcParams that aren't style
mdboom 22f99bb
Fix, per comments in PR
mdboom fdae5de
Fix
mdboom e68d239
Remove blacklisted styles from classic style
mdboom 0946c1c
Add what's new
mdboom d9d1671
Don't apply blacklisted params from default style
mdboom | @@ -0,0 +1,11 @@ | ||
| +Style parameter blacklist | ||
| +------------------------- | ||
| + | ||
| +In order to prevent unexpected consequences from using a style, style | ||
| +files are no longer able to set parameters that affect things | ||
| +unrelated to style. These parameters include:: | ||
| + | ||
| + 'interactive', 'backend', 'backend.qt4', 'webagg.port', | ||
| + 'webagg.port_retries', 'webagg.open_in_browser', 'backend_fallback', | ||
| + 'toolbar', 'timezone', 'datapath', 'figure.max_open_warning', | ||
| + 'savefig.directory', 'tk.window_focus', 'hardcopy.docstring' |
| @@ -18,10 +18,11 @@ | ||
| import os | ||
| import re | ||
| import contextlib | ||
| +import warnings | ||
| import matplotlib as mpl | ||
| from matplotlib import cbook | ||
| -from matplotlib import rc_params_from_file | ||
| +from matplotlib import rc_params_from_file, rcParamsDefault | ||
| __all__ = ['use', 'context', 'available', 'library', 'reload_library'] | ||
| @@ -34,11 +35,35 @@ | ||
| STYLE_FILE_PATTERN = re.compile('([\S]+).%s$' % STYLE_EXTENSION) | ||
| +# A list of rcParams that should not be applied from styles | ||
| +STYLE_BLACKLIST = set([ | ||
| + 'interactive', 'backend', 'backend.qt4', 'webagg.port', | ||
| + 'webagg.port_retries', 'webagg.open_in_browser', 'backend_fallback', | ||
| + 'toolbar', 'timezone', 'datapath', 'figure.max_open_warning', | ||
| + 'savefig.directory', 'tk.window_focus', 'hardcopy.docstring']) | ||
mdboom
Owner
|
||
| + | ||
| + | ||
| +def _remove_blacklisted_style_params(d): | ||
| + o = {} | ||
| + for key, val in d.items(): | ||
| + if key in STYLE_BLACKLIST: | ||
| + warnings.warn( | ||
| + "Style includes a parameter, '{0}', that is not related to " | ||
| + "style. Ignoring".format(key)) | ||
| + else: | ||
| + o[key] = val | ||
| + return o | ||
| + | ||
| + | ||
| def is_style_file(filename): | ||
| """Return True if the filename looks like a style file.""" | ||
| return STYLE_FILE_PATTERN.match(filename) is not None | ||
| +def _apply_style(d): | ||
| + mpl.rcParams.update(_remove_blacklisted_style_params(d)) | ||
| + | ||
| + | ||
| def use(style): | ||
| """Use matplotlib style settings from a style specification. | ||
| @@ -71,18 +96,15 @@ def use(style): | ||
| for style in styles: | ||
| if not cbook.is_string_like(style): | ||
| - mpl.rcParams.update(style) | ||
| - continue | ||
| + _apply_style(style) | ||
| elif style == 'default': | ||
| - mpl.rcdefaults() | ||
| - continue | ||
| - | ||
| - if style in library: | ||
| - mpl.rcParams.update(library[style]) | ||
| + _apply_style(rcParamsDefault) | ||
| + elif style in library: | ||
| + _apply_style(library[style]) | ||
| else: | ||
| try: | ||
| rc = rc_params_from_file(style, use_default_template=False) | ||
| - mpl.rcParams.update(rc) | ||
| + _apply_style(rc) | ||
| except IOError: | ||
| msg = ("'%s' not found in the style library and input is " | ||
| "not a valid URL or path. See `style.available` for " | ||
what about the default keymappings?