Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2022,13 +2022,7 @@ def __init__(self, buffer, encoding=None, errors=None, newline=None,
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)
Expand Down Expand Up @@ -2162,7 +2156,7 @@ def reconfigure(self, *,
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
Expand Down Expand Up @@ -2267,6 +2261,15 @@ def _get_decoded_chars(self, n=None):
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:
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -3570,6 +3570,10 @@ def seekable(self): return True
F.tell = lambda x: 0
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):
# latin1 -> utf8
# (latin1 can decode utf-8 encoded string)
Expand Down