@@ -863,6 +863,13 @@ sys_exit_impl(PyObject *module, PyObject *status)
863863}
864864
865865
866+ static PyObject *
867+ get_utf8_unicode (void )
868+ {
869+ _Py_DECLARE_STR (utf_8 , "utf-8" );
870+ PyObject * ret = & _Py_STR (utf_8 );
871+ return Py_NewRef (ret );
872+ }
866873
867874/*[clinic input]
868875sys.getdefaultencoding
@@ -874,9 +881,7 @@ static PyObject *
874881sys_getdefaultencoding_impl (PyObject * module )
875882/*[clinic end generated code: output=256d19dfcc0711e6 input=d416856ddbef6909]*/
876883{
877- _Py_DECLARE_STR (utf_8 , "utf-8" );
878- PyObject * ret = & _Py_STR (utf_8 );
879- return Py_NewRef (ret );
884+ return get_utf8_unicode ();
880885}
881886
882887/*[clinic input]
@@ -891,7 +896,17 @@ sys_getfilesystemencoding_impl(PyObject *module)
891896{
892897 PyInterpreterState * interp = _PyInterpreterState_GET ();
893898 const PyConfig * config = _PyInterpreterState_GetConfig (interp );
894- return PyUnicode_FromWideChar (config -> filesystem_encoding , -1 );
899+
900+ if (wcscmp (config -> filesystem_encoding , L"utf-8" ) == 0 ) {
901+ return get_utf8_unicode ();
902+ }
903+
904+ PyObject * u = PyUnicode_FromWideChar (config -> filesystem_encoding , -1 );
905+ if (u == NULL ) {
906+ return NULL ;
907+ }
908+ _PyUnicode_InternInPlace (interp , & u );
909+ return u ;
895910}
896911
897912/*[clinic input]
@@ -906,7 +921,12 @@ sys_getfilesystemencodeerrors_impl(PyObject *module)
906921{
907922 PyInterpreterState * interp = _PyInterpreterState_GET ();
908923 const PyConfig * config = _PyInterpreterState_GetConfig (interp );
909- return PyUnicode_FromWideChar (config -> filesystem_errors , -1 );
924+ PyObject * u = PyUnicode_FromWideChar (config -> filesystem_errors , -1 );
925+ if (u == NULL ) {
926+ return NULL ;
927+ }
928+ _PyUnicode_InternInPlace (interp , & u );
929+ return u ;
910930}
911931
912932/*[clinic input]
0 commit comments