[Python-checkins] cpython (3.3): fix format spec recursive expansion (closes #19729)

benjamin.peterson python-checkins at python.org
Wed Nov 27 02:24:13 CET 2013


http://hg.python.org/cpython/rev/bab7dc2ffc16
changeset:   87605:bab7dc2ffc16
branch:      3.3
parent:      87603:ca600150205a
user:        Benjamin Peterson <benjamin at python.org>
date:        Tue Nov 26 19:22:36 2013 -0600
summary:
  fix format spec recursive expansion (closes #19729)

files:
  Lib/test/test_unicode.py           |  1 +
  Misc/NEWS                          |  2 ++
  Objects/stringlib/unicode_format.h |  6 ++++--
  3 files changed, 7 insertions(+), 2 deletions(-)


diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -955,6 +955,7 @@
                          '')
 
         self.assertEqual("{[{}]}".format({"{}": 5}), "5")
+        self.assertEqual("0x{:0{:d}X}".format(0x0,16), "0x0000000000000000")
 
     def test_format_map(self):
         self.assertEqual(''.format_map({}), '')
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@
 Core and Builtins
 -----------------
 
+- Issue #19729: In str.format(), fix recursive expansion in format spec.
+
 - Issue #19638: Fix possible crash / undefined behaviour from huge (more than 2
   billion characters) input strings in _Py_dg_strtod.
 
diff --git a/Objects/stringlib/unicode_format.h b/Objects/stringlib/unicode_format.h
--- a/Objects/stringlib/unicode_format.h
+++ b/Objects/stringlib/unicode_format.h
@@ -727,8 +727,10 @@
     while (self->str.start < self->str.end) {
         switch (c = PyUnicode_READ_CHAR(self->str.str, self->str.start++)) {
         case ':':
-            hit_format_spec = 1;
-            count = 1;
+            if (!hit_format_spec) {
+                count = 1;
+                hit_format_spec = 1;
+            }
             break;
         case '{':
             /* the format spec needs to be recursively expanded.

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


More information about the Python-checkins mailing list