From 6627a9670541f33ae55f6ba8df6b6ce7264c3f34 Mon Sep 17 00:00:00 2001 From: Neal Norwitz Date: Sun, 17 Oct 2004 16:27:18 +0000 Subject: Invalid patterns to substitute and safe_substitute would crash since pattern is not a local variable. Add a test case. --- Lib/string.py | 6 ++++-- Lib/test/test_pep292.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Lib/string.py b/Lib/string.py index 7371c91..e10087e 100644 --- a/Lib/string.py +++ b/Lib/string.py @@ -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) diff --git a/Lib/test/test_pep292.py b/Lib/test/test_pep292.py index f955774..19952e4 100644 --- a/Lib/test/test_pep292.py +++ b/Lib/test/test_pep292.py @@ -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.*) | + (?P@{2}) | + @(?P[_a-z][._a-z0-9]*) | + @{(?P[_a-z][._a-z0-9]*)} | + (?P@) | + """ + 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') -- cgit v0.12