Commit 6eed5371 authored by Barry Warsaw's avatar Barry Warsaw

SF patch #1056967, changes the semantics of Template.safe_substitute() to not

raise a ValueError for dangling delimiters (the delimiter itself is returned).
parent 2841c024
...@@ -131,8 +131,16 @@ precedence. ...@@ -131,8 +131,16 @@ precedence.
Like \method{substitute()}, except that if placeholders are missing from Like \method{substitute()}, except that if placeholders are missing from
\var{mapping} and \var{kws}, instead of raising a \exception{KeyError} \var{mapping} and \var{kws}, instead of raising a \exception{KeyError}
exception, the original placeholder will appear in the resulting string exception, the original placeholder will appear in the resulting string
intact. Note that other exceptions may still be raised, including intact. Also, unlike with \method{substitute()}, any other appearances of the
\exception{ValueError} as described above. \samp{\$} will simply return \samp{\$} instead of raising
\exception{ValueError}.
While other exceptions may still occur, this method is called ``safe'' because
substitutions always tries to return a usable string instead of raising an
exception. In another sense, \method{safe_substitute()} may be anything other
than safe, since it will silently ignore malformed templates containing
dangling delimiters, unmatched braces, or placeholders that are not valid
Python identifiers.
\end{methoddesc} \end{methoddesc}
\class{Template} instances also provide one public data attribute: \class{Template} instances also provide one public data attribute:
......
...@@ -199,7 +199,7 @@ class Template: ...@@ -199,7 +199,7 @@ class Template:
if mo.group('escaped') is not None: if mo.group('escaped') is not None:
return self.delimiter return self.delimiter
if mo.group('invalid') is not None: if mo.group('invalid') is not None:
self._invalid(mo) return self.delimiter
raise ValueError('Unrecognized named group in pattern', raise ValueError('Unrecognized named group in pattern',
self.pattern) self.pattern)
return self.pattern.sub(convert, self.template) return self.pattern.sub(convert, self.template)
......
...@@ -163,20 +163,19 @@ class TestTemplate(unittest.TestCase): ...@@ -163,20 +163,19 @@ class TestTemplate(unittest.TestCase):
raises(TypeError, s.safe_substitute, d, {}) raises(TypeError, s.safe_substitute, d, {})
def test_delimiter_override(self): def test_delimiter_override(self):
eq = self.assertEqual
raises = self.assertRaises
class AmpersandTemplate(Template): class AmpersandTemplate(Template):
delimiter = '&' delimiter = '&'
s = AmpersandTemplate('this &gift is for &{who} &&') s = AmpersandTemplate('this &gift is for &{who} &&')
self.assertEqual(s.substitute(gift='bud', who='you'), eq(s.substitute(gift='bud', who='you'), 'this bud is for you &')
'this bud is for you &') raises(KeyError, s.substitute)
self.assertRaises(KeyError, s.substitute) eq(s.safe_substitute(gift='bud', who='you'), 'this bud is for you &')
self.assertEqual(s.safe_substitute(gift='bud', who='you'), eq(s.safe_substitute(), 'this &gift is for &{who} &')
'this bud is for you &')
self.assertEqual(s.safe_substitute(),
'this &gift is for &{who} &')
s = AmpersandTemplate('this &gift is for &{who} &') s = AmpersandTemplate('this &gift is for &{who} &')
self.assertRaises(ValueError, s.substitute, raises(ValueError, s.substitute, dict(gift='bud', who='you'))
dict(gift='bud', who='you')) eq(s.safe_substitute(), 'this &gift is for &{who} &')
self.assertRaises(ValueError, s.safe_substitute)
def test_main(): def test_main():
from test import test_support from test import test_support
......
...@@ -56,6 +56,10 @@ Extension Modules ...@@ -56,6 +56,10 @@ Extension Modules
Library Library
------- -------
- Patch #1056967 changes the semantics of Template.safe_substitute() so that
no ValueError is raised on an 'invalid' match group. Now the delimiter is
returned.
- Bug #1052503 pdb.runcall() was not passing along keyword arguments. - Bug #1052503 pdb.runcall() was not passing along keyword arguments.
- Bug #902037: XML.sax.saxutils.prepare_input_source() now combines relative - Bug #902037: XML.sax.saxutils.prepare_input_source() now combines relative
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment