diff options
author | Inada Naoki <songofacandy@gmail.com> | 2022-04-14 07:00:35 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-14 07:00:35 (GMT) |
commit | 13b17e2a0a56d506d62a0bf2774e3deb4cbaeb72 (patch) | |
tree | 506c197765d1aa1f14cfe51134db7088288fd83c /Lib | |
parent | 7b87e8af0cb8df0d76e8ab18a9b12affb4526103 (diff) | |
download | cpython-13b17e2a0a56d506d62a0bf2774e3deb4cbaeb72.zip cpython-13b17e2a0a56d506d62a0bf2774e3deb4cbaeb72.tar.gz cpython-13b17e2a0a56d506d62a0bf2774e3deb4cbaeb72.tar.bz2 |
gh-91156: Fix `encoding="locale"` in UTF-8 mode (GH-70056)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/_pyio.py | 8 | ||||
-rw-r--r-- | Lib/locale.py | 2 | ||||
-rw-r--r-- | Lib/test/test_io.py | 1 |
3 files changed, 7 insertions, 4 deletions
diff --git a/Lib/_pyio.py b/Lib/_pyio.py index e3ff59e..0f33ed5 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -1988,7 +1988,7 @@ class TextIOWrapper(TextIOBase): r"""Character and line based layer over a BufferedIOBase object, buffer. encoding gives the name of the encoding that the stream will be - decoded or encoded with. It defaults to locale.getpreferredencoding(False). + decoded or encoded with. It defaults to locale.getencoding(). errors determines the strictness of encoding and decoding (see the codecs.register) and defaults to "strict". @@ -2021,7 +2021,9 @@ class TextIOWrapper(TextIOBase): self._check_newline(newline) encoding = text_encoding(encoding) - if encoding == "locale": + if encoding == "locale" and sys.platform == "win32": + # On Unix, os.device_encoding() returns "utf-8" instead of locale encoding + # in the UTF-8 mode. So we use os.device_encoding() only on Windows. try: encoding = os.device_encoding(buffer.fileno()) or "locale" except (AttributeError, UnsupportedOperation): @@ -2034,7 +2036,7 @@ class TextIOWrapper(TextIOBase): # Importing locale may fail if Python is being built encoding = "utf-8" else: - encoding = locale.getpreferredencoding(False) + encoding = locale.getencoding() if not isinstance(encoding, str): raise ValueError("invalid encoding: %r" % encoding) diff --git a/Lib/locale.py b/Lib/locale.py index 496cc80..170e5ee 100644 --- a/Lib/locale.py +++ b/Lib/locale.py @@ -557,7 +557,7 @@ def getdefaultlocale(envvars=('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE')): import warnings warnings.warn( - "Use setlocale(), getpreferredencoding(False) and getlocale() instead", + "Use setlocale(), getencoding() and getlocale() instead", DeprecationWarning, stacklevel=2 ) diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 29fe287..c86251d 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -2737,6 +2737,7 @@ class TextIOWrapperTest(unittest.TestCase): os.environ.update(old_environ) @support.cpython_only + @unittest.skipIf(sys.platform != "win32", "Windows-only test") @unittest.skipIf(sys.flags.utf8_mode, "utf-8 mode is enabled") def test_device_encoding(self): # Issue 15989 |