diff -r 9f464eff218a Lib/string.py --- a/Lib/string.py Wed Oct 31 12:03:48 2012 +0200 +++ b/Lib/string.py Wed Oct 31 13:11:02 2012 +0000 @@ -169,7 +169,8 @@ self.check_unused_args(used_args, args, kwargs) return result - def _vformat(self, format_string, args, kwargs, used_args, recursion_depth): + def _vformat(self, format_string, args, kwargs, used_args, recursion_depth, + auto_arg_index=0): if recursion_depth < 0: raise ValueError('Max string recursion exceeded') result = [] @@ -185,6 +186,23 @@ # this is some markup, find the object and do # the formatting + # handle arg indexing when empty field_names are given. + if field_name == '': + if auto_arg_index is False: + raise ValueError('cannot switch from manual field ' + 'specification to automatic field ' + 'numbering') + field_name = str(auto_arg_index) + auto_arg_index += 1 + elif field_name.isdigit(): + if auto_arg_index: + raise ValueError('cannot switch from manual field ' + 'specification to automatic field ' + 'numbering') + # disable auto arg incrementing, if it gets + # used later on, then an exception will be raised + auto_arg_index = False + # given the field_name, find the object it references # and the argument it came from obj, arg_used = self.get_field(field_name, args, kwargs) @@ -195,7 +213,8 @@ # expand the format spec, if needed format_spec = self._vformat(format_spec, args, kwargs, - used_args, recursion_depth-1) + used_args, recursion_depth-1, + auto_arg_index=auto_arg_index) # format the object and append to the result result.append(self.format_field(obj, format_spec)) diff -r 9f464eff218a Lib/test/test_string.py --- a/Lib/test/test_string.py Wed Oct 31 12:03:48 2012 +0200 +++ b/Lib/test/test_string.py Wed Oct 31 13:11:02 2012 +0000 @@ -31,7 +31,16 @@ self.assertEqual(fmt.format("foo"), "foo") self.assertEqual(fmt.format("foo{0}", "bar"), "foobar") self.assertEqual(fmt.format("foo{1}{0}-{1}", "bar", 6), "foo6bar-6") - + self.assertEqual(fmt.format("foo{}{}", "bar", 6), "foobar6") + self.assertEqual(fmt.format("foo{1}{num}{1}", None, "bar", num=6), + "foo{1}{num}{1}".format(None, "bar", num=6)) + self.assertEqual(fmt.format("{:^{}}", 'bar', 6), ' bar ') + self.assertEqual(fmt.format("{:^{pad}}{}", 'foo', 'bar', pad=6), + "{:^{pad}}{}".format('foo', 'bar', pad=6)) + + with self.assertRaises(ValueError): + fmt.format("foo{1}{}", "bar", 6) + def test_conversion_specifiers(self): fmt = string.Formatter() self.assertEqual(fmt.format("-{arg!r}-", arg='test'), "-'test'-") @@ -82,7 +91,6 @@ fmt = NamespaceFormatter({'greeting':'hello'}) self.assertEqual(fmt.format("{greeting}, world!"), 'hello, world!') - def test_override_format_field(self): class CallFormatter(string.Formatter): def format_field(self, value, format_spec): @@ -91,7 +99,6 @@ fmt = CallFormatter() self.assertEqual(fmt.format('*{0}*', lambda : 'result'), '*result*') - def test_override_convert_field(self): class XFormatter(string.Formatter): def convert_field(self, value, conversion): @@ -102,7 +109,6 @@ fmt = XFormatter() self.assertEqual(fmt.format("{0!r}:{0!x}", 'foo', 'foo'), "'foo':None") - def test_override_parse(self): class BarFormatter(string.Formatter): # returns an iterable that contains tuples of the form: @@ -148,10 +154,19 @@ with self.assertRaises(ValueError) as err: fmt._vformat("{i}", args, kwargs, set(), -1) self.assertIn("recursion", str(err.exception)) - + + def test_formatter_with_dates(self): + from datetime import datetime + dt = datetime(year=1989, month=12, day=1) + fmt = string.Formatter() + self.assertEqual(fmt.format("{name}: born {:%U weeks into %Y}", + dt, name='Python'), + "Python: born 48 weeks into 1989") + def test_main(): support.run_unittest(ModuleTest) + if __name__ == "__main__": test_main()