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

Skip to content

Be more correct when validating bbox rc params #4647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 12, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,18 @@ def validate_float(s):


def validate_float_or_None(s):
"""convert s to float or raise"""
if s is None:
"""convert s to float, None or raise"""
# values directly from the rc file can only be strings,
# so we need to recognize the string "None" and convert
# it into the object. We will be case-sensitive here to
# avoid confusion between string values of 'none', which
# can be a valid string value for some other parameters.
if s is None or s == 'None':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why this is needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the object from an rcfile can't be a None object, only the string. There is only one situation that I can find where it is valid to say None in the rc file and mean the object and one situation where it is valid to say None/none in the rc file and mean the string or possibly the object (the default interpolation mode). This takes care of the former situation. We probably should double-check how we are handling the latter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I would suggest adding a comment explaining that

return None
try:
return float(s)
except ValueError:
raise ValueError('Could not convert "%s" to float' % s)
raise ValueError('Could not convert "%s" to float or None' % s)

def validate_dpi(s):
"""confirm s is string 'figure' or convert s to float or raise"""
Expand Down Expand Up @@ -476,6 +481,10 @@ def validate_bbox(s):
if s == 'standard':
return None
raise ValueError("bbox should be 'tight' or 'standard'")
elif s is not None:
# Backwards compatibility. None is equivalent to 'standard'.
raise ValueError("bbox should be 'tight' or 'standard'")
return s

def validate_sketch(s):
if isinstance(s, six.string_types):
Expand Down Expand Up @@ -790,7 +799,7 @@ def __call__(self, s):
# value checked by backend at runtime
'savefig.format': ['png', update_savefig_format],
# options are 'tight', or 'standard'. 'standard' validates to None.
'savefig.bbox': [None, validate_bbox],
'savefig.bbox': ['standard', validate_bbox],
'savefig.pad_inches': [0.1, validate_float],
# default directory in savefig dialog box
'savefig.directory': ['~', six.text_type],
Expand Down