Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d2b58a9

Browse files
committed
only recursively expand in the format spec (closes #17644)
1 parent 36f74aa commit d2b58a9

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

Lib/test/test_unicode.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,8 @@ def __format__(self, format_spec):
934934
self.assertEqual("{0:.0s}".format("ABC\u0410\u0411\u0412"),
935935
'')
936936

937+
self.assertEqual("{[{}]}".format({"{}": 5}), "5")
938+
937939
def test_format_map(self):
938940
self.assertEqual(''.format_map({}), '')
939941
self.assertEqual('a'.format_map({}), 'a')

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.3.3 release candidate 1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #17644: Fix a crash in str.format when curly braces are used in square
16+
brackets.
17+
1518
- Issue #17983: Raise a SyntaxError for a ``global __class__`` statement in a
1619
class body.
1720

Objects/stringlib/unicode_format.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ MarkupIterator_next(MarkupIterator *self, SubString *literal,
638638
SubString *format_spec, Py_UCS4 *conversion,
639639
int *format_spec_needs_expanding)
640640
{
641-
int at_end;
641+
int at_end, hit_format_spec;
642642
Py_UCS4 c = 0;
643643
Py_ssize_t start;
644644
int count;
@@ -723,12 +723,18 @@ MarkupIterator_next(MarkupIterator *self, SubString *literal,
723723

724724
/* we know we can't have a zero length string, so don't worry
725725
about that case */
726+
hit_format_spec = 0;
726727
while (self->str.start < self->str.end) {
727728
switch (c = PyUnicode_READ_CHAR(self->str.str, self->str.start++)) {
729+
case ':':
730+
hit_format_spec = 1;
731+
count = 1;
732+
break;
728733
case '{':
729734
/* the format spec needs to be recursively expanded.
730735
this is an optimization, and not strictly needed */
731-
*format_spec_needs_expanding = 1;
736+
if (hit_format_spec)
737+
*format_spec_needs_expanding = 1;
732738
count++;
733739
break;
734740
case '}':

0 commit comments

Comments
 (0)