Skip to content

Show the failing line in bad-rcparams warnings. #17405

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 1 commit into from
May 19, 2020
Merged
Show file tree
Hide file tree
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
27 changes: 12 additions & 15 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,9 +792,6 @@ def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False):
fail_on_error : bool, default: False
Whether invalid entries should result in an exception or a warning.
"""

_error_details_fmt = 'line #%d\n\t"%s"\n\tin file "%s"'

rc_temp = {}
with _open_file_or_url(fname) as fd:
try:
Expand All @@ -805,15 +802,15 @@ def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False):
continue
tup = strippedline.split(':', 1)
if len(tup) != 2:
error_details = _error_details_fmt % (line_no, line, fname)
_log.warning('Illegal %s', error_details)
_log.warning('Missing colon in file %r, line %d (%r)',
fname, line_no, line.rstrip('\n'))
continue
key, val = tup
key = key.strip()
val = val.strip()
if key in rc_temp:
_log.warning('Duplicate key in file %r line #%d.',
fname, line_no)
_log.warning('Duplicate key in file %r, line %d (%r)',
fname, line_no, line.rstrip('\n'))
rc_temp[key] = (val, line, line_no)
except UnicodeDecodeError:
_log.warning('Cannot decode configuration file %s with encoding '
Expand All @@ -833,22 +830,22 @@ def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False):
try:
config[key] = val # try to convert to proper type or skip
except Exception as msg:
error_details = _error_details_fmt % (line_no, line, fname)
_log.warning('Bad val %r on %s\n\t%s',
val, error_details, msg)
_log.warning('Bad value in file %r, line %d (%r): %s',
fname, line_no, line.rstrip('\n'), msg)
elif key in _deprecated_ignore_map:
version, alt_key = _deprecated_ignore_map[key]
cbook.warn_deprecated(
version, name=key, alternative=alt_key,
addendum="Please update your matplotlibrc.")
else:
version = 'master' if '.post' in __version__ else f'v{__version__}'
print(f"""
Bad key "{key}" on line {line_no} in
{fname}.
_log.warning("""
Bad key %(key)s in file %(fname)s, line %(line_no)s (%(line)r)
You probably need to get an updated matplotlibrc file from
https://github.com/matplotlib/matplotlib/blob/{version}/matplotlibrc.template
or from the matplotlib source distribution""", file=sys.stderr)
https://github.com/matplotlib/matplotlib/blob/%(version)s/matplotlibrc.template
or from the matplotlib source distribution""",
dict(key=key, fname=fname, line_no=line_no,
line=line.rstrip('\n'), version=version))
return config


Expand Down
5 changes: 3 additions & 2 deletions lib/matplotlib/tests/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ def temp_style(style_name, settings=None):
style.reload_library()


def test_invalid_rc_warning_includes_filename(capsys):
def test_invalid_rc_warning_includes_filename(caplog):
SETTINGS = {'foo': 'bar'}
basename = 'basename'
with temp_style(basename, SETTINGS):
# style.reload_library() in temp_style() triggers the warning
pass
assert basename in capsys.readouterr().err
assert (len(caplog.records) == 1
and basename in caplog.records[0].getMessage())


def test_available():
Expand Down