@@ -457,8 +457,9 @@ _io.text_encoding
457
457
458
458
A helper function to choose the text encoding.
459
459
460
- When encoding is not None, just return it.
461
- Otherwise, return the default text encoding (i.e. "locale").
460
+ When encoding is not None, this function returns it.
461
+ Otherwise, this function returns the default text encoding
462
+ (i.e. "locale" or "utf-8" depends on UTF-8 mode).
462
463
463
464
This function emits an EncodingWarning if encoding is None and
464
465
sys.flags.warn_default_encoding is true.
@@ -469,7 +470,7 @@ However, please consider using encoding="utf-8" for new APIs.
469
470
470
471
static PyObject *
471
472
_io_text_encoding_impl (PyObject * module , PyObject * encoding , int stacklevel )
472
- /*[clinic end generated code: output=91b2cfea6934cc0c input=bf70231213e2a7b4 ]*/
473
+ /*[clinic end generated code: output=91b2cfea6934cc0c input=4999aa8b3d90f3d4 ]*/
473
474
{
474
475
if (encoding == NULL || encoding == Py_None ) {
475
476
PyInterpreterState * interp = _PyInterpreterState_GET ();
@@ -479,7 +480,14 @@ _io_text_encoding_impl(PyObject *module, PyObject *encoding, int stacklevel)
479
480
return NULL ;
480
481
}
481
482
}
482
- return & _Py_ID (locale );
483
+ const PyPreConfig * preconfig = & _PyRuntime .preconfig ;
484
+ if (preconfig -> utf8_mode ) {
485
+ _Py_DECLARE_STR (utf_8 , "utf-8" );
486
+ encoding = & _Py_STR (utf_8 );
487
+ }
488
+ else {
489
+ encoding = & _Py_ID (locale );
490
+ }
483
491
}
484
492
Py_INCREF (encoding );
485
493
return encoding ;
0 commit comments