summaryrefslogtreecommitdiffstats
path: root/Modules/_io/_iomodule.c
diff options
context:
space:
mode:
authorInada Naoki <songofacandy@gmail.com>2022-04-04 02:46:57 (GMT)
committerGitHub <noreply@github.com>2022-04-04 02:46:57 (GMT)
commit4216dce04b7d3f329beaaafc82a77c4ac6cf4d57 (patch)
treeb3ff3df025ddb3c383beb156fd150df62d40ac8e /Modules/_io/_iomodule.c
parent6db2db91b96aaa1270c200ec931a2250fe2799c7 (diff)
downloadcpython-4216dce04b7d3f329beaaafc82a77c4ac6cf4d57.zip
cpython-4216dce04b7d3f329beaaafc82a77c4ac6cf4d57.tar.gz
cpython-4216dce04b7d3f329beaaafc82a77c4ac6cf4d57.tar.bz2
bpo-47000: Make `io.text_encoding()` respects UTF-8 mode (GH-32003)
Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
Diffstat (limited to 'Modules/_io/_iomodule.c')
-rw-r--r--Modules/_io/_iomodule.c16
1 files changed, 12 insertions, 4 deletions
diff --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c
index 7f029f2..065f5e2 100644
--- a/Modules/_io/_iomodule.c
+++ b/Modules/_io/_iomodule.c
@@ -457,8 +457,9 @@ _io.text_encoding
A helper function to choose the text encoding.
-When encoding is not None, just return it.
-Otherwise, return the default text encoding (i.e. "locale").
+When encoding is not None, this function returns it.
+Otherwise, this function returns the default text encoding
+(i.e. "locale" or "utf-8" depends on UTF-8 mode).
This function emits an EncodingWarning if encoding is None and
sys.flags.warn_default_encoding is true.
@@ -469,7 +470,7 @@ However, please consider using encoding="utf-8" for new APIs.
static PyObject *
_io_text_encoding_impl(PyObject *module, PyObject *encoding, int stacklevel)
-/*[clinic end generated code: output=91b2cfea6934cc0c input=bf70231213e2a7b4]*/
+/*[clinic end generated code: output=91b2cfea6934cc0c input=4999aa8b3d90f3d4]*/
{
if (encoding == NULL || encoding == Py_None) {
PyInterpreterState *interp = _PyInterpreterState_GET();
@@ -479,7 +480,14 @@ _io_text_encoding_impl(PyObject *module, PyObject *encoding, int stacklevel)
return NULL;
}
}
- return &_Py_ID(locale);
+ const PyPreConfig *preconfig = &_PyRuntime.preconfig;
+ if (preconfig->utf8_mode) {
+ _Py_DECLARE_STR(utf_8, "utf-8");
+ encoding = &_Py_STR(utf_8);
+ }
+ else {
+ encoding = &_Py_ID(locale);
+ }
}
Py_INCREF(encoding);
return encoding;