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

Skip to content

Commit 8ffe917

Browse files
Issue #23671: string.Template now allows to specify the "self" parameter as
keyword argument. string.Formatter now allows to specify the "self" and the "format_string" parameters as keyword arguments.
1 parent be1eb14 commit 8ffe917

4 files changed

Lines changed: 48 additions & 3 deletions

File tree

Lib/string.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ def _invalid(self, mo):
9494
raise ValueError('Invalid placeholder in string: line %d, col %d' %
9595
(lineno, colno))
9696

97-
def substitute(self, *args, **kws):
97+
def substitute(*args, **kws):
98+
if not args:
99+
raise TypeError("descriptor 'substitute' of 'Template' object "
100+
"needs an argument")
101+
self, *args = args # allow the "self" keyword be passed
98102
if len(args) > 1:
99103
raise TypeError('Too many positional arguments')
100104
if not args:
@@ -120,7 +124,11 @@ def convert(mo):
120124
self.pattern)
121125
return self.pattern.sub(convert, self.template)
122126

123-
def safe_substitute(self, *args, **kws):
127+
def safe_substitute(*args, **kws):
128+
if not args:
129+
raise TypeError("descriptor 'safe_substitute' of 'Template' object "
130+
"needs an argument")
131+
self, *args = args # allow the "self" keyword be passed
124132
if len(args) > 1:
125133
raise TypeError('Too many positional arguments')
126134
if not args:
@@ -160,7 +168,19 @@ def convert(mo):
160168
# The field name parser is implemented in _string.formatter_field_name_split
161169

162170
class Formatter:
163-
def format(self, format_string, *args, **kwargs):
171+
def format(*args, **kwargs):
172+
if not args:
173+
raise TypeError("descriptor 'format' of 'Formatter' object "
174+
"needs an argument")
175+
self, *args = args # allow the "self" keyword be passed
176+
try:
177+
format_string, *args = args # allow the "format_string" keyword be passed
178+
except ValueError:
179+
if 'format_string' in kwargs:
180+
format_string = kwargs.pop('format_string')
181+
else:
182+
raise TypeError("format() missing 1 required positional "
183+
"argument: 'format_string'") from None
164184
return self.vformat(format_string, args, kwargs)
165185

166186
def vformat(self, format_string, args, kwargs):

Lib/test/test_pep292.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def test_regular_templates(self):
2626
self.assertEqual(s.substitute(dict(who='tim', what='ham')),
2727
'tim likes to eat a bag of ham worth $100')
2828
self.assertRaises(KeyError, s.substitute, dict(who='tim'))
29+
self.assertRaises(TypeError, Template.substitute)
2930

3031
def test_regular_templates_with_braces(self):
3132
s = Template('$who likes ${what} for ${meal}')
@@ -198,6 +199,9 @@ def test_keyword_arguments(self):
198199
eq(s.substitute(dict(mapping='one'), mapping='two'),
199200
'the mapping is two')
200201

202+
s = Template('the self is $self')
203+
eq(s.substitute(self='bozo'), 'the self is bozo')
204+
201205
def test_keyword_arguments_safe(self):
202206
eq = self.assertEqual
203207
raises = self.assertRaises
@@ -216,6 +220,9 @@ def test_keyword_arguments_safe(self):
216220
raises(TypeError, s.substitute, d, {})
217221
raises(TypeError, s.safe_substitute, d, {})
218222

223+
s = Template('the self is $self')
224+
eq(s.safe_substitute(self='bozo'), 'the self is bozo')
225+
219226
def test_delimiter_override(self):
220227
eq = self.assertEqual
221228
raises = self.assertRaises

Lib/test/test_string.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ def test_basic_formatter(self):
3131
self.assertEqual(fmt.format("foo"), "foo")
3232
self.assertEqual(fmt.format("foo{0}", "bar"), "foobar")
3333
self.assertEqual(fmt.format("foo{1}{0}-{1}", "bar", 6), "foo6bar-6")
34+
self.assertRaises(TypeError, fmt.format)
35+
self.assertRaises(TypeError, string.Formatter.format)
36+
37+
def test_format_keyword_arguments(self):
38+
fmt = string.Formatter()
39+
self.assertEqual(fmt.format("-{arg}-", arg='test'), '-test-')
40+
self.assertRaises(KeyError, fmt.format, "-{arg}-")
41+
self.assertEqual(fmt.format("-{self}-", self='test'), '-test-')
42+
self.assertRaises(KeyError, fmt.format, "-{self}-")
43+
self.assertEqual(fmt.format("-{format_string}-", format_string='test'),
44+
'-test-')
45+
self.assertRaises(KeyError, fmt.format, "-{format_string}-")
46+
self.assertEqual(fmt.format(arg='test', format_string="-{arg}-"),
47+
'-test-')
3448

3549
def test_auto_numbering(self):
3650
fmt = string.Formatter()

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Core and Builtins
1818
Library
1919
-------
2020

21+
- Issue #23671: string.Template now allows to specify the "self" parameter as
22+
keyword argument. string.Formatter now allows to specify the "self" and
23+
the "format_string" parameters as keyword arguments.
24+
2125
- Issue #21560: An attempt to write a data of wrong type no longer cause
2226
GzipFile corruption. Original patch by Wolfgang Maier.
2327

0 commit comments

Comments
 (0)