]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-91952: Make TextIOWrapper.reconfigure() supports "locale" encoding (GH-91982)
authorInada Naoki <songofacandy@gmail.com>
Sun, 1 May 2022 01:44:14 +0000 (10:44 +0900)
committerGitHub <noreply@github.com>
Sun, 1 May 2022 01:44:14 +0000 (10:44 +0900)
Doc/library/io.rst
Lib/_pyio.py
Misc/NEWS.d/next/Library/2022-04-27-18-04-24.gh-issue-91952.9A4RXx.rst [new file with mode: 0644]
Modules/_io/textio.c

index ecd8c8043351d1288983b17c8c51fddaaa1635c8..d8e7b1621e2ea815dd3b0e1e96530c9d25ce0b84 100644 (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')
 
index 380a7a736c64e94491d2aaa4b01f46b9466e5a45..0f647eed99d81bda47eb82381b7e6798a5c7197c 100644 (file)
@@ -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 (file)
index 0000000..a0b48d1
--- /dev/null
@@ -0,0 +1 @@
+Add ``encoding="locale"`` support to :meth:`TextIOWrapper.reconfigure`.
index f1cd6d01da8597f16da74fc29e0dbd6d595a19d0..3cbaca3ef4606937122838840f85b11dedcbc647 100644 (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);