Skip to content

Commit 7ce9074

Browse files
committed
Issue #13598: Add auto-numbering of replacement fields to string.Formatter.
1 parent efeb9da commit 7ce9074

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

Lib/string.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ def vformat(self, format_string, args, kwargs):
169169
self.check_unused_args(used_args, args, kwargs)
170170
return result
171171

172-
def _vformat(self, format_string, args, kwargs, used_args, recursion_depth):
172+
def _vformat(self, format_string, args, kwargs, used_args, recursion_depth,
173+
auto_arg_index=0):
173174
if recursion_depth < 0:
174175
raise ValueError('Max string recursion exceeded')
175176
result = []
@@ -185,6 +186,23 @@ def _vformat(self, format_string, args, kwargs, used_args, recursion_depth):
185186
# this is some markup, find the object and do
186187
# the formatting
187188

189+
# handle arg indexing when empty field_names are given.
190+
if field_name == '':
191+
if auto_arg_index is False:
192+
raise ValueError('cannot switch from manual field '
193+
'specification to automatic field '
194+
'numbering')
195+
field_name = str(auto_arg_index)
196+
auto_arg_index += 1
197+
elif field_name.isdigit():
198+
if auto_arg_index:
199+
raise ValueError('cannot switch from manual field '
200+
'specification to automatic field '
201+
'numbering')
202+
# disable auto arg incrementing, if it gets
203+
# used later on, then an exception will be raised
204+
auto_arg_index = False
205+
188206
# given the field_name, find the object it references
189207
# and the argument it came from
190208
obj, arg_used = self.get_field(field_name, args, kwargs)
@@ -195,7 +213,8 @@ def _vformat(self, format_string, args, kwargs, used_args, recursion_depth):
195213

196214
# expand the format spec, if needed
197215
format_spec = self._vformat(format_spec, args, kwargs,
198-
used_args, recursion_depth-1)
216+
used_args, recursion_depth-1,
217+
auto_arg_index=auto_arg_index)
199218

200219
# format the object and append to the result
201220
result.append(self.format_field(obj, format_spec))

Lib/test/test_string.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,23 @@ def test_basic_formatter(self):
3232
self.assertEqual(fmt.format("foo{0}", "bar"), "foobar")
3333
self.assertEqual(fmt.format("foo{1}{0}-{1}", "bar", 6), "foo6bar-6")
3434

35+
def test_auto_numbering(self):
36+
fmt = string.Formatter()
37+
self.assertEqual(fmt.format('foo{}{}', 'bar', 6),
38+
'foo{}{}'.format('bar', 6))
39+
self.assertEqual(fmt.format('foo{1}{num}{1}', None, 'bar', num=6),
40+
'foo{1}{num}{1}'.format(None, 'bar', num=6))
41+
self.assertEqual(fmt.format('{:^{}}', 'bar', 6),
42+
'{:^{}}'.format('bar', 6))
43+
self.assertEqual(fmt.format('{:^{pad}}{}', 'foo', 'bar', pad=6),
44+
'{:^{pad}}{}'.format('foo', 'bar', pad=6))
45+
46+
with self.assertRaises(ValueError):
47+
fmt.format('foo{1}{}', 'bar', 6)
48+
49+
with self.assertRaises(ValueError):
50+
fmt.format('foo{}{1}', 'bar', 6)
51+
3552
def test_conversion_specifiers(self):
3653
fmt = string.Formatter()
3754
self.assertEqual(fmt.format("-{arg!r}-", arg='test'), "-'test'-")

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ Core and Builtins
3030
- Issue #12546: Allow \x00 to be used as a fill character when using str, int,
3131
float, and complex __format__ methods.
3232

33+
- Issue #13598: Modify string.Formatter to support auto-numbering of
34+
replacement fields. It now matches the behavior of str.format() in
35+
this regard.
36+
3337
Library
3438
-------
3539

0 commit comments

Comments
 (0)