@@ -1060,7 +1060,6 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
10601060 PyObject * raw , * codec_info = NULL ;
10611061 PyObject * res ;
10621062 int r ;
1063- int use_locale_encoding = 0 ; // Use locale encoding even in UTF-8 mode.
10641063
10651064 self -> ok = 0 ;
10661065 self -> detached = 0 ;
@@ -1074,10 +1073,6 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
10741073 }
10751074 }
10761075 }
1077- else if (strcmp (encoding , "locale" ) == 0 ) {
1078- encoding = NULL ;
1079- use_locale_encoding = 1 ;
1080- }
10811076
10821077 if (errors == Py_None ) {
10831078 errors = & _Py_ID (strict );
@@ -1114,57 +1109,18 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
11141109 self -> encodefunc = NULL ;
11151110 self -> b2cratio = 0.0 ;
11161111
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.
1122- if (encoding == NULL ) {
1123- /* Try os.device_encoding(fileno) */
1124- PyObject * fileno ;
1125- _PyIO_State * state = IO_STATE ();
1126- if (state == NULL )
1127- goto error ;
1128- fileno = PyObject_CallMethodNoArgs (buffer , & _Py_ID (fileno ));
1129- /* Ignore only AttributeError and UnsupportedOperation */
1130- if (fileno == NULL ) {
1131- if (PyErr_ExceptionMatches (PyExc_AttributeError ) ||
1132- PyErr_ExceptionMatches (state -> unsupported_operation )) {
1133- PyErr_Clear ();
1134- }
1135- else {
1136- goto error ;
1137- }
1138- }
1139- else {
1140- int fd = _PyLong_AsInt (fileno );
1141- Py_DECREF (fileno );
1142- if (fd == -1 && PyErr_Occurred ()) {
1143- goto error ;
1144- }
1145-
1146- self -> encoding = _Py_device_encoding (fd );
1147- if (self -> encoding == NULL )
1148- goto error ;
1149- else if (!PyUnicode_Check (self -> encoding ))
1150- Py_CLEAR (self -> encoding );
1151- }
1112+ if (encoding == NULL && _PyRuntime .preconfig .utf8_mode ) {
1113+ _Py_DECLARE_STR (utf_8 , "utf-8" );
1114+ self -> encoding = Py_NewRef (& _Py_STR (utf_8 ));
11521115 }
1153- #endif
1154-
1155- if (encoding == NULL && self -> encoding == NULL ) {
1156- if (_PyRuntime .preconfig .utf8_mode && !use_locale_encoding ) {
1157- _Py_DECLARE_STR (utf_8 , "utf-8" );
1158- self -> encoding = Py_NewRef (& _Py_STR (utf_8 ));
1159- }
1160- else {
1161- self -> encoding = _Py_GetLocaleEncodingObject ();
1162- }
1116+ else if (encoding == NULL || (strcmp (encoding , "locale" ) == 0 )) {
1117+ self -> encoding = _Py_GetLocaleEncodingObject ();
11631118 if (self -> encoding == NULL ) {
11641119 goto error ;
11651120 }
11661121 assert (PyUnicode_Check (self -> encoding ));
11671122 }
1123+
11681124 if (self -> encoding != NULL ) {
11691125 encoding = PyUnicode_AsUTF8 (self -> encoding );
11701126 if (encoding == NULL )
0 commit comments