-
-
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
Open
tonghuaroot
wants to merge
4
commits into
python:main
Choose a base branch
from
tonghuaroot:template-compile-ft-race
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.
+62
−1
Open
Changes from all commits
Commits
Show all changes
4 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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -299,6 +299,20 @@ def test_SafeTemplate(self): | |
| eq(s.safe_substitute(dict(who='tim', what='ham', meal='dinner')), | ||
| 'tim likes ham for dinner') | ||
|
|
||
| def test_precompiled_pattern(self): | ||
| # A subclass may supply an already-compiled pattern; it must be reused, | ||
| # not recompiled (re.compile() rejects flags on a compiled pattern). | ||
| import re | ||
|
Member
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. It'll be nice when we can get rid of all these and just use lazy imports 😄 |
||
| compiled = re.compile( | ||
| r'\$(?:(?P<escaped>\$)|(?P<named>[a-z]+)|' | ||
| r'\{(?P<braced>[a-z]+)\}|(?P<invalid>))') | ||
| class MyTemplate(Template): | ||
| pattern = compiled | ||
| self.assertIs(MyTemplate.pattern, compiled) | ||
| self.assertEqual( | ||
| MyTemplate('$who likes $what').substitute(who='tim', what='ham'), | ||
| 'tim likes ham') | ||
|
|
||
| def test_invalid_placeholders(self): | ||
| raises = self.assertRaises | ||
| s = Template('$who likes $') | ||
|
|
||
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.
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.
I think I'd add a comment here just above this line, something to the effect of: