@@ -1023,7 +1023,7 @@ _io.TextIOWrapper.__init__
1023
1023
Character and line based layer over a BufferedIOBase object, buffer.
1024
1024
1025
1025
encoding gives the name of the encoding that the stream will be
1026
- decoded or encoded with. It defaults to locale.getpreferredencoding(False ).
1026
+ decoded or encoded with. It defaults to locale.getencoding( ).
1027
1027
1028
1028
errors determines the strictness of encoding and decoding (see
1029
1029
help(codecs.Codec) or the documentation for codecs.register) and
@@ -1055,12 +1055,12 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
1055
1055
const char * encoding , PyObject * errors ,
1056
1056
const char * newline , int line_buffering ,
1057
1057
int write_through )
1058
- /*[clinic end generated code: output=72267c0c01032ed2 input=77d8696d1a1f460b ]*/
1058
+ /*[clinic end generated code: output=72267c0c01032ed2 input=72590963698f289b ]*/
1059
1059
{
1060
1060
PyObject * raw , * codec_info = NULL ;
1061
- _PyIO_State * state = NULL ;
1062
1061
PyObject * res ;
1063
1062
int r ;
1063
+ int use_locale_encoding = 0 ; // Use locale encoding even in UTF-8 mode.
1064
1064
1065
1065
self -> ok = 0 ;
1066
1066
self -> detached = 0 ;
@@ -1076,6 +1076,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
1076
1076
}
1077
1077
else if (strcmp (encoding , "locale" ) == 0 ) {
1078
1078
encoding = NULL ;
1079
+ use_locale_encoding = 1 ;
1079
1080
}
1080
1081
1081
1082
if (errors == Py_None ) {
@@ -1113,10 +1114,15 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
1113
1114
self -> encodefunc = NULL ;
1114
1115
self -> b2cratio = 0.0 ;
1115
1116
1117
+ #ifdef MS_WINDOWS
1118
+ // os.device_encoding() on Unix is the locale encoding or UTF-8
1119
+ // according to UTF-8 Mode.
1120
+ // Since UTF-8 mode shouldn't affect `encoding="locale"`, we call
1121
+ // os.device_encoding() only on Windows.
1116
1122
if (encoding == NULL ) {
1117
1123
/* Try os.device_encoding(fileno) */
1118
1124
PyObject * fileno ;
1119
- state = IO_STATE ();
1125
+ _PyIO_State * state = IO_STATE ();
1120
1126
if (state == NULL )
1121
1127
goto error ;
1122
1128
fileno = PyObject_CallMethodNoArgs (buffer , & _Py_ID (fileno ));
@@ -1144,8 +1150,10 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
1144
1150
Py_CLEAR (self -> encoding );
1145
1151
}
1146
1152
}
1153
+ #endif
1154
+
1147
1155
if (encoding == NULL && self -> encoding == NULL ) {
1148
- if (_PyRuntime .preconfig .utf8_mode ) {
1156
+ if (_PyRuntime .preconfig .utf8_mode && ! use_locale_encoding ) {
1149
1157
_Py_DECLARE_STR (utf_8 , "utf-8" );
1150
1158
self -> encoding = Py_NewRef (& _Py_STR (utf_8 ));
1151
1159
}
0 commit comments