FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

#6630: allow customizing flags for compiling string.Template.idpattern. · python/cpython@056cb93 · GitHub

Repository navigation

Commit 056cb93

Browse files
committed
#6630: allow customizing flags for compiling string.Template.idpattern.
1 parent 1cec3e3 commit 056cb93

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎Doc/library/string.rst‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,14 @@ to parse template strings. To do this, you can override these class attributes:
710710
appropriate). The default value is the regular expression
711711
``[_a-z][_a-z0-9]*``.
712712

713+
* *flags* -- The regular expression flags that will be applied when compiling
714+
the regular expression used for recognizing substitutions. The default value
715+
is ``re.IGNORECASE``. Note that ``re.VERBOSE`` will always be added to the
716+
flags, so custom *idpattern*\ s must follow conventions for verbose regular
717+
expressions.
718+
719+
.. versionadded:: 3.2
720+
713721
Alternatively, you can provide the entire regular expression pattern by
714722
overriding the class attribute *pattern*. If you do this, the value must be a
715723
regular expression object with four named capturing groups. The capturing

‎Lib/string.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,15 @@ def __init__(cls, name, bases, dct):
8181
'delim' : _re.escape(cls.delimiter),
8282
'id' : cls.idpattern,
8383
}
84-
cls.pattern = _re.compile(pattern, _re.IGNORECASE | _re.VERBOSE)
84+
cls.pattern = _re.compile(pattern, cls.flags | _re.VERBOSE)
8585

8686

8787
class Template(metaclass=_TemplateMetaclass):
8888
"""A string class for supporting $-substitutions."""
8989

9090
delimiter = '$'
9191
idpattern = r'[_a-z][_a-z0-9]*'
92+
flags = _re.IGNORECASE
9293

9394
def __init__(self, template):
9495
self.template = template

‎Misc/NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,9 @@ C-API
475475
Library
476476
-------
477477

478+
- Issue #6630: Allow customizing regex flags when subclassing the
479+
string.Template class.
480+
478481
- Issue #9411: Allow specifying an encoding for config files in the
479482
configparser module.
480483

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL