@@ -113,6 +113,9 @@ def test(f, result):
113113 self .assertEqual (1.5e-101 .__format__ ('e' ), '1.500000e-101' )
114114 self .assertEqual ('%e' % 1.5e-101 , '1.500000e-101' )
115115
116+ self .assertEqual ('%g' % 1.0 , '1' )
117+ self .assertEqual ('%#g' % 1.0 , '1.00000' )
118+
116119 def test_normal_integers (self ):
117120 # Ensure the first 256 integers are shared
118121 a = 256
@@ -358,6 +361,8 @@ def test(i, format_spec, result):
358361 self .assertRaises (TypeError , 3 .__format__ , 0 )
359362 # can't have ',' with 'n'
360363 self .assertRaises (ValueError , 3 .__format__ , ",n" )
364+ # can't have ',' with 'c'
365+ self .assertRaises (ValueError , 3 .__format__ , ",c" )
361366
362367 # ensure that only int and float type specifiers work
363368 for format_spec in ([chr (x ) for x in range (ord ('a' ), ord ('z' )+ 1 )] +
@@ -547,10 +552,34 @@ def test(f, format_spec, result):
547552 # a totaly empty format specifier means something else.
548553 # So, just use a sign flag
549554 test (1e200 , '+g' , '+1e+200' )
550- test (1e200 , '+' , '+1.0e+200' )
555+ test (1e200 , '+' , '+1e+200' )
556+
551557 test (1.1e200 , '+g' , '+1.1e+200' )
552558 test (1.1e200 , '+' , '+1.1e+200' )
553559
560+ # 0 padding
561+ test (1234. , '010f' , '1234.000000' )
562+ test (1234. , '011f' , '1234.000000' )
563+ test (1234. , '012f' , '01234.000000' )
564+ test (- 1234. , '011f' , '-1234.000000' )
565+ test (- 1234. , '012f' , '-1234.000000' )
566+ test (- 1234. , '013f' , '-01234.000000' )
567+ test (- 1234.12341234 , '013f' , '-01234.123412' )
568+ test (- 123456.12341234 , '011.2f' , '-0123456.12' )
569+
570+ # 0 padding with commas
571+ test (1234. , '011,f' , '1,234.000000' )
572+ test (1234. , '012,f' , '1,234.000000' )
573+ test (1234. , '013,f' , '01,234.000000' )
574+ test (- 1234. , '012,f' , '-1,234.000000' )
575+ test (- 1234. , '013,f' , '-1,234.000000' )
576+ test (- 1234. , '014,f' , '-01,234.000000' )
577+ test (- 12345. , '015,f' , '-012,345.000000' )
578+ test (- 123456. , '016,f' , '-0,123,456.000000' )
579+ test (- 123456. , '017,f' , '-0,123,456.000000' )
580+ test (- 123456.12341234 , '017,f' , '-0,123,456.123412' )
581+ test (- 123456.12341234 , '013,.2f' , '-0,123,456.12' )
582+
554583 # % formatting
555584 test (- 1.0 , '%' , '-100.000000%' )
556585
@@ -575,6 +604,24 @@ def test(f, format_spec, result):
575604 self .assertRaises (ValueError , format , 0.0 , '#' )
576605 self .assertRaises (ValueError , format , 0.0 , '#20f' )
577606
607+ def test_format_spec_errors (self ):
608+ # int, float, and string all share the same format spec
609+ # mini-language parser.
610+
611+ # Check that we can't ask for too many digits. This is
612+ # probably a CPython specific test. It tries to put the width
613+ # into a C long.
614+ self .assertRaises (ValueError , format , 0 , '1' * 10000 + 'd' )
615+
616+ # Similar with the precision.
617+ self .assertRaises (ValueError , format , 0 , '.' + '1' * 10000 + 'd' )
618+
619+ # And may as well test both.
620+ self .assertRaises (ValueError , format , 0 , '1' * 1000 + '.' + '1' * 10000 + 'd' )
621+
622+ # Make sure commas aren't allowed with various type codes
623+ for code in 'xXobns' :
624+ self .assertRaises (ValueError , format , 0 , ',' + code )
578625
579626def test_main ():
580627 run_unittest (TypesTests )
0 commit comments