]> granicus.if.org Git - python/commitdiff
Issue #1686: Fix string.Template when overriding the pattern attribute.
authorFlorent Xicluna <florent.xicluna@gmail.com>
Sat, 18 Sep 2010 23:34:07 +0000 (23:34 +0000)
committerFlorent Xicluna <florent.xicluna@gmail.com>
Sat, 18 Sep 2010 23:34:07 +0000 (23:34 +0000)
Lib/string.py
Lib/test/test_pep292.py
Misc/NEWS

index 97278034a0fb8209d9602a682885ece73024f0be..d762e040b1123ca7e21c1e248403d02aaf1dd58d 100644 (file)
@@ -182,24 +182,18 @@ class Template:
             mapping = args[0]
         # Helper function for .sub()
         def convert(mo):
-            named = mo.group('named')
+            named = mo.group('named') or mo.group('braced')
             if named is not None:
                 try:
                     # We use this idiom instead of str() because the latter
                     # will fail if val is a Unicode containing non-ASCII
                     return '%s' % (mapping[named],)
                 except KeyError:
-                    return self.delimiter + named
-            braced = mo.group('braced')
-            if braced is not None:
-                try:
-                    return '%s' % (mapping[braced],)
-                except KeyError:
-                    return self.delimiter + '{' + braced + '}'
+                    return mo.group()
             if mo.group('escaped') is not None:
                 return self.delimiter
             if mo.group('invalid') is not None:
-                return self.delimiter
+                return mo.group()
             raise ValueError('Unrecognized named group in pattern',
                              self.pattern)
         return self.pattern.sub(convert, self.template)
index cb8a244dc0e0ece4646623085df7b7800ed1b87b..5ff280d8be98ad09764579060adc20e10edfde89 100644 (file)
@@ -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=u't\xffm', what=u'f\xfe\fed')
index 5144b5082d08952159c9ce29908bbaf744619254..cc9fc76f958e3573da919a9871bea50b90448876 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -34,6 +34,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #1686: Fix string.Template when overriding the pattern attribute.
+
 - Issue #11866: Eliminated race condition in the computation of names
   for new threads.