1 | n/a | import sys |
---|
2 | n/a | |
---|
3 | n/a | import unittest |
---|
4 | n/a | from test import support |
---|
5 | n/a | from test.test_grammar import (VALID_UNDERSCORE_LITERALS, |
---|
6 | n/a | INVALID_UNDERSCORE_LITERALS) |
---|
7 | n/a | |
---|
8 | n/a | L = [ |
---|
9 | n/a | ('0', 0), |
---|
10 | n/a | ('1', 1), |
---|
11 | n/a | ('9', 9), |
---|
12 | n/a | ('10', 10), |
---|
13 | n/a | ('99', 99), |
---|
14 | n/a | ('100', 100), |
---|
15 | n/a | ('314', 314), |
---|
16 | n/a | (' 314', 314), |
---|
17 | n/a | ('314 ', 314), |
---|
18 | n/a | (' \t\t 314 \t\t ', 314), |
---|
19 | n/a | (repr(sys.maxsize), sys.maxsize), |
---|
20 | n/a | (' 1x', ValueError), |
---|
21 | n/a | (' 1 ', 1), |
---|
22 | n/a | (' 1\02 ', ValueError), |
---|
23 | n/a | ('', ValueError), |
---|
24 | n/a | (' ', ValueError), |
---|
25 | n/a | (' \t\t ', ValueError), |
---|
26 | n/a | ("\u0200", ValueError) |
---|
27 | n/a | ] |
---|
28 | n/a | |
---|
29 | n/a | class IntSubclass(int): |
---|
30 | n/a | pass |
---|
31 | n/a | |
---|
32 | n/a | class IntTestCases(unittest.TestCase): |
---|
33 | n/a | |
---|
34 | n/a | def test_basic(self): |
---|
35 | n/a | self.assertEqual(int(314), 314) |
---|
36 | n/a | self.assertEqual(int(3.14), 3) |
---|
37 | n/a | # Check that conversion from float truncates towards zero |
---|
38 | n/a | self.assertEqual(int(-3.14), -3) |
---|
39 | n/a | self.assertEqual(int(3.9), 3) |
---|
40 | n/a | self.assertEqual(int(-3.9), -3) |
---|
41 | n/a | self.assertEqual(int(3.5), 3) |
---|
42 | n/a | self.assertEqual(int(-3.5), -3) |
---|
43 | n/a | self.assertEqual(int("-3"), -3) |
---|
44 | n/a | self.assertEqual(int(" -3 "), -3) |
---|
45 | n/a | self.assertEqual(int("\N{EM SPACE}-3\N{EN SPACE}"), -3) |
---|
46 | n/a | # Different base: |
---|
47 | n/a | self.assertEqual(int("10",16), 16) |
---|
48 | n/a | # Test conversion from strings and various anomalies |
---|
49 | n/a | for s, v in L: |
---|
50 | n/a | for sign in "", "+", "-": |
---|
51 | n/a | for prefix in "", " ", "\t", " \t\t ": |
---|
52 | n/a | ss = prefix + sign + s |
---|
53 | n/a | vv = v |
---|
54 | n/a | if sign == "-" and v is not ValueError: |
---|
55 | n/a | vv = -v |
---|
56 | n/a | try: |
---|
57 | n/a | self.assertEqual(int(ss), vv) |
---|
58 | n/a | except ValueError: |
---|
59 | n/a | pass |
---|
60 | n/a | |
---|
61 | n/a | s = repr(-1-sys.maxsize) |
---|
62 | n/a | x = int(s) |
---|
63 | n/a | self.assertEqual(x+1, -sys.maxsize) |
---|
64 | n/a | self.assertIsInstance(x, int) |
---|
65 | n/a | # should return int |
---|
66 | n/a | self.assertEqual(int(s[1:]), sys.maxsize+1) |
---|
67 | n/a | |
---|
68 | n/a | # should return int |
---|
69 | n/a | x = int(1e100) |
---|
70 | n/a | self.assertIsInstance(x, int) |
---|
71 | n/a | x = int(-1e100) |
---|
72 | n/a | self.assertIsInstance(x, int) |
---|
73 | n/a | |
---|
74 | n/a | |
---|
75 | n/a | # SF bug 434186: 0x80000000/2 != 0x80000000>>1. |
---|
76 | n/a | # Worked by accident in Windows release build, but failed in debug build. |
---|
77 | n/a | # Failed in all Linux builds. |
---|
78 | n/a | x = -1-sys.maxsize |
---|
79 | n/a | self.assertEqual(x >> 1, x//2) |
---|
80 | n/a | |
---|
81 | n/a | x = int('1' * 600) |
---|
82 | n/a | self.assertIsInstance(x, int) |
---|
83 | n/a | |
---|
84 | n/a | |
---|
85 | n/a | self.assertRaises(TypeError, int, 1, 12) |
---|
86 | n/a | |
---|
87 | n/a | self.assertEqual(int('0o123', 0), 83) |
---|
88 | n/a | self.assertEqual(int('0x123', 16), 291) |
---|
89 | n/a | |
---|
90 | n/a | # Bug 1679: "0x" is not a valid hex literal |
---|
91 | n/a | self.assertRaises(ValueError, int, "0x", 16) |
---|
92 | n/a | self.assertRaises(ValueError, int, "0x", 0) |
---|
93 | n/a | |
---|
94 | n/a | self.assertRaises(ValueError, int, "0o", 8) |
---|
95 | n/a | self.assertRaises(ValueError, int, "0o", 0) |
---|
96 | n/a | |
---|
97 | n/a | self.assertRaises(ValueError, int, "0b", 2) |
---|
98 | n/a | self.assertRaises(ValueError, int, "0b", 0) |
---|
99 | n/a | |
---|
100 | n/a | # SF bug 1334662: int(string, base) wrong answers |
---|
101 | n/a | # Various representations of 2**32 evaluated to 0 |
---|
102 | n/a | # rather than 2**32 in previous versions |
---|
103 | n/a | |
---|
104 | n/a | self.assertEqual(int('100000000000000000000000000000000', 2), 4294967296) |
---|
105 | n/a | self.assertEqual(int('102002022201221111211', 3), 4294967296) |
---|
106 | n/a | self.assertEqual(int('10000000000000000', 4), 4294967296) |
---|
107 | n/a | self.assertEqual(int('32244002423141', 5), 4294967296) |
---|
108 | n/a | self.assertEqual(int('1550104015504', 6), 4294967296) |
---|
109 | n/a | self.assertEqual(int('211301422354', 7), 4294967296) |
---|
110 | n/a | self.assertEqual(int('40000000000', 8), 4294967296) |
---|
111 | n/a | self.assertEqual(int('12068657454', 9), 4294967296) |
---|
112 | n/a | self.assertEqual(int('4294967296', 10), 4294967296) |
---|
113 | n/a | self.assertEqual(int('1904440554', 11), 4294967296) |
---|
114 | n/a | self.assertEqual(int('9ba461594', 12), 4294967296) |
---|
115 | n/a | self.assertEqual(int('535a79889', 13), 4294967296) |
---|
116 | n/a | self.assertEqual(int('2ca5b7464', 14), 4294967296) |
---|
117 | n/a | self.assertEqual(int('1a20dcd81', 15), 4294967296) |
---|
118 | n/a | self.assertEqual(int('100000000', 16), 4294967296) |
---|
119 | n/a | self.assertEqual(int('a7ffda91', 17), 4294967296) |
---|
120 | n/a | self.assertEqual(int('704he7g4', 18), 4294967296) |
---|
121 | n/a | self.assertEqual(int('4f5aff66', 19), 4294967296) |
---|
122 | n/a | self.assertEqual(int('3723ai4g', 20), 4294967296) |
---|
123 | n/a | self.assertEqual(int('281d55i4', 21), 4294967296) |
---|
124 | n/a | self.assertEqual(int('1fj8b184', 22), 4294967296) |
---|
125 | n/a | self.assertEqual(int('1606k7ic', 23), 4294967296) |
---|
126 | n/a | self.assertEqual(int('mb994ag', 24), 4294967296) |
---|
127 | n/a | self.assertEqual(int('hek2mgl', 25), 4294967296) |
---|
128 | n/a | self.assertEqual(int('dnchbnm', 26), 4294967296) |
---|
129 | n/a | self.assertEqual(int('b28jpdm', 27), 4294967296) |
---|
130 | n/a | self.assertEqual(int('8pfgih4', 28), 4294967296) |
---|
131 | n/a | self.assertEqual(int('76beigg', 29), 4294967296) |
---|
132 | n/a | self.assertEqual(int('5qmcpqg', 30), 4294967296) |
---|
133 | n/a | self.assertEqual(int('4q0jto4', 31), 4294967296) |
---|
134 | n/a | self.assertEqual(int('4000000', 32), 4294967296) |
---|
135 | n/a | self.assertEqual(int('3aokq94', 33), 4294967296) |
---|
136 | n/a | self.assertEqual(int('2qhxjli', 34), 4294967296) |
---|
137 | n/a | self.assertEqual(int('2br45qb', 35), 4294967296) |
---|
138 | n/a | self.assertEqual(int('1z141z4', 36), 4294967296) |
---|
139 | n/a | |
---|
140 | n/a | # tests with base 0 |
---|
141 | n/a | # this fails on 3.0, but in 2.x the old octal syntax is allowed |
---|
142 | n/a | self.assertEqual(int(' 0o123 ', 0), 83) |
---|
143 | n/a | self.assertEqual(int(' 0o123 ', 0), 83) |
---|
144 | n/a | self.assertEqual(int('000', 0), 0) |
---|
145 | n/a | self.assertEqual(int('0o123', 0), 83) |
---|
146 | n/a | self.assertEqual(int('0x123', 0), 291) |
---|
147 | n/a | self.assertEqual(int('0b100', 0), 4) |
---|
148 | n/a | self.assertEqual(int(' 0O123 ', 0), 83) |
---|
149 | n/a | self.assertEqual(int(' 0X123 ', 0), 291) |
---|
150 | n/a | self.assertEqual(int(' 0B100 ', 0), 4) |
---|
151 | n/a | |
---|
152 | n/a | # without base still base 10 |
---|
153 | n/a | self.assertEqual(int('0123'), 123) |
---|
154 | n/a | self.assertEqual(int('0123', 10), 123) |
---|
155 | n/a | |
---|
156 | n/a | # tests with prefix and base != 0 |
---|
157 | n/a | self.assertEqual(int('0x123', 16), 291) |
---|
158 | n/a | self.assertEqual(int('0o123', 8), 83) |
---|
159 | n/a | self.assertEqual(int('0b100', 2), 4) |
---|
160 | n/a | self.assertEqual(int('0X123', 16), 291) |
---|
161 | n/a | self.assertEqual(int('0O123', 8), 83) |
---|
162 | n/a | self.assertEqual(int('0B100', 2), 4) |
---|
163 | n/a | |
---|
164 | n/a | # the code has special checks for the first character after the |
---|
165 | n/a | # type prefix |
---|
166 | n/a | self.assertRaises(ValueError, int, '0b2', 2) |
---|
167 | n/a | self.assertRaises(ValueError, int, '0b02', 2) |
---|
168 | n/a | self.assertRaises(ValueError, int, '0B2', 2) |
---|
169 | n/a | self.assertRaises(ValueError, int, '0B02', 2) |
---|
170 | n/a | self.assertRaises(ValueError, int, '0o8', 8) |
---|
171 | n/a | self.assertRaises(ValueError, int, '0o08', 8) |
---|
172 | n/a | self.assertRaises(ValueError, int, '0O8', 8) |
---|
173 | n/a | self.assertRaises(ValueError, int, '0O08', 8) |
---|
174 | n/a | self.assertRaises(ValueError, int, '0xg', 16) |
---|
175 | n/a | self.assertRaises(ValueError, int, '0x0g', 16) |
---|
176 | n/a | self.assertRaises(ValueError, int, '0Xg', 16) |
---|
177 | n/a | self.assertRaises(ValueError, int, '0X0g', 16) |
---|
178 | n/a | |
---|
179 | n/a | # SF bug 1334662: int(string, base) wrong answers |
---|
180 | n/a | # Checks for proper evaluation of 2**32 + 1 |
---|
181 | n/a | self.assertEqual(int('100000000000000000000000000000001', 2), 4294967297) |
---|
182 | n/a | self.assertEqual(int('102002022201221111212', 3), 4294967297) |
---|
183 | n/a | self.assertEqual(int('10000000000000001', 4), 4294967297) |
---|
184 | n/a | self.assertEqual(int('32244002423142', 5), 4294967297) |
---|
185 | n/a | self.assertEqual(int('1550104015505', 6), 4294967297) |
---|
186 | n/a | self.assertEqual(int('211301422355', 7), 4294967297) |
---|
187 | n/a | self.assertEqual(int('40000000001', 8), 4294967297) |
---|
188 | n/a | self.assertEqual(int('12068657455', 9), 4294967297) |
---|
189 | n/a | self.assertEqual(int('4294967297', 10), 4294967297) |
---|
190 | n/a | self.assertEqual(int('1904440555', 11), 4294967297) |
---|
191 | n/a | self.assertEqual(int('9ba461595', 12), 4294967297) |
---|
192 | n/a | self.assertEqual(int('535a7988a', 13), 4294967297) |
---|
193 | n/a | self.assertEqual(int('2ca5b7465', 14), 4294967297) |
---|
194 | n/a | self.assertEqual(int('1a20dcd82', 15), 4294967297) |
---|
195 | n/a | self.assertEqual(int('100000001', 16), 4294967297) |
---|
196 | n/a | self.assertEqual(int('a7ffda92', 17), 4294967297) |
---|
197 | n/a | self.assertEqual(int('704he7g5', 18), 4294967297) |
---|
198 | n/a | self.assertEqual(int('4f5aff67', 19), 4294967297) |
---|
199 | n/a | self.assertEqual(int('3723ai4h', 20), 4294967297) |
---|
200 | n/a | self.assertEqual(int('281d55i5', 21), 4294967297) |
---|
201 | n/a | self.assertEqual(int('1fj8b185', 22), 4294967297) |
---|
202 | n/a | self.assertEqual(int('1606k7id', 23), 4294967297) |
---|
203 | n/a | self.assertEqual(int('mb994ah', 24), 4294967297) |
---|
204 | n/a | self.assertEqual(int('hek2mgm', 25), 4294967297) |
---|
205 | n/a | self.assertEqual(int('dnchbnn', 26), 4294967297) |
---|
206 | n/a | self.assertEqual(int('b28jpdn', 27), 4294967297) |
---|
207 | n/a | self.assertEqual(int('8pfgih5', 28), 4294967297) |
---|
208 | n/a | self.assertEqual(int('76beigh', 29), 4294967297) |
---|
209 | n/a | self.assertEqual(int('5qmcpqh', 30), 4294967297) |
---|
210 | n/a | self.assertEqual(int('4q0jto5', 31), 4294967297) |
---|
211 | n/a | self.assertEqual(int('4000001', 32), 4294967297) |
---|
212 | n/a | self.assertEqual(int('3aokq95', 33), 4294967297) |
---|
213 | n/a | self.assertEqual(int('2qhxjlj', 34), 4294967297) |
---|
214 | n/a | self.assertEqual(int('2br45qc', 35), 4294967297) |
---|
215 | n/a | self.assertEqual(int('1z141z5', 36), 4294967297) |
---|
216 | n/a | |
---|
217 | n/a | def test_underscores(self): |
---|
218 | n/a | for lit in VALID_UNDERSCORE_LITERALS: |
---|
219 | n/a | if any(ch in lit for ch in '.eEjJ'): |
---|
220 | n/a | continue |
---|
221 | n/a | self.assertEqual(int(lit, 0), eval(lit)) |
---|
222 | n/a | self.assertEqual(int(lit, 0), int(lit.replace('_', ''), 0)) |
---|
223 | n/a | for lit in INVALID_UNDERSCORE_LITERALS: |
---|
224 | n/a | if any(ch in lit for ch in '.eEjJ'): |
---|
225 | n/a | continue |
---|
226 | n/a | self.assertRaises(ValueError, int, lit, 0) |
---|
227 | n/a | # Additional test cases with bases != 0, only for the constructor: |
---|
228 | n/a | self.assertEqual(int("1_00", 3), 9) |
---|
229 | n/a | self.assertEqual(int("0_100"), 100) # not valid as a literal! |
---|
230 | n/a | self.assertEqual(int(b"1_00"), 100) # byte underscore |
---|
231 | n/a | self.assertRaises(ValueError, int, "_100") |
---|
232 | n/a | self.assertRaises(ValueError, int, "+_100") |
---|
233 | n/a | self.assertRaises(ValueError, int, "1__00") |
---|
234 | n/a | self.assertRaises(ValueError, int, "100_") |
---|
235 | n/a | |
---|
236 | n/a | @support.cpython_only |
---|
237 | n/a | def test_small_ints(self): |
---|
238 | n/a | # Bug #3236: Return small longs from PyLong_FromString |
---|
239 | n/a | self.assertIs(int('10'), 10) |
---|
240 | n/a | self.assertIs(int('-1'), -1) |
---|
241 | n/a | self.assertIs(int(b'10'), 10) |
---|
242 | n/a | self.assertIs(int(b'-1'), -1) |
---|
243 | n/a | |
---|
244 | n/a | def test_no_args(self): |
---|
245 | n/a | self.assertEqual(int(), 0) |
---|
246 | n/a | |
---|
247 | n/a | def test_keyword_args(self): |
---|
248 | n/a | # Test invoking int() using keyword arguments. |
---|
249 | n/a | self.assertEqual(int(x=1.2), 1) |
---|
250 | n/a | self.assertEqual(int('100', base=2), 4) |
---|
251 | n/a | self.assertEqual(int(x='100', base=2), 4) |
---|
252 | n/a | self.assertRaises(TypeError, int, base=10) |
---|
253 | n/a | self.assertRaises(TypeError, int, base=0) |
---|
254 | n/a | |
---|
255 | n/a | def test_int_base_limits(self): |
---|
256 | n/a | """Testing the supported limits of the int() base parameter.""" |
---|
257 | n/a | self.assertEqual(int('0', 5), 0) |
---|
258 | n/a | with self.assertRaises(ValueError): |
---|
259 | n/a | int('0', 1) |
---|
260 | n/a | with self.assertRaises(ValueError): |
---|
261 | n/a | int('0', 37) |
---|
262 | n/a | with self.assertRaises(ValueError): |
---|
263 | n/a | int('0', -909) # An old magic value base from Python 2. |
---|
264 | n/a | with self.assertRaises(ValueError): |
---|
265 | n/a | int('0', base=0-(2**234)) |
---|
266 | n/a | with self.assertRaises(ValueError): |
---|
267 | n/a | int('0', base=2**234) |
---|
268 | n/a | # Bases 2 through 36 are supported. |
---|
269 | n/a | for base in range(2,37): |
---|
270 | n/a | self.assertEqual(int('0', base=base), 0) |
---|
271 | n/a | |
---|
272 | n/a | def test_int_base_bad_types(self): |
---|
273 | n/a | """Not integer types are not valid bases; issue16772.""" |
---|
274 | n/a | with self.assertRaises(TypeError): |
---|
275 | n/a | int('0', 5.5) |
---|
276 | n/a | with self.assertRaises(TypeError): |
---|
277 | n/a | int('0', 5.0) |
---|
278 | n/a | |
---|
279 | n/a | def test_int_base_indexable(self): |
---|
280 | n/a | class MyIndexable(object): |
---|
281 | n/a | def __init__(self, value): |
---|
282 | n/a | self.value = value |
---|
283 | n/a | def __index__(self): |
---|
284 | n/a | return self.value |
---|
285 | n/a | |
---|
286 | n/a | # Check out of range bases. |
---|
287 | n/a | for base in 2**100, -2**100, 1, 37: |
---|
288 | n/a | with self.assertRaises(ValueError): |
---|
289 | n/a | int('43', base) |
---|
290 | n/a | |
---|
291 | n/a | # Check in-range bases. |
---|
292 | n/a | self.assertEqual(int('101', base=MyIndexable(2)), 5) |
---|
293 | n/a | self.assertEqual(int('101', base=MyIndexable(10)), 101) |
---|
294 | n/a | self.assertEqual(int('101', base=MyIndexable(36)), 1 + 36**2) |
---|
295 | n/a | |
---|
296 | n/a | def test_non_numeric_input_types(self): |
---|
297 | n/a | # Test possible non-numeric types for the argument x, including |
---|
298 | n/a | # subclasses of the explicitly documented accepted types. |
---|
299 | n/a | class CustomStr(str): pass |
---|
300 | n/a | class CustomBytes(bytes): pass |
---|
301 | n/a | class CustomByteArray(bytearray): pass |
---|
302 | n/a | |
---|
303 | n/a | factories = [ |
---|
304 | n/a | bytes, |
---|
305 | n/a | bytearray, |
---|
306 | n/a | lambda b: CustomStr(b.decode()), |
---|
307 | n/a | CustomBytes, |
---|
308 | n/a | CustomByteArray, |
---|
309 | n/a | memoryview, |
---|
310 | n/a | ] |
---|
311 | n/a | try: |
---|
312 | n/a | from array import array |
---|
313 | n/a | except ImportError: |
---|
314 | n/a | pass |
---|
315 | n/a | else: |
---|
316 | n/a | factories.append(lambda b: array('B', b)) |
---|
317 | n/a | |
---|
318 | n/a | for f in factories: |
---|
319 | n/a | x = f(b'100') |
---|
320 | n/a | with self.subTest(type(x)): |
---|
321 | n/a | self.assertEqual(int(x), 100) |
---|
322 | n/a | if isinstance(x, (str, bytes, bytearray)): |
---|
323 | n/a | self.assertEqual(int(x, 2), 4) |
---|
324 | n/a | else: |
---|
325 | n/a | msg = "can't convert non-string" |
---|
326 | n/a | with self.assertRaisesRegex(TypeError, msg): |
---|
327 | n/a | int(x, 2) |
---|
328 | n/a | with self.assertRaisesRegex(ValueError, 'invalid literal'): |
---|
329 | n/a | int(f(b'A' * 0x10)) |
---|
330 | n/a | |
---|
331 | n/a | def test_int_memoryview(self): |
---|
332 | n/a | self.assertEqual(int(memoryview(b'123')[1:3]), 23) |
---|
333 | n/a | self.assertEqual(int(memoryview(b'123\x00')[1:3]), 23) |
---|
334 | n/a | self.assertEqual(int(memoryview(b'123 ')[1:3]), 23) |
---|
335 | n/a | self.assertEqual(int(memoryview(b'123A')[1:3]), 23) |
---|
336 | n/a | self.assertEqual(int(memoryview(b'1234')[1:3]), 23) |
---|
337 | n/a | |
---|
338 | n/a | def test_string_float(self): |
---|
339 | n/a | self.assertRaises(ValueError, int, '1.2') |
---|
340 | n/a | |
---|
341 | n/a | def test_intconversion(self): |
---|
342 | n/a | # Test __int__() |
---|
343 | n/a | class ClassicMissingMethods: |
---|
344 | n/a | pass |
---|
345 | n/a | self.assertRaises(TypeError, int, ClassicMissingMethods()) |
---|
346 | n/a | |
---|
347 | n/a | class MissingMethods(object): |
---|
348 | n/a | pass |
---|
349 | n/a | self.assertRaises(TypeError, int, MissingMethods()) |
---|
350 | n/a | |
---|
351 | n/a | class Foo0: |
---|
352 | n/a | def __int__(self): |
---|
353 | n/a | return 42 |
---|
354 | n/a | |
---|
355 | n/a | self.assertEqual(int(Foo0()), 42) |
---|
356 | n/a | |
---|
357 | n/a | class Classic: |
---|
358 | n/a | pass |
---|
359 | n/a | for base in (object, Classic): |
---|
360 | n/a | class IntOverridesTrunc(base): |
---|
361 | n/a | def __int__(self): |
---|
362 | n/a | return 42 |
---|
363 | n/a | def __trunc__(self): |
---|
364 | n/a | return -12 |
---|
365 | n/a | self.assertEqual(int(IntOverridesTrunc()), 42) |
---|
366 | n/a | |
---|
367 | n/a | class JustTrunc(base): |
---|
368 | n/a | def __trunc__(self): |
---|
369 | n/a | return 42 |
---|
370 | n/a | self.assertEqual(int(JustTrunc()), 42) |
---|
371 | n/a | |
---|
372 | n/a | class ExceptionalTrunc(base): |
---|
373 | n/a | def __trunc__(self): |
---|
374 | n/a | 1 / 0 |
---|
375 | n/a | with self.assertRaises(ZeroDivisionError): |
---|
376 | n/a | int(ExceptionalTrunc()) |
---|
377 | n/a | |
---|
378 | n/a | for trunc_result_base in (object, Classic): |
---|
379 | n/a | class Integral(trunc_result_base): |
---|
380 | n/a | def __int__(self): |
---|
381 | n/a | return 42 |
---|
382 | n/a | |
---|
383 | n/a | class TruncReturnsNonInt(base): |
---|
384 | n/a | def __trunc__(self): |
---|
385 | n/a | return Integral() |
---|
386 | n/a | self.assertEqual(int(TruncReturnsNonInt()), 42) |
---|
387 | n/a | |
---|
388 | n/a | class NonIntegral(trunc_result_base): |
---|
389 | n/a | def __trunc__(self): |
---|
390 | n/a | # Check that we avoid infinite recursion. |
---|
391 | n/a | return NonIntegral() |
---|
392 | n/a | |
---|
393 | n/a | class TruncReturnsNonIntegral(base): |
---|
394 | n/a | def __trunc__(self): |
---|
395 | n/a | return NonIntegral() |
---|
396 | n/a | try: |
---|
397 | n/a | int(TruncReturnsNonIntegral()) |
---|
398 | n/a | except TypeError as e: |
---|
399 | n/a | self.assertEqual(str(e), |
---|
400 | n/a | "__trunc__ returned non-Integral" |
---|
401 | n/a | " (type NonIntegral)") |
---|
402 | n/a | else: |
---|
403 | n/a | self.fail("Failed to raise TypeError with %s" % |
---|
404 | n/a | ((base, trunc_result_base),)) |
---|
405 | n/a | |
---|
406 | n/a | # Regression test for bugs.python.org/issue16060. |
---|
407 | n/a | class BadInt(trunc_result_base): |
---|
408 | n/a | def __int__(self): |
---|
409 | n/a | return 42.0 |
---|
410 | n/a | |
---|
411 | n/a | class TruncReturnsBadInt(base): |
---|
412 | n/a | def __trunc__(self): |
---|
413 | n/a | return BadInt() |
---|
414 | n/a | |
---|
415 | n/a | with self.assertRaises(TypeError): |
---|
416 | n/a | int(TruncReturnsBadInt()) |
---|
417 | n/a | |
---|
418 | n/a | def test_int_subclass_with_int(self): |
---|
419 | n/a | class MyInt(int): |
---|
420 | n/a | def __int__(self): |
---|
421 | n/a | return 42 |
---|
422 | n/a | |
---|
423 | n/a | class BadInt(int): |
---|
424 | n/a | def __int__(self): |
---|
425 | n/a | return 42.0 |
---|
426 | n/a | |
---|
427 | n/a | my_int = MyInt(7) |
---|
428 | n/a | self.assertEqual(my_int, 7) |
---|
429 | n/a | self.assertEqual(int(my_int), 42) |
---|
430 | n/a | |
---|
431 | n/a | self.assertRaises(TypeError, int, BadInt()) |
---|
432 | n/a | |
---|
433 | n/a | def test_int_returns_int_subclass(self): |
---|
434 | n/a | class BadInt: |
---|
435 | n/a | def __int__(self): |
---|
436 | n/a | return True |
---|
437 | n/a | |
---|
438 | n/a | class BadInt2(int): |
---|
439 | n/a | def __int__(self): |
---|
440 | n/a | return True |
---|
441 | n/a | |
---|
442 | n/a | class TruncReturnsBadInt: |
---|
443 | n/a | def __trunc__(self): |
---|
444 | n/a | return BadInt() |
---|
445 | n/a | |
---|
446 | n/a | class TruncReturnsIntSubclass: |
---|
447 | n/a | def __trunc__(self): |
---|
448 | n/a | return True |
---|
449 | n/a | |
---|
450 | n/a | bad_int = BadInt() |
---|
451 | n/a | with self.assertWarns(DeprecationWarning): |
---|
452 | n/a | n = int(bad_int) |
---|
453 | n/a | self.assertEqual(n, 1) |
---|
454 | n/a | self.assertIs(type(n), int) |
---|
455 | n/a | |
---|
456 | n/a | bad_int = BadInt2() |
---|
457 | n/a | with self.assertWarns(DeprecationWarning): |
---|
458 | n/a | n = int(bad_int) |
---|
459 | n/a | self.assertEqual(n, 1) |
---|
460 | n/a | self.assertIs(type(n), int) |
---|
461 | n/a | |
---|
462 | n/a | bad_int = TruncReturnsBadInt() |
---|
463 | n/a | with self.assertWarns(DeprecationWarning): |
---|
464 | n/a | n = int(bad_int) |
---|
465 | n/a | self.assertEqual(n, 1) |
---|
466 | n/a | self.assertIs(type(n), int) |
---|
467 | n/a | |
---|
468 | n/a | good_int = TruncReturnsIntSubclass() |
---|
469 | n/a | n = int(good_int) |
---|
470 | n/a | self.assertEqual(n, 1) |
---|
471 | n/a | self.assertIs(type(n), int) |
---|
472 | n/a | n = IntSubclass(good_int) |
---|
473 | n/a | self.assertEqual(n, 1) |
---|
474 | n/a | self.assertIs(type(n), IntSubclass) |
---|
475 | n/a | |
---|
476 | n/a | def test_error_message(self): |
---|
477 | n/a | def check(s, base=None): |
---|
478 | n/a | with self.assertRaises(ValueError, |
---|
479 | n/a | msg="int(%r, %r)" % (s, base)) as cm: |
---|
480 | n/a | if base is None: |
---|
481 | n/a | int(s) |
---|
482 | n/a | else: |
---|
483 | n/a | int(s, base) |
---|
484 | n/a | self.assertEqual(cm.exception.args[0], |
---|
485 | n/a | "invalid literal for int() with base %d: %r" % |
---|
486 | n/a | (10 if base is None else base, s)) |
---|
487 | n/a | |
---|
488 | n/a | check('\xbd') |
---|
489 | n/a | check('123\xbd') |
---|
490 | n/a | check(' 123 456 ') |
---|
491 | n/a | |
---|
492 | n/a | check('123\x00') |
---|
493 | n/a | # SF bug 1545497: embedded NULs were not detected with explicit base |
---|
494 | n/a | check('123\x00', 10) |
---|
495 | n/a | check('123\x00 245', 20) |
---|
496 | n/a | check('123\x00 245', 16) |
---|
497 | n/a | check('123\x00245', 20) |
---|
498 | n/a | check('123\x00245', 16) |
---|
499 | n/a | # byte string with embedded NUL |
---|
500 | n/a | check(b'123\x00') |
---|
501 | n/a | check(b'123\x00', 10) |
---|
502 | n/a | # non-UTF-8 byte string |
---|
503 | n/a | check(b'123\xbd') |
---|
504 | n/a | check(b'123\xbd', 10) |
---|
505 | n/a | # lone surrogate in Unicode string |
---|
506 | n/a | check('123\ud800') |
---|
507 | n/a | check('123\ud800', 10) |
---|
508 | n/a | |
---|
509 | n/a | if __name__ == "__main__": |
---|
510 | n/a | unittest.main() |
---|