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

Skip to content

Commit 0758a12

Browse files
authored
Fix issue with tex-encoding on non-Unicode platforms (#23033)
1 parent 04c8107 commit 0758a12

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/matplotlib/tests/test_texmanager.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import matplotlib.pyplot as plt
55
from matplotlib.texmanager import TexManager
6+
from matplotlib.testing._markers import needs_usetex
67
import pytest
78

89

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