diff options
author | Florent Xicluna <florent.xicluna@gmail.com> | 2010-09-18 23:34:07 (GMT) |
---|---|---|
committer | Florent Xicluna <florent.xicluna@gmail.com> | 2010-09-18 23:34:07 (GMT) |
commit | eb19dce085de9723678acb8909b403a4c459c86a (patch) | |
tree | 750a297a6ad1b5983411b52c6b16c726fcf09453 /Lib/test/test_pep292.py | |
parent | 98b46702d2798d0db258a02f6a1854dfc5f659fd (diff) | |
download | cpython-eb19dce085de9723678acb8909b403a4c459c86a.zip cpython-eb19dce085de9723678acb8909b403a4c459c86a.tar.gz cpython-eb19dce085de9723678acb8909b403a4c459c86a.tar.bz2 |
Issue #1686: Fix string.Template when overriding the pattern attribute.
Diffstat (limited to 'Lib/test/test_pep292.py')
-rw-r--r-- | Lib/test/test_pep292.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/Lib/test/test_pep292.py b/Lib/test/test_pep292.py index 8537b25..a967649 100644 --- a/Lib/test/test_pep292.py +++ b/Lib/test/test_pep292.py @@ -125,6 +125,40 @@ class TestTemplate(unittest.TestCase): self.assertRaises(ValueError, s.substitute, {}) self.assertRaises(ValueError, s.safe_substitute, {}) + def test_braced_override(self): + class MyTemplate(Template): + pattern = r""" + \$(?: + (?P<escaped>$) | + (?P<named>[_a-z][_a-z0-9]*) | + @@(?P<braced>[_a-z][_a-z0-9]*)@@ | + (?P<invalid>) | + ) + """ + + tmpl = 'PyCon in $@@location@@' + t = MyTemplate(tmpl) + self.assertRaises(KeyError, t.substitute, {}) + val = t.substitute({'location': 'Cleveland'}) + self.assertEqual(val, 'PyCon in Cleveland') + + def test_braced_override_safe(self): + class MyTemplate(Template): + pattern = r""" + \$(?: + (?P<escaped>$) | + (?P<named>[_a-z][_a-z0-9]*) | + @@(?P<braced>[_a-z][_a-z0-9]*)@@ | + (?P<invalid>) | + ) + """ + + tmpl = 'PyCon in $@@location@@' + t = MyTemplate(tmpl) + self.assertEqual(t.safe_substitute(), tmpl) + val = t.safe_substitute({'location': 'Cleveland'}) + self.assertEqual(val, 'PyCon in Cleveland') + def test_unicode_values(self): s = Template('$who likes $what') d = dict(who='t\xffm', what='f\xfe\fed') |