From c95a5d6465a952f462a32c49baf1a8013d373cdc Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 19 Nov 2026 21:46:03 +0100 Subject: [PATCH 1/3] [3.15] Backport regrtest changes from the main branch (#162281) * Enhance encodings in regrtest header (#161959) If the UTF-8 mode is disabled and/or if PYTHONCOERCECLOCALE environment variable is set to "0": mention it in the "== encodings" regrtest header. If the locale encoding and the filesystem encoding are equal, log "locale=FS=" to have a shorter output. On Windows, log ANSI and OEM code pages. Backport to 3.15: add types and _winapi imports to libregrtest.utils. (cherry picked from commit f4fbacf64a094e4ae8a8052b28331a3eee178035) * Display test header when running python -m test --tsan (#162249) When running "python -m test" with --tsan or --tsan-parallel, display the test header (log TSAN_OPTIONS env var). (cherry picked from commit 53f4cad43b87292aca163ea5a92d130bd024981d) * Add missing types import (cherry picked from commit ecf8cfe85fcbb946d151079bb8f393df4b80c061) --- Lib/test/libregrtest/main.py | 2 ++ Lib/test/libregrtest/utils.py | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/Lib/test/libregrtest/main.py b/Lib/test/libregrtest/main.py index 62f66adadebbb1..f00c352f29b52c 100644 --- a/Lib/test/libregrtest/main.py +++ b/Lib/test/libregrtest/main.py @@ -203,9 +203,11 @@ def find_tests(self, tests: TestList | None = None) -> tuple[TestTuple, TestList if self.tsan: setup_tsan_tests(self.cmdline_args) + self.want_header = True if self.tsan_parallel: setup_tsan_parallel_tests(self.cmdline_args) + self.want_header = True alltests = findtests(testdir=self.test_dir, exclude=exclude_tests) diff --git a/Lib/test/libregrtest/utils.py b/Lib/test/libregrtest/utils.py index 75ce76b5da0312..40295e6be50ffb 100644 --- a/Lib/test/libregrtest/utils.py +++ b/Lib/test/libregrtest/utils.py @@ -12,7 +12,13 @@ import sysconfig import tempfile import textwrap +import types from collections.abc import Callable +_winapi: types.ModuleType | None +try: + import _winapi +except ImportError: + _winapi = None from test import support from test.support import os_helper @@ -636,8 +642,32 @@ def display_header(use_resources: dict[str, str | None], if process_cpu_count and process_cpu_count != cpu_count: cpu_count = f"{process_cpu_count} (process) / {cpu_count} (system)" print("== CPU count:", cpu_count) - print("== encodings: locale=%s FS=%s" - % (locale.getencoding(), sys.getfilesystemencoding())) + + encodings = [] + if _winapi is not None: + # Log ANSI and OEM code pages + ansi = f'cp{_winapi.GetACP()}' + oem = f'cp{_winapi.GetOEMCP()}' + if ansi == oem: + encodings.append(f'ansi=oem={ansi}') + else: + encodings.append(f'ansi={ansi}') + encodings.append(f'oem={oem}') + encodings.append(f'FS={sys.getfilesystemencoding()}') + else: + locale_encoding = locale.getencoding() # nl_langinfo(CODESET) + fs_encoding = sys.getfilesystemencoding() # normalized name + if locale_encoding.lower() == fs_encoding.lower(): + encodings.append(f'locale=FS={fs_encoding}') + else: + encodings.append(f'locale={locale_encoding}') + encodings.append(f'FS={fs_encoding}') + if not sys.flags.utf8_mode: + encodings.append('utf8=0') + if (not sys.flags.ignore_environment + and os.environ.get('PYTHONCOERCECLOCALE') == '0'): + encodings.append('coerce=0') + print(f"== encodings: {' '.join(encodings)}") if use_resources: text = format_resources(use_resources) From 08c9f7c6287a6378278de9a223a9531a41dabf4c Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 19 Nov 2026 23:55:29 +0100 Subject: [PATCH 2/3] Backport _winapi.GetOEMCP() function --- Modules/_winapi.c | 14 ++++++++++++++ Modules/clinic/_winapi.c.h | 20 +++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Modules/_winapi.c b/Modules/_winapi.c index 767a5b35f45664..8ae89219fb05eb 100644 --- a/Modules/_winapi.c +++ b/Modules/_winapi.c @@ -2746,6 +2746,19 @@ _winapi_GetACP_impl(PyObject *module) return PyLong_FromUnsignedLong(GetACP()); } +/*[clinic input] +_winapi.GetOEMCP + +Get the current Windows ANSI code page identifier. +[clinic start generated code]*/ + +static PyObject * +_winapi_GetOEMCP_impl(PyObject *module) +/*[clinic end generated code: output=4def5b07a8be1b3b input=e8caf4353a28e28e]*/ +{ + return PyLong_FromUnsignedLong(GetOEMCP()); +} + /*[clinic input] _winapi.GetFileType -> DWORD @@ -3017,6 +3030,7 @@ static PyMethodDef winapi_functions[] = { _WINAPI_WAITFORSINGLEOBJECT_METHODDEF _WINAPI_WRITEFILE_METHODDEF _WINAPI_GETACP_METHODDEF + _WINAPI_GETOEMCP_METHODDEF _WINAPI_GETFILETYPE_METHODDEF _WINAPI__MIMETYPES_READ_WINDOWS_REGISTRY_METHODDEF _WINAPI_NEEDCURRENTDIRECTORYFOREXEPATH_METHODDEF diff --git a/Modules/clinic/_winapi.c.h b/Modules/clinic/_winapi.c.h index bd8a64bb57391c..21a1b805b02cae 100644 --- a/Modules/clinic/_winapi.c.h +++ b/Modules/clinic/_winapi.c.h @@ -1929,6 +1929,24 @@ _winapi_GetACP(PyObject *module, PyObject *Py_UNUSED(ignored)) return _winapi_GetACP_impl(module); } +PyDoc_STRVAR(_winapi_GetOEMCP__doc__, +"GetOEMCP($module, /)\n" +"--\n" +"\n" +"Get the current Windows ANSI code page identifier."); + +#define _WINAPI_GETOEMCP_METHODDEF \ + {"GetOEMCP", (PyCFunction)_winapi_GetOEMCP, METH_NOARGS, _winapi_GetOEMCP__doc__}, + +static PyObject * +_winapi_GetOEMCP_impl(PyObject *module); + +static PyObject * +_winapi_GetOEMCP(PyObject *module, PyObject *Py_UNUSED(ignored)) +{ + return _winapi_GetOEMCP_impl(module); +} + PyDoc_STRVAR(_winapi_GetFileType__doc__, "GetFileType($module, /, handle)\n" "--\n" @@ -2179,4 +2197,4 @@ _winapi_GetTickCount64(PyObject *module, PyObject *Py_UNUSED(ignored)) { return _winapi_GetTickCount64_impl(module); } -/*[clinic end generated code: output=4a408e816118d0a6 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=8583aef46a1b76ed input=a9049054013a1b77]*/ From de80432e8c5bbc8653df3528b42eba8cbc64e6a3 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 20 Nov 2026 00:11:43 +0100 Subject: [PATCH 3/3] regrtest: only log utf8=1 if UTF-8 Mode is enabled On Python 3.14, it's disabled by default. --- Lib/test/libregrtest/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/libregrtest/utils.py b/Lib/test/libregrtest/utils.py index 40295e6be50ffb..7012690cd83cdf 100644 --- a/Lib/test/libregrtest/utils.py +++ b/Lib/test/libregrtest/utils.py @@ -662,8 +662,8 @@ def display_header(use_resources: dict[str, str | None], else: encodings.append(f'locale={locale_encoding}') encodings.append(f'FS={fs_encoding}') - if not sys.flags.utf8_mode: - encodings.append('utf8=0') + if sys.flags.utf8_mode: + encodings.append('utf8=1') if (not sys.flags.ignore_environment and os.environ.get('PYTHONCOERCECLOCALE') == '0'): encodings.append('coerce=0')