This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: No ValueError for safe_substitute()
Type: Stage:
Components: Library (Lib) Versions: Python 2.4
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: barry Nosy List: barry, ncoghlan, rhettinger
Priority: normal Keywords: patch

Created on 2004-10-29 18:47 by barry, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
pep292-diff.txt barry, 2004-10-29 18:47
Messages (5)
msg47182 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2004-10-29 18:47
I think it's a mistake for Template.safe_substitute()
to raise a ValueError when the invalid group is
matched.  I just ran across such a situation in Real
Code that is using Template.  In my code, I'm using a
subclass to override idpattern and then calling
safe_substitute().  The source of the template is a
flattened mail message where what I'm substituting is
going into some of the header fields.

The body of the message contains a big blurb of XML,
including the string "...$1...".  Well, that $1
triggers the ValueError.

This patch changes the semantics of safe_substitute()
so that when the 'invalid' group matches, the original
delimiter is returned, which I think is the right thing
to do.  Included is an updated test (I will update the
docs too if you agree that this patch should be applied).
msg47183 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2004-10-31 00:13
Logged In: YES 
user_id=1038590

I'd agree with those semantics - if an unrecognised
identifier doesn't trigger a Key Error, I see no reason for
a non-identifier to trigger a Value Error.

For people who want stricter checking - well, that's what
substitute() is for.
msg47184 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-10-31 02:39
Logged In: YES 
user_id=80475

Simplify:
    return '%s' % self.delimiter
To just:
    return self.delimiter

Please add docs and Misc/NEWS entry, then apply.
msg47185 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-10-31 03:46
Logged In: YES 
user_id=80475

The docs should spell-out that the safe in safe_substitute()
means that no exceptions will be raised except possible
memory errors.  In some sense, it is now anything but safe.
 It will silently skip past dangling delimiters, unclosed
braced identifiers such as ${oops, identifiers with
incorrect bracing such $<angled>, and idenfiers that aren't
valid python identifiers such as $1two3.
msg47186 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2004-11-01 03:53
Logged In: YES 
user_id=12800

Committed, with doc updates.
History
Date User Action Args
2022-04-11 14:56:07adminsetgithub: 41102
2004-10-29 18:47:30barrycreate