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.
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 @@
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 @@
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 @@
# 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 @@
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)