summaryrefslogtreecommitdiffstats
path: root/Lib/_pyio.py
diff options
context:
space:
mode:
authorInada Naoki <songofacandy@gmail.com>2022-04-04 02:46:57 (GMT)
committerGitHub <noreply@github.com>2022-04-04 02:46:57 (GMT)
commit4216dce04b7d3f329beaaafc82a77c4ac6cf4d57 (patch)
treeb3ff3df025ddb3c383beb156fd150df62d40ac8e /Lib/_pyio.py
parent6db2db91b96aaa1270c200ec931a2250fe2799c7 (diff)
downloadcpython-4216dce04b7d3f329beaaafc82a77c4ac6cf4d57.zip
cpython-4216dce04b7d3f329beaaafc82a77c4ac6cf4d57.tar.gz
cpython-4216dce04b7d3f329beaaafc82a77c4ac6cf4d57.tar.bz2
bpo-47000: Make `io.text_encoding()` respects UTF-8 mode (GH-32003)
Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
Diffstat (limited to 'Lib/_pyio.py')
-rw-r--r--Lib/_pyio.py10
1 files changed, 7 insertions, 3 deletions
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index fd00d65..e3ff59e 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -44,8 +44,9 @@ def text_encoding(encoding, stacklevel=2):
"""
A helper function to choose the text encoding.
- When encoding is not None, just return it.
- Otherwise, return the default text encoding (i.e. "locale").
+ When encoding is not None, this function returns it.
+ Otherwise, this function returns the default text encoding
+ (i.e. "locale" or "utf-8" depends on UTF-8 mode).
This function emits an EncodingWarning if *encoding* is None and
sys.flags.warn_default_encoding is true.
@@ -55,7 +56,10 @@ def text_encoding(encoding, stacklevel=2):
However, please consider using encoding="utf-8" for new APIs.
"""
if encoding is None:
- encoding = "locale"
+ if sys.flags.utf8_mode:
+ encoding = "utf-8"
+ else:
+ encoding = "locale"
if sys.flags.warn_default_encoding:
import warnings
warnings.warn("'encoding' argument not specified.",