bpo-42236: os.device_encoding() respects UTF-8 Mode by vstinner · Pull Request #23119 · python/cpython · GitHub
Skip to content
Merged
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
8 changes: 8 additions & 0 deletions Doc/library/os.rst
27 changes: 27 additions & 0 deletions Lib/test/test_utf8_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
"""

import locale
import subprocess
import sys
import textwrap
import unittest
from test import support
from test.support.script_helper import assert_python_ok, assert_python_failure
from test.support import os_helper


MS_WINDOWS = (sys.platform == 'win32')
Expand Down Expand Up @@ -250,6 +252,31 @@ def test_optim_level(self):
out = self.get_output('-X', 'utf8', '-E', '-c', code)
self.assertEqual(out, '1')

@unittest.skipIf(MS_WINDOWS,
"os.device_encoding() doesn't implement "
"the UTF-8 Mode on Windows")
def test_device_encoding(self):
# Use stdout as TTY
if not sys.stdout.isatty():
self.skipTest("sys.stdout is not a TTY")

filename = 'out.txt'
self.addCleanup(os_helper.unlink, filename)

code = (f'import os, sys; fd = sys.stdout.fileno(); '
f'out = open({filename!r}, "w", encoding="utf-8"); '
f'print(os.isatty(fd), os.device_encoding(fd), file=out); '
f'out.close()')
cmd = [sys.executable, '-X', 'utf8', '-c', code]
# The stdout TTY is inherited to the child process
proc = subprocess.run(cmd, text=True)
self.assertEqual(proc.returncode, 0, proc)

# In UTF-8 Mode, device_encoding(fd) returns "UTF-8" if fd is a TTY
with open(filename, encoding="utf8") as fp:
out = fp.read().rstrip()
self.assertEqual(out, 'True UTF-8')


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
On Unix, the :func:`os.device_encoding` function now returns ``'UTF-8'`` rather
than the device encoding if the :ref:`Python UTF-8 Mode <utf8-mode>` is
enabled.
18 changes: 7 additions & 11 deletions Python/fileutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ get_surrogateescape(_Py_error_handler errors, int *surrogateescape)
PyObject *
_Py_device_encoding(int fd)
{
#if defined(MS_WINDOWS)
UINT cp;
#endif
int valid;
_Py_BEGIN_SUPPRESS_IPH
valid = isatty(fd);
Expand All @@ -66,6 +63,7 @@ _Py_device_encoding(int fd)
Py_RETURN_NONE;

#if defined(MS_WINDOWS)
UINT cp;
if (fd == 0)
cp = GetConsoleCP();
else if (fd == 1 || fd == 2)
Expand All @@ -74,16 +72,14 @@ _Py_device_encoding(int fd)
cp = 0;
/* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
has no console */
if (cp != 0)
return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
#elif defined(CODESET)
{
char *codeset = nl_langinfo(CODESET);
if (codeset != NULL && codeset[0] != 0)
return PyUnicode_FromString(codeset);
if (cp == 0) {
Py_RETURN_NONE;
}

return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
#else
return _Py_GetLocaleEncodingObject();
#endif
Py_RETURN_NONE;
}

#if !defined(_Py_FORCE_UTF8_FS_ENCODING) && !defined(MS_WINDOWS)
Expand Down
13 changes: 7 additions & 6 deletions Python/initconfig.c