[Python-checkins] gh-91952: Make TextIOWrapper.reconfigure() supports "locale" encoding (GH-91982)
methane
webhook-mailer at python.org
Sat Apr 30 21:44:52 EDT 2022
https://github.com/python/cpython/commit/0729b31a8b9ac35ef9b79fdc5aed22cccec9ba16
commit: 0729b31a8b9ac35ef9b79fdc5aed22cccec9ba16
branch: main
author: Inada Naoki <songofacandy at gmail.com>
committer: methane <songofacandy at gmail.com>
date: 2022-05-01T10:44:14+09:00
summary:
gh-91952: Make TextIOWrapper.reconfigure() supports "locale" encoding (GH-91982)
files:
A Misc/NEWS.d/next/Library/2022-04-27-18-04-24.gh-issue-91952.9A4RXx.rst
M Doc/library/io.rst
M Lib/_pyio.py
M Modules/_io/textio.c
diff --git a/Doc/library/io.rst b/Doc/library/io.rst
index ecd8c8043351d..d8e7b1621e2ea 100644
--- a/Doc/library/io.rst
+++ b/Doc/library/io.rst
@@ -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')
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index 380a7a736c64e..0f647eed99d81 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -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
diff --git a/Misc/NEWS.d/next/Library/2022-04-27-18-04-24.gh-issue-91952.9A4RXx.rst b/Misc/NEWS.d/next/Library/2022-04-27-18-04-24.gh-issue-91952.9A4RXx.rst
new file mode 100644
index 0000000000000..a0b48d16fe866
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-04-27-18-04-24.gh-issue-91952.9A4RXx.rst
@@ -0,0 +1 @@
+Add ``encoding="locale"`` support to :meth:`TextIOWrapper.reconfigure`.
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index f1cd6d01da859..3cbaca3ef4606 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -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);
More information about the Python-checkins
mailing list