Index: Python/mystrtoul.c =================================================================== --- Python/mystrtoul.c (revision 60075) +++ Python/mystrtoul.c (working copy) @@ -110,29 +110,34 @@ while (*str && isspace(Py_CHARMASK(*str))) ++str; - /* check for leading 0 or 0x for auto-base or base 16 */ - switch (base) { - case 0: /* look for leading 0, 0x or 0X */ - if (*str == '0') { - ++str; - if (*str == 'x' || *str == 'X') { - ++str; - base = 16; - } - else - base = 8; + /* handle auto-base */ + if (base == 0) { + if(*str == '0') { + if (str[1] == 'x' || str[1] == 'X') { + base = 16; + } else { + base = 8; } - else - base = 10; - break; + } else { + base = 10; + } + } - case 16: /* skip leading 0x or 0X */ - if (*str == '0') { + /* check for leading 0x for base 16 */ + if (base == 16 && *str == '0') { + ++str; + /* If 0x is followed by a digit, gobble it. + Otherwise, "0x" is not a valid int literal, but 0 is, + so only gobble the 0 and return 0. */ + if (*str == 'x' || *str == 'X') { + if (_PyLong_DigitValue[Py_CHARMASK(str[1])] < base) { ++str; - if (*str == 'x' || *str == 'X') - ++str; + } else { + if(ptr) + *ptr = str; + return 0; } - break; + } } /* catch silly bases */