mirror of
https://github.com/python/cpython.git
synced 2025-08-30 13:38:43 +00:00
At the cost of a modest (but useful in its own right) change in the semantics
of the Template.delimiter attribute, we make use of the delimiter in the escaped group, and in the safe_substitute() method more robust. Now, .delimiter should be the unescaped delimiter literal, e.g. '$' or '&', or whatever. The _TemplateMetaclass will re.escape() this value when it builds the pattern.
This commit is contained in:
parent
2e6fb4634c
commit
17cb60083c
1 changed files with 6 additions and 8 deletions
|
@ -113,7 +113,7 @@ class _TemplateMetaclass(type):
|
||||||
pattern = cls.pattern
|
pattern = cls.pattern
|
||||||
else:
|
else:
|
||||||
pattern = _TemplateMetaclass.pattern % {
|
pattern = _TemplateMetaclass.pattern % {
|
||||||
'delim' : cls.delimiter,
|
'delim' : _re.escape(cls.delimiter),
|
||||||
'id' : cls.idpattern,
|
'id' : cls.idpattern,
|
||||||
}
|
}
|
||||||
cls.pattern = _re.compile(pattern, _re.IGNORECASE | _re.VERBOSE)
|
cls.pattern = _re.compile(pattern, _re.IGNORECASE | _re.VERBOSE)
|
||||||
|
@ -123,7 +123,7 @@ class Template:
|
||||||
"""A string class for supporting $-substitutions."""
|
"""A string class for supporting $-substitutions."""
|
||||||
__metaclass__ = _TemplateMetaclass
|
__metaclass__ = _TemplateMetaclass
|
||||||
|
|
||||||
delimiter = r'\$'
|
delimiter = '$'
|
||||||
idpattern = r'[_a-z][_a-z0-9]*'
|
idpattern = r'[_a-z][_a-z0-9]*'
|
||||||
|
|
||||||
def __init__(self, template):
|
def __init__(self, template):
|
||||||
|
@ -152,7 +152,6 @@ class Template:
|
||||||
mapping = _multimap(kws, args[0])
|
mapping = _multimap(kws, args[0])
|
||||||
else:
|
else:
|
||||||
mapping = args[0]
|
mapping = args[0]
|
||||||
delimiter = self.delimiter[-1]
|
|
||||||
# Helper function for .sub()
|
# Helper function for .sub()
|
||||||
def convert(mo):
|
def convert(mo):
|
||||||
# Check the most common path first.
|
# Check the most common path first.
|
||||||
|
@ -163,7 +162,7 @@ class Template:
|
||||||
# fail if val is a Unicode containing non-ASCII characters.
|
# fail if val is a Unicode containing non-ASCII characters.
|
||||||
return '%s' % val
|
return '%s' % val
|
||||||
if mo.group('escaped') is not None:
|
if mo.group('escaped') is not None:
|
||||||
return delimiter
|
return self.delimiter
|
||||||
if mo.group('invalid') is not None:
|
if mo.group('invalid') is not None:
|
||||||
self._invalid(mo)
|
self._invalid(mo)
|
||||||
raise ValueError('Unrecognized named group in pattern', pattern)
|
raise ValueError('Unrecognized named group in pattern', pattern)
|
||||||
|
@ -178,7 +177,6 @@ class Template:
|
||||||
mapping = _multimap(kws, args[0])
|
mapping = _multimap(kws, args[0])
|
||||||
else:
|
else:
|
||||||
mapping = args[0]
|
mapping = args[0]
|
||||||
delimiter = self.delimiter[-1]
|
|
||||||
# Helper function for .sub()
|
# Helper function for .sub()
|
||||||
def convert(mo):
|
def convert(mo):
|
||||||
named = mo.group('named')
|
named = mo.group('named')
|
||||||
|
@ -188,15 +186,15 @@ class Template:
|
||||||
# will fail if val is a Unicode containing non-ASCII
|
# will fail if val is a Unicode containing non-ASCII
|
||||||
return '%s' % mapping[named]
|
return '%s' % mapping[named]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return delimiter + named
|
return self.delimiter + named
|
||||||
braced = mo.group('braced')
|
braced = mo.group('braced')
|
||||||
if braced is not None:
|
if braced is not None:
|
||||||
try:
|
try:
|
||||||
return '%s' % mapping[braced]
|
return '%s' % mapping[braced]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return delimiter + '{' + braced + '}'
|
return self.delimiter + '{' + braced + '}'
|
||||||
if mo.group('escaped') is not None:
|
if mo.group('escaped') is not None:
|
||||||
return delimiter
|
return self.delimiter
|
||||||
if mo.group('invalid') is not None:
|
if mo.group('invalid') is not None:
|
||||||
self._invalid(mo)
|
self._invalid(mo)
|
||||||
raise ValueError('Unrecognized named group in pattern', pattern)
|
raise ValueError('Unrecognized named group in pattern', pattern)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue