]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-153056: Fix a data race compiling the string.Template pattern in free-threading...
authortonghuaroot (童话) <tonghuaroot@gmail.com>
Wed, 8 Jul 2026 00:40:54 +0000 (08:40 +0800)
committerGitHub <noreply@github.com>
Wed, 8 Jul 2026 00:40:54 +0000 (00:40 +0000)
commit45729033bff28f8abc36c42e802cb2853c205737
treee8a8776e6130c37b927e12abb592de4626e2818f
parentaa533dce22bf20ba3c1e954aefb245ca1b63543b
gh-153056: Fix a data race compiling the string.Template pattern in free-threading builds (#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

---------

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]