]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
[3.14] 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 21:43:54 +0000 (23:43 +0200)
committerGitHub <noreply@github.com>
Wed, 8 Jul 2026 21:43:54 +0000 (14:43 -0700)
commit6107e915526f9bf162452e98ce043c2df0db712c
treeb75bdc5799643661cd3c46fea65cffa00f1e7fbc
parent6241d0d46bbf36d2c456e74e24d78076a743ca7d
[3.14] gh-153056: Fix a data race compiling the string.Template pattern in free-threading builds (GH-153057) (#153303)

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]