From c90bcfb00731e9808e7d5a12ec2574cec948bf24 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Sun, 8 Nov 2015 15:55:21 -0500 Subject: [PATCH 1/6] Fix #4855: Blacklist rcParams that aren't style --- lib/matplotlib/style/core.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/style/core.py b/lib/matplotlib/style/core.py index 7407e57df38e..6f5d1af4b97e 100644 --- a/lib/matplotlib/style/core.py +++ b/lib/matplotlib/style/core.py @@ -34,11 +34,27 @@ 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']) + + +def _blacklist_style_params(d): + return dict((k, v) for (k, v) in d.items() if k not in STYLE_BLACKLIST) + + 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.use(blacklist_style_params(d)) + + def use(style): """Use matplotlib style settings from a style specification. @@ -71,18 +87,18 @@ def use(style): for style in styles: if not cbook.is_string_like(style): - mpl.rcParams.update(style) + _apply_style(style) continue elif style == 'default': mpl.rcdefaults() continue if style in library: - mpl.rcParams.update(library[style]) + _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 " From 22f99bb171c9c2bc3e4f2204921ba6e284b598cb Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 9 Nov 2015 08:55:51 -0500 Subject: [PATCH 2/6] Fix, per comments in PR --- lib/matplotlib/style/core.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/style/core.py b/lib/matplotlib/style/core.py index 6f5d1af4b97e..29865ced013b 100644 --- a/lib/matplotlib/style/core.py +++ b/lib/matplotlib/style/core.py @@ -18,6 +18,7 @@ import os import re import contextlib +import warnings import matplotlib as mpl from matplotlib import cbook @@ -42,8 +43,16 @@ 'savefig.directory', 'tk.window_focus', 'hardcopy.docstring']) -def _blacklist_style_params(d): - return dict((k, v) for (k, v) in d.items() if k not in STYLE_BLACKLIST) +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): @@ -52,7 +61,7 @@ def is_style_file(filename): def _apply_style(d): - mpl.rcParams.use(blacklist_style_params(d)) + mpl.rcParams.use(_remove_blacklisted_style_params(d)) def use(style): From fdae5deb43fca5ee8db5725e058ea538ae7452ac Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 9 Nov 2015 11:06:54 -0500 Subject: [PATCH 3/6] Fix --- lib/matplotlib/style/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/style/core.py b/lib/matplotlib/style/core.py index 29865ced013b..2960a20acb30 100644 --- a/lib/matplotlib/style/core.py +++ b/lib/matplotlib/style/core.py @@ -61,7 +61,7 @@ def is_style_file(filename): def _apply_style(d): - mpl.rcParams.use(_remove_blacklisted_style_params(d)) + mpl.rcParams.update(_remove_blacklisted_style_params(d)) def use(style): From e68d239f144a1ef78424f99fc322fb017a6c6cff Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Thu, 12 Nov 2015 08:39:17 -0500 Subject: [PATCH 4/6] Remove blacklisted styles from classic style --- lib/matplotlib/mpl-data/stylelib/classic.mplstyle | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/lib/matplotlib/mpl-data/stylelib/classic.mplstyle b/lib/matplotlib/mpl-data/stylelib/classic.mplstyle index e30f95341f19..13f8886fdec0 100644 --- a/lib/matplotlib/mpl-data/stylelib/classic.mplstyle +++ b/lib/matplotlib/mpl-data/stylelib/classic.mplstyle @@ -278,9 +278,6 @@ figure.facecolor : 0.75 # figure facecolor; 0.75 is scalar gray figure.edgecolor : w # figure edgecolor figure.autolayout : False # When True, automatically adjust subplot # parameters to make the plot fit the figure -figure.max_open_warning : 20 # The maximum number of figures to open through - # the pyplot interface before emitting a warning. - # If less than one this feature is disabled. figure.frameon : True # The figure subplot parameters. All dimensions are a fraction of the @@ -385,8 +382,6 @@ savefig.bbox : standard # 'tight' or 'standard'. # use ffmpeg_file instead savefig.pad_inches : 0.1 # Padding to be used when bbox is set to 'tight' savefig.jpeg_quality: 95 # when a jpeg is saved, the default quality parameter. -savefig.directory : ~ # default directory in savefig dialog box, - # leave empty to always use current working directory savefig.transparent : False # setting that controls whether figures are saved with a # transparent background by default savefig.frameon : True @@ -394,9 +389,6 @@ savefig.orientation : portrait nbagg.transparent: True -# tk backend params -tk.window_focus : False # Maintain shell focus for TkAgg - # ps backend params ps.papersize : letter # auto, letter, legal, ledger, A0-A10, B0-B10 ps.useafm : False # use of afm fonts, results in small files @@ -418,7 +410,7 @@ pdf.use14corefonts : False pgf.debug : False pgf.texsystem : xelatex pgf.rcfonts : True -pgf.preamble : +pgf.preamble : # svg backend params svg.image_inline : True # write raster image data directly into the svg file @@ -467,8 +459,6 @@ keymap.yscale : l # toggle scaling of y-axes ('log'/'linear') keymap.xscale : k, L # toggle scaling of x-axes ('log'/'linear') keymap.all_axes : a # enable all axes -toolbar: toolbar2 - ###ANIMATION settings animation.writer : ffmpeg # MovieWriter 'backend' to use animation.codec : mpeg4 # Codec to use for writing movie From 0946c1cbc0877200495054e27a75d81484068e7d Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Thu, 12 Nov 2015 08:42:18 -0500 Subject: [PATCH 5/6] Add what's new --- doc/users/whats_new/style_blacklist.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 doc/users/whats_new/style_blacklist.rst diff --git a/doc/users/whats_new/style_blacklist.rst b/doc/users/whats_new/style_blacklist.rst new file mode 100644 index 000000000000..7a7a6146252b --- /dev/null +++ b/doc/users/whats_new/style_blacklist.rst @@ -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' From d9d16719c9f7571b46779c8248a5cf7e4a6e1ced Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Wed, 30 Dec 2015 16:28:03 -0500 Subject: [PATCH 6/6] Don't apply blacklisted params from default style --- lib/matplotlib/style/core.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/style/core.py b/lib/matplotlib/style/core.py index 2960a20acb30..90544639bf46 100644 --- a/lib/matplotlib/style/core.py +++ b/lib/matplotlib/style/core.py @@ -22,7 +22,7 @@ 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'] @@ -97,12 +97,9 @@ def use(style): for style in styles: if not cbook.is_string_like(style): _apply_style(style) - continue elif style == 'default': - mpl.rcdefaults() - continue - - if style in library: + _apply_style(rcParamsDefault) + elif style in library: _apply_style(library[style]) else: try: