[Python-checkins] cpython (merge 3.4 -> default): merge heads

senthil.kumaran python-checkins at python.org
Mon Apr 14 22:49:25 CEST 2014


http://hg.python.org/cpython/rev/8a0764a29585
changeset:   90298:8a0764a29585
parent:      90296:4c30718c9763
parent:      90297:83417ca91956
user:        Senthil Kumaran <senthil at uthcode.com>
date:        Mon Apr 14 16:47:05 2014 -0400
summary:
  merge heads

files:
  Lib/string.py           |  23 +++++++++++++++++++++--
  Lib/test/test_string.py |  17 +++++++++++++++++
  Misc/NEWS               |   4 ++++
  3 files changed, 42 insertions(+), 2 deletions(-)


diff --git a/Lib/string.py b/Lib/string.py
--- a/Lib/string.py
+++ b/Lib/string.py
@@ -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 --git a/Lib/test/test_string.py b/Lib/test/test_string.py
--- a/Lib/test/test_string.py
+++ b/Lib/test/test_string.py
@@ -32,6 +32,23 @@
         self.assertEqual(fmt.format("foo{0}", "bar"), "foobar")
         self.assertEqual(fmt.format("foo{1}{0}-{1}", "bar", 6), "foo6bar-6")
 
+    def test_auto_numbering(self):
+        fmt = string.Formatter()
+        self.assertEqual(fmt.format('foo{}{}', 'bar', 6),
+                         'foo{}{}'.format('bar', 6))
+        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),
+                         '{:^{}}'.format('bar', 6))
+        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)
+
+        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'-")
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -39,6 +39,10 @@
 
 - Issue #20480: Add ipaddress.reverse_pointer. Patch by Leon Weber.
 
+- Issue #13598: Modify string.Formatter to support auto-numbering of 
+  replacement fields. It now matches the behavior of str.format() in
+  this regard.
+
 Library
 -------
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list