Skip to content

Commit 764fa3f

Browse files
committed
Comment the three states of pattern and note the documented-behavior fix in NEWS
1 parent 9518fcb commit 764fa3f

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

Lib/string/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,13 @@ def __init_subclass__(cls):
8383
def _compile_pattern(cls):
8484
import re # deferred import, for performance
8585

86+
# `pattern` may be the `_TemplatePattern` sentinel (not yet compiled), an
87+
# already-compiled regular expression object (as documented), or a string
88+
# regular expression. An already-compiled object is returned as-is; the
89+
# other two are compiled and cached back on the class.
8690
pattern = cls.__dict__.get('pattern', _TemplatePattern)
8791
if isinstance(pattern, re.Pattern):
88-
# Already compiled; reuse it (re.compile() rejects flags on a Pattern).
92+
# re.compile() rejects flags on an already-compiled pattern.
8993
return pattern
9094
if pattern is _TemplatePattern:
9195
delim = re.escape(cls.delimiter)
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
Fix a race in :class:`string.Template` where concurrent first use on the
2-
free-threaded build could raise a spurious :exc:`ValueError`.
1+
Fix :class:`string.Template` raising a spurious :exc:`ValueError` when the
2+
*pattern* attribute is a compiled regular expression object, which the
3+
documentation allows. On the free-threaded build this also occurred as a data
4+
race on the first concurrent use.

0 commit comments

Comments
 (0)