Debian Codegraph @

Lib/test/test_int.py

raw
1 import sys
2
3 import unittest
4 from unittest import mock
5 from test import support
6 from test.support.numbers import (
7 VALID_UNDERSCORE_LITERALS,
8 INVALID_UNDERSCORE_LITERALS,
9 )
10
11 try:
12 import _pylong
13 except ImportError:
14 _pylong = None
15
16 try:
17 import _decimal
18 except ImportError:
19 _decimal = None
20
21 L = [
22 ('0', 0),
23 ('1', 1),
24 ('9', 9),
25 ('10', 10),
26 ('99', 99),
27 ('100', 100),
28 ('314', 314),
29 (' 314', 314),
30 ('314 ', 314),
31 (' \t\t 314 \t\t ', 314),
32 (repr(sys.maxsize), sys.maxsize),
33 (' 1x', ValueError),
34 (' 1 ', 1),
35 (' 1\02 ', ValueError),
36 ('', ValueError),
37 (' ', ValueError),
38 (' \t\t ', ValueError),
39 ("\u0200", ValueError)
40 ]
41
42 class IntSubclass(int):
43 pass
44
45 class IntTestCases(unittest.TestCase):
46
47 def test_basic(self):
48 self.assertEqual(int(314), 314)
49 self.assertEqual(int(3.14), 3)
50 # Check that conversion from float truncates towards zero
51 self.assertEqual(int(-3.14), -3)
52 self.assertEqual(int(3.9), 3)
53 self.assertEqual(int(-3.9), -3)
54 self.assertEqual(int(3.5), 3)
55 self.assertEqual(int(-3.5), -3)
56 self.assertEqual(int("-3"), -3)
57 self.assertEqual(int(" -3 "), -3)
58 self.assertEqual(int("\N{EM SPACE}-3\N{EN SPACE}"), -3)
59 # Different base:
60 self.assertEqual(int("10",16), 16)
61 # Test conversion from strings and various anomalies
62 for s, v in L:
63 for sign in "", "+", "-":
64 for prefix in "", " ", "\t", " \t\t ":
65 ss = prefix + sign + s
66 vv = v
67 if sign == "-" and v is not ValueError:
68 vv = -v
69 try:
70 self.assertEqual(int(ss), vv)
71 except ValueError:
72 pass
73
74 s = repr(-1-sys.maxsize)
75 x = int(s)
76 self.assertEqual(x+1, -sys.maxsize)
77 self.assertIsInstance(x, int)
78 # should return int
79 self.assertEqual(int(s[1:]), sys.maxsize+1)
80
81 # should return int
82 x = int(1e100)
83 self.assertIsInstance(x, int)
84 x = int(-1e100)
85 self.assertIsInstance(x, int)
86
87
88 # SF bug 434186: 0x80000000/2 != 0x80000000>>1.
89 # Worked by accident in Windows release build, but failed in debug build.
90 # Failed in all Linux builds.
91 x = -1-sys.maxsize
92 self.assertEqual(x >> 1, x//2)
93
94 x = int('1' * 600)
95 self.assertIsInstance(x, int)
96
97
98 self.assertRaises(TypeError, int, 1, 12)
99 self.assertRaises(TypeError, int, "10", 2, 1)
100
101 self.assertEqual(int('0o123', 0), 83)
102 self.assertEqual(int('0x123', 16), 291)
103
104 # Bug 1679: "0x" is not a valid hex literal
105 self.assertRaises(ValueError, int, "0x", 16)
106 self.assertRaises(ValueError, int, "0x", 0)
107
108 self.assertRaises(ValueError, int, "0o", 8)
109 self.assertRaises(ValueError, int, "0o", 0)
110
111 self.assertRaises(ValueError, int, "0b", 2)
112 self.assertRaises(ValueError, int, "0b", 0)
113
114 # SF bug 1334662: int(string, base) wrong answers
115 # Various representations of 2**32 evaluated to 0
116 # rather than 2**32 in previous versions
117
118 self.assertEqual(int('100000000000000000000000000000000', 2), 4294967296)
119 self.assertEqual(int('102002022201221111211', 3), 4294967296)
120 self.assertEqual(int('10000000000000000', 4), 4294967296)
121 self.assertEqual(int('32244002423141', 5), 4294967296)
122 self.assertEqual(int('1550104015504', 6), 4294967296)
123 self.assertEqual(int('211301422354', 7), 4294967296)
124 self.assertEqual(int('40000000000', 8), 4294967296)
125 self.assertEqual(int('12068657454', 9), 4294967296)
126 self.assertEqual(int('4294967296', 10), 4294967296)
127 self.assertEqual(int('1904440554', 11), 4294967296)
128 self.assertEqual(int('9ba461594', 12), 4294967296)
129 self.assertEqual(int('535a79889', 13), 4294967296)
130 self.assertEqual(int('2ca5b7464', 14), 4294967296)
131 self.assertEqual(int('1a20dcd81', 15), 4294967296)
132 self.assertEqual(int('100000000', 16), 4294967296)
133 self.assertEqual(int('a7ffda91', 17), 4294967296)
134 self.assertEqual(int('704he7g4', 18), 4294967296)
135 self.assertEqual(int('4f5aff66', 19), 4294967296)
136 self.assertEqual(int('3723ai4g', 20), 4294967296)
137 self.assertEqual(int('281d55i4', 21), 4294967296)
138 self.assertEqual(int('1fj8b184', 22), 4294967296)
139 self.assertEqual(int('1606k7ic', 23), 4294967296)
140 self.assertEqual(int('mb994ag', 24), 4294967296)
141 self.assertEqual(int('hek2mgl', 25), 4294967296)
142 self.assertEqual(int('dnchbnm', 26), 4294967296)
143 self.assertEqual(int('b28jpdm', 27), 4294967296)
144 self.assertEqual(int('8pfgih4', 28), 4294967296)
145 self.assertEqual(int('76beigg', 29), 4294967296)
146 self.assertEqual(int('5qmcpqg', 30), 4294967296)
147 self.assertEqual(int('4q0jto4', 31), 4294967296)
148 self.assertEqual(int('4000000', 32), 4294967296)
149 self.assertEqual(int('3aokq94', 33), 4294967296)
150 self.assertEqual(int('2qhxjli', 34), 4294967296)
151 self.assertEqual(int('2br45qb', 35), 4294967296)
152 self.assertEqual(int('1z141z4', 36), 4294967296)
153
154 # tests with base 0
155 # this fails on 3.0, but in 2.x the old octal syntax is allowed
156 self.assertEqual(int(' 0o123 ', 0), 83)
157 self.assertEqual(int(' 0o123 ', 0), 83)
158 self.assertEqual(int('000', 0), 0)
159 self.assertEqual(int('0o123', 0), 83)
160 self.assertEqual(int('0x123', 0), 291)
161 self.assertEqual(int('0b100', 0), 4)
162 self.assertEqual(int(' 0O123 ', 0), 83)
163 self.assertEqual(int(' 0X123 ', 0), 291)
164 self.assertEqual(int(' 0B100 ', 0), 4)
165 with self.assertRaises(ValueError):
166 int('010', 0)
167
168 # without base still base 10
169 self.assertEqual(int('0123'), 123)
170 self.assertEqual(int('0123', 10), 123)
171
172 # tests with prefix and base != 0
173 self.assertEqual(int('0x123', 16), 291)
174 self.assertEqual(int('0o123', 8), 83)
175 self.assertEqual(int('0b100', 2), 4)
176 self.assertEqual(int('0X123', 16), 291)
177 self.assertEqual(int('0O123', 8), 83)
178 self.assertEqual(int('0B100', 2), 4)
179
180 # the code has special checks for the first character after the
181 # type prefix
182 self.assertRaises(ValueError, int, '0b2', 2)
183 self.assertRaises(ValueError, int, '0b02', 2)
184 self.assertRaises(ValueError, int, '0B2', 2)
185 self.assertRaises(ValueError, int, '0B02', 2)
186 self.assertRaises(ValueError, int, '0o8', 8)
187 self.assertRaises(ValueError, int, '0o08', 8)
188 self.assertRaises(ValueError, int, '0O8', 8)
189 self.assertRaises(ValueError, int, '0O08', 8)
190 self.assertRaises(ValueError, int, '0xg', 16)
191 self.assertRaises(ValueError, int, '0x0g', 16)
192 self.assertRaises(ValueError, int, '0Xg', 16)
193 self.assertRaises(ValueError, int, '0X0g', 16)
194
195 # SF bug 1334662: int(string, base) wrong answers
196 # Checks for proper evaluation of 2**32 + 1
197 self.assertEqual(int('100000000000000000000000000000001', 2), 4294967297)
198 self.assertEqual(int('102002022201221111212', 3), 4294967297)
199 self.assertEqual(int('10000000000000001', 4), 4294967297)
200 self.assertEqual(int('32244002423142', 5), 4294967297)
201 self.assertEqual(int('1550104015505', 6), 4294967297)
202 self.assertEqual(int('211301422355', 7), 4294967297)
203 self.assertEqual(int('40000000001', 8), 4294967297)
204 self.assertEqual(int('12068657455', 9), 4294967297)
205 self.assertEqual(int('4294967297', 10), 4294967297)
206 self.assertEqual(int('1904440555', 11), 4294967297)
207 self.assertEqual(int('9ba461595', 12), 4294967297)
208 self.assertEqual(int('535a7988a', 13), 4294967297)
209 self.assertEqual(int('2ca5b7465', 14), 4294967297)
210 self.assertEqual(int('1a20dcd82', 15), 4294967297)
211 self.assertEqual(int('100000001', 16), 4294967297)
212 self.assertEqual(int('a7ffda92', 17), 4294967297)
213 self.assertEqual(int('704he7g5', 18), 4294967297)
214 self.assertEqual(int('4f5aff67', 19), 4294967297)
215 self.assertEqual(int('3723ai4h', 20), 4294967297)
216 self.assertEqual(int('281d55i5', 21), 4294967297)
217 self.assertEqual(int('1fj8b185', 22), 4294967297)
218 self.assertEqual(int('1606k7id', 23), 4294967297)
219 self.assertEqual(int('mb994ah', 24), 4294967297)
220 self.assertEqual(int('hek2mgm', 25), 4294967297)
221 self.assertEqual(int('dnchbnn', 26), 4294967297)
222 self.assertEqual(int('b28jpdn', 27), 4294967297)
223 self.assertEqual(int('8pfgih5', 28), 4294967297)
224 self.assertEqual(int('76beigh', 29), 4294967297)
225 self.assertEqual(int('5qmcpqh', 30), 4294967297)
226 self.assertEqual(int('4q0jto5', 31), 4294967297)
227 self.assertEqual(int('4000001', 32), 4294967297)
228 self.assertEqual(int('3aokq95', 33), 4294967297)
229 self.assertEqual(int('2qhxjlj', 34), 4294967297)
230 self.assertEqual(int('2br45qc', 35), 4294967297)
231 self.assertEqual(int('1z141z5', 36), 4294967297)
232
233 def test_invalid_signs(self):
234 with self.assertRaises(ValueError):
235 int('+')
236 with self.assertRaises(ValueError):
237 int('-')
238 with self.assertRaises(ValueError):
239 int('- 1')
240 with self.assertRaises(ValueError):
241 int('+ 1')
242 with self.assertRaises(ValueError):
243 int(' + 1 ')
244
245 def test_unicode(self):
246 self.assertEqual(int("१२३४५६७८९०1234567890"), 12345678901234567890)
247 self.assertEqual(int('١٢٣٤٥٦٧٨٩٠'), 1234567890)
248 self.assertEqual(int("१२३४५६७८९०1234567890", 0), 12345678901234567890)
249 self.assertEqual(int('١٢٣٤٥٦٧٨٩٠', 0), 1234567890)
250
251 def test_underscores(self):
252 for lit in VALID_UNDERSCORE_LITERALS:
253 if any(ch in lit for ch in '.eEjJ'):
254 continue
255 self.assertEqual(int(lit, 0), eval(lit))
256 self.assertEqual(int(lit, 0), int(lit.replace('_', ''), 0))
257 for lit in INVALID_UNDERSCORE_LITERALS:
258 if any(ch in lit for ch in '.eEjJ'):
259 continue
260 self.assertRaises(ValueError, int, lit, 0)
261 # Additional test cases with bases != 0, only for the constructor:
262 self.assertEqual(int("1_00", 3), 9)
263 self.assertEqual(int("0_100"), 100) # not valid as a literal!
264 self.assertEqual(int(b"1_00"), 100) # byte underscore
265 self.assertRaises(ValueError, int, "_100")
266 self.assertRaises(ValueError, int, "+_100")
267 self.assertRaises(ValueError, int, "1__00")
268 self.assertRaises(ValueError, int, "100_")
269
270 @support.cpython_only
271 def test_small_ints(self):
272 # Bug #3236: Return small longs from PyLong_FromString
273 self.assertIs(int('10'), 10)
274 self.assertIs(int('-1'), -1)
275 self.assertIs(int(b'10'), 10)
276 self.assertIs(int(b'-1'), -1)
277
278 def test_no_args(self):
279 self.assertEqual(int(), 0)
280
281 def test_keyword_args(self):
282 # Test invoking int() using keyword arguments.
283 self.assertEqual(int('100', base=2), 4)
284 with self.assertRaisesRegex(TypeError, 'keyword argument'):
285 int(x=1.2)
286 with self.assertRaisesRegex(TypeError, 'keyword argument'):
287 int(x='100', base=2)
288 self.assertRaises(TypeError, int, base=10)
289 self.assertRaises(TypeError, int, base=0)
290
291 def test_int_base_limits(self):
292 """Testing the supported limits of the int() base parameter."""
293 self.assertEqual(int('0', 5), 0)
294 with self.assertRaises(ValueError):
295 int('0', 1)
296 with self.assertRaises(ValueError):
297 int('0', 37)
298 with self.assertRaises(ValueError):
299 int('0', -909) # An old magic value base from Python 2.
300 with self.assertRaises(ValueError):
301 int('0', base=0-(2**234))
302 with self.assertRaises(ValueError):
303 int('0', base=2**234)
304 # Bases 2 through 36 are supported.
305 for base in range(2,37):
306 self.assertEqual(int('0', base=base), 0)
307
308 def test_int_base_bad_types(self):
309 """Not integer types are not valid bases; issue16772."""
310 with self.assertRaises(TypeError):
311 int('0', 5.5)
312 with self.assertRaises(TypeError):
313 int('0', 5.0)
314
315 def test_int_base_indexable(self):
316 class MyIndexable(object):
317 def __init__(self, value):
318 self.value = value
319 def __index__(self):
320 return self.value
321
322 # Check out of range bases.
323 for base in 2**100, -2**100, 1, 37:
324 with self.assertRaises(ValueError):
325 int('43', base)
326
327 # Check in-range bases.
328 self.assertEqual(int('101', base=MyIndexable(2)), 5)
329 self.assertEqual(int('101', base=MyIndexable(10)), 101)
330 self.assertEqual(int('101', base=MyIndexable(36)), 1 + 36**2)
331
332 def test_non_numeric_input_types(self):
333 # Test possible non-numeric types for the argument x, including
334 # subclasses of the explicitly documented accepted types.
335 class CustomStr(str): pass
336 class CustomBytes(bytes): pass
337 class CustomByteArray(bytearray): pass
338
339 factories = [
340 bytes,
341 bytearray,
342 lambda b: CustomStr(b.decode()),
343 CustomBytes,
344 CustomByteArray,
345 memoryview,
346 ]
347 try:
348 from array import array
349 except ImportError:
350 pass
351 else:
352 factories.append(lambda b: array('B', b))
353
354 for f in factories:
355 x = f(b'100')
356 with self.subTest(type(x)):
357 self.assertEqual(int(x), 100)
358 if isinstance(x, (str, bytes, bytearray)):
359 self.assertEqual(int(x, 2), 4)
360 else:
361 msg = "can't convert non-string"
362 with self.assertRaisesRegex(TypeError, msg):
363 int(x, 2)
364 with self.assertRaisesRegex(ValueError, 'invalid literal'):
365 int(f(b'A' * 0x10))
366
367 def test_int_memoryview(self):
368 self.assertEqual(int(memoryview(b'123')[1:3]), 23)
369 self.assertEqual(int(memoryview(b'123\x00')[1:3]), 23)
370 self.assertEqual(int(memoryview(b'123 ')[1:3]), 23)
371 self.assertEqual(int(memoryview(b'123A')[1:3]), 23)
372 self.assertEqual(int(memoryview(b'1234')[1:3]), 23)
373
374 def test_string_float(self):
375 self.assertRaises(ValueError, int, '1.2')
376
377 def test_intconversion(self):
378 # Test __int__()
379 class ClassicMissingMethods:
380 pass
381 self.assertRaises(TypeError, int, ClassicMissingMethods())
382
383 class MissingMethods(object):
384 pass
385 self.assertRaises(TypeError, int, MissingMethods())
386
387 class Foo0:
388 def __int__(self):
389 return 42
390
391 self.assertEqual(int(Foo0()), 42)
392
393 class Classic:
394 pass
395 for base in (object, Classic):
396 class IntOverridesTrunc(base):
397 def __int__(self):
398 return 42
399 def __trunc__(self):
400 return -12
401 self.assertEqual(int(IntOverridesTrunc()), 42)
402
403 class JustTrunc(base):
404 def __trunc__(self):
405 return 42
406 with self.assertRaises(TypeError):
407 int(JustTrunc())
408
409 def test_int_subclass_with_index(self):
410 class MyIndex(int):
411 def __index__(self):
412 return 42
413
414 class BadIndex(int):
415 def __index__(self):
416 return 42.0
417
418 my_int = MyIndex(7)
419 self.assertEqual(my_int, 7)
420 self.assertEqual(int(my_int), 7)
421
422 self.assertEqual(int(BadIndex()), 0)
423
424 def test_int_subclass_with_int(self):
425 class MyInt(int):
426 def __int__(self):
427 return 42
428
429 class BadInt(int):
430 def __int__(self):
431 return 42.0
432
433 my_int = MyInt(7)
434 self.assertEqual(my_int, 7)
435 self.assertEqual(int(my_int), 42)
436
437 my_int = BadInt(7)
438 self.assertEqual(my_int, 7)
439 self.assertRaises(TypeError, int, my_int)
440
441 def test_int_returns_int_subclass(self):
442 class BadIndex:
443 def __index__(self):
444 return True
445
446 class BadIndex2(int):
447 def __index__(self):
448 return True
449
450 class BadInt:
451 def __int__(self):
452 return True
453
454 class BadInt2(int):
455 def __int__(self):
456 return True
457
458 bad_int = BadIndex()
459 with self.assertWarns(DeprecationWarning):
460 n = int(bad_int)
461 self.assertEqual(n, 1)
462 self.assertIs(type(n), int)
463
464 bad_int = BadIndex2()
465 n = int(bad_int)
466 self.assertEqual(n, 0)
467 self.assertIs(type(n), int)
468
469 bad_int = BadInt()
470 with self.assertWarns(DeprecationWarning):
471 n = int(bad_int)
472 self.assertEqual(n, 1)
473 self.assertIs(type(n), int)
474
475 bad_int = BadInt2()
476 with self.assertWarns(DeprecationWarning):
477 n = int(bad_int)
478 self.assertEqual(n, 1)
479 self.assertIs(type(n), int)
480
481 def test_error_message(self):
482 def check(s, base=None):
483 with self.assertRaises(ValueError,
484 msg="int(%r, %r)" % (s, base)) as cm:
485 if base is None:
486 int(s)
487 else:
488 int(s, base)
489 self.assertEqual(cm.exception.args[0],
490 "invalid literal for int() with base %d: %r" %
491 (10 if base is None else base, s))
492
493 check('\xbd')
494 check('123\xbd')
495 check(' 123 456 ')
496
497 check('123\x00')
498 # SF bug 1545497: embedded NULs were not detected with explicit base
499 check('123\x00', 10)
500 check('123\x00 245', 20)
501 check('123\x00 245', 16)
502 check('123\x00245', 20)
503 check('123\x00245', 16)
504 # byte string with embedded NUL
505 check(b'123\x00')
506 check(b'123\x00', 10)
507 # non-UTF-8 byte string
508 check(b'123\xbd')
509 check(b'123\xbd', 10)
510 # lone surrogate in Unicode string
511 check('123\ud800')
512 check('123\ud800', 10)
513
514 def test_issue31619(self):
515 self.assertEqual(int('1_0_1_0_1_0_1_0_1_0_1_0_1_0_1_0_1_0_1_0_1_0_1_0_1_0_1_0_1_0_1', 2),
516 0b1010101010101010101010101010101)
517 self.assertEqual(int('1_2_3_4_5_6_7_0_1_2_3', 8), 0o12345670123)
518 self.assertEqual(int('1_2_3_4_5_6_7_8_9', 16), 0x123456789)
519 self.assertEqual(int('1_2_3_4_5_6_7', 32), 1144132807)
520
521 @support.cpython_only
522 def test_round_with_none_arg_direct_call(self):
523 for val in [(1).__round__(None),
524 round(1),
525 round(1, None)]:
526 self.assertEqual(val, 1)
527 self.assertIs(type(val), int)
528
529 class IntStrDigitLimitsTests(unittest.TestCase):
530
531 int_class = int # Override this in subclasses to reuse the suite.
532
533 def setUp(self):
534 super().setUp()
535 self._previous_limit = sys.get_int_max_str_digits()
536 sys.set_int_max_str_digits(2048)
537
538 def tearDown(self):
539 sys.set_int_max_str_digits(self._previous_limit)
540 super().tearDown()
541
542 def test_disabled_limit(self):
543 self.assertGreater(sys.get_int_max_str_digits(), 0)
544 self.assertLess(sys.get_int_max_str_digits(), 20_000)
545 with support.adjust_int_max_str_digits(0):
546 self.assertEqual(sys.get_int_max_str_digits(), 0)
547 i = self.int_class('1' * 20_000)
548 str(i)
549 self.assertGreater(sys.get_int_max_str_digits(), 0)
550
551 def test_max_str_digits_edge_cases(self):
552 """Ignore the +/- sign and space padding."""
553 int_class = self.int_class
554 maxdigits = sys.get_int_max_str_digits()
555
556 int_class('1' * maxdigits)
557 int_class(' ' + '1' * maxdigits)
558 int_class('1' * maxdigits + ' ')
559 int_class('+' + '1' * maxdigits)
560 int_class('-' + '1' * maxdigits)
561 self.assertEqual(len(str(10 ** (maxdigits - 1))), maxdigits)
562
563 def check(self, i, base=None):
564 with self.assertRaises(ValueError):
565 if base is None:
566 self.int_class(i)
567 else:
568 self.int_class(i, base)
569
570 def test_max_str_digits(self):
571 maxdigits = sys.get_int_max_str_digits()
572
573 self.check('1' * (maxdigits + 1))
574 self.check(' ' + '1' * (maxdigits + 1))
575 self.check('1' * (maxdigits + 1) + ' ')
576 self.check('+' + '1' * (maxdigits + 1))
577 self.check('-' + '1' * (maxdigits + 1))
578 self.check('1' * (maxdigits + 1))
579
580 i = 10 ** maxdigits
581 with self.assertRaises(ValueError):
582 str(i)
583
584 def test_denial_of_service_prevented_int_to_str(self):
585 """Regression test: ensure we fail before performing O(N**2) work."""
586 maxdigits = sys.get_int_max_str_digits()
587 assert maxdigits < 50_000, maxdigits # A test prerequisite.
588
589 huge_int = int(f'0x{"c"*65_000}', base=16) # 78268 decimal digits.
590 digits = 78_268
591 with (
592 support.adjust_int_max_str_digits(digits),
593 support.Stopwatch() as sw_convert):
594 huge_decimal = str(huge_int)
595 self.assertEqual(len(huge_decimal), digits)
596 # Ensuring that we chose a slow enough conversion to measure.
597 # It takes 0.1 seconds on a Zen based cloud VM in an opt build.
598 # Some OSes have a low res 1/64s timer, skip if hard to measure.
599 if sw_convert.seconds < sw_convert.clock_info.resolution * 2:
600 raise unittest.SkipTest('"slow" conversion took only '
601 f'{sw_convert.seconds} seconds.')
602
603 # We test with the limit almost at the size needed to check performance.
604 # The performant limit check is slightly fuzzy, give it a some room.
605 with support.adjust_int_max_str_digits(int(.995 * digits)):
606 with (
607 self.assertRaises(ValueError) as err,
608 support.Stopwatch() as sw_fail_huge):
609 str(huge_int)
610 self.assertIn('conversion', str(err.exception))
611 self.assertLessEqual(sw_fail_huge.seconds, sw_convert.seconds/2)
612
613 # Now we test that a conversion that would take 30x as long also fails
614 # in a similarly fast fashion.
615 extra_huge_int = int(f'0x{"c"*500_000}', base=16) # 602060 digits.
616 with (
617 self.assertRaises(ValueError) as err,
618 support.Stopwatch() as sw_fail_extra_huge):
619 # If not limited, 8 seconds said Zen based cloud VM.
620 str(extra_huge_int)
621 self.assertIn('conversion', str(err.exception))
622 self.assertLess(sw_fail_extra_huge.seconds, sw_convert.seconds/2)
623
624 def test_denial_of_service_prevented_str_to_int(self):
625 """Regression test: ensure we fail before performing O(N**2) work."""
626 maxdigits = sys.get_int_max_str_digits()
627 assert maxdigits < 100_000, maxdigits # A test prerequisite.
628
629 digits = 133700
630 huge = '8'*digits
631 with (
632 support.adjust_int_max_str_digits(digits),
633 support.Stopwatch() as sw_convert):
634 int(huge)
635 # Ensuring that we chose a slow enough conversion to measure.
636 # It takes 0.1 seconds on a Zen based cloud VM in an opt build.
637 # Some OSes have a low res 1/64s timer, skip if hard to measure.
638 if sw_convert.seconds < sw_convert.clock_info.resolution * 2:
639 raise unittest.SkipTest('"slow" conversion took only '
640 f'{sw_convert.seconds} seconds.')
641
642 with support.adjust_int_max_str_digits(digits - 1):
643 with (
644 self.assertRaises(ValueError) as err,
645 support.Stopwatch() as sw_fail_huge):
646 int(huge)
647 self.assertIn('conversion', str(err.exception))
648 self.assertLessEqual(sw_fail_huge.seconds, sw_convert.seconds/2)
649
650 # Now we test that a conversion that would take 30x as long also fails
651 # in a similarly fast fashion.
652 extra_huge = '7'*1_200_000
653 with (
654 self.assertRaises(ValueError) as err,
655 support.Stopwatch() as sw_fail_extra_huge):
656 # If not limited, 8 seconds in the Zen based cloud VM.
657 int(extra_huge)
658 self.assertIn('conversion', str(err.exception))
659 self.assertLessEqual(sw_fail_extra_huge.seconds, sw_convert.seconds/2)
660
661 def test_power_of_two_bases_unlimited(self):
662 """The limit does not apply to power of 2 bases."""
663 maxdigits = sys.get_int_max_str_digits()
664
665 for base in (2, 4, 8, 16, 32):
666 with self.subTest(base=base):
667 self.int_class('1' * (maxdigits + 1), base)
668 assert maxdigits < 100_000
669 self.int_class('1' * 100_000, base)
670
671 def test_underscores_ignored(self):
672 maxdigits = sys.get_int_max_str_digits()
673
674 triples = maxdigits // 3
675 s = '111' * triples
676 s_ = '1_11' * triples
677 self.int_class(s) # succeeds
678 self.int_class(s_) # succeeds
679 self.check(f'{s}111')
680 self.check(f'{s_}_111')
681
682 def test_sign_not_counted(self):
683 int_class = self.int_class
684 max_digits = sys.get_int_max_str_digits()
685 s = '5' * max_digits
686 i = int_class(s)
687 pos_i = int_class(f'+{s}')
688 assert i == pos_i
689 neg_i = int_class(f'-{s}')
690 assert -pos_i == neg_i
691 str(pos_i)
692 str(neg_i)
693
694 def _other_base_helper(self, base):
695 int_class = self.int_class
696 max_digits = sys.get_int_max_str_digits()
697 s = '2' * max_digits
698 i = int_class(s, base)
699 if base > 10:
700 with self.assertRaises(ValueError):
701 str(i)
702 elif base < 10:
703 str(i)
704 with self.assertRaises(ValueError) as err:
705 int_class(f'{s}1', base)
706
707 def test_int_from_other_bases(self):
708 base = 3
709 with self.subTest(base=base):
710 self._other_base_helper(base)
711 base = 36
712 with self.subTest(base=base):
713 self._other_base_helper(base)
714
715 def test_int_max_str_digits_is_per_interpreter(self):
716 # Changing the limit in one interpreter does not change others.
717 code = """if 1:
718 # Subinterpreters maintain and enforce their own limit
719 import sys
720 sys.set_int_max_str_digits(2323)
721 try:
722 int('3'*3333)
723 except ValueError:
724 pass
725 else:
726 raise AssertionError('Expected a int max str digits ValueError.')
727 """
728 with support.adjust_int_max_str_digits(4000):
729 before_value = sys.get_int_max_str_digits()
730 self.assertEqual(support.run_in_subinterp(code), 0,
731 'subinterp code failure, check stderr.')
732 after_value = sys.get_int_max_str_digits()
733 self.assertEqual(before_value, after_value)
734
735
736 class IntSubclassStrDigitLimitsTests(IntStrDigitLimitsTests):
737 int_class = IntSubclass
738
739
740 class PyLongModuleTests(unittest.TestCase):
741 # Tests of the functions in _pylong.py. Those get used when the
742 # number of digits in the input values are large enough.
743
744 def setUp(self):
745 super().setUp()
746 self._previous_limit = sys.get_int_max_str_digits()
747 sys.set_int_max_str_digits(0)
748
749 def tearDown(self):
750 sys.set_int_max_str_digits(self._previous_limit)
751 super().tearDown()
752
753 def _test_pylong_int_to_decimal(self, n, suffix):
754 s = str(n)
755 self.assertEqual(s[-10:], suffix)
756 s2 = str(-n)
757 self.assertEqual(s2, '-' + s)
758 s3 = '%d' % n
759 self.assertEqual(s3, s)
760 s4 = b'%d' % n
761 self.assertEqual(s4, s.encode('ascii'))
762
763 def test_pylong_int_to_decimal(self):
764 self._test_pylong_int_to_decimal((1 << 100_000), '9883109376')
765 self._test_pylong_int_to_decimal((1 << 100_000) - 1, '9883109375')
766 self._test_pylong_int_to_decimal(10**30_000, '0000000000')
767 self._test_pylong_int_to_decimal(10**30_000 - 1, '9999999999')
768 self._test_pylong_int_to_decimal(3**60_000, '9313200001')
769
770 @support.requires_resource('cpu')
771 def test_pylong_int_to_decimal_2(self):
772 self._test_pylong_int_to_decimal(2**1_000_000, '2747109376')
773 self._test_pylong_int_to_decimal(10**300_000, '0000000000')
774 self._test_pylong_int_to_decimal(3**600_000, '3132000001')
775
776 def test_pylong_int_divmod(self):
777 n = (1 << 100_000)
778 a, b = divmod(n*3 + 1, n)
779 assert a == 3 and b == 1
780
781 @support.cpython_only # tests implementation details of CPython.
782 @unittest.skipUnless(_pylong, "_pylong module required")
783 def test_pylong_int_divmod_crash(self):
784 # Regression test for https://github.com/python/cpython/issues/142554.
785 bad_int_divmod = lambda a, b: (1,)
786 # 'k' chosen such that divmod(2**(2*k), 2**k) uses _pylong.int_divmod()
787 k = 10_000
788 a, b = (1 << (2 * k)), (1 << k)
789 with mock.patch.object(_pylong, "int_divmod", wraps=bad_int_divmod):
790 msg = r"tuple of length 2 is required from int_divmod\(\)"
791 self.assertRaisesRegex(ValueError, msg, divmod, a, b)
792
793 def test_pylong_str_to_int(self):
794 v1 = 1 << 100_000
795 s = str(v1)
796 v2 = int(s)
797 assert v1 == v2
798 v3 = int(' -' + s)
799 assert -v1 == v3
800 v4 = int(' +' + s + ' ')
801 assert v1 == v4
802 with self.assertRaises(ValueError) as err:
803 int(s + 'z')
804 with self.assertRaises(ValueError) as err:
805 int(s + '_')
806 with self.assertRaises(ValueError) as err:
807 int('_' + s)
808
809 @support.cpython_only # tests implementation details of CPython.
810 @unittest.skipUnless(_pylong, "_pylong module required")
811 @mock.patch.object(_pylong, "int_to_decimal_string")
812 def test_pylong_misbehavior_error_path_to_str(
813 self, mock_int_to_str):
814 with support.adjust_int_max_str_digits(20_000):
815 big_value = int('7'*19_999)
816 mock_int_to_str.return_value = None # not a str
817 with self.assertRaises(TypeError) as ctx:
818 str(big_value)
819 self.assertIn('_pylong.int_to_decimal_string did not',
820 str(ctx.exception))
821 mock_int_to_str.side_effect = RuntimeError("testABC")
822 with self.assertRaises(RuntimeError):
823 str(big_value)
824
825 @support.cpython_only # tests implementation details of CPython.
826 @unittest.skipUnless(_pylong, "_pylong module required")
827 @mock.patch.object(_pylong, "int_from_string")
828 def test_pylong_misbehavior_error_path_from_str(
829 self, mock_int_from_str):
830 big_value = '7'*19_999
831 with support.adjust_int_max_str_digits(20_000):
832 mock_int_from_str.return_value = b'not an int'
833 with self.assertRaises(TypeError) as ctx:
834 int(big_value)
835 self.assertIn('_pylong.int_from_string did not',
836 str(ctx.exception))
837
838 mock_int_from_str.side_effect = RuntimeError("test123")
839 with self.assertRaises(RuntimeError):
840 int(big_value)
841
842 def test_pylong_roundtrip(self):
843 from random import randrange, getrandbits
844 bits = 5000
845 while bits <= 1_000_000:
846 bits += randrange(-100, 101) # break bitlength patterns
847 hibit = 1 << (bits - 1)
848 n = hibit | getrandbits(bits - 1)
849 assert n.bit_length() == bits
850 sn = str(n)
851 self.assertNotStartsWith(sn, '0')
852 self.assertEqual(n, int(sn))
853 bits <<= 1
854
855 @support.requires_resource('cpu')
856 @unittest.skipUnless(_decimal, "C _decimal module required")
857 def test_pylong_roundtrip_huge(self):
858 # k blocks of 1234567890
859 k = 1_000_000 # so 10 million digits in all
860 tentoten = 10**10
861 n = 1234567890 * ((tentoten**k - 1) // (tentoten - 1))
862 sn = "1234567890" * k
863 self.assertEqual(n, int(sn))
864 self.assertEqual(sn, str(n))
865
866 @support.requires_resource('cpu')
867 @unittest.skipUnless(_pylong, "_pylong module required")
868 @unittest.skipUnless(_decimal, "C _decimal module required")
869 def test_whitebox_dec_str_to_int_inner_failsafe(self):
870 # While I believe the number of GUARD digits in this function is
871 # always enough so that no more than one correction step is ever
872 # needed, the code has a "failsafe" path that takes over if I'm
873 # wrong about that. We have no input that reaches that block.
874 # Here we test a contrived input that _does_ reach that block,
875 # provided the number of guard digits is reduced to 1.
876 sn = "9" * 2000156
877 n = 10**len(sn) - 1
878 orig_spread = _pylong._spread.copy()
879 _pylong._spread.clear()
880 try:
881 self.assertEqual(n, _pylong._dec_str_to_int_inner(sn, GUARD=1))
882 self.assertIn(999, _pylong._spread)
883 finally:
884 _pylong._spread.clear()
885 _pylong._spread.update(orig_spread)
886
887 @unittest.skipUnless(_pylong, "pylong module required")
888 @unittest.skipUnless(_decimal, "C _decimal module required")
889 def test_whitebox_dec_str_to_int_inner_monster(self):
890 # I don't think anyone has enough RAM to build a string long enough
891 # for this function to complain. So lie about the string length.
892
893 class LyingStr(str):
894 def __len__(self):
895 return int((1 << 47) / _pylong._LOG_10_BASE_256)
896
897 liar = LyingStr("42")
898 # We have to pass the liar directly to the complaining function. If we
899 # just try `int(liar)`, earlier layers will replace it with plain old
900 # "43".
901 # Embedding `len(liar)` into the f-string failed on the WASI testbot
902 # (don't know what that is):
903 # OverflowError: cannot fit 'int' into an index-sized integer
904 # So a random stab at worming around that.
905 self.assertRaisesRegex(ValueError,
906 f"^cannot convert string of len {liar.__len__()} to int$",
907 _pylong._dec_str_to_int_inner,
908 liar)
909
910 @unittest.skipUnless(_pylong, "_pylong module required")
911 def test_pylong_compute_powers(self):
912 # Basic sanity tests. See end of _pylong.py for manual heavy tests.
913 def consumer(w, base, limit, need_hi):
914 seen = set()
915 need = set()
916 def inner(w):
917 if w <= limit or w in seen:
918 return
919 seen.add(w)
920 lo = w >> 1
921 hi = w - lo
922 need.add(hi if need_hi else lo)
923 inner(lo)
924 inner(hi)
925 inner(w)
926 d = _pylong.compute_powers(w, base, limit, need_hi=need_hi)
927 self.assertEqual(d.keys(), need)
928 for k, v in d.items():
929 self.assertEqual(v, base ** k)
930
931 for base in 2, 5:
932 for need_hi in False, True:
933 for limit in 1, 11:
934 for w in range(250, 550):
935 consumer(w, base, limit, need_hi)
936
937 if __name__ == "__main__":
938 unittest.main()