8000 Merge pull request #2555 from JanSchulz/bug_2543 · matplotlib/matplotlib@5079b6d · GitHub
[go: up one dir, main page]

Skip to content

Commit 5079b6d

Browse files
committed
Merge pull request #2555 from JanSchulz/bug_2543
Make it possible to add mpl.rcParams to itself or deepcopy
2 parents c38a71e + 4d01858 commit 5079b6d

File tree

3 files changed

+64
-19
lines changed

3 files changed

+64
-19
lines changed

lib/matplotlib/__init__.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -771,14 +771,14 @@ def matplotlib_fname():
771771

772772

773773
_deprecated_map = {
774-
'text.fontstyle': 'font.style',
775-
'text.fontangle': 'font.style',
776-
'text.fontvariant': 'font.variant',
777-
'text.fontweight': 'font.weight',
778-
'text.fontsize': 'font.size',
779-
'tick.size' : 'tick.major.size',
780-
'svg.embed_char_paths' : 'svg.fonttype',
781-
'savefig.extension' : 'savefig.format'
774+
'text.fontstyle': ('font.style',lambda x: x),
775+
'text.fontangle': ('font.style',lambda x: x),
776+
'text.fontvariant': ('font.variant',lambda x: x),
777+
'text.fontweight': ('font.weight',lambda x: x),
778+
'text.fontsize': ('font.size',lambda x: x),
779+
'tick.size' : ('tick.major.size',lambda x: x),
780+
'svg.embed_char_paths' : ('svg.fonttype',lambda x: "path" if x else "none"),
781+
'savefig.extension' : ('savefig.format',lambda x: x),
782782
}
783783

784784
_deprecated_ignore_map = {
@@ -802,24 +802,28 @@ class RcParams(dict):
802802
def __setitem__(self, key, val):
803803
try:
804804
if key in _deprecated_map:
805-
alt = _deprecated_map[key]
806-
warnings.warn(self.msg_depr % (key, alt))
807-
key = alt
805+
alt_key, alt_val = _deprecated_map[key]
806+
warnings.warn(self.msg_depr % (key, alt_key))
807+
key = alt_key
808+
val = alt_val(val)
808809
elif key in _deprecated_ignore_map:
809810
alt = _deprecated_ignore_map[key]
810811
warnings.warn(self.msg_depr_ignore % (key, alt))
811812
return
812-
cval = self.validate[key](val)
813+
try:
814+
cval = self.validate[key](val)
815+
except ValueError as ve:
816+
raise ValueError("Key %s: %s" % (key, str(ve)))
813817
dict.__setitem__(self, key, cval)
814818
except KeyError:
815819
raise KeyError('%s is not a valid rc parameter.\
816820
See rcParams.keys() for a list of valid parameters.' % (key,))
817821

818822
def __getitem__(self, key):
819823
if key in _deprecated_map:
820-
alt = _deprecated_map[key]
821-
warnings.warn(self.msg_depr % (key, alt))
822-
key = alt
824+
alt_key, alt_val = _deprecated_map[key]
825+
warnings.warn(self.msg_depr % (key, alt_key))
826+
key = alt_key
823827
elif key in _deprecated_ignore_map:
824828
alt = _deprecated_ignore_map[key]
825829
warnings.warn(self.msg_depr_ignore % (key, alt))

lib/matplotlib/rcsetup.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def validate_bool_maybe_none(b):
8888
'Convert b to a boolean or raise'
8989
if isinstance(b, six.string_types):
9090
b = b.lower()
91-
if b == 'none':
91+
if b is None or b == 'none':
9292
return None
9393
if b in ('t', 'y', 'yes', 'on', 'true', '1', 1, True):
9494
return True
@@ -336,7 +336,6 @@ def update_savefig_format(value):
336336
def validate_ps_distiller(s):
337337
if isinstance(s, six.string_types):
338338
s = s.lower()
339-
340339
if s in ('none', None):
341340
return None
342341
elif s in ('false', False):
@@ -395,7 +394,7 @@ def deprecate_svg_embed_char_paths(value):
395394
warnings.warn("svg.embed_char_paths is deprecated. Use "
396395
"svg.fonttype instead.")
397396

398-
validate_svg_fonttype = ValidateInStrings('fonttype',
397+
validate_svg_fonttype = ValidateInStrings('svg.fonttype',
399398
['none', 'path', 'svgfont'])
400399

401400

@@ -430,7 +429,9 @@ def validate_bbox(s):
430429
raise ValueError("bbox should be 'tight' or 'standard'")
431430

432431
def validate_sketch(s):
433-
if s == 'None' or s is None:
432+
if isinstance(s, six.string_types):
433+
s = s.lower()
434+
if s == 'none' or s is None:
434435
return None
435436
if isinstance(s, six.string_types):
436437
result = tuple([float(v.strip()) for v in s.split(',')])

lib/matplotlib/tests/test_rcparams.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
import six
55

66
import os
7+
import sys
78

89
import matplotlib as mpl
910
from matplotlib.tests import assert_str_equal
11+
from nose.tools import assert_true, assert_raises
12+
import nose
1013

1114

1215
mpl.rc('text', usetex=False)
@@ -92,6 +95,43 @@ def test_RcParams_class():
9295
assert ['font.cursive', 'font.size'] == sorted(rc.find_all('i[vz]').keys())
9396
assert ['font.family'] == list(six.iterkeys(rc.find_all('family')))
9497

98+
def test_Bug_2543():
99+
# Test that it possible to add all values to itself / deepcopy
100+
# This was not possible because validate_bool_maybe_none did not
101+
# accept None as an argument.
102+
# https://github.com/matplotlib/matplotlib/issues/2543
103+
with mpl.rc_context():
104+
_copy = mpl.rcParams.copy()
105+
for key in six.iterkeys(_copy):
106+
mpl.rcParams[key] = _copy[key]
107+
mpl.rcParams['text.dvipnghack'] = None
108+
with mpl.rc_context():
109+
from copy import deepcopy
110+
_deep_copy = deepcopy(mpl.rcParams)
111+
from matplotlib.rcsetup import validate_bool_maybe_none, validate_bool
112+
# real test is that this does not raise
113+
assert_true(validate_bool_maybe_none(None) is None)
114+
assert_true(validate_bool_maybe_none("none") is None)
115+
_fonttype = mpl.rcParams['svg.fonttype']
116+
assert_true(_fonttype == mpl.rcParams['svg.embed_char_paths'])
117+
with mpl.rc_context():
118+
mpl.rcParams['svg.embed_char_paths'] = False
119+
assert_true(mpl.rcParams['svg.fonttype'] == "none")
120+
121+
def test_Bug_2543_newer_python():
122+
# only split from above because of the usage of assert_raises
123+
# as a context manager, which only works in 2.7 and above
124+
if sys.version_info[:2] < (2, 7):
125+
raise nose.SkipTest("assert_raises as context manager not supported with Python < 2.7")
126+
from matplotlib.rcsetup import validate_bool_maybe_none, validate_bool
127+
with assert_raises(ValueError):
128+
validate_bool_maybe_none("blah")
129+
with assert_raises(ValueError):
130+
validate_bool(None)
131+
with assert_raises(ValueError):
132+
with mpl.rc_context():
133+
mpl.rcParams['svg.fonttype'] = True
134+
95135
if __name__ == '__main__':
96136
import nose
97137
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)
0