]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
[3.15] gh-153056: Fix a data race compiling the string.Template pattern in free-threa...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 8 Jul 2026 01:40:03 +0000 (03:40 +0200)
committerGitHub <noreply@github.com>
Wed, 8 Jul 2026 01:40:03 +0000 (18:40 -0700)
commite597358779cb965a687ba26422b434d70e3cd201
tree6cab75daf7403e0fcdf6c8e6ab53a2b0c27ac921
parentc30c8a4a65a9d5b0cf711dd761451a3a5128f927
[3.15] gh-153056: Fix a data race compiling the string.Template pattern in free-threading builds (GH-153057) (#153302)

gh-153056: Fix a data race compiling the string.Template pattern in free-threading builds (GH-153057)

* gh-153056: Fix a data race compiling the string.Template pattern in free-threading builds

Template compiles its substitution pattern lazily and caches it on the class.  On the free-threaded build two concurrent first uses could race: a thread that observed the pattern another thread had just compiled would try to recompile it, and re.compile() rejects flags on an already-compiled pattern, raising a spurious ValueError.  Return the already-compiled pattern instead.

As a side effect, a subclass that supplies an already-compiled pattern now works too; previously it raised the same ValueError at class definition.

* Trim test comments and NEWS wording

* Document that the pattern attribute accepts a string or a compiled regex

* Comment the three states of pattern and note the documented-behavior fix in NEWS

* Update Doc/library/string.rst

---------
(cherry picked from commit 45729033bff28f8abc36c42e802cb2853c205737)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
Co-authored-by: Barry Warsaw <barry@python.org>
Doc/library/string.rst
Lib/string/__init__.py
Lib/test/test_free_threading/test_string_template_race.py [new file with mode: 0644]
Lib/test/test_string/test_string.py
Misc/NEWS.d/next/Library/2026-07-05-12-00-00.gh-issue-153056.tMpLat.rst [new file with mode: 0644]