summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_builtin.py
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 /Lib/test/test_builtin.py
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 'Lib/test/test_builtin.py')
-rw-r--r--Lib/test/test_builtin.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 4057ba1..9fa973e 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -853,6 +853,12 @@ class BuiltinTest(unittest.TestCase):
self.assertRaises(ValueError, int, "0x", 16)
self.assertRaises(ValueError, int, "0x", 0)
+ self.assertRaises(ValueError, int, "0o", 8)
+ self.assertRaises(ValueError, int, "0o", 0)
+
+ self.assertRaises(ValueError, int, "0b", 2)
+ self.assertRaises(ValueError, int, "0b", 0)
+
# SF bug 1334662: int(string, base) wrong answers
# Various representations of 2**32 evaluated to 0
@@ -894,6 +900,45 @@ class BuiltinTest(unittest.TestCase):
self.assertEqual(int('2br45qb', 35), 4294967296L)
self.assertEqual(int('1z141z4', 36), 4294967296L)
+ # tests with base 0
+ # this fails on 3.0, but in 2.x the old octal syntax is allowed
+ self.assertEqual(int(' 0123 ', 0), 83)
+ self.assertEqual(int(' 0123 ', 0), 83)
+ self.assertEqual(int('000', 0), 0)
+ self.assertEqual(int('0o123', 0), 83)
+ self.assertEqual(int('0x123', 0), 291)
+ self.assertEqual(int('0b100', 0), 4)
+ self.assertEqual(int(' 0O123 ', 0), 83)
+ self.assertEqual(int(' 0X123 ', 0), 291)
+ self.assertEqual(int(' 0B100 ', 0), 4)
+
+ # without base still base 10
+ self.assertEqual(int('0123'), 123)
+ self.assertEqual(int('0123', 10), 123)
+
+ # tests with prefix and base != 0
+ self.assertEqual(int('0x123', 16), 291)
+ self.assertEqual(int('0o123', 8), 83)
+ self.assertEqual(int('0b100', 2), 4)
+ self.assertEqual(int('0X123', 16), 291)
+ self.assertEqual(int('0O123', 8), 83)
+ self.assertEqual(int('0B100', 2), 4)
+
+ # the code has special checks for the first character after the
+ # type prefix
+ self.assertRaises(ValueError, int, '0b2', 2)
+ self.assertRaises(ValueError, int, '0b02', 2)
+ self.assertRaises(ValueError, int, '0B2', 2)
+ self.assertRaises(ValueError, int, '0B02', 2)
+ self.assertRaises(ValueError, int, '0o8', 8)
+ self.assertRaises(ValueError, int, '0o08', 8)
+ self.assertRaises(ValueError, int, '0O8', 8)
+ self.assertRaises(ValueError, int, '0O08', 8)
+ self.assertRaises(ValueError, int, '0xg', 16)
+ self.assertRaises(ValueError, int, '0x0g', 16)
+ self.assertRaises(ValueError, int, '0Xg', 16)
+ self.assertRaises(ValueError, int, '0X0g', 16)
+
# SF bug 1334662: int(string, base) wrong answers
# Checks for proper evaluation of 2**32 + 1
self.assertEqual(int('100000000000000000000000000000001', 2), 4294967297L)