← 输入框|github.com|刷新
Skip to content

gh-145142: Make str.maketrans safe under free-threading - #145157

Merged
colesbury merged 13 commits into
python:mainfrom
VanshAgarwal24036:gh-145142-dict-next-critical-section
Feb 27, 2026
Merged

colesbury merged 13 commits into
python:mainfrom
VanshAgarwal24036:gh-145142-dict-next-critical-section

Conversation

@VanshAgarwal24036

@VanshAgarwal24036 VanshAgarwal24036 commented Feb 23, 2026 •

Copy link
Copy Markdown
Contributor

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.

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments below

Comment thread Lib/test/test_free_threading/test_maketrans_threading.py Outdated
Comment thread Objects/unicodeobject.c Outdated
Comment thread Misc/NEWS.d/next/Core_and_Builtins/2026-02-23-23-18-28.gh-issue-145142.T-XbVe.rst Outdated
Comment thread Lib/test/test_free_threading/test_maketrans_threading.py Outdated

ghost left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest isolating the loop in a separate function so that we do not have those weird labels jumps and confusing names but this should be benchmarked on PGO/LTO tocheck that we do not introduce an overhead (though I am not that worried as str.maketrans() is usually not a hot function).

Comment thread Objects/unicodeobject.c Outdated
Comment thread Objects/unicodeobject.c Outdated
@bedevere-app

ghost commented Feb 24, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

ghost left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bunch of syntax errors you need to fix as well.

I also agree with picnixz’s comments

@VanshAgarwal24036
VanshAgarwal24036 marked this pull request as draft February 24, 2026 12:44
@VanshAgarwal24036
VanshAgarwal24036 force-pushed the gh-145142-dict-next-critical-section branch from d487864 to 05ba3f3 Compare February 24, 2026 15:57
@VanshAgarwal24036
VanshAgarwal24036 marked this pull request as ready for review February 24, 2026 16:37
@VanshAgarwal24036
VanshAgarwal24036 force-pushed the gh-145142-dict-next-critical-section branch from 206f6b2 to 4e715b0 Compare February 24, 2026 17:34
@VanshAgarwal24036

ghost commented Feb 24, 2026

Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again.

@bedevere-app

ghost commented Feb 24, 2026

Copy link
Copy Markdown

Thanks for making the requested changes!

@picnixz: please review the changes made to this pull request.

Comment thread Objects/unicodeobject.c Outdated

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

@colesbury colesbury added the needs backport to 3.14 bugs and security fixes label Feb 27, 2026
@colesbury
colesbury enabled auto-merge (squash) February 27, 2026 15:41
Comment thread Objects/unicodeobject.c
}

static int
unicode_maketrans_from_dict(PyObject *x, PyObject *newdict)

ghost Feb 27, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
unicode_maketrans_from_dict(PyObject *x, PyObject *newdict)
unicode_maketrans_from_dict_lock_held(PyObject *x, PyObject *newdict)

Naming convention for methods that require locking.

@colesbury
colesbury merged commit a249795 into python:main Feb 27, 2026
@miss-islington-app

ghost commented Feb 27, 2026

Copy link
Copy Markdown

Thanks @VanshAgarwal24036 for the PR, and @colesbury for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

@bedevere-app

ghost commented Feb 27, 2026

Copy link
Copy Markdown

GH-145320 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Feb 27, 2026
@VanshAgarwal24036
VanshAgarwal24036 deleted the gh-145142-dict-next-critical-section branch February 27, 2026 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants