gh-91952: Make TextIOWrapper.reconfigure() supports "locale" encoding (GH-91982)

This commit is contained in:
Inada Naoki 2022-05-01 10:44:14 +09:00 committed by GitHub
parent b9636180b3
commit 0729b31a8b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 2 deletions

View file

@ -1038,6 +1038,9 @@ Text I/O
.. versionadded:: 3.7
.. versionchanged:: 3.11
The method supports ``encoding="locale"`` option.
.. class:: StringIO(initial_value='', newline='\\n')

View file

@ -2161,6 +2161,8 @@ def reconfigure(self, *,
else:
if not isinstance(encoding, str):
raise TypeError("invalid encoding: %r" % encoding)
if encoding == "locale":
encoding = locale.getencoding()
if newline is Ellipsis:
newline = self._readnl

View file

@ -0,0 +1 @@
Add ``encoding="locale"`` support to :meth:`TextIOWrapper.reconfigure`.

View file

@ -1248,8 +1248,16 @@ textiowrapper_change_encoding(textio *self, PyObject *encoding,
errors = self->errors;
}
}
else if (errors == Py_None) {
errors = &_Py_ID(strict);
else {
if (_PyUnicode_EqualToASCIIString(encoding, "locale")) {
encoding = _Py_GetLocaleEncodingObject();
if (encoding == NULL) {
return -1;
}
}
if (errors == Py_None) {
errors = &_Py_ID(strict);
}
}
const char *c_errors = PyUnicode_AsUTF8(errors);