Repository navigation
gh-161242: Cache strerror() messages per errno #161455
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
Draft
cmaloney
wants to merge
7
commits into
python:main
Choose a base branch
from
cmaloney:oserror_sterror_cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+145
−3
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
98e6d6e
gh-161242: Cache strerror() messages per errno
cmaloney 1629843
Use open() of a missing file in test_strerror_cached
cmaloney 53d9206
Clear cache when LC_MESSAGES changes.
cmaloney ffcbf0f
Validate the strerror cache against the current locale
cmaloney 98bfddf
Test a real locale change in test_strerror_cached
cmaloney 47944d3
Address strerror cache review
cmaloney 6389031
Keep the strerror comment in place
cmaloney File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3309,9 +3309,8 @@ _Py_IsValidFD(int fd) | |
| // | ||
| // On success, return a Unicode string. On error, set an exception and return | ||
| // NULL. | ||
| PyObject* | ||
| _Py_strerror(int code) | ||
| /*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/ | ||
| static PyObject * | ||
| _Py_strerror_uncached(int code) | ||
| { | ||
| const char *errors = "surrogateescape"; | ||
|
|
||
|
|
@@ -3396,3 +3395,101 @@ _Py_strerror(int code) | |
| return PyUnicode_DecodeLocale(message, errors); | ||
| #endif | ||
| } | ||
|
|
||
|
|
||
| // Return a strong reference to the tuple (codeset, LC_MESSAGES, LANGUAGE, | ||
| // {code: message}) of the current locale, replacing it if the locale changed. | ||
| static PyObject * | ||
| strerror_ensure_cache(struct _Py_exc_state *state) | ||
| { | ||
| // C code can change the locale without calling Python locale.setlocale(). | ||
| // Messages depend on LC_MESSAGES (language), the LC_CTYPE codeset | ||
| // (decoding) and, for glibc's gettext, the LANGUAGE priority list unless | ||
| // LC_MESSAGES is "C". Windows has none, and _Py_FORCE_UTF8_LOCALE ignores | ||
| // the codeset, see _Py_GetLocaleEncoding(). | ||
| #if defined(_Py_FORCE_UTF8_LOCALE) || defined(MS_WINDOWS) | ||
| const char *codeset = ""; | ||
| const char *messages = ""; | ||
| const char *language = ""; | ||
| #else | ||
| const char *codeset = nl_langinfo(CODESET); | ||
| const char *messages = setlocale(LC_MESSAGES, NULL); | ||
| const char *language = getenv("LANGUAGE"); | ||
| if (codeset == NULL) { | ||
| codeset = ""; | ||
| } | ||
| if (messages == NULL) { | ||
| messages = ""; | ||
| } | ||
| if (language == NULL) { | ||
| language = ""; | ||
| } | ||
| #endif | ||
| // Py_SETREF() isn't atomic. | ||
| PyObject *cache; | ||
| Py_BEGIN_CRITICAL_SECTION_MUTEX(&state->strerror_lock); | ||
| cache = Py_NewRef(state->strerror_cache); | ||
| Py_END_CRITICAL_SECTION(); | ||
| if (cache != Py_None | ||
| && strcmp(PyBytes_AS_STRING(PyTuple_GET_ITEM(cache, 0)), codeset) == 0 | ||
| && strcmp(PyBytes_AS_STRING(PyTuple_GET_ITEM(cache, 1)), messages) == 0 | ||
| && strcmp(PyBytes_AS_STRING(PyTuple_GET_ITEM(cache, 2)), language) == 0) | ||
| { | ||
| return cache; | ||
| } | ||
| Py_DECREF(cache); | ||
| cache = PyTuple_New(4); | ||
| if (cache == NULL) { | ||
| return NULL; | ||
| } | ||
| PyTuple_SET_ITEM(cache, 0, PyBytes_FromString(codeset)); | ||
| PyTuple_SET_ITEM(cache, 1, PyBytes_FromString(messages)); | ||
| PyTuple_SET_ITEM(cache, 2, PyBytes_FromString(language)); | ||
| PyTuple_SET_ITEM(cache, 3, PyDict_New()); | ||
| if (PyTuple_GET_ITEM(cache, 0) == NULL | ||
| || PyTuple_GET_ITEM(cache, 1) == NULL | ||
| || PyTuple_GET_ITEM(cache, 2) == NULL | ||
| || PyTuple_GET_ITEM(cache, 3) == NULL) | ||
| { | ||
| Py_DECREF(cache); | ||
| return NULL; | ||
| } | ||
| Py_BEGIN_CRITICAL_SECTION_MUTEX(&state->strerror_lock); | ||
| Py_SETREF(state->strerror_cache, Py_NewRef(cache)); | ||
| Py_END_CRITICAL_SECTION(); | ||
| return cache; | ||
| } | ||
|
|
||
| // os.strerror() takes any int, lets not cache INT_MAX. | ||
| #define STRERROR_CACHE_MAX 5000 | ||
|
|
||
| // _Py_strerror_uncached() with the messages cached per locale: decoding is a | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rewrite this comment... |
||
| // large part of the cost of raising OSError. | ||
| PyObject* | ||
| _Py_strerror(int code) | ||
| { | ||
| struct _Py_exc_state *state = &_PyInterpreterState_GET()->exc_state; | ||
| PyObject *cache = strerror_ensure_cache(state); | ||
| if (cache == NULL) { | ||
| return NULL; | ||
| } | ||
| PyObject *dict = PyTuple_GET_ITEM(cache, 3); | ||
| PyObject *key = PyLong_FromLong(code); | ||
| if (key == NULL) { | ||
| Py_DECREF(cache); | ||
| return NULL; | ||
| } | ||
| PyObject *message; | ||
| if (PyDict_GetItemRef(dict, key, &message) == 0) { | ||
| message = _Py_strerror_uncached(code); | ||
| if (message != NULL | ||
| && PyDict_GET_SIZE(dict) < STRERROR_CACHE_MAX | ||
| && PyDict_SetItem(dict, key, message) < 0) | ||
| { | ||
| Py_CLEAR(message); | ||
| } | ||
| } | ||
| Py_DECREF(key); | ||
| Py_DECREF(cache); | ||
| return message; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you keep the comment please?