cpython: 8a98ee6baa1e (original) (raw)
Mercurial > cpython
changeset 92856:8a98ee6baa1e 2.7
Issue #1686: Fix string.Template when overriding the pattern attribute. [#1686]
Florent Xicluna florent.xicluna@gmail.com | |
---|---|
date | Sat, 18 Sep 2010 23:34:07 +0000 |
parents | 6e2a72e05b4f |
children | b6fab5c89ca9 |
files | Lib/string.py Lib/test/test_pep292.py Misc/NEWS |
diffstat | 3 files changed, 39 insertions(+), 9 deletions(-)[+] [-] Lib/string.py 12 Lib/test/test_pep292.py 34 Misc/NEWS 2 |
line wrap: on
line diff
--- a/Lib/string.py +++ b/Lib/string.py @@ -182,24 +182,18 @@ class Template: mapping = args[0] # Helper function for .sub() def convert(mo):
named = mo.group('named')[](#l1.7)
named = mo.group('named') or mo.group('braced')[](#l1.8) if named is not None:[](#l1.9) try:[](#l1.10) # We use this idiom instead of str() because the latter[](#l1.11) # will fail if val is a Unicode containing non-ASCII[](#l1.12) return '%s' % (mapping[named],)[](#l1.13) except KeyError:[](#l1.14)
return self.delimiter + named[](#l1.15)
braced = mo.group('braced')[](#l1.16)
if braced is not None:[](#l1.17)
try:[](#l1.18)
return '%s' % (mapping[braced],)[](#l1.19)
except KeyError:[](#l1.20)
return self.delimiter + '{' + braced + '}'[](#l1.21)
return mo.group()[](#l1.22) if mo.group('escaped') is not None:[](#l1.23) return self.delimiter[](#l1.24) if mo.group('invalid') is not None:[](#l1.25)
return self.delimiter[](#l1.26)
return mo.group()[](#l1.27) raise ValueError('Unrecognized named group in pattern',[](#l1.28) self.pattern)[](#l1.29) return self.pattern.sub(convert, self.template)[](#l1.30)
--- 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):[](#l2.8)
pattern = r"""[](#l2.9)
\$(?:[](#l2.10)
(?P<escaped>$) |[](#l2.11)
(?P<named>[_a-z][_a-z0-9]*) |[](#l2.12)
@@(?P<braced>[_a-z][_a-z0-9]*)@@ |[](#l2.13)
(?P<invalid>) |[](#l2.14)
)[](#l2.15)
"""[](#l2.16)
tmpl = 'PyCon in $@@location@@'[](#l2.18)
t = MyTemplate(tmpl)[](#l2.19)
self.assertRaises(KeyError, t.substitute, {})[](#l2.20)
val = t.substitute({'location': 'Cleveland'})[](#l2.21)
self.assertEqual(val, 'PyCon in Cleveland')[](#l2.22)
- def test_braced_override_safe(self):
class MyTemplate(Template):[](#l2.25)
pattern = r"""[](#l2.26)
\$(?:[](#l2.27)
(?P<escaped>$) |[](#l2.28)
(?P<named>[_a-z][_a-z0-9]*) |[](#l2.29)
@@(?P<braced>[_a-z][_a-z0-9]*)@@ |[](#l2.30)
(?P<invalid>) |[](#l2.31)
)[](#l2.32)
"""[](#l2.33)
tmpl = 'PyCon in $@@location@@'[](#l2.35)
t = MyTemplate(tmpl)[](#l2.36)
self.assertEqual(t.safe_substitute(), tmpl)[](#l2.37)
val = t.safe_substitute({'location': 'Cleveland'})[](#l2.38)
self.assertEqual(val, 'PyCon in Cleveland')[](#l2.39)
+ def test_unicode_values(self): s = Template('$who likes $what') d = dict(who=u't\xffm', what=u'f\xfe\fed')
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -34,6 +34,8 @@ Core and Builtins Library ------- +- Issue #1686: Fix string.Template when overriding the pattern attribute. +