Commit b5c6b5bf authored by Barry Warsaw's avatar Barry Warsaw

Raymond's good suggestion to re-order the tests in the convert() helper so the

most common paths are tested first.  Also, that 'invalid' is better than
'bogus'.
parent 8ea5bdf7
...@@ -104,7 +104,7 @@ class _TemplateMetaclass(type): ...@@ -104,7 +104,7 @@ class _TemplateMetaclass(type):
(?P<escaped>%(delim)s{2}) | # Escape sequence of two delimiters (?P<escaped>%(delim)s{2}) | # Escape sequence of two delimiters
%(delim)s(?P<named>%(id)s) | # delimiter and a Python identifier %(delim)s(?P<named>%(id)s) | # delimiter and a Python identifier
%(delim)s{(?P<braced>%(id)s)} | # delimiter and a braced identifier %(delim)s{(?P<braced>%(id)s)} | # delimiter and a braced identifier
(?P<bogus>%(delim)s) # Other ill-formed delimiter exprs (?P<invalid>%(delim)s) # Other ill-formed delimiter exprs
""" """
def __init__(cls, name, bases, dct): def __init__(cls, name, bases, dct):
...@@ -131,8 +131,8 @@ class Template: ...@@ -131,8 +131,8 @@ class Template:
# Search for $$, $identifier, ${identifier}, and any bare $'s # Search for $$, $identifier, ${identifier}, and any bare $'s
def _bogus(self, mo): def _invalid(self, mo):
i = mo.start('bogus') i = mo.start('invalid')
lines = self.template[:i].splitlines(True) lines = self.template[:i].splitlines(True)
if not lines: if not lines:
colno = 1 colno = 1
...@@ -154,14 +154,17 @@ class Template: ...@@ -154,14 +154,17 @@ class Template:
mapping = args[0] mapping = args[0]
# Helper function for .sub() # Helper function for .sub()
def convert(mo): def convert(mo):
# Check the most common path first.
named = mo.group('named') or mo.group('braced')
if named is not None:
val = mapping[named]
# We use this idiom instead of str() because the latter will
# fail if val is a Unicode containing non-ASCII characters.
return '%s' % val
if mo.group('escaped') is not None: if mo.group('escaped') is not None:
return '$' return '$'
if mo.group('bogus') is not None: if mo.group('invalid') is not None:
self._bogus(mo) self._invalid(mo)
val = mapping[mo.group('named') or mo.group('braced')]
# We use this idiom instead of str() because the latter will fail
# if val is a Unicode containing non-ASCII characters.
return '%s' % val
return self.pattern.sub(convert, self.template) return self.pattern.sub(convert, self.template)
def safe_substitute(self, *args, **kws): def safe_substitute(self, *args, **kws):
...@@ -175,10 +178,6 @@ class Template: ...@@ -175,10 +178,6 @@ class Template:
mapping = args[0] mapping = args[0]
# Helper function for .sub() # Helper function for .sub()
def convert(mo): def convert(mo):
if mo.group('escaped') is not None:
return '$'
if mo.group('bogus') is not None:
self._bogus(mo)
named = mo.group('named') named = mo.group('named')
if named is not None: if named is not None:
try: try:
...@@ -192,6 +191,10 @@ class Template: ...@@ -192,6 +191,10 @@ class Template:
return '%s' % mapping[braced] return '%s' % mapping[braced]
except KeyError: except KeyError:
return '${' + braced + '}' return '${' + braced + '}'
if mo.group('escaped') is not None:
return '$'
if mo.group('invalid') is not None:
self._invalid(mo)
return self.pattern.sub(convert, self.template) return self.pattern.sub(convert, self.template)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment