Skip to content

Commit 54c539a

Browse files
committed
Fix some possible encoding issues for non-utf8 systems.
read_text/write_text default to the locale encoding, which may or may not be utf8. Fix these by making the encoding explicit or by using bytes.
1 parent 183c0e7 commit 54c539a

File tree

6 files changed

+18
-13
lines changed

6 files changed

+18
-13
lines changed

doc/sphinxext/redirect_from.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
This creates in the build directory a file ``build/html/topic/old-page.html``
1616
that contains a relative refresh::
1717
18+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
1819
<html>
1920
<head>
2021
<meta http-equiv="refresh" content="0; url=new-page.html">
@@ -38,7 +39,9 @@
3839
logger = logging.getLogger(__name__)
3940

4041

41-
HTML_TEMPLATE = """<html>
42+
HTML_TEMPLATE = """\
43+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
44+
<html>
4245
<head>
4346
<meta http-equiv="refresh" content="0; url={v}">
4447
</head>
@@ -115,4 +118,4 @@ def _generate_redirects(app, exception):
115118
else:
116119
logger.info(f'making refresh html file: {k} redirect to {v}')
117120
p.parent.mkdir(parents=True, exist_ok=True)
118-
p.write_text(html)
121+
p.write_text(html, encoding='utf-8')

lib/matplotlib/testing/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def _check_for_pgf(texsystem):
108108
\typeout{pgfversion=\pgfversion}
109109
\makeatletter
110110
\@@end
111-
""")
111+
""", encoding="utf-8")
112112
try:
113113
subprocess.check_call(
114114
[texsystem, "-halt-on-error", str(tex_path)], cwd=tmpdir,

lib/matplotlib/tests/test_animation.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,8 @@ def test_failing_ffmpeg(tmpdir, monkeypatch, anim):
288288
with tmpdir.as_cwd():
289289
monkeypatch.setenv("PATH", ".:" + os.environ["PATH"])
290290
exe_path = Path(str(tmpdir), "ffmpeg")
291-
exe_path.write_text("#!/bin/sh\n"
292-
"[[ $@ -eq 0 ]]\n")
293-
os.chmod(str(exe_path), 0o755)
291+
exe_path.write_bytes(b"#!/bin/sh\n[[ $@ -eq 0 ]]\n")
292+
os.chmod(exe_path, 0o755)
294293
with pytest.raises(subprocess.CalledProcessError):
295294
anim.save("test.mpeg")
296295

lib/matplotlib/tests/test_sphinxext.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ def plot_directive_file(num):
7575
assert filecmp.cmp(range_6, plot_file(17))
7676

7777
# Modify the included plot
78-
contents = (source_dir / 'included_plot_21.rst').read_text()
79-
contents = contents.replace('plt.plot(range(6))', 'plt.plot(range(4))')
80-
(source_dir / 'included_plot_21.rst').write_text(contents)
78+
contents = (source_dir / 'included_plot_21.rst').read_bytes()
79+
contents = contents.replace(b'plt.plot(range(6))', b'plt.plot(range(4))')
80+
(source_dir / 'included_plot_21.rst').write_bytes(contents)
8181
# Build the pages again and check that the modified file was updated
8282
modification_times = [plot_directive_file(i).stat().st_mtime
8383
for i in (1, 2, 3, 5)]

setupext.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ def do_custom_build(self, env):
668668
sln_path = base_path / vc / "freetype.sln"
669669
# https://developercommunity.visualstudio.com/comments/190992/view.html
670670
(sln_path.parent / "Directory.Build.props").write_text(
671+
"<?xml version='1.0' encoding='utf-8'?>"
671672
"<Project>"
672673
"<PropertyGroup>"
673674
# WindowsTargetPlatformVersion must be given on a single line.
@@ -676,8 +677,8 @@ def do_custom_build(self, env):
676677
"::GetLatestSDKTargetPlatformVersion('Windows', '10.0')"
677678
")</WindowsTargetPlatformVersion>"
678679
"</PropertyGroup>"
679-
"</Project>"
680-
)
680+
"</Project>",
681+
encoding="utf-8")
681682
# It is not a trivial task to determine PlatformToolset to plug it
682683
# into msbuild command, and Directory.Build.props will not override
683684
# the value in the project file.

tools/run_examples.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import sys
1111
from tempfile import TemporaryDirectory
1212
import time
13+
import tokenize
1314

1415

1516
_preamble = """\
@@ -73,8 +74,9 @@ def main():
7374
cwd.mkdir(parents=True)
7475
else:
7576
cwd = stack.enter_context(TemporaryDirectory())
76-
Path(cwd, relpath.name).write_text(
77-
_preamble + (root / relpath).read_text())
77+
with tokenize.open(root / relpath) as src:
78+
Path(cwd, relpath.name).write_text(
79+
_preamble + src.read(), encoding="utf-8")
7880
for backend in args.backend or [None]:
7981
env = {**os.environ}
8082
if backend is not None:

0 commit comments

Comments
 (0)