mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
Implementation of PEP 3101, Advanced String Formatting.
Known issues: The string.Formatter class, as discussed in the PEP, is incomplete. Error handling needs to conform to the PEP. Need to fix this warning that I introduced in Python/formatter_unicode.c: Objects/stringlib/unicodedefs.h:26: warning: `STRINGLIB_CMP' defined but not used Need to make sure sign formatting is correct, more tests needed. Need to remove '()' sign formatting, left over from an earlier version of the PEP.
This commit is contained in:
parent
e4dc324884
commit
8c66326368
22 changed files with 2669 additions and 18 deletions
|
@ -493,6 +493,50 @@ class LongTest(unittest.TestCase):
|
|||
eq(x > y, Rcmp > 0, Frm("%r > %r %d", x, y, Rcmp))
|
||||
eq(x >= y, Rcmp >= 0, Frm("%r >= %r %d", x, y, Rcmp))
|
||||
|
||||
def test_format(self):
|
||||
self.assertEqual(format(123456789, 'd'), '123456789')
|
||||
self.assertEqual(format(123456789, 'd'), '123456789')
|
||||
|
||||
# hex
|
||||
self.assertEqual(format(3, "x"), "3")
|
||||
self.assertEqual(format(3, "X"), "3")
|
||||
self.assertEqual(format(1234, "x"), "4d2")
|
||||
self.assertEqual(format(-1234, "x"), "-4d2")
|
||||
self.assertEqual(format(1234, "8x"), " 4d2")
|
||||
# XXX fix self.assertEqual(format(-1234, "8x"), " -4d2")
|
||||
self.assertEqual(format(1234, "x"), "4d2")
|
||||
self.assertEqual(format(-1234, "x"), "-4d2")
|
||||
self.assertEqual(format(-3, "x"), "-3")
|
||||
self.assertEqual(format(-3, "X"), "-3")
|
||||
self.assertEqual(format(int('be', 16), "x"), "be")
|
||||
self.assertEqual(format(int('be', 16), "X"), "BE")
|
||||
self.assertEqual(format(-int('be', 16), "x"), "-be")
|
||||
self.assertEqual(format(-int('be', 16), "X"), "-BE")
|
||||
|
||||
# octal
|
||||
self.assertEqual(format(3, "b"), "11")
|
||||
self.assertEqual(format(-3, "b"), "-11")
|
||||
self.assertEqual(format(1234, "b"), "10011010010")
|
||||
self.assertEqual(format(-1234, "b"), "-10011010010")
|
||||
self.assertEqual(format(1234, "-b"), "10011010010")
|
||||
self.assertEqual(format(-1234, "-b"), "-10011010010")
|
||||
self.assertEqual(format(1234, " b"), " 10011010010")
|
||||
self.assertEqual(format(-1234, " b"), "-10011010010")
|
||||
self.assertEqual(format(1234, "+b"), "+10011010010")
|
||||
self.assertEqual(format(-1234, "+b"), "-10011010010")
|
||||
|
||||
# conversion to float
|
||||
self.assertEqual(format(0, 'f'), '0.000000')
|
||||
|
||||
# make sure these are errors
|
||||
self.assertRaises(ValueError, format, 3, "1.3") # precision disallowed
|
||||
return
|
||||
self.assertRaises(ValueError, format, 3, "+c") # sign not allowed
|
||||
# with 'c'
|
||||
self.assertRaises(ValueError, format, 3, "R") # bogus format type
|
||||
# conversion to string should fail
|
||||
self.assertRaises(ValueError, format, 3, "s")
|
||||
|
||||
def test_main():
|
||||
test_support.run_unittest(LongTest)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue