Skip to content

Commit 87be28f

Browse files
bpo-31672: Restore the former behavior when override flags in Template. (#5099)
Overriding flags to 0 will make the default pattern matching only lower case letters.
1 parent 78e24d4 commit 87be28f

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

Doc/library/string.rst

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -755,19 +755,14 @@ attributes:
755755

756756
* *idpattern* -- This is the regular expression describing the pattern for
757757
non-braced placeholders. The default value is the regular expression
758-
``(?a:[_a-zA-Z][_a-zA-Z0-9]*)``. If this is given and *braceidpattern* is
758+
``(?a:[_a-z][_a-z0-9]*)``. If this is given and *braceidpattern* is
759759
``None`` this pattern will also apply to braced placeholders.
760760

761761
.. note::
762762

763763
Since default *flags* is ``re.IGNORECASE``, pattern ``[a-z]`` can match
764764
with some non-ASCII characters. That's why we use the local ``a`` flag
765-
here. Further, with the default *flags* value, including ``A-Z`` in the
766-
ranges is redundant, but required for backward compatibility.
767-
768-
While *flags* is kept to ``re.IGNORECASE`` for backward compatibility,
769-
you can override it to ``0`` or ``re.IGNORECASE | re.ASCII`` when
770-
subclassing.
765+
here.
771766

772767
.. versionchanged:: 3.7
773768
*braceidpattern* can be used to define separate patterns used inside and

Lib/string.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,8 @@ class Template(metaclass=_TemplateMetaclass):
8282
# r'[a-z]' matches to non-ASCII letters when used with IGNORECASE, but
8383
# without the ASCII flag. We can't add re.ASCII to flags because of
8484
# backward compatibility. So we use the ?a local flag and [a-z] pattern.
85-
# We also can't remove the A-Z ranges, because although they are
86-
# technically redundant with the IGNORECASE flag, the value is part of the
87-
# publicly documented API.
8885
# See https://bugs.python.org/issue31672
89-
idpattern = r'(?a:[_a-zA-Z][_a-zA-Z0-9]*)'
86+
idpattern = r'(?a:[_a-z][_a-z0-9]*)'
9087
braceidpattern = None
9188
flags = _re.IGNORECASE
9289

Lib/test/test_string.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,16 @@ def test_regular_templates_with_braces(self):
219219
self.assertRaises(KeyError, s.substitute,
220220
dict(who='tim', what='ham'))
221221

222+
def test_regular_templates_with_upper_case(self):
223+
s = Template('$WHO likes ${WHAT} for ${MEAL}')
224+
d = dict(WHO='tim', WHAT='ham', MEAL='dinner')
225+
self.assertEqual(s.substitute(d), 'tim likes ham for dinner')
226+
227+
def test_regular_templates_with_non_letters(self):
228+
s = Template('$_wh0_ likes ${_w_h_a_t_} for ${mea1}')
229+
d = dict(_wh0_='tim', _w_h_a_t_='ham', mea1='dinner')
230+
self.assertEqual(s.substitute(d), 'tim likes ham for dinner')
231+
222232
def test_escapes(self):
223233
eq = self.assertEqual
224234
s = Template('$who likes to eat a bag of $$what worth $$100')
@@ -288,6 +298,14 @@ class PathPattern(Template):
288298
s = PathPattern('$bag.foo.who likes to eat a bag of $bag.what')
289299
self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')
290300

301+
def test_flags_override(self):
302+
class MyPattern(Template):
303+
flags = 0
304+
s = MyPattern('$wHO likes ${WHAT} for ${meal}')
305+
d = dict(wHO='tim', WHAT='ham', meal='dinner', w='fred')
306+
self.assertRaises(ValueError, s.substitute, d)
307+
self.assertEqual(s.safe_substitute(d), 'fredHO likes ${WHAT} for dinner')
308+
291309
def test_idpattern_override_inside_outside(self):
292310
# bpo-1198569: Allow the regexp inside and outside braces to be
293311
# different when deriving from Template.

0 commit comments

Comments
 (0)