From a3af1d803b00efd2256b4afbd399b3e1a1bb9d66 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 20 Jun 2021 20:07:16 +0200 Subject: [PATCH] Make lack of support more explicit for non-postscript fonts + usetex. The info-level log can easily be hidden, and the KeyError may look like a bug on Matplotlib's side. Instead, explicitly raise an exception whose error message states that Matplotlib doesn't currently support certain cases. Use a LookupError (a parent class of KeyError, so a slight API break) rather than a KeyError as KeyError formats its argument as if it was the missing key (calling repr on it), which would not be the case here. --- .../next_api_changes/deprecations/20474-AL.rst | 2 ++ lib/matplotlib/dviread.py | 15 ++++++--------- lib/matplotlib/tests/test_dviread.py | 4 ++-- 3 files changed, 10 insertions(+), 11 deletions(-) create mode 100644 doc/api/next_api_changes/deprecations/20474-AL.rst diff --git a/doc/api/next_api_changes/deprecations/20474-AL.rst b/doc/api/next_api_changes/deprecations/20474-AL.rst new file mode 100644 index 000000000000..89c627023ba3 --- /dev/null +++ b/doc/api/next_api_changes/deprecations/20474-AL.rst @@ -0,0 +1,2 @@ +``dviread.PsfontsMap`` now raises LookupError instead of KeyError for missing fonts +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/lib/matplotlib/dviread.py b/lib/matplotlib/dviread.py index 04af497e95db..df7923dad45f 100644 --- a/lib/matplotlib/dviread.py +++ b/lib/matplotlib/dviread.py @@ -27,7 +27,6 @@ import struct import subprocess import sys -import textwrap import numpy as np @@ -852,14 +851,12 @@ def __getitem__(self, texname): try: return self._parsed[texname] except KeyError: - fmt = ('An associated PostScript font (required by Matplotlib) ' - 'could not be found for TeX font {0!r} in {1!r}. This ' - 'problem can often be solved by installing a suitable ' - 'PostScript font package in your TeX package manager.') - _log.info(textwrap.fill( - fmt.format(texname.decode('ascii'), self._filename), - break_on_hyphens=False, break_long_words=False)) - raise + raise LookupError( + f"An associated PostScript font (required by Matplotlib) " + f"could not be found for TeX font {texname.decode('ascii')!r} " + f"in {self._filename!r}; this problem can often be solved by " + f"installing a suitable PostScript font package in your TeX " + f"package manager") from None def _parse_and_cache_line(self, line): """ diff --git a/lib/matplotlib/tests/test_dviread.py b/lib/matplotlib/tests/test_dviread.py index 3e88b6bd31ef..a40151fd555f 100644 --- a/lib/matplotlib/tests/test_dviread.py +++ b/lib/matplotlib/tests/test_dviread.py @@ -54,9 +54,9 @@ def test_PsfontsMap(monkeypatch): entry = fontmap[b'TeXfontC'] assert entry.psname == b'PSfontC3' # Missing font - with pytest.raises(KeyError, match='no-such-font'): + with pytest.raises(LookupError, match='no-such-font'): fontmap[b'no-such-font'] - with pytest.raises(KeyError, match='%'): + with pytest.raises(LookupError, match='%'): fontmap[b'%']