diff --git a/Lib/test/test_format.py b/Lib/test/test_format.py index 6a6060384b9..ce5d5f2c807 100644 --- a/Lib/test/test_format.py +++ b/Lib/test/test_format.py @@ -164,6 +164,22 @@ def testboth(formatstr, *args): testboth("%d", -42, "-42") testboth("%d", 42L, "42") testboth("%d", -42L, "-42") +testboth("%#x", 1, "0x1") +testboth("%#x", 1L, "0x1") +testboth("%#X", 1, "0X1") +testboth("%#X", 1L, "0X1") +testboth("%#o", 1, "01") +testboth("%#o", 1L, "01") +testboth("%#o", 0, "0") +testboth("%#o", 0L, "0") +testboth("%o", 0, "0") +testboth("%o", 0L, "0") +testboth("%d", 0, "0") +testboth("%d", 0L, "0") +testboth("%#x", 0, "0") +testboth("%#x", 0L, "0") +testboth("%#X", 0, "0") +testboth("%#X", 0L, "0") testboth("%x", 0x42, "42") # testboth("%x", -0x42, "ffffffbe") # Alas, that's specific to 32-bit machines diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 740cbe294e4..8e1153693d0 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -2575,8 +2575,16 @@ _PyString_FormatLong(PyObject *val, int flags, int prec, int type, numdigits = len - numnondigits; assert(numdigits > 0); - /* Get rid of base marker unless F_ALT */ - if ((flags & F_ALT) == 0) { + /* Get rid of base marker unless F_ALT. Even if F_ALT, leading 0x + * must be stripped if the *value* is 0. + */ + if ((flags & F_ALT) == 0 || + ((flags & F_ALT) && + (type == 'x' || type == 'X') && + numdigits == 1 && + !sign && + buf[2] == '0' + )) { /* Need to skip 0x, 0X or 0. */ int skipped = 0; switch (type) { @@ -3015,17 +3023,21 @@ PyString_Format(PyObject *format, PyObject *args) width--; } if ((flags & F_ALT) && (c == 'x' || c == 'X')) { + /* There's a base marker ("0x" or "0X") if and + * only if the value is non-zero. + */ assert(pbuf[0] == '0'); - assert(pbuf[1] == c); - if (fill != ' ') { - *res++ = *pbuf++; - *res++ = *pbuf++; + if (pbuf[1] == c) { + if (fill != ' ') { + *res++ = *pbuf++; + *res++ = *pbuf++; + } + rescnt -= 2; + width -= 2; + if (width < 0) + width = 0; + len -= 2; } - rescnt -= 2; - width -= 2; - if (width < 0) - width = 0; - len -= 2; } if (width > len && !(flags & F_LJUST)) { do { @@ -3037,9 +3049,8 @@ PyString_Format(PyObject *format, PyObject *args) if (sign) *res++ = sign; if ((flags & F_ALT) && - (c == 'x' || c == 'X')) { - assert(pbuf[0] == '0'); - assert(pbuf[1] == c); + (c == 'x' || c == 'X') && + pbuf[1] == c) { *res++ = *pbuf++; *res++ = *pbuf++; } diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index c237789a79e..aecc2618d15 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -5081,16 +5081,17 @@ PyObject *PyUnicode_Format(PyObject *format, } if ((flags & F_ALT) && (c == 'x' || c == 'X')) { assert(pbuf[0] == '0'); - assert(pbuf[1] == c); - if (fill != ' ') { - *res++ = *pbuf++; - *res++ = *pbuf++; + if (pbuf[1] == c) { + if (fill != ' ') { + *res++ = *pbuf++; + *res++ = *pbuf++; + } + rescnt -= 2; + width -= 2; + if (width < 0) + width = 0; + len -= 2; } - rescnt -= 2; - width -= 2; - if (width < 0) - width = 0; - len -= 2; } if (width > len && !(flags & F_LJUST)) { do { @@ -5101,9 +5102,9 @@ PyObject *PyUnicode_Format(PyObject *format, if (fill == ' ') { if (sign) *res++ = sign; - if ((flags & F_ALT) && (c == 'x' || c == 'X')) { + if ((flags & F_ALT) && (c == 'x' || c == 'X') && + pbuf[1] == c) { assert(pbuf[0] == '0'); - assert(pbuf[1] == c); *res++ = *pbuf++; *res++ = *pbuf++; }