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

Skip to content

Commit 0edf0f1

Browse files
committed
Fix issue with tex-encoding on non-Unicode platforms
1 parent 04c8107 commit 0edf0f1

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/matplotlib/tests/test_texmanager.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from pathlib import Path
22
import re
33

4+
import matplotlib as mpl
45
import matplotlib.pyplot as plt
56
from matplotlib.texmanager import TexManager
7+
from matplotlib.testing._markers import needs_usetex
68
import pytest
79

810

@@ -39,3 +41,20 @@ def test_font_selection(rc, preamble, family):
3941
src = Path(tm.make_tex("hello, world", fontsize=12)).read_text()
4042
assert preamble in src
4143
assert [*re.findall(r"\\\w+family", src)] == [family]
44+
45+
46+
@needs_usetex
47+
def test_unicode_characters():
48+
# Smoke test to see that Unicode characters does not cause issues
49+
# See #23019
50+
plt.rcParams['text.usetex'] = True
51+
fig, ax = plt.subplots()
52+
ax.set_ylabel('\\textit{Velocity (\N{DEGREE SIGN}/sec)}')
53+
ax.set_xlabel('\N{VULGAR FRACTION ONE QUARTER}Öøæ')
54+
fig.canvas.draw()
55+
56+
# But not all characters.
57+
# Should raise RuntimeError, not UnicodeDecodeError
58+
with pytest.raises(RuntimeError):
59+
ax.set_title('\N{SNOWMAN}')
60+
fig.canvas.draw()

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
@@ -267,7 +268,8 @@ def _run_checked_subprocess(cls, command, tex, *, cwd=None):
267268
prog=command[0],
268269
format_command=cbook._pformat_subprocess(command),
269270
tex=tex.encode('unicode_escape'),
270-
exc=exc.output.decode('utf-8'))) from None
271+
exc=exc.output.decode('utf-8', 'backslashreplace'))
272+
) from None
271273
_log.debug(report)
272274
return report
273275

0 commit comments

Comments
 (0)