66# they crash python)
77# test on unicode strings as well
88
9+ overflowok = 1
10+
911def testformat (formatstr , args , output = None ):
10- if verbose :
11- if output :
12- print "%s %% %s =? %s ..." % \
13- (repr (formatstr ), repr (args ), repr (output )),
14- else :
15- print "%s %% %s works? ..." % (repr (formatstr ), repr (args )),
16- try :
17- result = formatstr % args
18- except OverflowError :
19- if verbose :
20- print 'overflow (this is fine)'
21- else :
22- if output and result != output :
23- if verbose :
24- print 'no'
25- print "%s %% %s == %s != %s" % \
26- (repr (formatstr ), repr (args ), repr (result ), repr (output ))
27- else :
28- if verbose :
29- print 'yes'
12+ if verbose :
13+ if output :
14+ print "%s %% %s =? %s ..." % \
15+ (repr (formatstr ), repr (args ), repr (output )),
16+ else :
17+ print "%s %% %s works? ..." % (repr (formatstr ), repr (args )),
18+ try :
19+ result = formatstr % args
20+ except OverflowError :
21+ if not overflowok :
22+ raise
23+ if verbose :
24+ print 'overflow (this is fine)'
25+ else :
26+ if output and result != output :
27+ if verbose :
28+ print 'no'
29+ print "%s %% %s == %s != %s" % \
30+ (repr (formatstr ), repr (args ), repr (result ), repr (output ))
31+ else :
32+ if verbose :
33+ print 'yes'
3034
3135def testboth (formatstr , * args ):
32- testformat (formatstr , * args )
33- testformat (unicode (formatstr ), * args )
34-
36+ testformat (formatstr , * args )
37+ testformat (unicode (formatstr ), * args )
38+
3539
3640testboth ("%.1d" , (1 ,), "1" )
3741testboth ("%.*d" , (sys .maxint ,1 )) # expect overflow
@@ -50,3 +54,112 @@ def testboth(formatstr, *args):
5054# test some ridiculously large precision, expect overflow
5155testboth ('%12.*f' , (123456 , 1.0 ))
5256
57+ # Formatting of long integers. Overflow is not ok
58+ overflowok = 0
59+ testboth ("%x" , 10L , "a" )
60+ testboth ("%x" , 100000000000L , "174876e800" )
61+ testboth ("%o" , 10L , "12" )
62+ testboth ("%o" , 100000000000L , "1351035564000" )
63+ testboth ("%d" , 10L , "10" )
64+ testboth ("%d" , 100000000000L , "100000000000" )
65+
66+ # Make sure big is too big to fit in a 64-bit int, else the unbounded
67+ # int formatting will be sidestepped on some machines. That's vital,
68+ # because bitwise (x, X, o) formats of regular Python ints never
69+ # produce a sign ("+" or "-").
70+
71+ big = 123456789012345678901234567890L
72+ testboth ("%d" , big , "123456789012345678901234567890" )
73+ testboth ("%d" , - big , "-123456789012345678901234567890" )
74+ testboth ("%5d" , - big , "-123456789012345678901234567890" )
75+ testboth ("%31d" , - big , "-123456789012345678901234567890" )
76+ testboth ("%32d" , - big , " -123456789012345678901234567890" )
77+ testboth ("%-32d" , - big , "-123456789012345678901234567890 " )
78+ testboth ("%032d" , - big , "-0123456789012345678901234567890" )
79+ testboth ("%-032d" , - big , "-123456789012345678901234567890 " )
80+ testboth ("%034d" , - big , "-000123456789012345678901234567890" )
81+ testboth ("%034d" , big , "0000123456789012345678901234567890" )
82+ testboth ("%0+34d" , big , "+000123456789012345678901234567890" )
83+ testboth ("%+34d" , big , " +123456789012345678901234567890" )
84+ testboth ("%34d" , big , " 123456789012345678901234567890" )
85+ testboth ("%.2d" , big , "123456789012345678901234567890" )
86+ testboth ("%.30d" , big , "123456789012345678901234567890" )
87+ testboth ("%.31d" , big , "0123456789012345678901234567890" )
88+ testboth ("%32.31d" , big , " 0123456789012345678901234567890" )
89+
90+ big = 0x1234567890abcdef12345L # 21 hex digits
91+ testboth ("%x" , big , "1234567890abcdef12345" )
92+ testboth ("%x" , - big , "-1234567890abcdef12345" )
93+ testboth ("%5x" , - big , "-1234567890abcdef12345" )
94+ testboth ("%22x" , - big , "-1234567890abcdef12345" )
95+ testboth ("%23x" , - big , " -1234567890abcdef12345" )
96+ testboth ("%-23x" , - big , "-1234567890abcdef12345 " )
97+ testboth ("%023x" , - big , "-01234567890abcdef12345" )
98+ testboth ("%-023x" , - big , "-1234567890abcdef12345 " )
99+ testboth ("%025x" , - big , "-0001234567890abcdef12345" )
100+ testboth ("%025x" , big , "00001234567890abcdef12345" )
101+ testboth ("%0+25x" , big , "+0001234567890abcdef12345" )
102+ testboth ("%+25x" , big , " +1234567890abcdef12345" )
103+ testboth ("%25x" , big , " 1234567890abcdef12345" )
104+ testboth ("%.2x" , big , "1234567890abcdef12345" )
105+ testboth ("%.21x" , big , "1234567890abcdef12345" )
106+ testboth ("%.22x" , big , "01234567890abcdef12345" )
107+ testboth ("%23.22x" , big , " 01234567890abcdef12345" )
108+ testboth ("%-23.22x" , big , "01234567890abcdef12345 " )
109+ testboth ("%X" , big , "1234567890ABCDEF12345" )
110+ testboth ("%#X" , big , "0X1234567890ABCDEF12345" )
111+ testboth ("%#x" , big , "0x1234567890abcdef12345" )
112+ testboth ("%#x" , - big , "-0x1234567890abcdef12345" )
113+ testboth ("%#.23x" , - big , "-0x001234567890abcdef12345" )
114+ testboth ("%#+.23x" , big , "+0x001234567890abcdef12345" )
115+ testboth ("%# .23x" , big , " 0x001234567890abcdef12345" )
116+ testboth ("%#+.23X" , big , "+0X001234567890ABCDEF12345" )
117+ testboth ("%#-+.23X" , big , "+0X001234567890ABCDEF12345" )
118+ testboth ("%#-+26.23X" , big , "+0X001234567890ABCDEF12345" )
119+ testboth ("%#-+27.23X" , big , "+0X001234567890ABCDEF12345 " )
120+ testboth ("%#+27.23X" , big , " +0X001234567890ABCDEF12345" )
121+ # next one gets two leading zeroes from precision, and another from the
122+ # 0 flag and the width
123+ testboth ("%#+027.23X" , big , "+0X0001234567890ABCDEF12345" )
124+ # same, except no 0 flag
125+ testboth ("%#+27.23X" , big , " +0X001234567890ABCDEF12345" )
126+
127+ big = 012345670123456701234567012345670L # 32 octal digits
128+ testboth ("%o" , big , "12345670123456701234567012345670" )
129+ testboth ("%o" , - big , "-12345670123456701234567012345670" )
130+ testboth ("%5o" , - big , "-12345670123456701234567012345670" )
131+ testboth ("%33o" , - big , "-12345670123456701234567012345670" )
132+ testboth ("%34o" , - big , " -12345670123456701234567012345670" )
133+ testboth ("%-34o" , - big , "-12345670123456701234567012345670 " )
134+ testboth ("%034o" , - big , "-012345670123456701234567012345670" )
135+ testboth ("%-034o" , - big , "-12345670123456701234567012345670 " )
136+ testboth ("%036o" , - big , "-00012345670123456701234567012345670" )
137+ testboth ("%036o" , big , "000012345670123456701234567012345670" )
138+ testboth ("%0+36o" , big , "+00012345670123456701234567012345670" )
139+ testboth ("%+36o" , big , " +12345670123456701234567012345670" )
140+ testboth ("%36o" , big , " 12345670123456701234567012345670" )
141+ testboth ("%.2o" , big , "12345670123456701234567012345670" )
142+ testboth ("%.32o" , big , "12345670123456701234567012345670" )
143+ testboth ("%.33o" , big , "012345670123456701234567012345670" )
144+ testboth ("%34.33o" , big , " 012345670123456701234567012345670" )
145+ testboth ("%-34.33o" , big , "012345670123456701234567012345670 " )
146+ testboth ("%o" , big , "12345670123456701234567012345670" )
147+ testboth ("%#o" , big , "012345670123456701234567012345670" )
148+ testboth ("%#o" , - big , "-012345670123456701234567012345670" )
149+ testboth ("%#.34o" , - big , "-0012345670123456701234567012345670" )
150+ testboth ("%#+.34o" , big , "+0012345670123456701234567012345670" )
151+ testboth ("%# .34o" , big , " 0012345670123456701234567012345670" )
152+ testboth ("%#+.34o" , big , "+0012345670123456701234567012345670" )
153+ testboth ("%#-+.34o" , big , "+0012345670123456701234567012345670" )
154+ testboth ("%#-+37.34o" , big , "+0012345670123456701234567012345670 " )
155+ testboth ("%#+37.34o" , big , " +0012345670123456701234567012345670" )
156+ # next one gets one leading zero from precision
157+ testboth ("%.33o" , big , "012345670123456701234567012345670" )
158+ # base marker shouldn't change that, since "0" is redundant
159+ testboth ("%#.33o" , big , "012345670123456701234567012345670" )
160+ # but reduce precision, and base marker should add a zero
161+ testboth ("%#.32o" , big , "012345670123456701234567012345670" )
162+ # one leading zero from precision, and another from "0" flag & width
163+ testboth ("%034.33o" , big , "0012345670123456701234567012345670" )
164+ # base marker shouldn't change that
165+ testboth ("%0#34.33o" , big , "0012345670123456701234567012345670" )
0 commit comments