Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Lib/test/test_cmd_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,42 @@ def test_python_legacy_windows_stdio(self):
support.skip_on_low_desktop_heap_memory_subprocess(p.returncode)
self.assertEqual(p.returncode, 0)

@unittest.skipUnless(support.MS_WINDOWS, 'Test only applicable on Windows')
def test_python_legacy_windows_stdio_encoding(self):
# gh-86427: In the legacy mode the encoding of a standard stream is
# the encoding of the console it is connected to, which can differ
# for input and output.
import ctypes
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
try:
fin = open('CONIN$')
except OSError:
self.skipTest('no console')
# We cannot use PIPE, because the standard streams should be
# connected to the console. So we use the exit code.
code = ("import sys; sys.exit(sys.stdin.encoding != 'cp850' or "
"sys.stdout.encoding != 'cp437')")
env = os.environ.copy()
env['PYTHONLEGACYWINDOWSSTDIO'] = '1'
env['PYTHONUTF8'] = '0'
env.pop('PYTHONIOENCODING', None)
old_cp = kernel32.GetConsoleCP()
old_output_cp = kernel32.GetConsoleOutputCP()
with fin, open('CONOUT$', 'w') as fout:
try:
if not kernel32.SetConsoleCP(850):
self.skipTest('cannot set the console input code page')
if not kernel32.SetConsoleOutputCP(437):
self.skipTest('cannot set the console output code page')
proc = subprocess.run([sys.executable, '-c', code], env=env,
stdin=fin, stdout=fout,
stderr=subprocess.DEVNULL)
finally:
kernel32.SetConsoleCP(old_cp)
kernel32.SetConsoleOutputCP(old_output_cp)
support.skip_on_low_desktop_heap_memory_subprocess(proc.returncode)
self.assertEqual(proc.returncode, 0)

@unittest.skipIf("-fsanitize" in sysconfig.get_config_vars().get('PY_CFLAGS', ()),
"PYTHONMALLOCSTATS doesn't work with ASAN")
def test_python_malloc_stats(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix the encoding of the standard streams in the legacy Windows stdio mode
(:envvar:`PYTHONLEGACYWINDOWSSTDIO`). It is now the encoding of the device
the stream is connected to, as in Python 3.7, not the ANSI code page.
4 changes: 4 additions & 0 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -15209,6 +15209,10 @@ init_stdio_encoding(PyInterpreterState *interp)
{
/* Update the stdio encoding to the normalized Python codec name. */
PyConfig *config = (PyConfig*)_PyInterpreterState_GetConfig(interp);
if (config->stdio_encoding == NULL) {
/* gh-86427: The encoding is determined for every stream. */
return _PyStatus_OK();
}
if (config_get_codec_name(&config->stdio_encoding) < 0) {
return _PyStatus_ERR("failed to get the Python codec name "
"of the stdio encoding");
Expand Down
16 changes: 12 additions & 4 deletions Python/initconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ static const PyConfigSpec PYCONFIG_SPEC[] = {
SPEC(show_ref_count, BOOL, READ_ONLY, NO_SYS, NO_GLOBAL),
SPEC(site_import, BOOL, READ_ONLY, NO_SYS, GLOBAL(&Py_NoSiteFlag, 1)), // sys.flags.no_site
SPEC(skip_source_first_line, BOOL, READ_ONLY, NO_SYS, NO_GLOBAL),
SPEC(stdio_encoding, WSTR, READ_ONLY, NO_SYS, NO_GLOBAL),
SPEC(stdio_encoding, WSTR_OPT, READ_ONLY, NO_SYS, NO_GLOBAL),
SPEC(stdio_errors, WSTR, READ_ONLY, NO_SYS, NO_GLOBAL),
SPEC(tracemalloc, UINT, READ_ONLY, NO_SYS, NO_GLOBAL),
SPEC(use_frozen_modules, BOOL, READ_ONLY, NO_SYS, NO_GLOBAL),
Expand Down Expand Up @@ -1074,11 +1074,14 @@ config_check_consistency(const PyConfig *config)
assert(config->module_search_paths_set >= 0);
assert(config->filesystem_encoding != NULL);
assert(config->filesystem_errors != NULL);
assert(config->stdio_encoding != NULL);
assert(config->stdio_errors != NULL);
#ifdef MS_WINDOWS
/* stdio_encoding can be NULL in the legacy Windows stdio mode. */
assert(config->stdio_encoding != NULL || config->legacy_windows_stdio);
assert(config->legacy_windows_stdio >= 0);
#else
assert(config->stdio_encoding != NULL);
#endif
assert(config->stdio_errors != NULL);
/* -c and -m options are exclusive */
assert(!(config->run_command != NULL && config->run_module != NULL));
assert(config->check_hash_pycs_mode != NULL);
Expand Down Expand Up @@ -2709,7 +2712,12 @@ config_init_stdio_encoding(PyConfig *config,
}

/* Choose the default error handler based on the current locale. */
if (config->stdio_encoding == NULL) {
if (config->stdio_encoding == NULL
#ifdef MS_WINDOWS
/* gh-86427: it is determined for each stream. */
&& !config->legacy_windows_stdio
#endif
) {
status = config_get_locale_encoding(config, preconfig,
&config->stdio_encoding);
if (_PyStatus_EXCEPTION(status)) {
Expand Down
13 changes: 12 additions & 1 deletion Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -3070,7 +3070,18 @@ create_stdio(const PyConfig *config, PyObject* io,
newline = "\n";
#endif

PyObject *encoding_str = PyUnicode_FromWideChar(encoding, -1);
PyObject *encoding_str;
if (encoding != NULL) {
encoding_str = PyUnicode_FromWideChar(encoding, -1);
}
else {
/* gh-86427: use the encoding of the device. */
encoding_str = _Py_device_encoding(fd);
if (encoding_str == Py_None) {
Py_DECREF(encoding_str);
encoding_str = _Py_GetLocaleEncodingObject();
}
}
if (encoding_str == NULL) {
Py_CLEAR(buf);
goto error;
Expand Down
Loading