From 54c539a445128b2bb953c2625591acc76cab55e4 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 9 May 2022 20:26:46 +0200 Subject: [PATCH] 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. --- doc/sphinxext/redirect_from.py | 7 +++++-- lib/matplotlib/testing/__init__.py | 2 +- lib/matplotlib/tests/test_animation.py | 5 ++--- lib/matplotlib/tests/test_sphinxext.py | 6 +++--- setupext.py | 5 +++-- tools/run_examples.py | 6 ++++-- 6 files changed, 18 insertions(+), 13 deletions(-) diff --git a/doc/sphinxext/redirect_from.py b/doc/sphinxext/redirect_from.py index 34f00bf45cb9..555ba129002c 100644 --- a/doc/sphinxext/redirect_from.py +++ b/doc/sphinxext/redirect_from.py @@ -15,6 +15,7 @@ This creates in the build directory a file ``build/html/topic/old-page.html`` that contains a relative refresh:: + @@ -38,7 +39,9 @@ logger = logging.getLogger(__name__) -HTML_TEMPLATE = """ +HTML_TEMPLATE = """\ + + @@ -115,4 +118,4 @@ def _generate_redirects(app, exception): else: logger.info(f'making refresh html file: {k} redirect to {v}') p.parent.mkdir(parents=True, exist_ok=True) - p.write_text(html) + p.write_text(html, encoding='utf-8') diff --git a/lib/matplotlib/testing/__init__.py b/lib/matplotlib/testing/__init__.py index eba878e0a4a3..43e215e7f994 100644 --- a/lib/matplotlib/testing/__init__.py +++ b/lib/matplotlib/testing/__init__.py @@ -108,7 +108,7 @@ def _check_for_pgf(texsystem): \typeout{pgfversion=\pgfversion} \makeatletter \@@end - """) + """, encoding="utf-8") try: subprocess.check_call( [texsystem, "-halt-on-error", str(tex_path)], cwd=tmpdir, diff --git a/lib/matplotlib/tests/test_animation.py b/lib/matplotlib/tests/test_animation.py index ffa06964e839..3aab1fd7c190 100644 --- a/lib/matplotlib/tests/test_animation.py +++ b/lib/matplotlib/tests/test_animation.py @@ -288,9 +288,8 @@ def test_failing_ffmpeg(tmpdir, monkeypatch, anim): with tmpdir.as_cwd(): monkeypatch.setenv("PATH", ".:" + os.environ["PATH"]) exe_path = Path(str(tmpdir), "ffmpeg") - exe_path.write_text("#!/bin/sh\n" - "[[ $@ -eq 0 ]]\n") - os.chmod(str(exe_path), 0o755) + exe_path.write_bytes(b"#!/bin/sh\n[[ $@ -eq 0 ]]\n") + os.chmod(exe_path, 0o755) with pytest.raises(subprocess.CalledProcessError): anim.save("test.mpeg") diff --git a/lib/matplotlib/tests/test_sphinxext.py b/lib/matplotlib/tests/test_sphinxext.py index f71c6e32018a..d4fb94ade5d1 100644 --- a/lib/matplotlib/tests/test_sphinxext.py +++ b/lib/matplotlib/tests/test_sphinxext.py @@ -75,9 +75,9 @@ def plot_directive_file(num): assert filecmp.cmp(range_6, plot_file(17)) # Modify the included plot - contents = (source_dir / 'included_plot_21.rst').read_text() - contents = contents.replace('plt.plot(range(6))', 'plt.plot(range(4))') - (source_dir / 'included_plot_21.rst').write_text(contents) + contents = (source_dir / 'included_plot_21.rst').read_bytes() + contents = contents.replace(b'plt.plot(range(6))', b'plt.plot(range(4))') + (source_dir / 'included_plot_21.rst').write_bytes(contents) # Build the pages again and check that the modified file was updated modification_times = [plot_directive_file(i).stat().st_mtime for i in (1, 2, 3, 5)] diff --git a/setupext.py b/setupext.py index e68bacdedcb1..2b1fd9349c07 100644 --- a/setupext.py +++ b/setupext.py @@ -668,6 +668,7 @@ def do_custom_build(self, env): sln_path = base_path / vc / "freetype.sln" # https://developercommunity.visualstudio.com/comments/190992/view.html (sln_path.parent / "Directory.Build.props").write_text( + "" "" "" # WindowsTargetPlatformVersion must be given on a single line. @@ -676,8 +677,8 @@ def do_custom_build(self, env): "::GetLatestSDKTargetPlatformVersion('Windows', '10.0')" ")" "" - "" - ) + "", + encoding="utf-8") # It is not a trivial task to determine PlatformToolset to plug it # into msbuild command, and Directory.Build.props will not override # the value in the project file. diff --git a/tools/run_examples.py b/tools/run_examples.py index 97dfe5be7ec5..e6542e9ddf23 100644 --- a/tools/run_examples.py +++ b/tools/run_examples.py @@ -10,6 +10,7 @@ import sys from tempfile import TemporaryDirectory import time +import tokenize _preamble = """\ @@ -73,8 +74,9 @@ def main(): cwd.mkdir(parents=True) else: cwd = stack.enter_context(TemporaryDirectory()) - Path(cwd, relpath.name).write_text( - _preamble + (root / relpath).read_text()) + with tokenize.open(root / relpath) as src: + Path(cwd, relpath.name).write_text( + _preamble + src.read(), encoding="utf-8") for backend in args.backend or [None]: env = {**os.environ} if backend is not None: