Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 1, 2022
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
3 changes: 3 additions & 0 deletions Doc/library/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
2 changes: 2 additions & 0 deletions Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``encoding="locale"`` support to :meth:`TextIOWrapper.reconfigure`.
12 changes: 10 additions & 2 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down