summaryrefslogtreecommitdiffstats
path: root/Python
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 /Python
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 'Python')
-rw-r--r--Python/mystrtoul.c60
1 files changed, 54 insertions, 6 deletions
diff --git a/Python/mystrtoul.c b/Python/mystrtoul.c
index cb3c012..ebd468c 100644
--- a/Python/mystrtoul.c
+++ b/Python/mystrtoul.c
@@ -83,9 +83,9 @@ static int digitlimit[] = {
** This is a general purpose routine for converting
** an ascii string to an integer in an arbitrary base.
** Leading white space is ignored. If 'base' is zero
-** it looks for a leading 0, 0x or 0X to tell which
-** base. If these are absent it defaults to 10.
-** Base must be 0 or between 2 and 36 (inclusive).
+** it looks for a leading 0, 0b, 0B, 0o, 0O, 0x or 0X
+** to tell which base. If these are absent it defaults
+** to 10. Base must be 0 or between 2 and 36 (inclusive).
** If 'ptr' is non-NULL it will contain a pointer to
** the end of the scan.
** Errors due to bad pointers will probably result in
@@ -104,7 +104,7 @@ PyOS_strtoul(register char *str, char **ptr, int base)
/* check for leading 0 or 0x for auto-base or base 16 */
switch (base) {
- case 0: /* look for leading 0, 0x or 0X */
+ case 0: /* look for leading 0, 0b, 0o or 0x */
if (*str == '0') {
++str;
if (*str == 'x' || *str == 'X') {
@@ -116,14 +116,62 @@ PyOS_strtoul(register char *str, char **ptr, int base)
}
++str;
base = 16;
- }
- else
+ } else if (*str == 'o' || *str == 'O') {
+ /* there must be at least one digit after 0o */
+ if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 8) {
+ if (ptr)
+ *ptr = str;
+ return 0;
+ }
+ ++str;
+ base = 8;
+ } else if (*str == 'b' || *str == 'B') {
+ /* there must be at least one digit after 0b */
+ if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 2) {
+ if (ptr)
+ *ptr = str;
+ return 0;
+ }
+ ++str;
+ base = 2;
+ } else {
base = 8;
+ }
}
else
base = 10;
break;
+ case 2: /* skip leading 0b or 0B */
+ if (*str == '0') {
+ ++str;
+ if (*str == 'b' || *str == 'B') {
+ /* there must be at least one digit after 0b */
+ if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 2) {
+ if (ptr)
+ *ptr = str;
+ return 0;
+ }
+ ++str;
+ }
+ }
+ break;
+
+ case 8: /* skip leading 0o or 0O */
+ if (*str == '0') {
+ ++str;
+ if (*str == 'o' || *str == 'O') {
+ /* there must be at least one digit after 0o */
+ if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 8) {
+ if (ptr)
+ *ptr = str;
+ return 0;
+ }
+ ++str;
+ }
+ }
+ break;
+
case 16: /* skip leading 0x or 0X */
if (*str == '0') {
++str;