Skip to content

gh-106182: sys: Intern getfilesystemencoding() and getfilesystemencodeerrors() #106183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:func:`sys.getfilesystemencoding` and :mod:`sys.getfilesystemencodeerrors`
now return interned Unicode object.
30 changes: 25 additions & 5 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,13 @@ sys_exit_impl(PyObject *module, PyObject *status)
}


static PyObject *
get_utf8_unicode(void)
{
_Py_DECLARE_STR(utf_8, "utf-8");
PyObject *ret = &_Py_STR(utf_8);
return Py_NewRef(ret);
}

/*[clinic input]
sys.getdefaultencoding
Expand All @@ -874,9 +881,7 @@ static PyObject *
sys_getdefaultencoding_impl(PyObject *module)
/*[clinic end generated code: output=256d19dfcc0711e6 input=d416856ddbef6909]*/
{
_Py_DECLARE_STR(utf_8, "utf-8");
PyObject *ret = &_Py_STR(utf_8);
return Py_NewRef(ret);
return get_utf8_unicode();
}

/*[clinic input]
Expand All @@ -891,7 +896,17 @@ sys_getfilesystemencoding_impl(PyObject *module)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
const PyConfig *config = _PyInterpreterState_GetConfig(interp);
return PyUnicode_FromWideChar(config->filesystem_encoding, -1);

if (wcscmp(config->filesystem_encoding, L"utf-8") == 0) {
return get_utf8_unicode();
}

PyObject *u = PyUnicode_FromWideChar(config->filesystem_encoding, -1);
if (u == NULL) {
return NULL;
}
_PyUnicode_InternInPlace(interp, &u);
return u;
}

/*[clinic input]
Expand All @@ -906,7 +921,12 @@ sys_getfilesystemencodeerrors_impl(PyObject *module)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
const PyConfig *config = _PyInterpreterState_GetConfig(interp);
return PyUnicode_FromWideChar(config->filesystem_errors, -1);
PyObject *u = PyUnicode_FromWideChar(config->filesystem_errors, -1);
if (u == NULL) {
return NULL;
}
_PyUnicode_InternInPlace(interp, &u);
return u;
}

/*[clinic input]
Expand Down