From 64c514277840191fd139f9b2df31f0f4b5cbf289 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Wed, 27 Apr 2022 18:00:20 +0900 Subject: [PATCH 1/2] TextIOWrapper.reconfigure() supports "locale" encoding. --- Lib/_pyio.py | 2 ++ Modules/_io/textio.c | 12 ++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Lib/_pyio.py b/Lib/_pyio.py index 380a7a736c64e9..0f647eed99d81b 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/Modules/_io/textio.c b/Modules/_io/textio.c index f1cd6d01da8597..3cbaca3ef46069 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); From 429298b9b7c2bca49ee53aa9833a476ed13c3726 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Wed, 27 Apr 2022 18:04:29 +0900 Subject: [PATCH 2/2] Add NEWS entry and update the doc --- Doc/library/io.rst | 3 +++ .../next/Library/2022-04-27-18-04-24.gh-issue-91952.9A4RXx.rst | 1 + 2 files changed, 4 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2022-04-27-18-04-24.gh-issue-91952.9A4RXx.rst diff --git a/Doc/library/io.rst b/Doc/library/io.rst index 82757539c62ac1..fdeae9b67465ec 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/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 00000000000000..a0b48d16fe866d --- /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`.