summaryrefslogtreecommitdiffstats
path: root/Objects/longobject.c
diff options
context:
space:
mode:
authorEric Smith <eric@trueblade.com>2008-03-17 17:32:20 (GMT)
committerEric Smith <eric@trueblade.com>2008-03-17 17:32:20 (GMT)
commit9ff19b54346d39d15cdcf75e9d66ab46ea6064d6 (patch)
treee4a83605262567c9c9abff7a7afa3edc1bd61681 /Objects/longobject.c
parent7cfbf0c421137dfac5d9d2e4c879302ba5f80d88 (diff)
downloadcpython-9ff19b54346d39d15cdcf75e9d66ab46ea6064d6.zip
cpython-9ff19b54346d39d15cdcf75e9d66ab46ea6064d6.tar.gz
cpython-9ff19b54346d39d15cdcf75e9d66ab46ea6064d6.tar.bz2
Finished backporting PEP 3127, Integer Literal Support and Syntax.
Added 0b and 0o literals to tokenizer. Modified PyOS_strtoul to support 0b and 0o inputs. Modified PyLong_FromString to support guessing 0b and 0o inputs. Renamed test_hexoct.py to test_int_literal.py and added binary tests. Added upper and lower case 0b, 0O, and 0X tests to test_int_literal.py
Diffstat (limited to 'Objects/longobject.c')
-rw-r--r--Objects/longobject.c15
1 files changed, 14 insertions, 1 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 46ed713..afa1b75 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -1465,14 +1465,27 @@ PyLong_FromString(char *str, char **pend, int base)
while (*str != '\0' && isspace(Py_CHARMASK(*str)))
str++;
if (base == 0) {
+ /* No base given. Deduce the base from the contents
+ of the string */
if (str[0] != '0')
base = 10;
else if (str[1] == 'x' || str[1] == 'X')
base = 16;
+ else if (str[1] == 'o' || str[1] == 'O')
+ base = 8;
+ else if (str[1] == 'b' || str[1] == 'B')
+ base = 2;
else
+ /* "old" (C-style) octal literal, still valid in
+ 2.x, although illegal in 3.x */
base = 8;
}
- if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
+ /* Whether or not we were deducing the base, skip leading chars
+ as needed */
+ if (str[0] == '0' &&
+ ((base == 16 && (str[1] == 'x' || str[1] == 'X')) ||
+ (base == 8 && (str[1] == 'o' || str[1] == 'O')) ||
+ (base == 2 && (str[1] == 'b' || str[1] == 'B'))))
str += 2;
start = str;