diff options
author | Eric Smith <eric@trueblade.com> | 2009-04-22 13:29:05 (GMT) |
---|---|---|
committer | Eric Smith <eric@trueblade.com> | 2009-04-22 13:29:05 (GMT) |
commit | aca19e6a740c424aec243a4721b18d12e9129aa7 (patch) | |
tree | 89b7a3d0e2b7246f483baba919002a24e95bb1d2 /Lib/test | |
parent | cbb530872354fb4eb3b8b5bbaa36db38a0d9a64a (diff) | |
download | cpython-aca19e6a740c424aec243a4721b18d12e9129aa7.zip cpython-aca19e6a740c424aec243a4721b18d12e9129aa7.tar.gz cpython-aca19e6a740c424aec243a4721b18d12e9129aa7.tar.bz2 |
Backport of some of the work in r71665 to trunk. This reworks much of
int, long, and float __format__(), and it keeps their implementation
in sync with py3k.
Also added PyOS_double_to_string. This is the "fallback" version
that's also available in trunk, and should be kept in sync with that
code. I'll add an issue to document PyOS_double_to_string in the C
API.
There are many internal cleanups. Externally visible changes include:
- Implement PEP 378, Format Specifier for Thousands Separator, for
floats, ints, and longs.
- Issue #5515: 'n' formatting for ints, longs, and floats handles
leading zero formatting poorly.
- Issue #5772: For float.__format__, don't add a trailing ".0" if
we're using no type code and we have an exponent.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_format.py | 4 | ||||
-rw-r--r-- | Lib/test/test_types.py | 54 |
2 files changed, 56 insertions, 2 deletions
diff --git a/Lib/test/test_format.py b/Lib/test/test_format.py index cd46bc2..06179de 100644 --- a/Lib/test/test_format.py +++ b/Lib/test/test_format.py @@ -232,6 +232,10 @@ class FormatTest(unittest.TestCase): testboth("%o", -042L, "-42") testboth("%o", float(042), "42") + # alternate float formatting + testformat('%g', 1.1, '1.1') + testformat('%#g', 1.1, '1.10000') + # Test exception for unknown format characters if verbose: print 'Testing exceptions' diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index 10ad634..6fff22a 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -113,6 +113,9 @@ class TypesTests(unittest.TestCase): self.assertEqual(1.5e-101.__format__('e'), '1.500000e-101') self.assertEqual('%e' % 1.5e-101, '1.500000e-101') + self.assertEqual('%g' % 1.0, '1') + self.assertEqual('%#g' % 1.0, '1.00000') + def test_normal_integers(self): # Ensure the first 256 integers are shared a = 256 @@ -412,6 +415,9 @@ class TypesTests(unittest.TestCase): self.assertRaises(TypeError, 3 .__format__, None) self.assertRaises(TypeError, 3 .__format__, 0) + # can't have ',' with 'c' + self.assertRaises(ValueError, 3 .__format__, ",c") + # ensure that only int and float type specifiers work for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] + [chr(x) for x in range(ord('A'), ord('Z')+1)]): @@ -609,11 +615,37 @@ class TypesTests(unittest.TestCase): # a totaly empty format specifier means something else. # So, just use a sign flag test(1e200, '+g', '+1e+200') - test(1e200, '+', '+1.0e+200') + test(1e200, '+', '+1e+200') + test(1.1e200, '+g', '+1.1e+200') + test(1.1e200, '+', '+1.1e+200') + test(1.1e200, '+g', '+1.1e+200') test(1.1e200, '+', '+1.1e+200') - # % formatting + # 0 padding + test(1234., '010f', '1234.000000') + test(1234., '011f', '1234.000000') + test(1234., '012f', '01234.000000') + test(-1234., '011f', '-1234.000000') + test(-1234., '012f', '-1234.000000') + test(-1234., '013f', '-01234.000000') + test(-1234.12341234, '013f', '-01234.123412') + test(-123456.12341234, '011.2f', '-0123456.12') + + # 0 padding with commas + test(1234., '011,f', '1,234.000000') + test(1234., '012,f', '1,234.000000') + test(1234., '013,f', '01,234.000000') + test(-1234., '012,f', '-1,234.000000') + test(-1234., '013,f', '-1,234.000000') + test(-1234., '014,f', '-01,234.000000') + test(-12345., '015,f', '-012,345.000000') + test(-123456., '016,f', '-0,123,456.000000') + test(-123456., '017,f', '-0,123,456.000000') + test(-123456.12341234, '017,f', '-0,123,456.123412') + test(-123456.12341234, '013,.2f', '-0,123,456.12') + + # % formatting test(-1.0, '%', '-100.000000%') # format spec must be string @@ -637,6 +669,24 @@ class TypesTests(unittest.TestCase): self.assertRaises(ValueError, format, 0.0, '#') self.assertRaises(ValueError, format, 0.0, '#20f') + def test_format_spec_errors(self): + # int, float, and string all share the same format spec + # mini-language parser. + + # Check that we can't ask for too many digits. This is + # probably a CPython specific test. It tries to put the width + # into a C long. + self.assertRaises(ValueError, format, 0, '1'*10000 + 'd') + + # Similar with the precision. + self.assertRaises(ValueError, format, 0, '.' + '1'*10000 + 'd') + + # And may as well test both. + self.assertRaises(ValueError, format, 0, '1'*1000 + '.' + '1'*10000 + 'd') + + # Make sure commas aren't allowed with various type codes + for code in 'xXobns': + self.assertRaises(ValueError, format, 0, ',' + code) def test_main(): run_unittest(TypesTests) |