mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
gh-91156: Fix encoding="locale"
in UTF-8 mode (GH-70056)
This commit is contained in:
parent
7b87e8af0c
commit
13b17e2a0a
11 changed files with 35 additions and 24 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue