Skip to content

Commit 0ee22bf

Browse files
committed
fix format spec recursive expansion (closes python#19729)
1 parent bb65b5b commit 0ee22bf

3 files changed

Lines changed: 7 additions & 2 deletions

File tree

Lib/test/test_unicode.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,7 @@ def __format__(self, format_spec):
955955
'')
956956

957957
self.assertEqual("{[{}]}".format({"{}": 5}), "5")
958+
self.assertEqual("0x{:0{:d}X}".format(0x0,16), "0x0000000000000000")
958959

959960
def test_format_map(self):
960961
self.assertEqual(''.format_map({}), '')

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ What's New in Python 3.3.4 release candidate 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #19729: In str.format(), fix recursive expansion in format spec.
14+
1315
- Issue #19638: Fix possible crash / undefined behaviour from huge (more than 2
1416
billion characters) input strings in _Py_dg_strtod.
1517

Objects/stringlib/unicode_format.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,8 +727,10 @@ MarkupIterator_next(MarkupIterator *self, SubString *literal,
727727
while (self->str.start < self->str.end) {
728728
switch (c = PyUnicode_READ_CHAR(self->str.str, self->str.start++)) {
729729
case ':':
730-
hit_format_spec = 1;
731-
count = 1;
730+
if (!hit_format_spec) {
731+
count = 1;
732+
hit_format_spec = 1;
733+
}
732734
break;
733735
case '{':
734736
/* the format spec needs to be recursively expanded.

0 commit comments

Comments
 (0)