mirror of
https://github.com/python/cpython.git
synced 2025-08-02 16:13:13 +00:00
gh-93099: Fix _pyio to use locale module properly (gh-93136)
(cherry picked from commit f7fabae75c
)
Co-authored-by: Dong-hee Na <donghee.na@python.org>
This commit is contained in:
parent
fd35be511a
commit
97fe65a7be
2 changed files with 15 additions and 8 deletions
19
Lib/_pyio.py
19
Lib/_pyio.py
|
@ -2022,13 +2022,7 @@ class TextIOWrapper(TextIOBase):
|
||||||
encoding = text_encoding(encoding)
|
encoding = text_encoding(encoding)
|
||||||
|
|
||||||
if encoding == "locale":
|
if encoding == "locale":
|
||||||
try:
|
encoding = self._get_locale_encoding()
|
||||||
import locale
|
|
||||||
except ImportError:
|
|
||||||
# Importing locale may fail if Python is being built
|
|
||||||
encoding = "utf-8"
|
|
||||||
else:
|
|
||||||
encoding = locale.getencoding()
|
|
||||||
|
|
||||||
if not isinstance(encoding, str):
|
if not isinstance(encoding, str):
|
||||||
raise ValueError("invalid encoding: %r" % encoding)
|
raise ValueError("invalid encoding: %r" % encoding)
|
||||||
|
@ -2162,7 +2156,7 @@ class TextIOWrapper(TextIOBase):
|
||||||
if not isinstance(encoding, str):
|
if not isinstance(encoding, str):
|
||||||
raise TypeError("invalid encoding: %r" % encoding)
|
raise TypeError("invalid encoding: %r" % encoding)
|
||||||
if encoding == "locale":
|
if encoding == "locale":
|
||||||
encoding = locale.getencoding()
|
encoding = self._get_locale_encoding()
|
||||||
|
|
||||||
if newline is Ellipsis:
|
if newline is Ellipsis:
|
||||||
newline = self._readnl
|
newline = self._readnl
|
||||||
|
@ -2267,6 +2261,15 @@ class TextIOWrapper(TextIOBase):
|
||||||
self._decoded_chars_used += len(chars)
|
self._decoded_chars_used += len(chars)
|
||||||
return 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):
|
def _rewind_decoded_chars(self, n):
|
||||||
"""Rewind the _decoded_chars buffer."""
|
"""Rewind the _decoded_chars buffer."""
|
||||||
if self._decoded_chars_used < n:
|
if self._decoded_chars_used < n:
|
||||||
|
|
|
@ -3570,6 +3570,10 @@ class TextIOWrapperTest(unittest.TestCase):
|
||||||
F.tell = lambda x: 0
|
F.tell = lambda x: 0
|
||||||
t = self.TextIOWrapper(F(), encoding='utf-8')
|
t = self.TextIOWrapper(F(), encoding='utf-8')
|
||||||
|
|
||||||
|
def test_reconfigure_locale(self):
|
||||||
|
wrapper = io.TextIOWrapper(io.BytesIO(b"test"))
|
||||||
|
wrapper.reconfigure(encoding="locale")
|
||||||
|
|
||||||
def test_reconfigure_encoding_read(self):
|
def test_reconfigure_encoding_read(self):
|
||||||
# latin1 -> utf8
|
# latin1 -> utf8
|
||||||
# (latin1 can decode utf-8 encoded string)
|
# (latin1 can decode utf-8 encoded string)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue