summaryrefslogtreecommitdiffstats
path: root/Lib/_pyio.py
diff options
context:
space:
mode:
authorDong-hee Na <donghee.na@python.org>2022-05-24 00:37:01 (GMT)
committerGitHub <noreply@github.com>2022-05-24 00:37:01 (GMT)
commitf7fabae75c7b8ecd0c5673b5d62a15db24a05953 (patch)
treeb73c663d2e75ab6e581e5518a5a10732ad017156 /Lib/_pyio.py
parente739ff141680fd7e2a762cf98c4352c6c850af1f (diff)
downloadcpython-f7fabae75c7b8ecd0c5673b5d62a15db24a05953.zip
cpython-f7fabae75c7b8ecd0c5673b5d62a15db24a05953.tar.gz
cpython-f7fabae75c7b8ecd0c5673b5d62a15db24a05953.tar.bz2
gh-93099: Fix _pyio to use locale module properly (gh-93136)
Diffstat (limited to 'Lib/_pyio.py')
-rw-r--r--Lib/_pyio.py19
1 files changed, 11 insertions, 8 deletions
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index 0f647ee..0bfdeaa 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -2022,13 +2022,7 @@ class TextIOWrapper(TextIOBase):
encoding = text_encoding(encoding)
if encoding == "locale":
- try:
- import locale
- except ImportError:
- # Importing locale may fail if Python is being built
- encoding = "utf-8"
- else:
- encoding = locale.getencoding()
+ encoding = self._get_locale_encoding()
if not isinstance(encoding, str):
raise ValueError("invalid encoding: %r" % encoding)
@@ -2162,7 +2156,7 @@ class TextIOWrapper(TextIOBase):
if not isinstance(encoding, str):
raise TypeError("invalid encoding: %r" % encoding)
if encoding == "locale":
- encoding = locale.getencoding()
+ encoding = self._get_locale_encoding()
if newline is Ellipsis:
newline = self._readnl
@@ -2267,6 +2261,15 @@ class TextIOWrapper(TextIOBase):
self._decoded_chars_used += len(chars)
return chars
+ def _get_locale_encoding(self):
+ try:
+ import locale
+ except ImportError:
+ # Importing locale may fail if Python is being built
+ return "utf-8"
+ else:
+ return locale.getencoding()
+
def _rewind_decoded_chars(self, n):
"""Rewind the _decoded_chars buffer."""
if self._decoded_chars_used < n: