]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
only recursively expand in the format spec (closes #17644)
authorBenjamin Peterson <benjamin@python.org>
Fri, 17 May 2013 22:34:30 +0000 (17:34 -0500)
committerBenjamin Peterson <benjamin@python.org>
Fri, 17 May 2013 22:34:30 +0000 (17:34 -0500)
Lib/test/test_unicode.py
Misc/NEWS
Objects/stringlib/unicode_format.h

index d4e2222fa92dc43a337f5df6bf23cc87e779e51c..bef64aa7ccbbb3c3afdf7a77fff66f235bf986a6 100644 (file)
@@ -934,6 +934,8 @@ class UnicodeTest(string_tests.CommonTest,
         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')
index 251804dd5263a508783480ed06d8ce96a16fa6a7..14be9391d35d5c756e3debeb9328e8d111dd85a9 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 3.3.3 release candidate 1?
 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.
 
index e9be516318f22f71371409e7929c542a93237a09..c1c2cf37812a816a1ed9ad18b0b8701c755dfc19 100644 (file)
@@ -638,7 +638,7 @@ MarkupIterator_next(MarkupIterator *self, SubString *literal,
                     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 @@ MarkupIterator_next(MarkupIterator *self, SubString *literal,
 
     /* 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 '}':