diff --git a/lib/matplotlib/tests/test_texmanager.py b/lib/matplotlib/tests/test_texmanager.py index 2182c32e8ce1..29ed9d86597f 100644 --- a/lib/matplotlib/tests/test_texmanager.py +++ b/lib/matplotlib/tests/test_texmanager.py @@ -3,6 +3,7 @@ import matplotlib.pyplot as plt from matplotlib.texmanager import TexManager +from matplotlib.testing._markers import needs_usetex import pytest @@ -39,3 +40,20 @@ def test_font_selection(rc, preamble, family): src = Path(tm.make_tex("hello, world", fontsize=12)).read_text() assert preamble in src assert [*re.findall(r"\\\w+family", src)] == [family] + + +@needs_usetex +def test_unicode_characters(): + # Smoke test to see that Unicode characters does not cause issues + # See #23019 + plt.rcParams['text.usetex'] = True + fig, ax = plt.subplots() + ax.set_ylabel('\\textit{Velocity (\N{DEGREE SIGN}/sec)}') + ax.set_xlabel('\N{VULGAR FRACTION ONE QUARTER}Öøæ') + fig.canvas.draw() + + # But not all characters. + # Should raise RuntimeError, not UnicodeDecodeError + with pytest.raises(RuntimeError): + ax.set_title('\N{SNOWMAN}') + fig.canvas.draw() diff --git a/lib/matplotlib/texmanager.py b/lib/matplotlib/texmanager.py index d1b6d92b1689..4865463a349c 100644 --- a/lib/matplotlib/texmanager.py +++ b/lib/matplotlib/texmanager.py @@ -243,7 +243,8 @@ def make_tex(cls, tex, fontsize): Return the file name. """ texfile = cls.get_basefile(tex, fontsize) + ".tex" - Path(texfile).write_text(cls._get_tex_source(tex, fontsize)) + Path(texfile).write_text(cls._get_tex_source(tex, fontsize), + encoding='utf-8') return texfile @classmethod @@ -267,7 +268,8 @@ def _run_checked_subprocess(cls, command, tex, *, cwd=None): prog=command[0], format_command=cbook._pformat_subprocess(command), tex=tex.encode('unicode_escape'), - exc=exc.output.decode('utf-8'))) from None + exc=exc.output.decode('utf-8', 'backslashreplace')) + ) from None _log.debug(report) return report