]> granicus.if.org Git - python/commitdiff
#6630: allow customizing flags for compiling string.Template.idpattern.
authorGeorg Brandl <georg@python.org>
Thu, 29 Jul 2010 17:16:10 +0000 (17:16 +0000)
committerGeorg Brandl <georg@python.org>
Thu, 29 Jul 2010 17:16:10 +0000 (17:16 +0000)
Doc/library/string.rst
Lib/string.py
Misc/NEWS

index 38c872d8f1a01fb9e1c1605fbb7f25460f5daede..68269b9dd852fa188d52aef674d97f00ba0932a7 100644 (file)
@@ -710,6 +710,14 @@ to parse template strings.  To do this, you can override these class attributes:
   appropriate).  The default value is the regular expression
   ``[_a-z][_a-z0-9]*``.
 
+* *flags* -- The regular expression flags that will be applied when compiling
+  the regular expression used for recognizing substitutions.  The default value
+  is ``re.IGNORECASE``.  Note that ``re.VERBOSE`` will always be added to the
+  flags, so custom *idpattern*\ s must follow conventions for verbose regular
+  expressions.
+
+  .. versionadded:: 3.2
+
 Alternatively, you can provide the entire regular expression pattern by
 overriding the class attribute *pattern*.  If you do this, the value must be a
 regular expression object with four named capturing groups.  The capturing
index 5089193c7d628187180b76611538850af96fac60..defe894414c07e9bcdba01a1c13435c727c1bcaf 100644 (file)
@@ -81,7 +81,7 @@ class _TemplateMetaclass(type):
                 'delim' : _re.escape(cls.delimiter),
                 'id'    : cls.idpattern,
                 }
-        cls.pattern = _re.compile(pattern, _re.IGNORECASE | _re.VERBOSE)
+        cls.pattern = _re.compile(pattern, cls.flags | _re.VERBOSE)
 
 
 class Template(metaclass=_TemplateMetaclass):
@@ -89,6 +89,7 @@ class Template(metaclass=_TemplateMetaclass):
 
     delimiter = '$'
     idpattern = r'[_a-z][_a-z0-9]*'
+    flags = _re.IGNORECASE
 
     def __init__(self, template):
         self.template = template
index 25b479833d19133ec838c8f769264b8ef9095d47..26a55999aad8aa5ca7323405d1ed737e802fa9e0 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -475,6 +475,9 @@ C-API
 Library
 -------
 
+- Issue #6630: Allow customizing regex flags when subclassing the
+  string.Template class.
+
 - Issue #9411: Allow specifying an encoding for config files in the
   configparser module.