Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 618e86d

Browse files
committed
Fix issue with tex-encoding on non-Unicode platforms
1 parent 63e9ea3 commit 618e86d

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/matplotlib/tests/test_texmanager.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,17 @@ def test_font_selection(rc, preamble, family):
3939
src = Path(tm.make_tex("hello, world", fontsize=12)).read_text()
4040
assert preamble in src
4141
assert [*re.findall(r"\\\w+family", src)] == [family]
42+
43+
44+
def test_unicode_characters():
45+
# Smoke test to see that Unicode characters does not cause issues
46+
# See #23019
47+
plt.rcParams['text.usetex'] = True
48+
fig, ax = plt.subplots()
49+
ax.set_ylabel('\\textit{Velocity (\N{DEGREE SIGN}/sec)}')
50+
ax.set_xlabel('\N{VULGAR FRACTION ONE QUARTER}Öøæ')
51+
52+
# Not all characters are supported though
53+
# Should raise RuntimeError, NOT UnicodeDecodeError
54+
with pytest.raises(RuntimeError):
55+
ax.set_title('\N{snowman}')

lib/matplotlib/texmanager.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ def make_tex(cls, tex, fontsize):
243243
Return the file name.
244244
"""
245245
texfile = cls.get_basefile(tex, fontsize) + ".tex"
246-
Path(texfile).write_text(cls._get_tex_source(tex, fontsize))
246+
Path(texfile).write_text(cls._get_tex_source(tex, fontsize),
247+
encoding='utf-8')
247248
return texfile
248249

249250
@classmethod
@@ -265,7 +266,8 @@ def _run_checked_subprocess(cls, command, tex, *, cwd=None):
265266
'{exc}\n\n'.format(
266267
prog=command[0],
267268
tex=tex.encode('unicode_escape'),
268-
exc=exc.output.decode('utf-8'))) from exc
269+
exc=exc.output.decode('utf-8', 'backslashreplace'))
270+
) from exc
269271
_log.debug(report)
270272
return report
271273

0 commit comments

Comments
 (0)