summaryrefslogtreecommitdiffstats
path: root/Lib
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
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')
-rw-r--r--Lib/_pyio.py10
-rw-r--r--Lib/test/test_io.py11
-rw-r--r--Lib/test/test_utf8_mode.py6
3 files changed, 21 insertions, 6 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.",
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index 2d0ca87..67be108 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -4289,6 +4289,17 @@ class MiscIOTest(unittest.TestCase):
self.assertTrue(
warnings[1].startswith(b"<string>:8: EncodingWarning: "))
+ def test_text_encoding(self):
+ # PEP 597, bpo-47000. io.text_encoding() returns "locale" or "utf-8"
+ # based on sys.flags.utf8_mode
+ code = "import io; print(io.text_encoding(None))"
+
+ proc = assert_python_ok('-X', 'utf8=0', '-c', code)
+ self.assertEqual(b"locale", proc.out.strip())
+
+ proc = assert_python_ok('-X', 'utf8=1', '-c', code)
+ self.assertEqual(b"utf-8", proc.out.strip())
+
@support.cpython_only
# Depending if OpenWrapper was already created or not, the warning is
# emitted or not. For example, the attribute is already created when this
diff --git a/Lib/test/test_utf8_mode.py b/Lib/test/test_utf8_mode.py
index 2b96f76..308e8e8 100644
--- a/Lib/test/test_utf8_mode.py
+++ b/Lib/test/test_utf8_mode.py
@@ -161,7 +161,7 @@ class UTF8ModeTests(unittest.TestCase):
filename = __file__
out = self.get_output('-c', code, filename, PYTHONUTF8='1')
- self.assertEqual(out, 'UTF-8/strict')
+ self.assertEqual(out.lower(), 'utf-8/strict')
def _check_io_encoding(self, module, encoding=None, errors=None):
filename = __file__
@@ -183,10 +183,10 @@ class UTF8ModeTests(unittest.TestCase):
PYTHONUTF8='1')
if not encoding:
- encoding = 'UTF-8'
+ encoding = 'utf-8'
if not errors:
errors = 'strict'
- self.assertEqual(out, f'{encoding}/{errors}')
+ self.assertEqual(out.lower(), f'{encoding}/{errors}')
def check_io_encoding(self, module):
self._check_io_encoding(module, encoding="latin1")