Skip to content

Commit eef8ec9

Browse files
committed
Show the failing line in bad-rcparams warnings.
... and emit all warnings through logging, rather than having one of them go through `print()`.
1 parent cc10355 commit eef8ec9

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

lib/matplotlib/__init__.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -783,8 +783,6 @@ def _rc_params_in_file(fname, fail_on_error=False):
783783
Unlike `rc_params_from_file`, the configuration class only contains the
784784
parameters specified in the file (i.e. default values are not filled in).
785785
"""
786-
_error_details_fmt = 'line #%d\n\t"%s"\n\tin file "%s"'
787-
788786
rc_temp = {}
789787
with _open_file_or_url(fname) as fd:
790788
try:
@@ -794,15 +792,15 @@ def _rc_params_in_file(fname, fail_on_error=False):
794792
continue
795793
tup = strippedline.split(':', 1)
796794
if len(tup) != 2:
797-
error_details = _error_details_fmt % (line_no, line, fname)
798-
_log.warning('Illegal %s', error_details)
795+
_log.warning('Missing colon in file %r, line %d (%r)',
796+
fname, line_no, line.rstrip('\n'))
799797
continue
800798
key, val = tup
801799
key = key.strip()
802800
val = val.strip()
803801
if key in rc_temp:
804-
_log.warning('Duplicate key in file %r line #%d.',
805-
fname, line_no)
802+
_log.warning('Duplicate key in file %r, line %d (%r)',
803+
fname, line_no, line.rstrip('\n'))
806804
rc_temp[key] = (val, line, line_no)
807805
except UnicodeDecodeError:
808806
_log.warning('Cannot decode configuration file %s with encoding '
@@ -822,22 +820,22 @@ def _rc_params_in_file(fname, fail_on_error=False):
822820
try:
823821
config[key] = val # try to convert to proper type or skip
824822
except Exception as msg:
825-
error_details = _error_details_fmt % (line_no, line, fname)
826-
_log.warning('Bad val %r on %s\n\t%s',
827-
val, error_details, msg)
823+
_log.warning('Bad value in file %r, line %d (%r): %s',
824+
fname, line_no, line.rstrip('\n'), msg)
828825
elif key in _deprecated_ignore_map:
829826
version, alt_key = _deprecated_ignore_map[key]
830827
cbook.warn_deprecated(
831828
version, name=key, alternative=alt_key,
832829
addendum="Please update your matplotlibrc.")
833830
else:
834831
version = 'master' if '.post' in __version__ else f'v{__version__}'
835-
print(f"""
836-
Bad key "{key}" on line {line_no} in
837-
{fname}.
832+
_log.warning("""
833+
Bad key %(key)s in file %(fname)s, line %(line_no)s (%(line)r)
838834
You probably need to get an updated matplotlibrc file from
839-
https://github.com/matplotlib/matplotlib/blob/{version}/matplotlibrc.template
840-
or from the matplotlib source distribution""", file=sys.stderr)
835+
https://github.com/matplotlib/matplotlib/blob/%(version)s/matplotlibrc.template
836+
or from the matplotlib source distribution""",
837+
dict(key=key, fname=fname, line_no=line_no,
838+
line=line.rstrip('\n'), version=version))
841839
return config
842840

843841

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)