|
1 import sys |
|
2 from test.test_support import verbose, have_unicode, TestFailed |
|
3 import test.test_support as test_support |
|
4 import unittest |
|
5 |
|
6 maxsize = test_support.MAX_Py_ssize_t |
|
7 |
|
8 # test string formatting operator (I am not sure if this is being tested |
|
9 # elsewhere but, surely, some of the given cases are *not* tested because |
|
10 # they crash python) |
|
11 # test on unicode strings as well |
|
12 |
|
13 overflowok = 1 |
|
14 overflowrequired = 0 |
|
15 |
|
16 |
|
17 def testformat(formatstr, args, output=None, limit=None): |
|
18 if verbose: |
|
19 if output: |
|
20 print "%s %% %s =? %s ..." %\ |
|
21 (repr(formatstr), repr(args), repr(output)), |
|
22 else: |
|
23 print "%s %% %s works? ..." % (repr(formatstr), repr(args)), |
|
24 try: |
|
25 result = formatstr % args |
|
26 except OverflowError: |
|
27 if not overflowok: |
|
28 raise |
|
29 if verbose: |
|
30 print 'overflow (this is fine)' |
|
31 else: |
|
32 if overflowrequired: |
|
33 if verbose: |
|
34 print 'no' |
|
35 print "overflow expected on %s %% %s" % \ |
|
36 (repr(formatstr), repr(args)) |
|
37 elif output and limit is None and result != output: |
|
38 if verbose: |
|
39 print 'no' |
|
40 print "%s %% %s == %s != %s" % \ |
|
41 (repr(formatstr), repr(args), repr(result), repr(output)) |
|
42 # when 'limit' is specified, it determines how many characters |
|
43 # must match exactly; lengths must always match. |
|
44 # ex: limit=5, '12345678' matches '12345___' |
|
45 # (mainly for floating point format tests for which an exact match |
|
46 # can't be guaranteed due to rounding and representation errors) |
|
47 elif output and limit is not None and ( |
|
48 len(result)!=len(output) or result[:limit]!=output[:limit]): |
|
49 if verbose: |
|
50 print 'no' |
|
51 print "%s %% %s == %s != %s" % \ |
|
52 (repr(formatstr), repr(args), repr(result), repr(output)) |
|
53 else: |
|
54 if verbose: |
|
55 print 'yes' |
|
56 |
|
57 |
|
58 def testboth(formatstr, *args): |
|
59 testformat(formatstr, *args) |
|
60 if have_unicode: |
|
61 testformat(unicode(formatstr), *args) |
|
62 |
|
63 |
|
64 class FormatTest(unittest.TestCase): |
|
65 def test_format(self): |
|
66 testboth("%.1d", (1,), "1") |
|
67 testboth("%.*d", (sys.maxint,1)) # expect overflow |
|
68 testboth("%.100d", (1,), '0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001') |
|
69 testboth("%#.117x", (1,), '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001') |
|
70 testboth("%#.118x", (1,), '0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001') |
|
71 |
|
72 testboth("%f", (1.0,), "1.000000") |
|
73 # these are trying to test the limits of the internal magic-number-length |
|
74 # formatting buffer, if that number changes then these tests are less |
|
75 # effective |
|
76 testboth("%#.*g", (109, -1.e+49/3.)) |
|
77 testboth("%#.*g", (110, -1.e+49/3.)) |
|
78 testboth("%#.*g", (110, -1.e+100/3.)) |
|
79 |
|
80 # test some ridiculously large precision, expect overflow |
|
81 testboth('%12.*f', (123456, 1.0)) |
|
82 |
|
83 # check for internal overflow validation on length of precision |
|
84 overflowrequired = 1 |
|
85 testboth("%#.*g", (110, -1.e+100/3.)) |
|
86 testboth("%#.*G", (110, -1.e+100/3.)) |
|
87 testboth("%#.*f", (110, -1.e+100/3.)) |
|
88 testboth("%#.*F", (110, -1.e+100/3.)) |
|
89 overflowrequired = 0 |
|
90 |
|
91 # Formatting of long integers. Overflow is not ok |
|
92 overflowok = 0 |
|
93 testboth("%x", 10L, "a") |
|
94 testboth("%x", 100000000000L, "174876e800") |
|
95 testboth("%o", 10L, "12") |
|
96 testboth("%o", 100000000000L, "1351035564000") |
|
97 testboth("%d", 10L, "10") |
|
98 testboth("%d", 100000000000L, "100000000000") |
|
99 |
|
100 big = 123456789012345678901234567890L |
|
101 testboth("%d", big, "123456789012345678901234567890") |
|
102 testboth("%d", -big, "-123456789012345678901234567890") |
|
103 testboth("%5d", -big, "-123456789012345678901234567890") |
|
104 testboth("%31d", -big, "-123456789012345678901234567890") |
|
105 testboth("%32d", -big, " -123456789012345678901234567890") |
|
106 testboth("%-32d", -big, "-123456789012345678901234567890 ") |
|
107 testboth("%032d", -big, "-0123456789012345678901234567890") |
|
108 testboth("%-032d", -big, "-123456789012345678901234567890 ") |
|
109 testboth("%034d", -big, "-000123456789012345678901234567890") |
|
110 testboth("%034d", big, "0000123456789012345678901234567890") |
|
111 testboth("%0+34d", big, "+000123456789012345678901234567890") |
|
112 testboth("%+34d", big, " +123456789012345678901234567890") |
|
113 testboth("%34d", big, " 123456789012345678901234567890") |
|
114 testboth("%.2d", big, "123456789012345678901234567890") |
|
115 testboth("%.30d", big, "123456789012345678901234567890") |
|
116 testboth("%.31d", big, "0123456789012345678901234567890") |
|
117 testboth("%32.31d", big, " 0123456789012345678901234567890") |
|
118 testboth("%d", float(big), "123456________________________", 6) |
|
119 |
|
120 big = 0x1234567890abcdef12345L # 21 hex digits |
|
121 testboth("%x", big, "1234567890abcdef12345") |
|
122 testboth("%x", -big, "-1234567890abcdef12345") |
|
123 testboth("%5x", -big, "-1234567890abcdef12345") |
|
124 testboth("%22x", -big, "-1234567890abcdef12345") |
|
125 testboth("%23x", -big, " -1234567890abcdef12345") |
|
126 testboth("%-23x", -big, "-1234567890abcdef12345 ") |
|
127 testboth("%023x", -big, "-01234567890abcdef12345") |
|
128 testboth("%-023x", -big, "-1234567890abcdef12345 ") |
|
129 testboth("%025x", -big, "-0001234567890abcdef12345") |
|
130 testboth("%025x", big, "00001234567890abcdef12345") |
|
131 testboth("%0+25x", big, "+0001234567890abcdef12345") |
|
132 testboth("%+25x", big, " +1234567890abcdef12345") |
|
133 testboth("%25x", big, " 1234567890abcdef12345") |
|
134 testboth("%.2x", big, "1234567890abcdef12345") |
|
135 testboth("%.21x", big, "1234567890abcdef12345") |
|
136 testboth("%.22x", big, "01234567890abcdef12345") |
|
137 testboth("%23.22x", big, " 01234567890abcdef12345") |
|
138 testboth("%-23.22x", big, "01234567890abcdef12345 ") |
|
139 testboth("%X", big, "1234567890ABCDEF12345") |
|
140 testboth("%#X", big, "0X1234567890ABCDEF12345") |
|
141 testboth("%#x", big, "0x1234567890abcdef12345") |
|
142 testboth("%#x", -big, "-0x1234567890abcdef12345") |
|
143 testboth("%#.23x", -big, "-0x001234567890abcdef12345") |
|
144 testboth("%#+.23x", big, "+0x001234567890abcdef12345") |
|
145 testboth("%# .23x", big, " 0x001234567890abcdef12345") |
|
146 testboth("%#+.23X", big, "+0X001234567890ABCDEF12345") |
|
147 testboth("%#-+.23X", big, "+0X001234567890ABCDEF12345") |
|
148 testboth("%#-+26.23X", big, "+0X001234567890ABCDEF12345") |
|
149 testboth("%#-+27.23X", big, "+0X001234567890ABCDEF12345 ") |
|
150 testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345") |
|
151 # next one gets two leading zeroes from precision, and another from the |
|
152 # 0 flag and the width |
|
153 testboth("%#+027.23X", big, "+0X0001234567890ABCDEF12345") |
|
154 # same, except no 0 flag |
|
155 testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345") |
|
156 testboth("%x", float(big), "123456_______________", 6) |
|
157 |
|
158 big = 012345670123456701234567012345670L # 32 octal digits |
|
159 testboth("%o", big, "12345670123456701234567012345670") |
|
160 testboth("%o", -big, "-12345670123456701234567012345670") |
|
161 testboth("%5o", -big, "-12345670123456701234567012345670") |
|
162 testboth("%33o", -big, "-12345670123456701234567012345670") |
|
163 testboth("%34o", -big, " -12345670123456701234567012345670") |
|
164 testboth("%-34o", -big, "-12345670123456701234567012345670 ") |
|
165 testboth("%034o", -big, "-012345670123456701234567012345670") |
|
166 testboth("%-034o", -big, "-12345670123456701234567012345670 ") |
|
167 testboth("%036o", -big, "-00012345670123456701234567012345670") |
|
168 testboth("%036o", big, "000012345670123456701234567012345670") |
|
169 testboth("%0+36o", big, "+00012345670123456701234567012345670") |
|
170 testboth("%+36o", big, " +12345670123456701234567012345670") |
|
171 testboth("%36o", big, " 12345670123456701234567012345670") |
|
172 testboth("%.2o", big, "12345670123456701234567012345670") |
|
173 testboth("%.32o", big, "12345670123456701234567012345670") |
|
174 testboth("%.33o", big, "012345670123456701234567012345670") |
|
175 testboth("%34.33o", big, " 012345670123456701234567012345670") |
|
176 testboth("%-34.33o", big, "012345670123456701234567012345670 ") |
|
177 testboth("%o", big, "12345670123456701234567012345670") |
|
178 testboth("%#o", big, "012345670123456701234567012345670") |
|
179 testboth("%#o", -big, "-012345670123456701234567012345670") |
|
180 testboth("%#.34o", -big, "-0012345670123456701234567012345670") |
|
181 testboth("%#+.34o", big, "+0012345670123456701234567012345670") |
|
182 testboth("%# .34o", big, " 0012345670123456701234567012345670") |
|
183 testboth("%#+.34o", big, "+0012345670123456701234567012345670") |
|
184 testboth("%#-+.34o", big, "+0012345670123456701234567012345670") |
|
185 testboth("%#-+37.34o", big, "+0012345670123456701234567012345670 ") |
|
186 testboth("%#+37.34o", big, " +0012345670123456701234567012345670") |
|
187 # next one gets one leading zero from precision |
|
188 testboth("%.33o", big, "012345670123456701234567012345670") |
|
189 # base marker shouldn't change that, since "0" is redundant |
|
190 testboth("%#.33o", big, "012345670123456701234567012345670") |
|
191 # but reduce precision, and base marker should add a zero |
|
192 testboth("%#.32o", big, "012345670123456701234567012345670") |
|
193 # one leading zero from precision, and another from "0" flag & width |
|
194 testboth("%034.33o", big, "0012345670123456701234567012345670") |
|
195 # base marker shouldn't change that |
|
196 testboth("%0#34.33o", big, "0012345670123456701234567012345670") |
|
197 testboth("%o", float(big), "123456__________________________", 6) |
|
198 |
|
199 # Some small ints, in both Python int and long flavors). |
|
200 testboth("%d", 42, "42") |
|
201 testboth("%d", -42, "-42") |
|
202 testboth("%d", 42L, "42") |
|
203 testboth("%d", -42L, "-42") |
|
204 testboth("%d", 42.0, "42") |
|
205 testboth("%#x", 1, "0x1") |
|
206 testboth("%#x", 1L, "0x1") |
|
207 testboth("%#X", 1, "0X1") |
|
208 testboth("%#X", 1L, "0X1") |
|
209 testboth("%#x", 1.0, "0x1") |
|
210 testboth("%#o", 1, "01") |
|
211 testboth("%#o", 1L, "01") |
|
212 testboth("%#o", 0, "0") |
|
213 testboth("%#o", 0L, "0") |
|
214 testboth("%o", 0, "0") |
|
215 testboth("%o", 0L, "0") |
|
216 testboth("%d", 0, "0") |
|
217 testboth("%d", 0L, "0") |
|
218 testboth("%#x", 0, "0x0") |
|
219 testboth("%#x", 0L, "0x0") |
|
220 testboth("%#X", 0, "0X0") |
|
221 testboth("%#X", 0L, "0X0") |
|
222 |
|
223 testboth("%x", 0x42, "42") |
|
224 testboth("%x", -0x42, "-42") |
|
225 testboth("%x", 0x42L, "42") |
|
226 testboth("%x", -0x42L, "-42") |
|
227 testboth("%x", float(0x42), "42") |
|
228 |
|
229 testboth("%o", 042, "42") |
|
230 testboth("%o", -042, "-42") |
|
231 testboth("%o", 042L, "42") |
|
232 testboth("%o", -042L, "-42") |
|
233 testboth("%o", float(042), "42") |
|
234 |
|
235 # Test exception for unknown format characters |
|
236 if verbose: |
|
237 print 'Testing exceptions' |
|
238 |
|
239 def test_exc(formatstr, args, exception, excmsg): |
|
240 try: |
|
241 testformat(formatstr, args) |
|
242 except exception, exc: |
|
243 if str(exc) == excmsg: |
|
244 if verbose: |
|
245 print "yes" |
|
246 else: |
|
247 if verbose: print 'no' |
|
248 print 'Unexpected ', exception, ':', repr(str(exc)) |
|
249 except: |
|
250 if verbose: print 'no' |
|
251 print 'Unexpected exception' |
|
252 raise |
|
253 else: |
|
254 raise TestFailed, 'did not get expected exception: %s' % excmsg |
|
255 |
|
256 test_exc('abc %a', 1, ValueError, |
|
257 "unsupported format character 'a' (0x61) at index 5") |
|
258 if have_unicode: |
|
259 test_exc(unicode('abc %\u3000','raw-unicode-escape'), 1, ValueError, |
|
260 "unsupported format character '?' (0x3000) at index 5") |
|
261 |
|
262 test_exc('%d', '1', TypeError, "%d format: a number is required, not str") |
|
263 test_exc('%g', '1', TypeError, "float argument required, not str") |
|
264 test_exc('no format', '1', TypeError, |
|
265 "not all arguments converted during string formatting") |
|
266 test_exc('no format', u'1', TypeError, |
|
267 "not all arguments converted during string formatting") |
|
268 test_exc(u'no format', '1', TypeError, |
|
269 "not all arguments converted during string formatting") |
|
270 test_exc(u'no format', u'1', TypeError, |
|
271 "not all arguments converted during string formatting") |
|
272 |
|
273 class Foobar(long): |
|
274 def __oct__(self): |
|
275 # Returning a non-string should not blow up. |
|
276 return self + 1 |
|
277 |
|
278 test_exc('%o', Foobar(), TypeError, |
|
279 "expected string or Unicode object, long found") |
|
280 |
|
281 if maxsize == 2**31-1: |
|
282 # crashes 2.2.1 and earlier: |
|
283 try: |
|
284 "%*d"%(maxsize, -127) |
|
285 except MemoryError: |
|
286 pass |
|
287 else: |
|
288 raise TestFailed, '"%*d"%(maxsize, -127) should fail' |
|
289 |
|
290 def test_main(): |
|
291 test_support.run_unittest(FormatTest) |
|
292 |
|
293 |
|
294 if __name__ == "__main__": |
|
295 unittest.main() |