Skip to content

Fix issue with tex-encoding on non-Unicode platforms #23033

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
Jun 30, 2022
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
18 changes: 18 additions & 0 deletions lib/matplotlib/tests/test_texmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import matplotlib.pyplot as plt
from matplotlib.texmanager import TexManager
from matplotlib.testing._markers import needs_usetex
import pytest


Expand Down Expand Up @@ -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()
6 changes: 4 additions & 2 deletions lib/matplotlib/texmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down