diff options
author | Eric Smith <eric@trueblade.com> | 2008-07-15 10:10:07 (GMT) |
---|---|---|
committer | Eric Smith <eric@trueblade.com> | 2008-07-15 10:10:07 (GMT) |
commit | d0c841243cf3966c05a98ce06dc3380a06c33679 (patch) | |
tree | e6fe5c14bb322499d3125766f7df9322beca2bdf /Lib | |
parent | a6864e0d9f1fc06c50db36ed913ac48a3d2ddde5 (diff) | |
download | cpython-d0c841243cf3966c05a98ce06dc3380a06c33679.zip cpython-d0c841243cf3966c05a98ce06dc3380a06c33679.tar.gz cpython-d0c841243cf3966c05a98ce06dc3380a06c33679.tar.bz2 |
Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_string.py | 4 | ||||
-rw-r--r-- | Lib/test/test_types.py | 41 |
2 files changed, 45 insertions, 0 deletions
diff --git a/Lib/test/test_string.py b/Lib/test/test_string.py index 5c20a2b..20cff0a 100644 --- a/Lib/test/test_string.py +++ b/Lib/test/test_string.py @@ -192,6 +192,10 @@ class ModuleTest(unittest.TestCase): self.assertRaises(ValueError, fmt.format, "{0}", 10, 20, i=100) self.assertRaises(ValueError, fmt.format, "{i}", 10, 20, i=100) + # Alternate formatting is not supported + self.assertRaises(ValueError, format, '', '#') + self.assertRaises(ValueError, format, '', '#20') + class BytesAliasTest(unittest.TestCase): def test_builtin(self): diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index a10f2a6..b185479 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -357,6 +357,40 @@ class TypesTests(unittest.TestCase): test(1234, "+b", "+10011010010") test(-1234, "+b", "-10011010010") + # alternate (#) formatting + test(0, "#b", '0b0') + test(0, "-#b", '0b0') + test(1, "-#b", '0b1') + test(-1, "-#b", '-0b1') + test(-1, "-#5b", ' -0b1') + test(1, "+#5b", ' +0b1') + test(100, "+#b", '+0b1100100') +# test(100, "#012b", '0b001100100') + + test(0, "#o", '0o0') + test(0, "-#o", '0o0') + test(1, "-#o", '0o1') + test(-1, "-#o", '-0o1') + test(-1, "-#5o", ' -0o1') + test(1, "+#5o", ' +0o1') + test(100, "+#o", '+0o144') + + test(0, "#x", '0x0') + test(0, "-#x", '0x0') + test(1, "-#x", '0x1') + test(-1, "-#x", '-0x1') + test(-1, "-#5x", ' -0x1') + test(1, "+#5x", ' +0x1') + test(100, "+#x", '+0x64') + + test(0, "#X", '0X0') + test(0, "-#X", '0X0') + test(1, "-#X", '0X1') + test(-1, "-#X", '-0X1') + test(-1, "-#5X", ' -0X1') + test(1, "+#5X", ' +0X1') + test(100, "+#X", '+0X64') + # make sure these are errors # precision disallowed @@ -461,6 +495,9 @@ class TypesTests(unittest.TestCase): # format spec must be string self.assertRaises(TypeError, 3L .__format__, None) self.assertRaises(TypeError, 3L .__format__, 0) + # alternate specifier in wrong place + self.assertRaises(ValueError, 1L .__format__, "#+5x") + self.assertRaises(ValueError, 1L .__format__, "+5#x") # ensure that only int and float type specifiers work for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] + @@ -579,6 +616,10 @@ class TypesTests(unittest.TestCase): self.assertRaises(ValueError, format, 1e-100, format_spec) self.assertRaises(ValueError, format, -1e-100, format_spec) + # Alternate formatting is not supported + self.assertRaises(ValueError, format, 0.0, '#') + self.assertRaises(ValueError, format, 0.0, '#20f') + def test_main(): run_unittest(TypesTests) |