diff options
author | Inada Naoki <songofacandy@gmail.com> | 2022-04-14 07:00:35 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-14 07:00:35 (GMT) |
commit | 13b17e2a0a56d506d62a0bf2774e3deb4cbaeb72 (patch) | |
tree | 506c197765d1aa1f14cfe51134db7088288fd83c /Modules/_io/textio.c | |
parent | 7b87e8af0cb8df0d76e8ab18a9b12affb4526103 (diff) | |
download | cpython-13b17e2a0a56d506d62a0bf2774e3deb4cbaeb72.zip cpython-13b17e2a0a56d506d62a0bf2774e3deb4cbaeb72.tar.gz cpython-13b17e2a0a56d506d62a0bf2774e3deb4cbaeb72.tar.bz2 |
gh-91156: Fix `encoding="locale"` in UTF-8 mode (GH-70056)
Diffstat (limited to 'Modules/_io/textio.c')
-rw-r--r-- | Modules/_io/textio.c | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index 0e20741..6ba7393 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -1023,7 +1023,7 @@ _io.TextIOWrapper.__init__ Character and line based layer over a BufferedIOBase object, buffer. encoding gives the name of the encoding that the stream will be -decoded or encoded with. It defaults to locale.getpreferredencoding(False). +decoded or encoded with. It defaults to locale.getencoding(). errors determines the strictness of encoding and decoding (see help(codecs.Codec) or the documentation for codecs.register) and @@ -1055,12 +1055,12 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer, const char *encoding, PyObject *errors, const char *newline, int line_buffering, int write_through) -/*[clinic end generated code: output=72267c0c01032ed2 input=77d8696d1a1f460b]*/ +/*[clinic end generated code: output=72267c0c01032ed2 input=72590963698f289b]*/ { PyObject *raw, *codec_info = NULL; - _PyIO_State *state = NULL; PyObject *res; int r; + int use_locale_encoding = 0; // Use locale encoding even in UTF-8 mode. self->ok = 0; self->detached = 0; @@ -1076,6 +1076,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer, } else if (strcmp(encoding, "locale") == 0) { encoding = NULL; + use_locale_encoding = 1; } if (errors == Py_None) { @@ -1113,10 +1114,15 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer, self->encodefunc = NULL; self->b2cratio = 0.0; +#ifdef MS_WINDOWS + // os.device_encoding() on Unix is the locale encoding or UTF-8 + // according to UTF-8 Mode. + // Since UTF-8 mode shouldn't affect `encoding="locale"`, we call + // os.device_encoding() only on Windows. if (encoding == NULL) { /* Try os.device_encoding(fileno) */ PyObject *fileno; - state = IO_STATE(); + _PyIO_State *state = IO_STATE(); if (state == NULL) goto error; fileno = PyObject_CallMethodNoArgs(buffer, &_Py_ID(fileno)); @@ -1144,8 +1150,10 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer, Py_CLEAR(self->encoding); } } +#endif + if (encoding == NULL && self->encoding == NULL) { - if (_PyRuntime.preconfig.utf8_mode) { + if (_PyRuntime.preconfig.utf8_mode && !use_locale_encoding) { _Py_DECLARE_STR(utf_8, "utf-8"); self->encoding = Py_NewRef(&_Py_STR(utf_8)); } |