diff options
author | Inada Naoki <songofacandy@gmail.com> | 2022-05-01 01:44:14 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-01 01:44:14 (GMT) |
commit | 0729b31a8b9ac35ef9b79fdc5aed22cccec9ba16 (patch) | |
tree | a363dea749d30f142dfcaefb1ab733f261dcdcb3 | |
parent | b9636180b32ea483f12564657ba1c3eb5b6d73d8 (diff) | |
download | cpython-0729b31a8b9ac35ef9b79fdc5aed22cccec9ba16.zip cpython-0729b31a8b9ac35ef9b79fdc5aed22cccec9ba16.tar.gz cpython-0729b31a8b9ac35ef9b79fdc5aed22cccec9ba16.tar.bz2 |
gh-91952: Make TextIOWrapper.reconfigure() supports "locale" encoding (GH-91982)
-rw-r--r-- | Doc/library/io.rst | 3 | ||||
-rw-r--r-- | Lib/_pyio.py | 2 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2022-04-27-18-04-24.gh-issue-91952.9A4RXx.rst | 1 | ||||
-rw-r--r-- | Modules/_io/textio.c | 12 |
4 files changed, 16 insertions, 2 deletions
diff --git a/Doc/library/io.rst b/Doc/library/io.rst index ecd8c80..d8e7b16 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 380a7a7..0f647ee 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -2161,6 +2161,8 @@ class TextIOWrapper(TextIOBase): 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 0000000..a0b48d1 --- /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 f1cd6d0..3cbaca3 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); |