Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0c1ae10

Browse files
committed
Merge pull request #5796 from jenshnielsen/backportblacklist
Backport blacklist
2 parents 1d89de2 + b68d050 commit 0c1ae10

5 files changed

Lines changed: 47 additions & 23 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Style parameter blacklist
2+
-------------------------
3+
4+
In order to prevent unexpected consequences from using a style, style
5+
files are no longer able to set parameters that affect things
6+
unrelated to style. These parameters include::
7+
8+
'interactive', 'backend', 'backend.qt4', 'webagg.port',
9+
'webagg.port_retries', 'webagg.open_in_browser', 'backend_fallback',
10+
'toolbar', 'timezone', 'datapath', 'figure.max_open_warning',
11+
'savefig.directory', 'tk.window_focus', 'hardcopy.docstring'

lib/matplotlib/mpl-data/stylelib/classic.mplstyle

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,6 @@ figure.facecolor : 0.75 # figure facecolor; 0.75 is scalar gray
293293
figure.edgecolor : w # figure edgecolor
294294
figure.autolayout : False # When True, automatically adjust subplot
295295
# parameters to make the plot fit the figure
296-
figure.max_open_warning : 20 # The maximum number of figures to open through
297-
# the pyplot interface before emitting a warning.
298-
# If less than one this feature is disabled.
299296
figure.frameon : True
300297

301298
# The figure subplot parameters. All dimensions are a fraction of the
@@ -400,18 +397,13 @@ savefig.bbox : standard # 'tight' or 'standard'.
400397
# use ffmpeg_file instead
401398
savefig.pad_inches : 0.1 # Padding to be used when bbox is set to 'tight'
402399
savefig.jpeg_quality: 95 # when a jpeg is saved, the default quality parameter.
403-
savefig.directory : ~ # default directory in savefig dialog box,
404-
# leave empty to always use current working directory
405400
savefig.transparent : False # setting that controls whether figures are saved with a
406401
# transparent background by default
407402
savefig.frameon : True
408403
savefig.orientation : portrait
409404

410405
nbagg.transparent: True
411406

412-
# tk backend params
413-
tk.window_focus : False # Maintain shell focus for TkAgg
414-
415407
# ps backend params
416408
ps.papersize : letter # auto, letter, legal, ledger, A0-A10, B0-B10
417409
ps.useafm : False # use of afm fonts, results in small files
@@ -482,8 +474,6 @@ keymap.yscale : l # toggle scaling of y-axes ('log'/'linear')
482474
keymap.xscale : k, L # toggle scaling of x-axes ('log'/'linear')
483475
keymap.all_axes : a # enable all axes
484476

485-
toolbar: toolbar2
486-
487477
###ANIMATION settings
488478
animation.writer : ffmpeg # MovieWriter 'backend' to use
489479
animation.codec : mpeg4 # Codec to use for writing movie

lib/matplotlib/style/core.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
import os
1919
import re
2020
import contextlib
21+
import warnings
2122

2223
import matplotlib as mpl
2324
from matplotlib import cbook
24-
from matplotlib import rc_params_from_file
25+
from matplotlib import rc_params_from_file, rcParamsDefault
2526

2627

2728
__all__ = ['use', 'context', 'available', 'library', 'reload_library']
@@ -34,11 +35,36 @@
3435
STYLE_FILE_PATTERN = re.compile('([\S]+).%s$' % STYLE_EXTENSION)
3536

3637

38+
# A list of rcParams that should not be applied from styles
39+
STYLE_BLACKLIST = set([
40+
'interactive', 'backend', 'backend.qt4', 'webagg.port',
41+
'webagg.port_retries', 'webagg.open_in_browser', 'backend_fallback',
42+
'toolbar', 'timezone', 'datapath', 'figure.max_open_warning',
43+
'savefig.directory', 'tk.window_focus', 'hardcopy.docstring'])
44+
45+
46+
def _remove_blacklisted_style_params(d, warn=True):
47+
o = {}
48+
for key, val in d.items():
49+
if key in STYLE_BLACKLIST:
50+
if warn:
51+
warnings.warn(
52+
"Style includes a parameter, '{0}', that is not related "
53+
"to style. Ignoring".format(key))
54+
else:
55+
o[key] = val
56+
return o
57+
58+
3759
def is_style_file(filename):
3860
"""Return True if the filename looks like a style file."""
3961
return STYLE_FILE_PATTERN.match(filename) is not None
4062

4163

64+
def _apply_style(d, warn=True):
65+
mpl.rcParams.update(_remove_blacklisted_style_params(d, warn=warn))
66+
67+
4268
def use(style):
4369
"""Use matplotlib style settings from a style specification.
4470
@@ -71,18 +97,15 @@ def use(style):
7197

7298
for style in styles:
7399
if not cbook.is_string_like(style):
74-
mpl.rcParams.update(style)
75-
continue
100+
_apply_style(style)
76101
elif style == 'default':
77-
mpl.rcdefaults()
78-
continue
79-
80-
if style in library:
81-
mpl.rcParams.update(library[style])
102+
_apply_style(rcParamsDefault, warn=False)
103+
elif style in library:
104+
_apply_style(library[style])
82105
else:
83106
try:
84107
rc = rc_params_from_file(style, use_default_template=False)
85-
mpl.rcParams.update(rc)
108+
_apply_style(rc)
86109
except IOError:
87110
msg = ("'%s' not found in the style library and input is "
88111
"not a valid URL or path. See `style.available` for "

lib/matplotlib/tests/test_backend_qt4.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from matplotlib.testing.decorators import cleanup, switch_backend
88
from matplotlib.testing.decorators import knownfailureif
99
from matplotlib._pylab_helpers import Gcf
10-
import matplotlib.style as mstyle
10+
import matplotlib
1111
import copy
1212

1313
try:
@@ -17,7 +17,7 @@
1717
import mock
1818

1919
try:
20-
with mstyle.context({'backend': 'Qt4Agg'}):
20+
with matplotlib.rc_context(rc={'backend': 'Qt4Agg'}):
2121
from matplotlib.backends.qt_compat import QtCore
2222

2323
from matplotlib.backends.backend_qt4 import (MODIFIER_KEYS,

lib/matplotlib/tests/test_backend_qt5.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from matplotlib.testing.decorators import cleanup, switch_backend
77
from matplotlib.testing.decorators import knownfailureif
88
from matplotlib._pylab_helpers import Gcf
9-
import matplotlib.style as mstyle
9+
import matplotlib
1010
import copy
1111

1212
try:
@@ -16,7 +16,7 @@
1616
import mock
1717

1818
try:
19-
with mstyle.context({'backend': 'Qt5Agg'}):
19+
with matplotlib.rc_context(rc={'backend': 'Qt5Agg'}):
2020
from matplotlib.backends.qt_compat import QtCore, __version__
2121
from matplotlib.backends.backend_qt5 import (MODIFIER_KEYS,
2222
SUPER, ALT, CTRL, SHIFT)

0 commit comments

Comments
 (0)