(original) (raw)

changeset: 92856:8a98ee6baa1e branch: 2.7 parent: 92851:6e2a72e05b4f user: Florent Xicluna florent.xicluna@gmail.com date: Sat Sep 18 23:34:07 2010 +0000 files: Lib/string.py Lib/test/test_pep292.py Misc/NEWS description: Issue #1686: Fix string.Template when overriding the pattern attribute. diff -r 6e2a72e05b4f -r 8a98ee6baa1e Lib/string.py --- a/Lib/string.py Mon Oct 06 16:36:20 2014 +0200 +++ b/Lib/string.py Sat Sep 18 23:34:07 2010 +0000 @@ -182,24 +182,18 @@ 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) diff -r 6e2a72e05b4f -r 8a98ee6baa1e Lib/test/test_pep292.py --- a/Lib/test/test_pep292.py Mon Oct 06 16:36:20 2014 +0200 +++ b/Lib/test/test_pep292.py Sat Sep 18 23:34:07 2010 +0000 @@ -125,6 +125,40 @@ self.assertRaises(ValueError, s.substitute, {}) self.assertRaises(ValueError, s.safe_substitute, {}) + def test_braced_override(self): + class MyTemplate(Template): + pattern = r""" + \$(?: + (?P$) | + (?P[_a-z][_a-z0-9]*) | + @@(?P[_a-z][_a-z0-9]*)@@ | + (?P) | + ) + """ + + tmpl = 'PyCon in @@location@@′+t=MyTemplate(tmpl)+self.assertRaises(KeyError,t.substitute,)+val=t.substitute(′location′:′Cleveland′)+self.assertEqual(val,′PyConinCleveland′)++deftestbracedoverridesafe(self):+classMyTemplate(Template):+pattern=r"""+@@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""" + \@@location@@+t=MyTemplate(tmpl)+self.assertRaises(KeyError,t.substitute,)+val=t.substitute(location:Cleveland)+self.assertEqual(val,PyConinCleveland)++deftestbracedoverridesafe(self):+classMyTemplate(Template):+pattern=r"""+(?: + (?P$) | + (?P[_a-z][_a-z0-9]*) | + @@(?P[_a-z][_a-z0-9]*)@@ | + (?P) | + ) + """ + + tmpl = 'PyCon in @@location@@′+t=MyTemplate(tmpl)+self.assertEqual(t.safesubstitute(),tmpl)+val=t.safesubstitute(′location′:′Cleveland′)+self.assertEqual(val,′PyConinCleveland′)+deftestunicodevalues(self):s=Template(′@@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('@@location@@+t=MyTemplate(tmpl)+self.assertEqual(t.safesubstitute(),tmpl)+val=t.safesubstitute(location:Cleveland)+self.assertEqual(val,PyConinCleveland)+deftestunicodevalues(self):s=Template(who likes $what') d = dict(who=u't\xffm', what=u'f\xfe\fed') diff -r 6e2a72e05b4f -r 8a98ee6baa1e Misc/NEWS --- a/Misc/NEWS Mon Oct 06 16:36:20 2014 +0200 +++ b/Misc/NEWS Sat Sep 18 23:34:07 2010 +0000 @@ -34,6 +34,8 @@ Library ------- +- Issue #1686: Fix string.Template when overriding the pattern attribute. + - Issue #11866: Eliminated race condition in the computation of names for new threads./florent.xicluna@gmail.com