GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-42236: os.device_encoding() respects UTF-8 Mode by vstinner · Pull Request #23119 · python/cpython

bpo-42236: os.device_encoding() respects UTF-8 Mode #23119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
vstinner merged 2 commits into python:master from vstinner:device_encoding
Nov 4, 2020

Uh oh!

There was an error while loading. Please reload this page.

Merged

bpo-42236: os.device_encoding() respects UTF-8 Mode #23119

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
4288a46
bpo-42236: os.device_encoding() respects UTF-8 Mode
vstinner Nov 2, 2020
c5e768b
Update the UTF-8 Mode documentation
vstinner Nov 3, 2020
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

8 changes: 8 additions & 0 deletions 8 Doc/library/os.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ of the UTF-8 encoding:
:ref:`error handler <error-handlers>` being enabled for :data:`sys.stdin`
and :data:`sys.stdout` (:data:`sys.stderr` continues to use
``backslashreplace`` as it does in the default locale-aware mode)
* On Unix, :func:`os.device_encoding` returns ``'UTF-8'``. rather than the
device encoding.

Note that the standard stream settings in UTF-8 mode can be overridden by
:envvar:`PYTHONIOENCODING` (just as they can be in the default locale-aware
Expand Down Expand Up @@ -808,6 +810,12 @@ as internal buffering of data.
Return a string describing the encoding of the device associated with *fd*
if it is connected to a terminal; else return :const:`None`.

On Unix, if the :ref:`Python UTF-8 Mode <utf8-mode>` is enabled, return
``'UTF-8'`` rather than the device encoding.

.. versionchanged:: 3.10
On Unix, the function now implements the Python UTF-8 Mode.


.. function:: dup(fd)

Expand Down
27 changes: 27 additions & 0 deletions 27 Lib/test/test_utf8_mode.py
Show comments
View file
Open in desktop
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()
3 changes: 3 additions & 0 deletions 3 Misc/NEWS.d/next/Library/2020-11-02-23-05-17.bpo-42236.aJ6ZBR.rst
Show comments
View file
Open in desktop
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 18 Python/fileutils.c
Show comments
View file
Open in desktop
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 13 Python/initconfig.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1515,8 +1515,8 @@ config_init_stdio_encoding(PyConfig *config,
{
PyStatus status;

/* If Py_SetStandardStreamEncoding() have been called, use these
parameters. */
/* If Py_SetStandardStreamEncoding() has been called, use its
arguments if they are not NULL. */
if (config->stdio_encoding == NULL && _Py_StandardStreamEncoding != NULL) {
status = CONFIG_SET_BYTES_STR(config, &config->stdio_encoding,
_Py_StandardStreamEncoding,
Expand All @@ -1535,6 +1535,7 @@ config_init_stdio_encoding(PyConfig *config,
}
}

// Exit if encoding and errors are defined
if (config->stdio_encoding != NULL && config->stdio_errors != NULL) {
return _PyStatus_OK();
}
Expand Down Expand Up @@ -1634,12 +1635,12 @@ config_get_fs_encoding(PyConfig *config, const PyPreConfig *preconfig,
if (preconfig->utf8_mode) {
return PyConfig_SetString(config, fs_encoding, L"utf-8");
}
else if (_Py_GetForceASCII()) {

if (_Py_GetForceASCII()) {
return PyConfig_SetString(config, fs_encoding, L"ascii");
}
else {
return config_get_locale_encoding(config, preconfig, fs_encoding);
}

return config_get_locale_encoding(config, preconfig, fs_encoding);
#endif // !MS_WINDOWS
}

Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.