[Python-checkins] cpython (3.3): only recursively expand in the format spec (closes #17644)

benjamin.peterson python-checkins at python.org
Sat May 18 01:23:07 CEST 2013


http://hg.python.org/cpython/rev/6786e681ed58
changeset:   83817:6786e681ed58
branch:      3.3
parent:      83812:cd2457463eeb
user:        Benjamin Peterson <benjamin at python.org>
date:        Fri May 17 17:34:30 2013 -0500
summary:
  only recursively expand in the format spec (closes #17644)

files:
  Lib/test/test_unicode.py           |   2 ++
  Misc/NEWS                          |   3 +++
  Objects/stringlib/unicode_format.h |  10 ++++++++--
  3 files changed, 13 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
@@ -934,6 +934,8 @@
         self.assertEqual("{0:.0s}".format("ABC\u0410\u0411\u0412"),
                          '')
 
+        self.assertEqual("{[{}]}".format({"{}": 5}), "5")
+
     def test_format_map(self):
         self.assertEqual(''.format_map({}), '')
         self.assertEqual('a'.format_map({}), 'a')
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@
 Core and Builtins
 -----------------
 
+- Issue #17644: Fix a crash in str.format when curly braces are used in square
+  brackets.
+
 - Issue #17983: Raise a SyntaxError for a ``global __class__`` statement in a
   class body.
 
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
@@ -638,7 +638,7 @@
                     SubString *format_spec, Py_UCS4 *conversion,
                     int *format_spec_needs_expanding)
 {
-    int at_end;
+    int at_end, hit_format_spec;
     Py_UCS4 c = 0;
     Py_ssize_t start;
     int count;
@@ -723,12 +723,18 @@
 
     /* we know we can't have a zero length string, so don't worry
        about that case */
+    hit_format_spec = 0;
     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;
+            break;
         case '{':
             /* the format spec needs to be recursively expanded.
                this is an optimization, and not strictly needed */
-            *format_spec_needs_expanding = 1;
+            if (hit_format_spec)
+                *format_spec_needs_expanding = 1;
             count++;
             break;
         case '}':

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


More information about the Python-checkins mailing list