Invalid patterns to substitute and safe_substitute would crash since pattern

is not a local variable.  Add a test case.
This commit is contained in:
Neal Norwitz 2004-10-17 16:27:18 +00:00
parent 1338946c7b
commit 6627a96705
2 changed files with 16 additions and 2 deletions

View File

@ -167,7 +167,8 @@ class Template:
return self.delimiter
if mo.group('invalid') is not None:
self._invalid(mo)
raise ValueError('Unrecognized named group in pattern', pattern)
raise ValueError('Unrecognized named group in pattern',
self.pattern)
return self.pattern.sub(convert, self.template)
def safe_substitute(self, *args, **kws):
@ -199,7 +200,8 @@ class Template:
return self.delimiter
if mo.group('invalid') is not None:
self._invalid(mo)
raise ValueError('Unrecognized named group in pattern', pattern)
raise ValueError('Unrecognized named group in pattern',
self.pattern)
return self.pattern.sub(convert, self.template)

View File

@ -113,6 +113,18 @@ class TestTemplate(unittest.TestCase):
s = MyPattern('@bag.foo.who likes to eat a bag of @bag.what')
self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')
class BadPattern(Template):
pattern = r"""
(?P<badname>.*) |
(?P<escaped>@{2}) |
@(?P<named>[_a-z][._a-z0-9]*) |
@{(?P<braced>[_a-z][._a-z0-9]*)} |
(?P<invalid>@) |
"""
s = BadPattern('@bag.foo.who likes to eat a bag of @bag.what')
self.assertRaises(ValueError, s.substitute, {})
self.assertRaises(ValueError, s.safe_substitute, {})
def test_unicode_values(self):
s = Template('$who likes $what')
d = dict(who=u't\xffm', what=u'f\xfe\fed')