Skip to content

Commit 0729b31

Browse files
authored
gh-91952: Make TextIOWrapper.reconfigure() supports "locale" encoding (GH-91982)
1 parent b963618 commit 0729b31

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

Doc/library/io.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,9 @@ Text I/O
10381038

10391039
.. versionadded:: 3.7
10401040

1041+
.. versionchanged:: 3.11
1042+
The method supports ``encoding="locale"`` option.
1043+
10411044

10421045
.. class:: StringIO(initial_value='', newline='\\n')
10431046

Lib/_pyio.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,6 +2161,8 @@ def reconfigure(self, *,
21612161
else:
21622162
if not isinstance(encoding, str):
21632163
raise TypeError("invalid encoding: %r" % encoding)
2164+
if encoding == "locale":
2165+
encoding = locale.getencoding()
21642166

21652167
if newline is Ellipsis:
21662168
newline = self._readnl
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add ``encoding="locale"`` support to :meth:`TextIOWrapper.reconfigure`.

Modules/_io/textio.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,8 +1248,16 @@ textiowrapper_change_encoding(textio *self, PyObject *encoding,
12481248
errors = self->errors;
12491249
}
12501250
}
1251-
else if (errors == Py_None) {
1252-
errors = &_Py_ID(strict);
1251+
else {
1252+
if (_PyUnicode_EqualToASCIIString(encoding, "locale")) {
1253+
encoding = _Py_GetLocaleEncodingObject();
1254+
if (encoding == NULL) {
1255+
return -1;
1256+
}
1257+
}
1258+
if (errors == Py_None) {
1259+
errors = &_Py_ID(strict);
1260+
}
12531261
}
12541262

12551263
const char *c_errors = PyUnicode_AsUTF8(errors);

0 commit comments

Comments
 (0)