diff options
author | Georg Brandl <georg@python.org> | 2008-01-19 19:27:05 +0000 |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2008-01-19 19:27:05 +0000 |
commit | 14404b68d8c5a501a2f5ee6f45494865b7b38276 (patch) | |
tree | f1db16ee7f39d81414ad24bd682e9df61b74e4dd /Lib | |
parent | 2686f4d9d14e2b30a61e5350dcb4a56c43c82b57 (diff) | |
download | cpython-git-14404b68d8c5a501a2f5ee6f45494865b7b38276.tar.gz |
Fix #1679: "0x" was taken as a valid integer literal.
Fixes the tokenizer, tokenize.py and int() to reject this.
Patches by Malte Helmert.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_builtin.py | 5 | ||||
-rw-r--r-- | Lib/test/test_grammar.py | 2 | ||||
-rw-r--r-- | Lib/tokenize.py | 2 |
3 files changed, 8 insertions, 1 deletions
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index d56e6fff87..f7b7c0ce74 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -816,6 +816,11 @@ class BuiltinTest(unittest.TestCase): self.assertEqual(int('0123', 0), 83) self.assertEqual(int('0x123', 16), 291) + # Bug 1679: "0x" is not a valid hex literal + self.assertRaises(ValueError, int, "0x", 16) + self.assertRaises(ValueError, int, "0x", 0) + + # SF bug 1334662: int(string, base) wrong answers # Various representations of 2**32 evaluated to 0 # rather than 2**32 in previous versions diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py index 51d77f2ef5..435227521e 100644 --- a/Lib/test/test_grammar.py +++ b/Lib/test/test_grammar.py @@ -30,6 +30,8 @@ class TokenTests(unittest.TestCase): self.assertEquals(0xff, 255) self.assertEquals(0377, 255) self.assertEquals(2147483647, 017777777777) + # "0x" is not a valid literal + self.assertRaises(SyntaxError, eval, "0x") from sys import maxint if maxint == 2147483647: self.assertEquals(-2147483647-1, -020000000000) diff --git a/Lib/tokenize.py b/Lib/tokenize.py index 9322e0fe1b..1c93944368 100644 --- a/Lib/tokenize.py +++ b/Lib/tokenize.py @@ -50,7 +50,7 @@ Comment = r'#[^\r\n]*' Ignore = Whitespace + any(r'\\\r?\n' + Whitespace) + maybe(Comment) Name = r'[a-zA-Z_]\w*' -Hexnumber = r'0[xX][\da-fA-F]*[lL]?' +Hexnumber = r'0[xX][\da-fA-F]+[lL]?' Octnumber = r'0[0-7]*[lL]?' Decnumber = r'[1-9]\d*[lL]?' Intnumber = group(Hexnumber, Octnumber, Decnumber) |