gh-145142: Make str.maketrans safe under free-threading#145157
Open
VanshAgarwal24036 wants to merge 7 commits intopython:mainfrom
Open
gh-145142: Make str.maketrans safe under free-threading#145157VanshAgarwal24036 wants to merge 7 commits intopython:mainfrom
VanshAgarwal24036 wants to merge 7 commits intopython:mainfrom
Conversation
colesbury
reviewed
Feb 23, 2026
Objects/unicodeobject.c
Outdated
| } | ||
| PyObject *items = PyDict_Items(x); | ||
| if(items == NULL) goto err; | ||
| Py_ssize_t n = PyList_GET_SIZE(items); |
Contributor
There was a problem hiding this comment.
Use a critical section here instead of copying the items to a list. You'll need to fix up the goto err. Something like:
{
...
Py_BEGIN_CRITICAL_SECTION(x);
while (PyDict_Next(x, &i, &key, &value)) {
...
}
goto done;
err:
Py_CLEAR(new);
done:
Py_END_CRITICAL_SECTION();
return new;
}
Contributor
Author
There was a problem hiding this comment.
- It fails the Windows _freeze_module build (syntax errors from macro expansion).
- A version with early returns builds but causes test_str failures under free-threading on macOS/Linux
Is there a preferred pattern for using Py_BEGIN_CRITICAL_SECTION here, or would you prefer using the dict’s internal locking helpers instead?
Misc/NEWS.d/next/Core_and_Builtins/2026-02-23-23-18-28.gh-issue-145142.T-XbVe.rst
Outdated
Show resolved
Hide resolved
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Replace unsafe PyDict_Next iteration with snapshot iteration using PyDict_Items to avoid crashes when the input dict is mutated concurrently under free-threading.
Adds a regression test.