]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-95027: Fix regrtest stdout encoding on Windows (#98492)
authorVictor Stinner <vstinner@python.org>
Fri, 21 Oct 2022 14:21:36 +0000 (16:21 +0200)
committerGitHub <noreply@github.com>
Fri, 21 Oct 2022 14:21:36 +0000 (16:21 +0200)
On Windows, when the Python test suite is run with the -jN option,
the ANSI code page is now used as the encoding for the stdout
temporary file, rather than using UTF-8 which can lead to decoding
errors.

Lib/test/libregrtest/runtest_mp.py
Misc/NEWS.d/next/Tests/2022-10-20-17-49-50.gh-issue-95027.viRpJB.rst [new file with mode: 0644]

index a4d3e5c3146a8cfe93f42a777a56d64c797549d0..a12fcb46e0fd0be3de84a42141ab26bcd7752a1d 100644 (file)
@@ -22,6 +22,9 @@ from test.libregrtest.runtest import (
 from test.libregrtest.setup import setup_tests
 from test.libregrtest.utils import format_duration, print_warning
 
+if sys.platform == 'win32':
+    import locale
+
 
 # Display the running tests if nothing happened last N seconds
 PROGRESS_UPDATE = 30.0   # seconds
@@ -267,11 +270,16 @@ class TestWorkerProcess(threading.Thread):
             self.current_test_name = None
 
     def _runtest(self, test_name: str) -> MultiprocessResult:
+        if sys.platform == 'win32':
+            # gh-95027: When stdout is not a TTY, Python uses the ANSI code
+            # page for the sys.stdout encoding. If the main process runs in a
+            # terminal, sys.stdout uses WindowsConsoleIO with UTF-8 encoding.
+            encoding = locale.getencoding()
+        else:
+            encoding = sys.stdout.encoding
         # gh-94026: Write stdout+stderr to a tempfile as workaround for
         # non-blocking pipes on Emscripten with NodeJS.
-        with tempfile.TemporaryFile(
-            'w+', encoding=sys.stdout.encoding
-        ) as stdout_fh:
+        with tempfile.TemporaryFile('w+', encoding=encoding) as stdout_fh:
             # gh-93353: Check for leaked temporary files in the parent process,
             # since the deletion of temporary files can happen late during
             # Python finalization: too late for libregrtest.
diff --git a/Misc/NEWS.d/next/Tests/2022-10-20-17-49-50.gh-issue-95027.viRpJB.rst b/Misc/NEWS.d/next/Tests/2022-10-20-17-49-50.gh-issue-95027.viRpJB.rst
new file mode 100644 (file)
index 0000000..8bf1a9d
--- /dev/null
@@ -0,0 +1,4 @@
+On Windows, when the Python test suite is run with the ``-jN`` option, the
+ANSI code page is now used as the encoding for the stdout temporary file,
+rather than using UTF-8 which can lead to decoding errors. Patch by Victor
+Stinner.