-
-
Notifications
You must be signed in to change notification settings - Fork 34.8k
gh-153056: Fix a data race compiling the string.Template pattern in free-threading builds #153057
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
+62
−1
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
169fcfc
gh-153056: Fix a data race compiling the string.Template pattern in f…
tonghuaroot 21fd115
Trim test comments and NEWS wording
tonghuaroot 9518fcb
Document that the pattern attribute accepts a string or a compiled regex
tonghuaroot 764fa3f
Comment the three states of pattern and note the documented-behavior …
tonghuaroot ab17838
Merge branch 'main' into template-compile-ft-race
warsaw 09bf724
Update Doc/library/string.rst
warsaw 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import string | ||
| import unittest | ||
| from string import Template | ||
|
|
||
| from test.support import threading_helper | ||
|
|
||
|
|
||
| @threading_helper.requires_working_threading() | ||
| class TestTemplateCompileRace(unittest.TestCase): | ||
| def test_concurrent_first_use(self): | ||
| # Racing the lazy pattern compile must not raise a spurious ValueError | ||
| # from recompiling an already-compiled pattern. A throwaway subclass, | ||
| # re-armed to the sentinel each round, keeps string.Template unmutated | ||
| # (subclasses precompile eagerly in __init_subclass__). | ||
| uncompiled = string._TemplatePattern | ||
| errors = [] | ||
|
|
||
| def use_template(cls): | ||
| try: | ||
| cls("$x and ${y}").substitute(x=1, y=2) | ||
| except Exception as e: | ||
| errors.append(e) | ||
|
|
||
| for _ in range(20): | ||
| class T(Template): | ||
| pass | ||
| T.pattern = uncompiled | ||
| T.flags = None | ||
| threading_helper.run_concurrently(use_template, nthreads=10, args=(T,)) | ||
|
|
||
| self.assertEqual(errors, [], msg=f"unexpected errors: {errors}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Library/2026-07-05-12-00-00.gh-issue-153056.tMpLat.rst
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 |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Fix :class:`string.Template` raising a spurious :exc:`ValueError` when the | ||
| *pattern* attribute is a compiled regular expression object, which the | ||
| documentation allows. On the free-threaded build this also occurred as a data | ||
| race on the first concurrent use. |
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.
Uh oh!
There was an error while loading. Please reload this page.