Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8c72eae

Browse files
committed
SF patch #1056967, changes the semantics of Template.safe_substitute() to not
raise a ValueError for dangling delimiters (the delimiter itself is returned).
1 parent 4590c00 commit 8c72eae

4 files changed

Lines changed: 24 additions & 13 deletions

File tree

Doc/lib/libstring.tex

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,16 @@ \subsection{Template strings}
131131
Like \method{substitute()}, except that if placeholders are missing from
132132
\var{mapping} and \var{kws}, instead of raising a \exception{KeyError}
133133
exception, the original placeholder will appear in the resulting string
134-
intact. Note that other exceptions may still be raised, including
135-
\exception{ValueError} as described above.
134+
intact. Also, unlike with \method{substitute()}, any other appearances of the
135+
\samp{\$} will simply return \samp{\$} instead of raising
136+
\exception{ValueError}.
137+
138+
While other exceptions may still occur, this method is called ``safe'' because
139+
substitutions always tries to return a usable string instead of raising an
140+
exception. In another sense, \method{safe_substitute()} may be anything other
141+
than safe, since it will silently ignore malformed templates containing
142+
dangling delimiters, unmatched braces, or placeholders that are not valid
143+
Python identifiers.
136144
\end{methoddesc}
137145

138146
\class{Template} instances also provide one public data attribute:

Lib/string.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def convert(mo):
199199
if mo.group('escaped') is not None:
200200
return self.delimiter
201201
if mo.group('invalid') is not None:
202-
self._invalid(mo)
202+
return self.delimiter
203203
raise ValueError('Unrecognized named group in pattern',
204204
self.pattern)
205205
return self.pattern.sub(convert, self.template)

Lib/test/test_pep292.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,20 +163,19 @@ def test_keyword_arguments_safe(self):
163163
raises(TypeError, s.safe_substitute, d, {})
164164

165165
def test_delimiter_override(self):
166+
eq = self.assertEqual
167+
raises = self.assertRaises
166168
class AmpersandTemplate(Template):
167169
delimiter = '&'
168170
s = AmpersandTemplate('this &gift is for &{who} &&')
169-
self.assertEqual(s.substitute(gift='bud', who='you'),
170-
'this bud is for you &')
171-
self.assertRaises(KeyError, s.substitute)
172-
self.assertEqual(s.safe_substitute(gift='bud', who='you'),
173-
'this bud is for you &')
174-
self.assertEqual(s.safe_substitute(),
175-
'this &gift is for &{who} &')
171+
eq(s.substitute(gift='bud', who='you'), 'this bud is for you &')
172+
raises(KeyError, s.substitute)
173+
eq(s.safe_substitute(gift='bud', who='you'), 'this bud is for you &')
174+
eq(s.safe_substitute(), 'this &gift is for &{who} &')
176175
s = AmpersandTemplate('this &gift is for &{who} &')
177-
self.assertRaises(ValueError, s.substitute,
178-
dict(gift='bud', who='you'))
179-
self.assertRaises(ValueError, s.safe_substitute)
176+
raises(ValueError, s.substitute, dict(gift='bud', who='you'))
177+
eq(s.safe_substitute(), 'this &gift is for &{who} &')
178+
180179

181180
def test_main():
182181
from test import test_support

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ Extension Modules
5656
Library
5757
-------
5858

59+
- Patch #1056967 changes the semantics of Template.safe_substitute() so that
60+
no ValueError is raised on an 'invalid' match group. Now the delimiter is
61+
returned.
62+
5963
- Bug #1052503 pdb.runcall() was not passing along keyword arguments.
6064

6165
- Bug #902037: XML.sax.saxutils.prepare_input_source() now combines relative

0 commit comments

Comments
 (0)