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: string.Template.safe_substitute fail when overriding pattern attribute
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: flox Nosy List: BreamoreBoy, barry, dstanek, flox, hagaigold, python-dev, rhettinger
Priority: normal Keywords: patch

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 $@@..@@, 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<escaped>\&) |                  # Escape sequence of two
delimiters
...       (?P<named>[_a-z][_a-z0-9]*)      | # delimiter and a Python
identifier
...       @@(?P<braced>[_a-z][_a-z0-9]*)@@ | # delimiter and a braced
identifier
...       (?P<invalid>)                      # 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 msg112156 rather than the -0 from msg63791, 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:29adminsetgithub: 46027
2014-10-06 15:38:12python-devsetnosy: + python-dev
messages: + msg228705
2010-09-18 23:35:18floxsetstatus: open -> closed
messages: + msg116841

assignee: flox
resolution: accepted -> fixed
stage: patch review -> resolved
2010-09-18 22:39:09rhettingersetassignee: barry -> (no value)
resolution: accepted
messages: + msg116831
2010-09-18 22:37:35rhettingersetmessages: - msg63791
2010-09-18 18:30:32BreamoreBoysetnosy: + BreamoreBoy
messages: + msg116822
2010-07-31 15:28:57floxsetversions: + Python 3.2, - Python 3.0
nosy: + flox

messages: + msg112156

stage: patch review
2008-03-17 21:58:21rhettingersetnosy: + rhettinger
messages: + msg63791
2008-03-17 21:56:08ilansetpriority: normal
assignee: barry
type: behavior
nosy: + barry
2008-03-17 21:48:27dstaneksetfiles: + braced_override.diff
keywords: + patch
messages: + msg63787
nosy: + dstanek
2007-12-22 05:34:15hagaigoldsetmessages: + msg58959
2007-12-22 05:21:37hagaigoldcreate