cpython: c2d3d8c3e0bf (original) (raw)
Mercurial > cpython
changeset 101625:c2d3d8c3e0bf
Merge: #27185: move test_pep292 into test_string. [#27185]
R David Murray rdmurray@bitdance.com | |
---|---|
date | Thu, 02 Jun 2016 19:38:20 -0400 |
parents | 7f3ebd86464b(current diff)89abefdebf4d(diff) |
children | 5a4ace14b350 |
files | Lib/test/test_pep292.py Misc/ACKS |
diffstat | 3 files changed, 241 insertions(+), 248 deletions(-)[+] [-] Lib/test/test_pep292.py 248 Lib/test/test_string.py 240 Misc/ACKS 1 |
line wrap: on
line diff
deleted file mode 100644 --- a/Lib/test/test_pep292.py +++ /dev/null @@ -1,248 +0,0 @@ -# Copyright (C) 2004 Python Software Foundation -# Author: barry@python.org (Barry Warsaw) -# License: http://www.opensource.org/licenses/PythonSoftFoundation.php[](#l1.7) - -import unittest -from string import Template - - -class Bag:
- def getitem(self, name):
obj = self[](#l1.18)
for part in name.split('.'):[](#l1.19)
try:[](#l1.20)
obj = getattr(obj, part)[](#l1.21)
except AttributeError:[](#l1.22)
raise KeyError(name)[](#l1.23)
return obj[](#l1.24)
- - -class TestTemplate(unittest.TestCase):
- def test_regular_templates(self):
s = Template('$who likes to eat a bag of <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>w</mi><mi>h</mi><mi>a</mi><mi>t</mi><mi>w</mi><mi>o</mi><mi>r</mi><mi>t</mi><mi>h</mi></mrow><annotation encoding="application/x-tex">what worth </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">ha</span><span class="mord mathnormal" style="margin-right:0.02691em;">tw</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span><span class="mord mathnormal">t</span><span class="mord mathnormal">h</span></span></span></span>$100')[](#l1.29)
self.assertEqual(s.substitute(dict(who='tim', what='ham')),[](#l1.30)
'tim likes to eat a bag of ham worth $100')[](#l1.31)
self.assertRaises(KeyError, s.substitute, dict(who='tim'))[](#l1.32)
self.assertRaises(TypeError, Template.substitute)[](#l1.33)
- def test_regular_templates_with_braces(self):
s = Template('$who likes <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>w</mi><mi>h</mi><mi>a</mi><mi>t</mi></mrow><mi>f</mi><mi>o</mi><mi>r</mi></mrow><annotation encoding="application/x-tex">{what} for </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">ha</span><span class="mord mathnormal">t</span></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span></span></span></span>{meal}')[](#l1.36)
d = dict(who='tim', what='ham', meal='dinner')[](#l1.37)
self.assertEqual(s.substitute(d), 'tim likes ham for dinner')[](#l1.38)
self.assertRaises(KeyError, s.substitute,[](#l1.39)
dict(who='tim', what='ham'))[](#l1.40)
- def test_escapes(self):
eq = self.assertEqual[](#l1.43)
s = Template('$who likes to eat a bag of <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow></mrow><annotation encoding="application/x-tex"></annotation></semantics></math></span><span class="katex-html" aria-hidden="true"></span></span>what worth <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow></mrow><annotation encoding="application/x-tex"></annotation></semantics></math></span><span class="katex-html" aria-hidden="true"></span></span>100')[](#l1.44)
eq(s.substitute(dict(who='tim', what='ham')),[](#l1.45)
'tim likes to eat a bag of <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>w</mi><mi>h</mi><mi>a</mi><mi>t</mi><mi>w</mi><mi>o</mi><mi>r</mi><mi>t</mi><mi>h</mi></mrow><annotation encoding="application/x-tex">what worth </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">ha</span><span class="mord mathnormal" style="margin-right:0.02691em;">tw</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span><span class="mord mathnormal">t</span><span class="mord mathnormal">h</span></span></span></span>100')[](#l1.46)
s = Template('$who likes <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow></mrow><annotation encoding="application/x-tex"></annotation></semantics></math></span><span class="katex-html" aria-hidden="true"></span></span>')[](#l1.47)
eq(s.substitute(dict(who='tim', what='ham')), 'tim likes $')[](#l1.48)
- def test_percents(self):
eq = self.assertEqual[](#l1.51)
s = Template('%(foo)s <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>o</mi><mi>o</mi></mrow><annotation encoding="application/x-tex">foo </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">oo</span></span></span></span>{foo}')[](#l1.52)
d = dict(foo='baz')[](#l1.53)
eq(s.substitute(d), '%(foo)s baz baz')[](#l1.54)
eq(s.safe_substitute(d), '%(foo)s baz baz')[](#l1.55)
- def test_stringification(self):
eq = self.assertEqual[](#l1.58)
s = Template('tim has eaten $count bags of ham today')[](#l1.59)
d = dict(count=7)[](#l1.60)
eq(s.substitute(d), 'tim has eaten 7 bags of ham today')[](#l1.61)
eq(s.safe_substitute(d), 'tim has eaten 7 bags of ham today')[](#l1.62)
s = Template('tim has eaten ${count} bags of ham today')[](#l1.63)
eq(s.substitute(d), 'tim has eaten 7 bags of ham today')[](#l1.64)
- def test_tupleargs(self):
eq = self.assertEqual[](#l1.67)
s = Template('$who ate ${meal}')[](#l1.68)
d = dict(who=('tim', 'fred'), meal=('ham', 'kung pao'))[](#l1.69)
eq(s.substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')")[](#l1.70)
eq(s.safe_substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')")[](#l1.71)
- def test_SafeTemplate(self):
eq = self.assertEqual[](#l1.74)
s = Template('$who likes <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>w</mi><mi>h</mi><mi>a</mi><mi>t</mi></mrow><mi>f</mi><mi>o</mi><mi>r</mi></mrow><annotation encoding="application/x-tex">{what} for </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">ha</span><span class="mord mathnormal">t</span></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span></span></span></span>{meal}')[](#l1.75)
eq(s.safe_substitute(dict(who='tim')), 'tim likes <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>w</mi><mi>h</mi><mi>a</mi><mi>t</mi></mrow><mi>f</mi><mi>o</mi><mi>r</mi></mrow><annotation encoding="application/x-tex">{what} for </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">ha</span><span class="mord mathnormal">t</span></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span></span></span></span>{meal}')[](#l1.76)
eq(s.safe_substitute(dict(what='ham')), '$who likes ham for ${meal}')[](#l1.77)
eq(s.safe_substitute(dict(what='ham', meal='dinner')),[](#l1.78)
'$who likes ham for dinner')[](#l1.79)
eq(s.safe_substitute(dict(who='tim', what='ham')),[](#l1.80)
'tim likes ham for ${meal}')[](#l1.81)
eq(s.safe_substitute(dict(who='tim', what='ham', meal='dinner')),[](#l1.82)
'tim likes ham for dinner')[](#l1.83)
- def test_invalid_placeholders(self):
raises = self.assertRaises[](#l1.86)
s = Template('$who likes $')[](#l1.87)
raises(ValueError, s.substitute, dict(who='tim'))[](#l1.88)
s = Template('$who likes ${what)')[](#l1.89)
raises(ValueError, s.substitute, dict(who='tim'))[](#l1.90)
s = Template('$who likes $100')[](#l1.91)
raises(ValueError, s.substitute, dict(who='tim'))[](#l1.92)
- def test_idpattern_override(self):
class PathPattern(Template):[](#l1.95)
idpattern = r'[_a-z][._a-z0-9]*'[](#l1.96)
m = Mapping()[](#l1.97)
m.bag = Bag()[](#l1.98)
m.bag.foo = Bag()[](#l1.99)
m.bag.foo.who = 'tim'[](#l1.100)
m.bag.what = 'ham'[](#l1.101)
s = PathPattern('$bag.foo.who likes to eat a bag of $bag.what')[](#l1.102)
self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')[](#l1.103)
- def test_pattern_override(self):
class MyPattern(Template):[](#l1.106)
pattern = r"""[](#l1.107)
(?P<escaped>@{2}) |[](#l1.108)
@(?P<named>[_a-z][._a-z0-9]*) |[](#l1.109)
@{(?P<braced>[_a-z][._a-z0-9]*)} |[](#l1.110)
(?P<invalid>@)[](#l1.111)
"""[](#l1.112)
m = Mapping()[](#l1.113)
m.bag = Bag()[](#l1.114)
m.bag.foo = Bag()[](#l1.115)
m.bag.foo.who = 'tim'[](#l1.116)
m.bag.what = 'ham'[](#l1.117)
s = MyPattern('@bag.foo.who likes to eat a bag of @bag.what')[](#l1.118)
self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')[](#l1.119)
class BadPattern(Template):[](#l1.121)
pattern = r"""[](#l1.122)
(?P<badname>.*) |[](#l1.123)
(?P<escaped>@{2}) |[](#l1.124)
@(?P<named>[_a-z][._a-z0-9]*) |[](#l1.125)
@{(?P<braced>[_a-z][._a-z0-9]*)} |[](#l1.126)
(?P<invalid>@) |[](#l1.127)
"""[](#l1.128)
s = BadPattern('@bag.foo.who likes to eat a bag of @bag.what')[](#l1.129)
self.assertRaises(ValueError, s.substitute, {})[](#l1.130)
self.assertRaises(ValueError, s.safe_substitute, {})[](#l1.131)
- def test_braced_override(self):
class MyTemplate(Template):[](#l1.134)
pattern = r"""[](#l1.135)
\$(?:[](#l1.136)
(?P<escaped>$) |[](#l1.137)
(?P<named>[_a-z][_a-z0-9]*) |[](#l1.138)
@@(?P<braced>[_a-z][_a-z0-9]*)@@ |[](#l1.139)
(?P<invalid>) |[](#l1.140)
)[](#l1.141)
"""[](#l1.142)
tmpl = 'PyCon in $@@location@@'[](#l1.144)
t = MyTemplate(tmpl)[](#l1.145)
self.assertRaises(KeyError, t.substitute, {})[](#l1.146)
val = t.substitute({'location': 'Cleveland'})[](#l1.147)
self.assertEqual(val, 'PyCon in Cleveland')[](#l1.148)
- def test_braced_override_safe(self):
class MyTemplate(Template):[](#l1.151)
pattern = r"""[](#l1.152)
\$(?:[](#l1.153)
(?P<escaped>$) |[](#l1.154)
(?P<named>[_a-z][_a-z0-9]*) |[](#l1.155)
@@(?P<braced>[_a-z][_a-z0-9]*)@@ |[](#l1.156)
(?P<invalid>) |[](#l1.157)
)[](#l1.158)
"""[](#l1.159)
tmpl = 'PyCon in $@@location@@'[](#l1.161)
t = MyTemplate(tmpl)[](#l1.162)
self.assertEqual(t.safe_substitute(), tmpl)[](#l1.163)
val = t.safe_substitute({'location': 'Cleveland'})[](#l1.164)
self.assertEqual(val, 'PyCon in Cleveland')[](#l1.165)
- def test_invalid_with_no_lines(self):
# The error formatting for invalid templates[](#l1.168)
# has a special case for no data that the default[](#l1.169)
# pattern can't trigger (always has at least '$')[](#l1.170)
# So we craft a pattern that is always invalid[](#l1.171)
# with no leading data.[](#l1.172)
class MyTemplate(Template):[](#l1.173)
pattern = r"""[](#l1.174)
(?P<invalid>) |[](#l1.175)
unreachable([](#l1.176)
(?P<named>) |[](#l1.177)
(?P<braced>) |[](#l1.178)
(?P<escaped>)[](#l1.179)
)[](#l1.180)
"""[](#l1.181)
s = MyTemplate('')[](#l1.182)
with self.assertRaises(ValueError) as err:[](#l1.183)
s.substitute({})[](#l1.184)
self.assertIn('line 1, col 1', str(err.exception))[](#l1.185)
- def test_unicode_values(self):
s = Template('$who likes $what')[](#l1.188)
d = dict(who='t\xffm', what='f\xfe\fed')[](#l1.189)
self.assertEqual(s.substitute(d), 't\xffm likes f\xfe\x0ced')[](#l1.190)
- def test_keyword_arguments(self):
eq = self.assertEqual[](#l1.193)
s = Template('$who likes $what')[](#l1.194)
eq(s.substitute(who='tim', what='ham'), 'tim likes ham')[](#l1.195)
eq(s.substitute(dict(who='tim'), what='ham'), 'tim likes ham')[](#l1.196)
eq(s.substitute(dict(who='fred', what='kung pao'),[](#l1.197)
who='tim', what='ham'),[](#l1.198)
'tim likes ham')[](#l1.199)
s = Template('the mapping is $mapping')[](#l1.200)
eq(s.substitute(dict(foo='none'), mapping='bozo'),[](#l1.201)
'the mapping is bozo')[](#l1.202)
eq(s.substitute(dict(mapping='one'), mapping='two'),[](#l1.203)
'the mapping is two')[](#l1.204)
s = Template('the self is $self')[](#l1.206)
eq(s.substitute(self='bozo'), 'the self is bozo')[](#l1.207)
- def test_keyword_arguments_safe(self):
eq = self.assertEqual[](#l1.210)
raises = self.assertRaises[](#l1.211)
s = Template('$who likes $what')[](#l1.212)
eq(s.safe_substitute(who='tim', what='ham'), 'tim likes ham')[](#l1.213)
eq(s.safe_substitute(dict(who='tim'), what='ham'), 'tim likes ham')[](#l1.214)
eq(s.safe_substitute(dict(who='fred', what='kung pao'),[](#l1.215)
who='tim', what='ham'),[](#l1.216)
'tim likes ham')[](#l1.217)
s = Template('the mapping is $mapping')[](#l1.218)
eq(s.safe_substitute(dict(foo='none'), mapping='bozo'),[](#l1.219)
'the mapping is bozo')[](#l1.220)
eq(s.safe_substitute(dict(mapping='one'), mapping='two'),[](#l1.221)
'the mapping is two')[](#l1.222)
d = dict(mapping='one')[](#l1.223)
raises(TypeError, s.substitute, d, {})[](#l1.224)
raises(TypeError, s.safe_substitute, d, {})[](#l1.225)
s = Template('the self is $self')[](#l1.227)
eq(s.safe_substitute(self='bozo'), 'the self is bozo')[](#l1.228)
- def test_delimiter_override(self):
eq = self.assertEqual[](#l1.231)
raises = self.assertRaises[](#l1.232)
class AmpersandTemplate(Template):[](#l1.233)
delimiter = '&'[](#l1.234)
s = AmpersandTemplate('this &gift is for &{who} &&')[](#l1.235)
eq(s.substitute(gift='bud', who='you'), 'this bud is for you &')[](#l1.236)
raises(KeyError, s.substitute)[](#l1.237)
eq(s.safe_substitute(gift='bud', who='you'), 'this bud is for you &')[](#l1.238)
eq(s.safe_substitute(), 'this &gift is for &{who} &')[](#l1.239)
s = AmpersandTemplate('this &gift is for &{who} &')[](#l1.240)
raises(ValueError, s.substitute, dict(gift='bud', who='you'))[](#l1.241)
eq(s.safe_substitute(), 'this &gift is for &{who} &')[](#l1.242)
class PieDelims(Template):[](#l1.244)
delimiter = '@'[](#l1.245)
s = PieDelims('@who likes to eat a bag of @{what} worth $100')[](#l1.246)
self.assertEqual(s.substitute(dict(who='tim', what='ham')),[](#l1.247)
'tim likes to eat a bag of ham worth $100')[](#l1.248)
--- a/Lib/test/test_string.py +++ b/Lib/test/test_string.py @@ -187,5 +187,245 @@ class ModuleTest(unittest.TestCase): self.assertIn("recursion", str(err.exception)) +# Template tests (formerly housed in test_pep292.py) + +class Bag:
- def getitem(self, name):
obj = self[](#l2.14)
for part in name.split('.'):[](#l2.15)
try:[](#l2.16)
obj = getattr(obj, part)[](#l2.17)
except AttributeError:[](#l2.18)
raise KeyError(name)[](#l2.19)
return obj[](#l2.20)
+ + +class TestTemplate(unittest.TestCase):
- def test_regular_templates(self):
s = string.Template('$who likes to eat a bag of <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>w</mi><mi>h</mi><mi>a</mi><mi>t</mi><mi>w</mi><mi>o</mi><mi>r</mi><mi>t</mi><mi>h</mi></mrow><annotation encoding="application/x-tex">what worth </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">ha</span><span class="mord mathnormal" style="margin-right:0.02691em;">tw</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span><span class="mord mathnormal">t</span><span class="mord mathnormal">h</span></span></span></span>$100')[](#l2.25)
self.assertEqual(s.substitute(dict(who='tim', what='ham')),[](#l2.26)
'tim likes to eat a bag of ham worth $100')[](#l2.27)
self.assertRaises(KeyError, s.substitute, dict(who='tim'))[](#l2.28)
self.assertRaises(TypeError, string.Template.substitute)[](#l2.29)
- def test_regular_templates_with_braces(self):
s = string.Template('$who likes <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>w</mi><mi>h</mi><mi>a</mi><mi>t</mi></mrow><mi>f</mi><mi>o</mi><mi>r</mi></mrow><annotation encoding="application/x-tex">{what} for </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">ha</span><span class="mord mathnormal">t</span></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span></span></span></span>{meal}')[](#l2.32)
d = dict(who='tim', what='ham', meal='dinner')[](#l2.33)
self.assertEqual(s.substitute(d), 'tim likes ham for dinner')[](#l2.34)
self.assertRaises(KeyError, s.substitute,[](#l2.35)
dict(who='tim', what='ham'))[](#l2.36)
- def test_escapes(self):
eq = self.assertEqual[](#l2.39)
s = string.Template('$who likes to eat a bag of <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow></mrow><annotation encoding="application/x-tex"></annotation></semantics></math></span><span class="katex-html" aria-hidden="true"></span></span>what worth <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow></mrow><annotation encoding="application/x-tex"></annotation></semantics></math></span><span class="katex-html" aria-hidden="true"></span></span>100')[](#l2.40)
eq(s.substitute(dict(who='tim', what='ham')),[](#l2.41)
'tim likes to eat a bag of <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>w</mi><mi>h</mi><mi>a</mi><mi>t</mi><mi>w</mi><mi>o</mi><mi>r</mi><mi>t</mi><mi>h</mi></mrow><annotation encoding="application/x-tex">what worth </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">ha</span><span class="mord mathnormal" style="margin-right:0.02691em;">tw</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span><span class="mord mathnormal">t</span><span class="mord mathnormal">h</span></span></span></span>100')[](#l2.42)
s = string.Template('$who likes <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow></mrow><annotation encoding="application/x-tex"></annotation></semantics></math></span><span class="katex-html" aria-hidden="true"></span></span>')[](#l2.43)
eq(s.substitute(dict(who='tim', what='ham')), 'tim likes $')[](#l2.44)
- def test_percents(self):
eq = self.assertEqual[](#l2.47)
s = string.Template('%(foo)s <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>o</mi><mi>o</mi></mrow><annotation encoding="application/x-tex">foo </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">oo</span></span></span></span>{foo}')[](#l2.48)
d = dict(foo='baz')[](#l2.49)
eq(s.substitute(d), '%(foo)s baz baz')[](#l2.50)
eq(s.safe_substitute(d), '%(foo)s baz baz')[](#l2.51)
- def test_stringification(self):
eq = self.assertEqual[](#l2.54)
s = string.Template('tim has eaten $count bags of ham today')[](#l2.55)
d = dict(count=7)[](#l2.56)
eq(s.substitute(d), 'tim has eaten 7 bags of ham today')[](#l2.57)
eq(s.safe_substitute(d), 'tim has eaten 7 bags of ham today')[](#l2.58)
s = string.Template('tim has eaten ${count} bags of ham today')[](#l2.59)
eq(s.substitute(d), 'tim has eaten 7 bags of ham today')[](#l2.60)
- def test_tupleargs(self):
eq = self.assertEqual[](#l2.63)
s = string.Template('$who ate ${meal}')[](#l2.64)
d = dict(who=('tim', 'fred'), meal=('ham', 'kung pao'))[](#l2.65)
eq(s.substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')")[](#l2.66)
eq(s.safe_substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')")[](#l2.67)
- def test_SafeTemplate(self):
eq = self.assertEqual[](#l2.70)
s = string.Template('$who likes <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>w</mi><mi>h</mi><mi>a</mi><mi>t</mi></mrow><mi>f</mi><mi>o</mi><mi>r</mi></mrow><annotation encoding="application/x-tex">{what} for </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">ha</span><span class="mord mathnormal">t</span></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span></span></span></span>{meal}')[](#l2.71)
eq(s.safe_substitute(dict(who='tim')), 'tim likes <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>w</mi><mi>h</mi><mi>a</mi><mi>t</mi></mrow><mi>f</mi><mi>o</mi><mi>r</mi></mrow><annotation encoding="application/x-tex">{what} for </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">ha</span><span class="mord mathnormal">t</span></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span></span></span></span>{meal}')[](#l2.72)
eq(s.safe_substitute(dict(what='ham')), '$who likes ham for ${meal}')[](#l2.73)
eq(s.safe_substitute(dict(what='ham', meal='dinner')),[](#l2.74)
'$who likes ham for dinner')[](#l2.75)
eq(s.safe_substitute(dict(who='tim', what='ham')),[](#l2.76)
'tim likes ham for ${meal}')[](#l2.77)
eq(s.safe_substitute(dict(who='tim', what='ham', meal='dinner')),[](#l2.78)
'tim likes ham for dinner')[](#l2.79)
- def test_invalid_placeholders(self):
raises = self.assertRaises[](#l2.82)
s = string.Template('$who likes $')[](#l2.83)
raises(ValueError, s.substitute, dict(who='tim'))[](#l2.84)
s = string.Template('$who likes ${what)')[](#l2.85)
raises(ValueError, s.substitute, dict(who='tim'))[](#l2.86)
s = string.Template('$who likes $100')[](#l2.87)
raises(ValueError, s.substitute, dict(who='tim'))[](#l2.88)
- def test_idpattern_override(self):
class PathPattern(string.Template):[](#l2.91)
idpattern = r'[_a-z][._a-z0-9]*'[](#l2.92)
m = Mapping()[](#l2.93)
m.bag = Bag()[](#l2.94)
m.bag.foo = Bag()[](#l2.95)
m.bag.foo.who = 'tim'[](#l2.96)
m.bag.what = 'ham'[](#l2.97)
s = PathPattern('$bag.foo.who likes to eat a bag of $bag.what')[](#l2.98)
self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')[](#l2.99)
- def test_pattern_override(self):
class MyPattern(string.Template):[](#l2.102)
pattern = r"""[](#l2.103)
(?P<escaped>@{2}) |[](#l2.104)
@(?P<named>[_a-z][._a-z0-9]*) |[](#l2.105)
@{(?P<braced>[_a-z][._a-z0-9]*)} |[](#l2.106)
(?P<invalid>@)[](#l2.107)
"""[](#l2.108)
m = Mapping()[](#l2.109)
m.bag = Bag()[](#l2.110)
m.bag.foo = Bag()[](#l2.111)
m.bag.foo.who = 'tim'[](#l2.112)
m.bag.what = 'ham'[](#l2.113)
s = MyPattern('@bag.foo.who likes to eat a bag of @bag.what')[](#l2.114)
self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')[](#l2.115)
class BadPattern(string.Template):[](#l2.117)
pattern = r"""[](#l2.118)
(?P<badname>.*) |[](#l2.119)
(?P<escaped>@{2}) |[](#l2.120)
@(?P<named>[_a-z][._a-z0-9]*) |[](#l2.121)
@{(?P<braced>[_a-z][._a-z0-9]*)} |[](#l2.122)
(?P<invalid>@) |[](#l2.123)
"""[](#l2.124)
s = BadPattern('@bag.foo.who likes to eat a bag of @bag.what')[](#l2.125)
self.assertRaises(ValueError, s.substitute, {})[](#l2.126)
self.assertRaises(ValueError, s.safe_substitute, {})[](#l2.127)
- def test_braced_override(self):
class MyTemplate(string.Template):[](#l2.130)
pattern = r"""[](#l2.131)
\$(?:[](#l2.132)
(?P<escaped>$) |[](#l2.133)
(?P<named>[_a-z][_a-z0-9]*) |[](#l2.134)
@@(?P<braced>[_a-z][_a-z0-9]*)@@ |[](#l2.135)
(?P<invalid>) |[](#l2.136)
)[](#l2.137)
"""[](#l2.138)
tmpl = 'PyCon in $@@location@@'[](#l2.140)
t = MyTemplate(tmpl)[](#l2.141)
self.assertRaises(KeyError, t.substitute, {})[](#l2.142)
val = t.substitute({'location': 'Cleveland'})[](#l2.143)
self.assertEqual(val, 'PyCon in Cleveland')[](#l2.144)
- def test_braced_override_safe(self):
class MyTemplate(string.Template):[](#l2.147)
pattern = r"""[](#l2.148)
\$(?:[](#l2.149)
(?P<escaped>$) |[](#l2.150)
(?P<named>[_a-z][_a-z0-9]*) |[](#l2.151)
@@(?P<braced>[_a-z][_a-z0-9]*)@@ |[](#l2.152)
(?P<invalid>) |[](#l2.153)
)[](#l2.154)
"""[](#l2.155)
tmpl = 'PyCon in $@@location@@'[](#l2.157)
t = MyTemplate(tmpl)[](#l2.158)
self.assertEqual(t.safe_substitute(), tmpl)[](#l2.159)
val = t.safe_substitute({'location': 'Cleveland'})[](#l2.160)
self.assertEqual(val, 'PyCon in Cleveland')[](#l2.161)
- def test_invalid_with_no_lines(self):
# The error formatting for invalid templates[](#l2.164)
# has a special case for no data that the default[](#l2.165)
# pattern can't trigger (always has at least '$')[](#l2.166)
# So we craft a pattern that is always invalid[](#l2.167)
# with no leading data.[](#l2.168)
class MyTemplate(string.Template):[](#l2.169)
pattern = r"""[](#l2.170)
(?P<invalid>) |[](#l2.171)
unreachable([](#l2.172)
(?P<named>) |[](#l2.173)
(?P<braced>) |[](#l2.174)
(?P<escaped>)[](#l2.175)
)[](#l2.176)
"""[](#l2.177)
s = MyTemplate('')[](#l2.178)
with self.assertRaises(ValueError) as err:[](#l2.179)
s.substitute({})[](#l2.180)
self.assertIn('line 1, col 1', str(err.exception))[](#l2.181)
- def test_unicode_values(self):
s = string.Template('$who likes $what')[](#l2.184)
d = dict(who='t\xffm', what='f\xfe\fed')[](#l2.185)
self.assertEqual(s.substitute(d), 't\xffm likes f\xfe\x0ced')[](#l2.186)
- def test_keyword_arguments(self):
eq = self.assertEqual[](#l2.189)
s = string.Template('$who likes $what')[](#l2.190)
eq(s.substitute(who='tim', what='ham'), 'tim likes ham')[](#l2.191)
eq(s.substitute(dict(who='tim'), what='ham'), 'tim likes ham')[](#l2.192)
eq(s.substitute(dict(who='fred', what='kung pao'),[](#l2.193)
who='tim', what='ham'),[](#l2.194)
'tim likes ham')[](#l2.195)
s = string.Template('the mapping is $mapping')[](#l2.196)
eq(s.substitute(dict(foo='none'), mapping='bozo'),[](#l2.197)
'the mapping is bozo')[](#l2.198)
eq(s.substitute(dict(mapping='one'), mapping='two'),[](#l2.199)
'the mapping is two')[](#l2.200)
s = string.Template('the self is $self')[](#l2.202)
eq(s.substitute(self='bozo'), 'the self is bozo')[](#l2.203)
- def test_keyword_arguments_safe(self):
eq = self.assertEqual[](#l2.206)
raises = self.assertRaises[](#l2.207)
s = string.Template('$who likes $what')[](#l2.208)
eq(s.safe_substitute(who='tim', what='ham'), 'tim likes ham')[](#l2.209)
eq(s.safe_substitute(dict(who='tim'), what='ham'), 'tim likes ham')[](#l2.210)
eq(s.safe_substitute(dict(who='fred', what='kung pao'),[](#l2.211)
who='tim', what='ham'),[](#l2.212)
'tim likes ham')[](#l2.213)
s = string.Template('the mapping is $mapping')[](#l2.214)
eq(s.safe_substitute(dict(foo='none'), mapping='bozo'),[](#l2.215)
'the mapping is bozo')[](#l2.216)
eq(s.safe_substitute(dict(mapping='one'), mapping='two'),[](#l2.217)
'the mapping is two')[](#l2.218)
d = dict(mapping='one')[](#l2.219)
raises(TypeError, s.substitute, d, {})[](#l2.220)
raises(TypeError, s.safe_substitute, d, {})[](#l2.221)
s = string.Template('the self is $self')[](#l2.223)
eq(s.safe_substitute(self='bozo'), 'the self is bozo')[](#l2.224)
- def test_delimiter_override(self):
eq = self.assertEqual[](#l2.227)
raises = self.assertRaises[](#l2.228)
class AmpersandTemplate(string.Template):[](#l2.229)
delimiter = '&'[](#l2.230)
s = AmpersandTemplate('this &gift is for &{who} &&')[](#l2.231)
eq(s.substitute(gift='bud', who='you'), 'this bud is for you &')[](#l2.232)
raises(KeyError, s.substitute)[](#l2.233)
eq(s.safe_substitute(gift='bud', who='you'), 'this bud is for you &')[](#l2.234)
eq(s.safe_substitute(), 'this &gift is for &{who} &')[](#l2.235)
s = AmpersandTemplate('this &gift is for &{who} &')[](#l2.236)
raises(ValueError, s.substitute, dict(gift='bud', who='you'))[](#l2.237)
eq(s.safe_substitute(), 'this &gift is for &{who} &')[](#l2.238)
class PieDelims(string.Template):[](#l2.240)
delimiter = '@'[](#l2.241)
s = PieDelims('@who likes to eat a bag of @{what} worth $100')[](#l2.242)
self.assertEqual(s.substitute(dict(who='tim', what='ham')),[](#l2.243)
'tim likes to eat a bag of ham worth $100')[](#l2.244)
+ + if name == "main": unittest.main()
--- a/Misc/ACKS +++ b/Misc/ACKS @@ -176,6 +176,7 @@ Monty Brandenberg Georg Brandl Christopher Brannon Terrence Brannon +Erin Braswell Sven Brauch Germán M. Bravo Erik Bray