8000 gh-106182: sys: Intern getfilesystemencoding() and getfilesystemencod… · python/cpython@f1034ba · GitHub
[go: up one dir, main page]

Skip to content

Commit f1034ba

Browse files
authored
gh-106182: sys: Intern getfilesystemencoding() and getfilesystemencodeerrors() (#106183)
sys: Intern getfilesystemencoding() and getfilesystemencodeerrors()
1 parent 77ddc9a commit f1034ba

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`sys.getfilesystemencoding` and :mod:`sys.getfilesystemencodeerrors`
2+
now return interned Unicode object.

Python/sysmodule.c

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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]
868875
sys.getdefaultencoding
@@ -874,9 +881,7 @@ static PyObject *
874881
sys_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

Comments
 (0)
0