Issue 1686: string.Template.safe_substitute fail when overriding pattern attribute (original) (raw)

Issue1686

Created on 2007-12-22 05:21 by hagaigold, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
string.py.diff hagaigold,2007-12-22 05:21
braced_override.diff dstanek,2008-03-17 21:48
Messages (7)
msg58959 - (view) Author: Hagai Gold (hagaigold) Date: 2007-12-22 05:34
When overriding the pattern attribute on string.Template, with your own delimiters, exmp: change the ..to{..} to ..to@@..@@, safe_substitute method fail to restore the new delimiters, in case of keyerror. Problem is that the method return a specific value ==> """ return self.delimiter + '{' + braced + '}' """ I change it to be generic, with the help of the MatchObject.group() that return the whole match. Demonstration of the problem: >>> from string import Template >>> class MyTemplate(Template): ... pattern = r""" ... \$(?: ... (?P\&) | # Escape sequence of two delimiters ... (?P[_a-z][_a-z0-9]*) # delimiter and a Python identifier ... @@(?P[_a-z][_a-z0-9]*)@@ # delimiter and a braced identifier ... (?P) # Other ill-formed delimiter exprs ... ) ... """ ... >>> b4_str = '$@@keyError@@ change the orignal string' >>> t = MyTemplate(b4_str) >>> res = t.safe_substitute() >>> print res ${keyError} change the orignal string >>> print res == b4_str False >>>
msg63787 - (view) Author: David Stanek (dstanek) Date: 2008-03-17 21:48
I am uploading a new diff that includes the original fix from Hagai along with some tests.
msg112156 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-07-31 15:28
The patch (braced_override.diff) still applies on 3.2. I am +0, because it is small and tested. What is the decision?
msg116822 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010-09-18 18:30
Well what is the decision? FWIW I sway towards the +0 from rather than the -0 from , but then what do I know?
msg116831 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-09-18 22:39
This looks fine.
msg116841 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-09-18 23:35
Committed with r84888.
msg228705 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-10-06 15:38
New changeset 8a98ee6baa1e by Florent Xicluna in branch '2.7': Issue #1686: Fix string.Template when overriding the pattern attribute. https://hg.python.org/cpython/rev/8a98ee6baa1e
History
Date User Action Args
2022-04-11 14:56:29 admin set github: 46027
2014-10-06 15:38:12 python-dev set nosy: + python-devmessages: +
2010-09-18 23:35:18 flox set status: open -> closedmessages: + assignee: floxresolution: accepted -> fixedstage: patch review -> resolved
2010-09-18 22:39:09 rhettinger set assignee: barry -> (no value)resolution: acceptedmessages: +
2010-09-18 22:37:35 rhettinger set messages: -
2010-09-18 18:30:32 BreamoreBoy set nosy: + BreamoreBoymessages: +
2010-07-31 15:28:57 flox set versions: + Python 3.2, - Python 3.0nosy: + floxmessages: + stage: patch review
2008-03-17 21:58:21 rhettinger set nosy: + rhettingermessages: +
2008-03-17 21:56:08 ilan set priority: normalassignee: barrytype: behaviornosy: + barry
2008-03-17 21:48:27 dstanek set files: + braced_override.diffkeywords: + patchmessages: + nosy: + dstanek
2007-12-22 05:34:15 hagaigold set messages: +
2007-12-22 05:21:37 hagaigold create