Skip to content

Commit 0c0848b

Browse files
authored
Merge pull request #17405 from anntzer/badrc
Show the failing line in bad-rcparams warnings.
2 parents 2bdfea8 + ec579de commit 0c0848b

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

lib/matplotlib/__init__.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -792,9 +792,6 @@ def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False):
792792
fail_on_error : bool, default: False
793793
Whether invalid entries should result in an exception or a warning.
794794
"""
795-
796-
_error_details_fmt = 'line #%d\n\t"%s"\n\tin file "%s"'
797-
798795
rc_temp = {}
799796
with _open_file_or_url(fname) as fd:
800797
try:
@@ -805,15 +802,15 @@ def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False):
805802
continue
806803
tup = strippedline.split(':', 1)
807804
if len(tup) != 2:
808-
error_details = _error_details_fmt % (line_no, line, fname)
809-
_log.warning('Illegal %s', error_details)
805+
_log.warning('Missing colon in file %r, line %d (%r)',
806+
fname, line_no, line.rstrip('\n'))
810807
continue
811808
key, val = tup
812809
key = key.strip()
813810
val = val.strip()
814811
if key in rc_temp:
815-
_log.warning('Duplicate key in file %r line #%d.',
816-
fname, line_no)
812+
_log.warning('Duplicate key in file %r, line %d (%r)',
813+
fname, line_no, line.rstrip('\n'))
817814
rc_temp[key] = (val, line, line_no)
818815
except UnicodeDecodeError:
819816
_log.warning('Cannot decode configuration file %s with encoding '
@@ -833,22 +830,22 @@ def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False):
833830
try:
834831
config[key] = val # try to convert to proper type or skip
835832
except Exception as msg:
836-
error_details = _error_details_fmt % (line_no, line, fname)
837-
_log.warning('Bad val %r on %s\n\t%s',
838-
val, error_details, msg)
833+
_log.warning('Bad value in file %r, line %d (%r): %s',
834+
fname, line_no, line.rstrip('\n'), msg)
839835
elif key in _deprecated_ignore_map:
840836
version, alt_key = _deprecated_ignore_map[key]
841837
cbook.warn_deprecated(
842838
version, name=key, alternative=alt_key,
843839
addendum="Please update your matplotlibrc.")
844840
else:
845841
version = 'master' if '.post' in __version__ else f'v{__version__}'
846-
print(f"""
847-
Bad key "{key}" on line {line_no} in
848-
{fname}.
842+
_log.warning("""
843+
Bad key %(key)s in file %(fname)s, line %(line_no)s (%(line)r)
849844
You probably need to get an updated matplotlibrc file from
850-
https://github.com/matplotlib/matplotlib/blob/{version}/matplotlibrc.template
851-
or from the matplotlib source distribution""", file=sys.stderr)
845+
https://github.com/matplotlib/matplotlib/blob/%(version)s/matplotlibrc.template
846+
or from the matplotlib source distribution""",
847+
dict(key=key, fname=fname, line_no=line_no,
848+
line=line.rstrip('\n'), version=version))
852849
return config
853850

854851

lib/matplotlib/tests/test_style.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ def temp_style(style_name, settings=None):
3636
style.reload_library()
3737

3838

39-
def test_invalid_rc_warning_includes_filename(capsys):
39+
def test_invalid_rc_warning_includes_filename(caplog):
4040
SETTINGS = {'foo': 'bar'}
4141
basename = 'basename'
4242
with temp_style(basename, SETTINGS):
4343
# style.reload_library() in temp_style() triggers the warning
4444
pass
45-
assert basename in capsys.readouterr().err
45+
assert (len(caplog.records) == 1
46+
and basename in caplog.records[0].getMessage())
4647

4748

4849
def test_available():

0 commit comments

Comments
 (0)