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