From ba4279683f8eb8f59be10d12547ea89480614388 Mon Sep 17 00:00:00 2001 From: Barry Warsaw Date: Mon, 4 Sep 2017 16:32:10 -0400 Subject: bpo-1198569: Allow string.Template braced pattern to be different (#3288) * bpo-1198569: Allow the braced pattern to be different ``string.Template`` subclasses can optionally define ``braceidpattern`` if they want to specify different placeholder patterns inside and outside the braces. If None (the default) it falls back to ``idpattern``. --- Lib/string.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'Lib/string.py') diff --git a/Lib/string.py b/Lib/string.py index bc9508c1e6e..b46e60c38f4 100644 --- a/Lib/string.py +++ b/Lib/string.py @@ -57,7 +57,7 @@ class _TemplateMetaclass(type): %(delim)s(?: (?P%(delim)s) | # Escape sequence of two delimiters (?P%(id)s) | # delimiter and a Python identifier - {(?P%(id)s)} | # delimiter and a braced identifier + {(?P%(bid)s)} | # delimiter and a braced identifier (?P) # Other ill-formed delimiter exprs ) """ @@ -70,6 +70,7 @@ class _TemplateMetaclass(type): pattern = _TemplateMetaclass.pattern % { 'delim' : _re.escape(cls.delimiter), 'id' : cls.idpattern, + 'bid' : cls.braceidpattern or cls.idpattern, } cls.pattern = _re.compile(pattern, cls.flags | _re.VERBOSE) @@ -79,6 +80,7 @@ class Template(metaclass=_TemplateMetaclass): delimiter = '$' idpattern = r'[_a-z][_a-z0-9]*' + braceidpattern = None flags = _re.IGNORECASE def __init__(self, template): -- cgit v1.2.3