1 | n/a | from test.support import verbose, TestFailed |
---|
2 | n/a | import locale |
---|
3 | n/a | import sys |
---|
4 | n/a | import test.support as support |
---|
5 | n/a | import unittest |
---|
6 | n/a | |
---|
7 | n/a | maxsize = support.MAX_Py_ssize_t |
---|
8 | n/a | |
---|
9 | n/a | # test string formatting operator (I am not sure if this is being tested |
---|
10 | n/a | # elsewhere but, surely, some of the given cases are *not* tested because |
---|
11 | n/a | # they crash python) |
---|
12 | n/a | # test on bytes object as well |
---|
13 | n/a | |
---|
14 | n/a | def testformat(formatstr, args, output=None, limit=None, overflowok=False): |
---|
15 | n/a | if verbose: |
---|
16 | n/a | if output: |
---|
17 | n/a | print("{!a} % {!a} =? {!a} ...".format(formatstr, args, output), |
---|
18 | n/a | end=' ') |
---|
19 | n/a | else: |
---|
20 | n/a | print("{!a} % {!a} works? ...".format(formatstr, args), end=' ') |
---|
21 | n/a | try: |
---|
22 | n/a | result = formatstr % args |
---|
23 | n/a | except OverflowError: |
---|
24 | n/a | if not overflowok: |
---|
25 | n/a | raise |
---|
26 | n/a | if verbose: |
---|
27 | n/a | print('overflow (this is fine)') |
---|
28 | n/a | else: |
---|
29 | n/a | if output and limit is None and result != output: |
---|
30 | n/a | if verbose: |
---|
31 | n/a | print('no') |
---|
32 | n/a | raise AssertionError("%r %% %r == %r != %r" % |
---|
33 | n/a | (formatstr, args, result, output)) |
---|
34 | n/a | # when 'limit' is specified, it determines how many characters |
---|
35 | n/a | # must match exactly; lengths must always match. |
---|
36 | n/a | # ex: limit=5, '12345678' matches '12345___' |
---|
37 | n/a | # (mainly for floating point format tests for which an exact match |
---|
38 | n/a | # can't be guaranteed due to rounding and representation errors) |
---|
39 | n/a | elif output and limit is not None and ( |
---|
40 | n/a | len(result)!=len(output) or result[:limit]!=output[:limit]): |
---|
41 | n/a | if verbose: |
---|
42 | n/a | print('no') |
---|
43 | n/a | print("%s %% %s == %s != %s" % \ |
---|
44 | n/a | (repr(formatstr), repr(args), repr(result), repr(output))) |
---|
45 | n/a | else: |
---|
46 | n/a | if verbose: |
---|
47 | n/a | print('yes') |
---|
48 | n/a | |
---|
49 | n/a | def testcommon(formatstr, args, output=None, limit=None, overflowok=False): |
---|
50 | n/a | # if formatstr is a str, test str, bytes, and bytearray; |
---|
51 | n/a | # otherwise, test bytes and bytearry |
---|
52 | n/a | if isinstance(formatstr, str): |
---|
53 | n/a | testformat(formatstr, args, output, limit, overflowok) |
---|
54 | n/a | b_format = formatstr.encode('ascii') |
---|
55 | n/a | else: |
---|
56 | n/a | b_format = formatstr |
---|
57 | n/a | ba_format = bytearray(b_format) |
---|
58 | n/a | b_args = [] |
---|
59 | n/a | if not isinstance(args, tuple): |
---|
60 | n/a | args = (args, ) |
---|
61 | n/a | b_args = tuple(args) |
---|
62 | n/a | if output is None: |
---|
63 | n/a | b_output = ba_output = None |
---|
64 | n/a | else: |
---|
65 | n/a | if isinstance(output, str): |
---|
66 | n/a | b_output = output.encode('ascii') |
---|
67 | n/a | else: |
---|
68 | n/a | b_output = output |
---|
69 | n/a | ba_output = bytearray(b_output) |
---|
70 | n/a | testformat(b_format, b_args, b_output, limit, overflowok) |
---|
71 | n/a | testformat(ba_format, b_args, ba_output, limit, overflowok) |
---|
72 | n/a | |
---|
73 | n/a | |
---|
74 | n/a | class FormatTest(unittest.TestCase): |
---|
75 | n/a | |
---|
76 | n/a | def test_common_format(self): |
---|
77 | n/a | # test the format identifiers that work the same across |
---|
78 | n/a | # str, bytes, and bytearrays (integer, float, oct, hex) |
---|
79 | n/a | testcommon("%.1d", (1,), "1") |
---|
80 | n/a | testcommon("%.*d", (sys.maxsize,1), overflowok=True) # expect overflow |
---|
81 | n/a | testcommon("%.100d", (1,), '00000000000000000000000000000000000000' |
---|
82 | n/a | '000000000000000000000000000000000000000000000000000000' |
---|
83 | n/a | '00000001', overflowok=True) |
---|
84 | n/a | testcommon("%#.117x", (1,), '0x00000000000000000000000000000000000' |
---|
85 | n/a | '000000000000000000000000000000000000000000000000000000' |
---|
86 | n/a | '0000000000000000000000000001', |
---|
87 | n/a | overflowok=True) |
---|
88 | n/a | testcommon("%#.118x", (1,), '0x00000000000000000000000000000000000' |
---|
89 | n/a | '000000000000000000000000000000000000000000000000000000' |
---|
90 | n/a | '00000000000000000000000000001', |
---|
91 | n/a | overflowok=True) |
---|
92 | n/a | |
---|
93 | n/a | testcommon("%f", (1.0,), "1.000000") |
---|
94 | n/a | # these are trying to test the limits of the internal magic-number-length |
---|
95 | n/a | # formatting buffer, if that number changes then these tests are less |
---|
96 | n/a | # effective |
---|
97 | n/a | testcommon("%#.*g", (109, -1.e+49/3.)) |
---|
98 | n/a | testcommon("%#.*g", (110, -1.e+49/3.)) |
---|
99 | n/a | testcommon("%#.*g", (110, -1.e+100/3.)) |
---|
100 | n/a | # test some ridiculously large precision, expect overflow |
---|
101 | n/a | testcommon('%12.*f', (123456, 1.0)) |
---|
102 | n/a | |
---|
103 | n/a | # check for internal overflow validation on length of precision |
---|
104 | n/a | # these tests should no longer cause overflow in Python |
---|
105 | n/a | # 2.7/3.1 and later. |
---|
106 | n/a | testcommon("%#.*g", (110, -1.e+100/3.)) |
---|
107 | n/a | testcommon("%#.*G", (110, -1.e+100/3.)) |
---|
108 | n/a | testcommon("%#.*f", (110, -1.e+100/3.)) |
---|
109 | n/a | testcommon("%#.*F", (110, -1.e+100/3.)) |
---|
110 | n/a | # Formatting of integers. Overflow is not ok |
---|
111 | n/a | testcommon("%x", 10, "a") |
---|
112 | n/a | testcommon("%x", 100000000000, "174876e800") |
---|
113 | n/a | testcommon("%o", 10, "12") |
---|
114 | n/a | testcommon("%o", 100000000000, "1351035564000") |
---|
115 | n/a | testcommon("%d", 10, "10") |
---|
116 | n/a | testcommon("%d", 100000000000, "100000000000") |
---|
117 | n/a | |
---|
118 | n/a | big = 123456789012345678901234567890 |
---|
119 | n/a | testcommon("%d", big, "123456789012345678901234567890") |
---|
120 | n/a | testcommon("%d", -big, "-123456789012345678901234567890") |
---|
121 | n/a | testcommon("%5d", -big, "-123456789012345678901234567890") |
---|
122 | n/a | testcommon("%31d", -big, "-123456789012345678901234567890") |
---|
123 | n/a | testcommon("%32d", -big, " -123456789012345678901234567890") |
---|
124 | n/a | testcommon("%-32d", -big, "-123456789012345678901234567890 ") |
---|
125 | n/a | testcommon("%032d", -big, "-0123456789012345678901234567890") |
---|
126 | n/a | testcommon("%-032d", -big, "-123456789012345678901234567890 ") |
---|
127 | n/a | testcommon("%034d", -big, "-000123456789012345678901234567890") |
---|
128 | n/a | testcommon("%034d", big, "0000123456789012345678901234567890") |
---|
129 | n/a | testcommon("%0+34d", big, "+000123456789012345678901234567890") |
---|
130 | n/a | testcommon("%+34d", big, " +123456789012345678901234567890") |
---|
131 | n/a | testcommon("%34d", big, " 123456789012345678901234567890") |
---|
132 | n/a | testcommon("%.2d", big, "123456789012345678901234567890") |
---|
133 | n/a | testcommon("%.30d", big, "123456789012345678901234567890") |
---|
134 | n/a | testcommon("%.31d", big, "0123456789012345678901234567890") |
---|
135 | n/a | testcommon("%32.31d", big, " 0123456789012345678901234567890") |
---|
136 | n/a | testcommon("%d", float(big), "123456________________________", 6) |
---|
137 | n/a | |
---|
138 | n/a | big = 0x1234567890abcdef12345 # 21 hex digits |
---|
139 | n/a | testcommon("%x", big, "1234567890abcdef12345") |
---|
140 | n/a | testcommon("%x", -big, "-1234567890abcdef12345") |
---|
141 | n/a | testcommon("%5x", -big, "-1234567890abcdef12345") |
---|
142 | n/a | testcommon("%22x", -big, "-1234567890abcdef12345") |
---|
143 | n/a | testcommon("%23x", -big, " -1234567890abcdef12345") |
---|
144 | n/a | testcommon("%-23x", -big, "-1234567890abcdef12345 ") |
---|
145 | n/a | testcommon("%023x", -big, "-01234567890abcdef12345") |
---|
146 | n/a | testcommon("%-023x", -big, "-1234567890abcdef12345 ") |
---|
147 | n/a | testcommon("%025x", -big, "-0001234567890abcdef12345") |
---|
148 | n/a | testcommon("%025x", big, "00001234567890abcdef12345") |
---|
149 | n/a | testcommon("%0+25x", big, "+0001234567890abcdef12345") |
---|
150 | n/a | testcommon("%+25x", big, " +1234567890abcdef12345") |
---|
151 | n/a | testcommon("%25x", big, " 1234567890abcdef12345") |
---|
152 | n/a | testcommon("%.2x", big, "1234567890abcdef12345") |
---|
153 | n/a | testcommon("%.21x", big, "1234567890abcdef12345") |
---|
154 | n/a | testcommon("%.22x", big, "01234567890abcdef12345") |
---|
155 | n/a | testcommon("%23.22x", big, " 01234567890abcdef12345") |
---|
156 | n/a | testcommon("%-23.22x", big, "01234567890abcdef12345 ") |
---|
157 | n/a | testcommon("%X", big, "1234567890ABCDEF12345") |
---|
158 | n/a | testcommon("%#X", big, "0X1234567890ABCDEF12345") |
---|
159 | n/a | testcommon("%#x", big, "0x1234567890abcdef12345") |
---|
160 | n/a | testcommon("%#x", -big, "-0x1234567890abcdef12345") |
---|
161 | n/a | testcommon("%#27x", big, " 0x1234567890abcdef12345") |
---|
162 | n/a | testcommon("%#-27x", big, "0x1234567890abcdef12345 ") |
---|
163 | n/a | testcommon("%#027x", big, "0x00001234567890abcdef12345") |
---|
164 | n/a | testcommon("%#.23x", big, "0x001234567890abcdef12345") |
---|
165 | n/a | testcommon("%#.23x", -big, "-0x001234567890abcdef12345") |
---|
166 | n/a | testcommon("%#27.23x", big, " 0x001234567890abcdef12345") |
---|
167 | n/a | testcommon("%#-27.23x", big, "0x001234567890abcdef12345 ") |
---|
168 | n/a | testcommon("%#027.23x", big, "0x00001234567890abcdef12345") |
---|
169 | n/a | testcommon("%#+.23x", big, "+0x001234567890abcdef12345") |
---|
170 | n/a | testcommon("%# .23x", big, " 0x001234567890abcdef12345") |
---|
171 | n/a | testcommon("%#+.23X", big, "+0X001234567890ABCDEF12345") |
---|
172 | n/a | # next one gets two leading zeroes from precision, and another from the |
---|
173 | n/a | # 0 flag and the width |
---|
174 | n/a | testcommon("%#+027.23X", big, "+0X0001234567890ABCDEF12345") |
---|
175 | n/a | testcommon("%# 027.23X", big, " 0X0001234567890ABCDEF12345") |
---|
176 | n/a | # same, except no 0 flag |
---|
177 | n/a | testcommon("%#+27.23X", big, " +0X001234567890ABCDEF12345") |
---|
178 | n/a | testcommon("%#-+27.23x", big, "+0x001234567890abcdef12345 ") |
---|
179 | n/a | testcommon("%#- 27.23x", big, " 0x001234567890abcdef12345 ") |
---|
180 | n/a | |
---|
181 | n/a | big = 0o12345670123456701234567012345670 # 32 octal digits |
---|
182 | n/a | testcommon("%o", big, "12345670123456701234567012345670") |
---|
183 | n/a | testcommon("%o", -big, "-12345670123456701234567012345670") |
---|
184 | n/a | testcommon("%5o", -big, "-12345670123456701234567012345670") |
---|
185 | n/a | testcommon("%33o", -big, "-12345670123456701234567012345670") |
---|
186 | n/a | testcommon("%34o", -big, " -12345670123456701234567012345670") |
---|
187 | n/a | testcommon("%-34o", -big, "-12345670123456701234567012345670 ") |
---|
188 | n/a | testcommon("%034o", -big, "-012345670123456701234567012345670") |
---|
189 | n/a | testcommon("%-034o", -big, "-12345670123456701234567012345670 ") |
---|
190 | n/a | testcommon("%036o", -big, "-00012345670123456701234567012345670") |
---|
191 | n/a | testcommon("%036o", big, "000012345670123456701234567012345670") |
---|
192 | n/a | testcommon("%0+36o", big, "+00012345670123456701234567012345670") |
---|
193 | n/a | testcommon("%+36o", big, " +12345670123456701234567012345670") |
---|
194 | n/a | testcommon("%36o", big, " 12345670123456701234567012345670") |
---|
195 | n/a | testcommon("%.2o", big, "12345670123456701234567012345670") |
---|
196 | n/a | testcommon("%.32o", big, "12345670123456701234567012345670") |
---|
197 | n/a | testcommon("%.33o", big, "012345670123456701234567012345670") |
---|
198 | n/a | testcommon("%34.33o", big, " 012345670123456701234567012345670") |
---|
199 | n/a | testcommon("%-34.33o", big, "012345670123456701234567012345670 ") |
---|
200 | n/a | testcommon("%o", big, "12345670123456701234567012345670") |
---|
201 | n/a | testcommon("%#o", big, "0o12345670123456701234567012345670") |
---|
202 | n/a | testcommon("%#o", -big, "-0o12345670123456701234567012345670") |
---|
203 | n/a | testcommon("%#38o", big, " 0o12345670123456701234567012345670") |
---|
204 | n/a | testcommon("%#-38o", big, "0o12345670123456701234567012345670 ") |
---|
205 | n/a | testcommon("%#038o", big, "0o000012345670123456701234567012345670") |
---|
206 | n/a | testcommon("%#.34o", big, "0o0012345670123456701234567012345670") |
---|
207 | n/a | testcommon("%#.34o", -big, "-0o0012345670123456701234567012345670") |
---|
208 | n/a | testcommon("%#38.34o", big, " 0o0012345670123456701234567012345670") |
---|
209 | n/a | testcommon("%#-38.34o", big, "0o0012345670123456701234567012345670 ") |
---|
210 | n/a | testcommon("%#038.34o", big, "0o000012345670123456701234567012345670") |
---|
211 | n/a | testcommon("%#+.34o", big, "+0o0012345670123456701234567012345670") |
---|
212 | n/a | testcommon("%# .34o", big, " 0o0012345670123456701234567012345670") |
---|
213 | n/a | testcommon("%#+38.34o", big, " +0o0012345670123456701234567012345670") |
---|
214 | n/a | testcommon("%#-+38.34o", big, "+0o0012345670123456701234567012345670 ") |
---|
215 | n/a | testcommon("%#- 38.34o", big, " 0o0012345670123456701234567012345670 ") |
---|
216 | n/a | testcommon("%#+038.34o", big, "+0o00012345670123456701234567012345670") |
---|
217 | n/a | testcommon("%# 038.34o", big, " 0o00012345670123456701234567012345670") |
---|
218 | n/a | # next one gets one leading zero from precision |
---|
219 | n/a | testcommon("%.33o", big, "012345670123456701234567012345670") |
---|
220 | n/a | # base marker added in spite of leading zero (different to Python 2) |
---|
221 | n/a | testcommon("%#.33o", big, "0o012345670123456701234567012345670") |
---|
222 | n/a | # reduce precision, and base marker is always added |
---|
223 | n/a | testcommon("%#.32o", big, "0o12345670123456701234567012345670") |
---|
224 | n/a | # one leading zero from precision, plus two from "0" flag & width |
---|
225 | n/a | testcommon("%035.33o", big, "00012345670123456701234567012345670") |
---|
226 | n/a | # base marker shouldn't change the size |
---|
227 | n/a | testcommon("%0#35.33o", big, "0o012345670123456701234567012345670") |
---|
228 | n/a | |
---|
229 | n/a | # Some small ints, in both Python int and flavors). |
---|
230 | n/a | testcommon("%d", 42, "42") |
---|
231 | n/a | testcommon("%d", -42, "-42") |
---|
232 | n/a | testcommon("%d", 42.0, "42") |
---|
233 | n/a | testcommon("%#x", 1, "0x1") |
---|
234 | n/a | testcommon("%#X", 1, "0X1") |
---|
235 | n/a | testcommon("%#o", 1, "0o1") |
---|
236 | n/a | testcommon("%#o", 0, "0o0") |
---|
237 | n/a | testcommon("%o", 0, "0") |
---|
238 | n/a | testcommon("%d", 0, "0") |
---|
239 | n/a | testcommon("%#x", 0, "0x0") |
---|
240 | n/a | testcommon("%#X", 0, "0X0") |
---|
241 | n/a | testcommon("%x", 0x42, "42") |
---|
242 | n/a | testcommon("%x", -0x42, "-42") |
---|
243 | n/a | testcommon("%o", 0o42, "42") |
---|
244 | n/a | testcommon("%o", -0o42, "-42") |
---|
245 | n/a | # alternate float formatting |
---|
246 | n/a | testcommon('%g', 1.1, '1.1') |
---|
247 | n/a | testcommon('%#g', 1.1, '1.10000') |
---|
248 | n/a | |
---|
249 | n/a | def test_str_format(self): |
---|
250 | n/a | testformat("%r", "\u0378", "'\\u0378'") # non printable |
---|
251 | n/a | testformat("%a", "\u0378", "'\\u0378'") # non printable |
---|
252 | n/a | testformat("%r", "\u0374", "'\u0374'") # printable |
---|
253 | n/a | testformat("%a", "\u0374", "'\\u0374'") # printable |
---|
254 | n/a | |
---|
255 | n/a | # Test exception for unknown format characters, etc. |
---|
256 | n/a | if verbose: |
---|
257 | n/a | print('Testing exceptions') |
---|
258 | n/a | def test_exc(formatstr, args, exception, excmsg): |
---|
259 | n/a | try: |
---|
260 | n/a | testformat(formatstr, args) |
---|
261 | n/a | except exception as exc: |
---|
262 | n/a | if str(exc) == excmsg: |
---|
263 | n/a | if verbose: |
---|
264 | n/a | print("yes") |
---|
265 | n/a | else: |
---|
266 | n/a | if verbose: print('no') |
---|
267 | n/a | print('Unexpected ', exception, ':', repr(str(exc))) |
---|
268 | n/a | except: |
---|
269 | n/a | if verbose: print('no') |
---|
270 | n/a | print('Unexpected exception') |
---|
271 | n/a | raise |
---|
272 | n/a | else: |
---|
273 | n/a | raise TestFailed('did not get expected exception: %s' % excmsg) |
---|
274 | n/a | test_exc('abc %b', 1, ValueError, |
---|
275 | n/a | "unsupported format character 'b' (0x62) at index 5") |
---|
276 | n/a | #test_exc(unicode('abc %\u3000','raw-unicode-escape'), 1, ValueError, |
---|
277 | n/a | # "unsupported format character '?' (0x3000) at index 5") |
---|
278 | n/a | test_exc('%d', '1', TypeError, "%d format: a number is required, not str") |
---|
279 | n/a | test_exc('%x', '1', TypeError, "%x format: an integer is required, not str") |
---|
280 | n/a | test_exc('%x', 3.14, TypeError, "%x format: an integer is required, not float") |
---|
281 | n/a | test_exc('%g', '1', TypeError, "must be real number, not str") |
---|
282 | n/a | test_exc('no format', '1', TypeError, |
---|
283 | n/a | "not all arguments converted during string formatting") |
---|
284 | n/a | test_exc('%c', -1, OverflowError, "%c arg not in range(0x110000)") |
---|
285 | n/a | test_exc('%c', sys.maxunicode+1, OverflowError, |
---|
286 | n/a | "%c arg not in range(0x110000)") |
---|
287 | n/a | #test_exc('%c', 2**128, OverflowError, "%c arg not in range(0x110000)") |
---|
288 | n/a | test_exc('%c', 3.14, TypeError, "%c requires int or char") |
---|
289 | n/a | test_exc('%c', 'ab', TypeError, "%c requires int or char") |
---|
290 | n/a | test_exc('%c', b'x', TypeError, "%c requires int or char") |
---|
291 | n/a | |
---|
292 | n/a | if maxsize == 2**31-1: |
---|
293 | n/a | # crashes 2.2.1 and earlier: |
---|
294 | n/a | try: |
---|
295 | n/a | "%*d"%(maxsize, -127) |
---|
296 | n/a | except MemoryError: |
---|
297 | n/a | pass |
---|
298 | n/a | else: |
---|
299 | n/a | raise TestFailed('"%*d"%(maxsize, -127) should fail') |
---|
300 | n/a | |
---|
301 | n/a | def test_bytes_and_bytearray_format(self): |
---|
302 | n/a | # %c will insert a single byte, either from an int in range(256), or |
---|
303 | n/a | # from a bytes argument of length 1, not from a str. |
---|
304 | n/a | testcommon(b"%c", 7, b"\x07") |
---|
305 | n/a | testcommon(b"%c", b"Z", b"Z") |
---|
306 | n/a | testcommon(b"%c", bytearray(b"Z"), b"Z") |
---|
307 | n/a | testcommon(b"%5c", 65, b" A") |
---|
308 | n/a | testcommon(b"%-5c", 65, b"A ") |
---|
309 | n/a | # %b will insert a series of bytes, either from a type that supports |
---|
310 | n/a | # the Py_buffer protocol, or something that has a __bytes__ method |
---|
311 | n/a | class FakeBytes(object): |
---|
312 | n/a | def __bytes__(self): |
---|
313 | n/a | return b'123' |
---|
314 | n/a | fb = FakeBytes() |
---|
315 | n/a | testcommon(b"%b", b"abc", b"abc") |
---|
316 | n/a | testcommon(b"%b", bytearray(b"def"), b"def") |
---|
317 | n/a | testcommon(b"%b", fb, b"123") |
---|
318 | n/a | # # %s is an alias for %b -- should only be used for Py2/3 code |
---|
319 | n/a | testcommon(b"%s", b"abc", b"abc") |
---|
320 | n/a | testcommon(b"%s", bytearray(b"def"), b"def") |
---|
321 | n/a | testcommon(b"%s", fb, b"123") |
---|
322 | n/a | # %a will give the equivalent of |
---|
323 | n/a | # repr(some_obj).encode('ascii', 'backslashreplace') |
---|
324 | n/a | testcommon(b"%a", 3.14, b"3.14") |
---|
325 | n/a | testcommon(b"%a", b"ghi", b"b'ghi'") |
---|
326 | n/a | testcommon(b"%a", "jkl", b"'jkl'") |
---|
327 | n/a | testcommon(b"%a", "\u0544", b"'\\u0544'") |
---|
328 | n/a | # %r is an alias for %a |
---|
329 | n/a | testcommon(b"%r", 3.14, b"3.14") |
---|
330 | n/a | testcommon(b"%r", b"ghi", b"b'ghi'") |
---|
331 | n/a | testcommon(b"%r", "jkl", b"'jkl'") |
---|
332 | n/a | testcommon(b"%r", "\u0544", b"'\\u0544'") |
---|
333 | n/a | |
---|
334 | n/a | # Test exception for unknown format characters, etc. |
---|
335 | n/a | if verbose: |
---|
336 | n/a | print('Testing exceptions') |
---|
337 | n/a | def test_exc(formatstr, args, exception, excmsg): |
---|
338 | n/a | try: |
---|
339 | n/a | testformat(formatstr, args) |
---|
340 | n/a | except exception as exc: |
---|
341 | n/a | if str(exc) == excmsg: |
---|
342 | n/a | if verbose: |
---|
343 | n/a | print("yes") |
---|
344 | n/a | else: |
---|
345 | n/a | if verbose: print('no') |
---|
346 | n/a | print('Unexpected ', exception, ':', repr(str(exc))) |
---|
347 | n/a | except: |
---|
348 | n/a | if verbose: print('no') |
---|
349 | n/a | print('Unexpected exception') |
---|
350 | n/a | raise |
---|
351 | n/a | else: |
---|
352 | n/a | raise TestFailed('did not get expected exception: %s' % excmsg) |
---|
353 | n/a | test_exc(b'%d', '1', TypeError, |
---|
354 | n/a | "%d format: a number is required, not str") |
---|
355 | n/a | test_exc(b'%d', b'1', TypeError, |
---|
356 | n/a | "%d format: a number is required, not bytes") |
---|
357 | n/a | test_exc(b'%x', 3.14, TypeError, |
---|
358 | n/a | "%x format: an integer is required, not float") |
---|
359 | n/a | test_exc(b'%g', '1', TypeError, "float argument required, not str") |
---|
360 | n/a | test_exc(b'%g', b'1', TypeError, "float argument required, not bytes") |
---|
361 | n/a | test_exc(b'no format', 7, TypeError, |
---|
362 | n/a | "not all arguments converted during bytes formatting") |
---|
363 | n/a | test_exc(b'no format', b'1', TypeError, |
---|
364 | n/a | "not all arguments converted during bytes formatting") |
---|
365 | n/a | test_exc(b'no format', bytearray(b'1'), TypeError, |
---|
366 | n/a | "not all arguments converted during bytes formatting") |
---|
367 | n/a | test_exc(b"%c", -1, OverflowError, |
---|
368 | n/a | "%c arg not in range(256)") |
---|
369 | n/a | test_exc(b"%c", 256, OverflowError, |
---|
370 | n/a | "%c arg not in range(256)") |
---|
371 | n/a | test_exc(b"%c", 2**128, OverflowError, |
---|
372 | n/a | "%c arg not in range(256)") |
---|
373 | n/a | test_exc(b"%c", b"Za", TypeError, |
---|
374 | n/a | "%c requires an integer in range(256) or a single byte") |
---|
375 | n/a | test_exc(b"%c", "Y", TypeError, |
---|
376 | n/a | "%c requires an integer in range(256) or a single byte") |
---|
377 | n/a | test_exc(b"%c", 3.14, TypeError, |
---|
378 | n/a | "%c requires an integer in range(256) or a single byte") |
---|
379 | n/a | test_exc(b"%b", "Xc", TypeError, |
---|
380 | n/a | "%b requires bytes, or an object that implements __bytes__, not 'str'") |
---|
381 | n/a | test_exc(b"%s", "Wd", TypeError, |
---|
382 | n/a | "%b requires bytes, or an object that implements __bytes__, not 'str'") |
---|
383 | n/a | |
---|
384 | n/a | if maxsize == 2**31-1: |
---|
385 | n/a | # crashes 2.2.1 and earlier: |
---|
386 | n/a | try: |
---|
387 | n/a | "%*d"%(maxsize, -127) |
---|
388 | n/a | except MemoryError: |
---|
389 | n/a | pass |
---|
390 | n/a | else: |
---|
391 | n/a | raise TestFailed('"%*d"%(maxsize, -127) should fail') |
---|
392 | n/a | |
---|
393 | n/a | def test_nul(self): |
---|
394 | n/a | # test the null character |
---|
395 | n/a | testcommon("a\0b", (), 'a\0b') |
---|
396 | n/a | testcommon("a%cb", (0,), 'a\0b') |
---|
397 | n/a | testformat("a%sb", ('c\0d',), 'ac\0db') |
---|
398 | n/a | testcommon(b"a%sb", (b'c\0d',), b'ac\0db') |
---|
399 | n/a | |
---|
400 | n/a | def test_non_ascii(self): |
---|
401 | n/a | testformat("\u20ac=%f", (1.0,), "\u20ac=1.000000") |
---|
402 | n/a | |
---|
403 | n/a | self.assertEqual(format("abc", "\u2007<5"), "abc\u2007\u2007") |
---|
404 | n/a | self.assertEqual(format(123, "\u2007<5"), "123\u2007\u2007") |
---|
405 | n/a | self.assertEqual(format(12.3, "\u2007<6"), "12.3\u2007\u2007") |
---|
406 | n/a | self.assertEqual(format(0j, "\u2007<4"), "0j\u2007\u2007") |
---|
407 | n/a | self.assertEqual(format(1+2j, "\u2007<8"), "(1+2j)\u2007\u2007") |
---|
408 | n/a | |
---|
409 | n/a | self.assertEqual(format("abc", "\u2007>5"), "\u2007\u2007abc") |
---|
410 | n/a | self.assertEqual(format(123, "\u2007>5"), "\u2007\u2007123") |
---|
411 | n/a | self.assertEqual(format(12.3, "\u2007>6"), "\u2007\u200712.3") |
---|
412 | n/a | self.assertEqual(format(1+2j, "\u2007>8"), "\u2007\u2007(1+2j)") |
---|
413 | n/a | self.assertEqual(format(0j, "\u2007>4"), "\u2007\u20070j") |
---|
414 | n/a | |
---|
415 | n/a | self.assertEqual(format("abc", "\u2007^5"), "\u2007abc\u2007") |
---|
416 | n/a | self.assertEqual(format(123, "\u2007^5"), "\u2007123\u2007") |
---|
417 | n/a | self.assertEqual(format(12.3, "\u2007^6"), "\u200712.3\u2007") |
---|
418 | n/a | self.assertEqual(format(1+2j, "\u2007^8"), "\u2007(1+2j)\u2007") |
---|
419 | n/a | self.assertEqual(format(0j, "\u2007^4"), "\u20070j\u2007") |
---|
420 | n/a | |
---|
421 | n/a | def test_locale(self): |
---|
422 | n/a | try: |
---|
423 | n/a | oldloc = locale.setlocale(locale.LC_ALL) |
---|
424 | n/a | locale.setlocale(locale.LC_ALL, '') |
---|
425 | n/a | except locale.Error as err: |
---|
426 | n/a | self.skipTest("Cannot set locale: {}".format(err)) |
---|
427 | n/a | try: |
---|
428 | n/a | localeconv = locale.localeconv() |
---|
429 | n/a | sep = localeconv['thousands_sep'] |
---|
430 | n/a | point = localeconv['decimal_point'] |
---|
431 | n/a | |
---|
432 | n/a | text = format(123456789, "n") |
---|
433 | n/a | self.assertIn(sep, text) |
---|
434 | n/a | self.assertEqual(text.replace(sep, ''), '123456789') |
---|
435 | n/a | |
---|
436 | n/a | text = format(1234.5, "n") |
---|
437 | n/a | self.assertIn(sep, text) |
---|
438 | n/a | self.assertIn(point, text) |
---|
439 | n/a | self.assertEqual(text.replace(sep, ''), '1234' + point + '5') |
---|
440 | n/a | finally: |
---|
441 | n/a | locale.setlocale(locale.LC_ALL, oldloc) |
---|
442 | n/a | |
---|
443 | n/a | @support.cpython_only |
---|
444 | n/a | def test_optimisations(self): |
---|
445 | n/a | text = "abcde" # 5 characters |
---|
446 | n/a | |
---|
447 | n/a | self.assertIs("%s" % text, text) |
---|
448 | n/a | self.assertIs("%.5s" % text, text) |
---|
449 | n/a | self.assertIs("%.10s" % text, text) |
---|
450 | n/a | self.assertIs("%1s" % text, text) |
---|
451 | n/a | self.assertIs("%5s" % text, text) |
---|
452 | n/a | |
---|
453 | n/a | self.assertIs("{0}".format(text), text) |
---|
454 | n/a | self.assertIs("{0:s}".format(text), text) |
---|
455 | n/a | self.assertIs("{0:.5s}".format(text), text) |
---|
456 | n/a | self.assertIs("{0:.10s}".format(text), text) |
---|
457 | n/a | self.assertIs("{0:1s}".format(text), text) |
---|
458 | n/a | self.assertIs("{0:5s}".format(text), text) |
---|
459 | n/a | |
---|
460 | n/a | self.assertIs(text % (), text) |
---|
461 | n/a | self.assertIs(text.format(), text) |
---|
462 | n/a | |
---|
463 | n/a | def test_precision(self): |
---|
464 | n/a | f = 1.2 |
---|
465 | n/a | self.assertEqual(format(f, ".0f"), "1") |
---|
466 | n/a | self.assertEqual(format(f, ".3f"), "1.200") |
---|
467 | n/a | with self.assertRaises(ValueError) as cm: |
---|
468 | n/a | format(f, ".%sf" % (sys.maxsize + 1)) |
---|
469 | n/a | |
---|
470 | n/a | c = complex(f) |
---|
471 | n/a | self.assertEqual(format(c, ".0f"), "1+0j") |
---|
472 | n/a | self.assertEqual(format(c, ".3f"), "1.200+0.000j") |
---|
473 | n/a | with self.assertRaises(ValueError) as cm: |
---|
474 | n/a | format(c, ".%sf" % (sys.maxsize + 1)) |
---|
475 | n/a | |
---|
476 | n/a | @support.cpython_only |
---|
477 | n/a | def test_precision_c_limits(self): |
---|
478 | n/a | from _testcapi import INT_MAX |
---|
479 | n/a | |
---|
480 | n/a | f = 1.2 |
---|
481 | n/a | with self.assertRaises(ValueError) as cm: |
---|
482 | n/a | format(f, ".%sf" % (INT_MAX + 1)) |
---|
483 | n/a | |
---|
484 | n/a | c = complex(f) |
---|
485 | n/a | with self.assertRaises(ValueError) as cm: |
---|
486 | n/a | format(c, ".%sf" % (INT_MAX + 1)) |
---|
487 | n/a | |
---|
488 | n/a | |
---|
489 | n/a | if __name__ == "__main__": |
---|
490 | n/a | unittest.main() |
---|