Debian Codegraph @

Lib/test/test_capi/test_long.py

raw
1 import unittest
2 import sys
3 import test.support as support
4
5 from test.support import import_helper
6
7 # Skip this test if the _testcapi and _testlimitedcapi modules isn't available.
8 _testcapi = import_helper.import_module('_testcapi')
9 _testlimitedcapi = import_helper.import_module('_testlimitedcapi')
10
11 NULL = None
12
13
14 class IntSubclass(int):
15 pass
16
17 class Index:
18 def __init__(self, value):
19 self.value = value
20
21 def __index__(self):
22 return self.value
23
24 # use __index__(), not __int__()
25 class MyIndexAndInt:
26 def __index__(self):
27 return 10
28 def __int__(self):
29 return 22
30
31
32 class LongTests(unittest.TestCase):
33
34 def test_compact(self):
35 for n in {
36 # Edge cases
37 *(2**n for n in range(66)),
38 *(-2**n for n in range(66)),
39 *(2**n - 1 for n in range(66)),
40 *(-2**n + 1 for n in range(66)),
41 # Essentially random
42 *(37**n for n in range(14)),
43 *(-37**n for n in range(14)),
44 }:
45 with self.subTest(n=n):
46 is_compact, value = _testcapi.call_long_compact_api(n)
47 if is_compact:
48 self.assertEqual(n, value)
49
50 def test_compact_known(self):
51 # Sanity-check some implementation details (we don't guarantee
52 # that these are/aren't compact)
53 self.assertEqual(_testcapi.call_long_compact_api(-1), (True, -1))
54 self.assertEqual(_testcapi.call_long_compact_api(0), (True, 0))
55 self.assertEqual(_testcapi.call_long_compact_api(256), (True, 256))
56 self.assertEqual(_testcapi.call_long_compact_api(sys.maxsize),
57 (False, -1))
58
59 def test_long_check(self):
60 # Test PyLong_Check()
61 check = _testlimitedcapi.pylong_check
62 self.assertTrue(check(1))
63 self.assertTrue(check(123456789012345678901234567890))
64 self.assertTrue(check(-1))
65 self.assertTrue(check(True))
66 self.assertTrue(check(IntSubclass(1)))
67 self.assertFalse(check(1.0))
68 self.assertFalse(check(object()))
69 # CRASHES check(NULL)
70
71 def test_long_checkexact(self):
72 # Test PyLong_CheckExact()
73 check = _testlimitedcapi.pylong_checkexact
74 self.assertTrue(check(1))
75 self.assertTrue(check(123456789012345678901234567890))
76 self.assertTrue(check(-1))
77 self.assertFalse(check(True))
78 self.assertFalse(check(IntSubclass(1)))
79 self.assertFalse(check(1.0))
80 self.assertFalse(check(object()))
81 # CRASHES check(NULL)
82
83 def test_long_fromdouble(self):
84 # Test PyLong_FromDouble()
85 fromdouble = _testlimitedcapi.pylong_fromdouble
86 float_max = sys.float_info.max
87 for value in (5.0, 5.1, 5.9, -5.1, -5.9, 0.0, -0.0, float_max, -float_max):
88 with self.subTest(value=value):
89 self.assertEqual(fromdouble(value), int(value))
90 self.assertRaises(OverflowError, fromdouble, float('inf'))
91 self.assertRaises(OverflowError, fromdouble, float('-inf'))
92 self.assertRaises(ValueError, fromdouble, float('nan'))
93
94 def test_long_fromvoidptr(self):
95 # Test PyLong_FromVoidPtr()
96 fromvoidptr = _testlimitedcapi.pylong_fromvoidptr
97 obj = object()
98 x = fromvoidptr(obj)
99 y = fromvoidptr(NULL)
100 self.assertIsInstance(x, int)
101 self.assertGreaterEqual(x, 0)
102 self.assertIsInstance(y, int)
103 self.assertEqual(y, 0)
104 self.assertNotEqual(x, y)
105
106 def test_long_fromstring(self):
107 # Test PyLong_FromString()
108 fromstring = _testlimitedcapi.pylong_fromstring
109 self.assertEqual(fromstring(b'123', 10), (123, 3))
110 self.assertEqual(fromstring(b'cafe', 16), (0xcafe, 4))
111 self.assertEqual(fromstring(b'xyz', 36), (44027, 3))
112 self.assertEqual(fromstring(b'123', 0), (123, 3))
113 self.assertEqual(fromstring(b'0xcafe', 0), (0xcafe, 6))
114 self.assertRaises(ValueError, fromstring, b'cafe', 0)
115 self.assertEqual(fromstring(b'-123', 10), (-123, 4))
116 self.assertEqual(fromstring(b' -123 ', 10), (-123, 6))
117 self.assertEqual(fromstring(b'1_23', 10), (123, 4))
118 self.assertRaises(ValueError, fromstring, b'- 123', 10)
119 self.assertRaises(ValueError, fromstring, b'', 10)
120
121 self.assertRaises(ValueError, fromstring, b'123', 1)
122 self.assertRaises(ValueError, fromstring, b'123', -1)
123 self.assertRaises(ValueError, fromstring, b'123', 37)
124
125 self.assertRaises(ValueError, fromstring, '١٢٣٤٥٦٧٨٩٠'.encode(), 0)
126 self.assertRaises(ValueError, fromstring, '١٢٣٤٥٦٧٨٩٠'.encode(), 16)
127
128 self.assertEqual(fromstring(b'123\x00', 0), (123, 3))
129 self.assertEqual(fromstring(b'123\x00456', 0), (123, 3))
130 self.assertEqual(fromstring(b'123\x00', 16), (0x123, 3))
131 self.assertEqual(fromstring(b'123\x00456', 16), (0x123, 3))
132
133 # CRASHES fromstring(NULL, 0)
134 # CRASHES fromstring(NULL, 16)
135
136 def test_long_fromunicodeobject(self):
137 # Test PyLong_FromUnicodeObject()
138 fromunicodeobject = _testcapi.pylong_fromunicodeobject
139 self.assertEqual(fromunicodeobject('123', 10), 123)
140 self.assertEqual(fromunicodeobject('cafe', 16), 0xcafe)
141 self.assertEqual(fromunicodeobject('xyz', 36), 44027)
142 self.assertEqual(fromunicodeobject('123', 0), 123)
143 self.assertEqual(fromunicodeobject('0xcafe', 0), 0xcafe)
144 self.assertRaises(ValueError, fromunicodeobject, 'cafe', 0)
145 self.assertEqual(fromunicodeobject('-123', 10), -123)
146 self.assertEqual(fromunicodeobject(' -123 ', 10), -123)
147 self.assertEqual(fromunicodeobject('1_23', 10), 123)
148 self.assertRaises(ValueError, fromunicodeobject, '- 123', 10)
149 self.assertRaises(ValueError, fromunicodeobject, '', 10)
150
151 self.assertRaises(ValueError, fromunicodeobject, '123', 1)
152 self.assertRaises(ValueError, fromunicodeobject, '123', -1)
153 self.assertRaises(ValueError, fromunicodeobject, '123', 37)
154
155 self.assertEqual(fromunicodeobject('١٢٣٤٥٦٧٨٩٠', 0), 1234567890)
156 self.assertEqual(fromunicodeobject('١٢٣٤٥٦٧٨٩٠', 16), 0x1234567890)
157
158 self.assertRaises(ValueError, fromunicodeobject, '123\x00', 0)
159 self.assertRaises(ValueError, fromunicodeobject, '123\x00456', 0)
160 self.assertRaises(ValueError, fromunicodeobject, '123\x00', 16)
161 self.assertRaises(ValueError, fromunicodeobject, '123\x00456', 16)
162
163 # CRASHES fromunicodeobject(NULL, 0)
164 # CRASHES fromunicodeobject(NULL, 16)
165
166 def check_long_asint(self, func, min_val, max_val, *,
167 use_index=True,
168 mask=False,
169 negative_value_error=OverflowError):
170 # round trip (object -> C integer -> object)
171 values = (0, 1, 512, 1234, max_val)
172 if min_val < 0:
173 values += (-1, -512, -1234, min_val)
174 for value in values:
175 with self.subTest(value=value):
176 self.assertEqual(func(value), value)
177 self.assertEqual(func(IntSubclass(value)), value)
178 if use_index:
179 self.assertEqual(func(Index(value)), value)
180
181 if use_index:
182 self.assertEqual(func(MyIndexAndInt()), 10)
183 else:
184 self.assertRaises(TypeError, func, Index(42))
185 self.assertRaises(TypeError, func, MyIndexAndInt())
186
187 if mask:
188 self.assertEqual(func(min_val - 1), max_val)
189 self.assertEqual(func(max_val + 1), min_val)
190 self.assertEqual(func(-1 << 1000), 0)
191 self.assertEqual(func(1 << 1000), 0)
192 else:
193 self.assertRaises(negative_value_error, func, min_val - 1)
194 self.assertRaises(negative_value_error, func, -1 << 1000)
195 self.assertRaises(OverflowError, func, max_val + 1)
196 self.assertRaises(OverflowError, func, 1 << 1000)
197 self.assertRaises(TypeError, func, 1.0)
198 self.assertRaises(TypeError, func, b'2')
199 self.assertRaises(TypeError, func, '3')
200 self.assertRaises(SystemError, func, NULL)
201
202 def check_long_asintandoverflow(self, func, min_val, max_val):
203 # round trip (object -> C integer -> object)
204 for value in (min_val, max_val, -1, 0, 1, 1234):
205 with self.subTest(value=value):
206 self.assertEqual(func(value), (value, 0))
207 self.assertEqual(func(IntSubclass(value)), (value, 0))
208 self.assertEqual(func(Index(value)), (value, 0))
209
210 self.assertEqual(func(MyIndexAndInt()), (10, 0))
211
212 self.assertEqual(func(min_val - 1), (-1, -1))
213 self.assertEqual(func(max_val + 1), (-1, +1))
214 self.assertRaises(SystemError, func, None)
215 self.assertRaises(TypeError, func, 1.0)
216
217 def test_long_asint(self):
218 # Test PyLong_AsInt()
219 PyLong_AsInt = _testlimitedcapi.PyLong_AsInt
220 from _testcapi import INT_MIN, INT_MAX
221 self.check_long_asint(PyLong_AsInt, INT_MIN, INT_MAX)
222
223 def test_long_aslong(self):
224 # Test PyLong_AsLong() and PyLong_FromLong()
225 aslong = _testlimitedcapi.pylong_aslong
226 from _testcapi import LONG_MIN, LONG_MAX
227 self.check_long_asint(aslong, LONG_MIN, LONG_MAX)
228
229 def test_long_aslongandoverflow(self):
230 # Test PyLong_AsLongAndOverflow()
231 aslongandoverflow = _testlimitedcapi.pylong_aslongandoverflow
232 from _testcapi import LONG_MIN, LONG_MAX
233 self.check_long_asintandoverflow(aslongandoverflow, LONG_MIN, LONG_MAX)
234
235 def test_long_asunsignedlong(self):
236 # Test PyLong_AsUnsignedLong() and PyLong_FromUnsignedLong()
237 asunsignedlong = _testlimitedcapi.pylong_asunsignedlong
238 from _testcapi import ULONG_MAX
239 self.check_long_asint(asunsignedlong, 0, ULONG_MAX,
240 use_index=False)
241
242 def test_long_asunsignedlongmask(self):
243 # Test PyLong_AsUnsignedLongMask()
244 asunsignedlongmask = _testlimitedcapi.pylong_asunsignedlongmask
245 from _testcapi import ULONG_MAX
246 self.check_long_asint(asunsignedlongmask, 0, ULONG_MAX, mask=True)
247
248 def test_long_aslonglong(self):
249 # Test PyLong_AsLongLong() and PyLong_FromLongLong()
250 aslonglong = _testlimitedcapi.pylong_aslonglong
251 from _testcapi import LLONG_MIN, LLONG_MAX
252 self.check_long_asint(aslonglong, LLONG_MIN, LLONG_MAX)
253
254 def test_long_aslonglongandoverflow(self):
255 # Test PyLong_AsLongLongAndOverflow()
256 aslonglongandoverflow = _testlimitedcapi.pylong_aslonglongandoverflow
257 from _testcapi import LLONG_MIN, LLONG_MAX
258 self.check_long_asintandoverflow(aslonglongandoverflow, LLONG_MIN, LLONG_MAX)
259
260 def test_long_asunsignedlonglong(self):
261 # Test PyLong_AsUnsignedLongLong() and PyLong_FromUnsignedLongLong()
262 asunsignedlonglong = _testlimitedcapi.pylong_asunsignedlonglong
263 from _testcapi import ULLONG_MAX
264 self.check_long_asint(asunsignedlonglong, 0, ULLONG_MAX, use_index=False)
265
266 def test_long_asunsignedlonglongmask(self):
267 # Test PyLong_AsUnsignedLongLongMask()
268 asunsignedlonglongmask = _testlimitedcapi.pylong_asunsignedlonglongmask
269 from _testcapi import ULLONG_MAX
270 self.check_long_asint(asunsignedlonglongmask, 0, ULLONG_MAX, mask=True)
271
272 def test_long_as_ssize_t(self):
273 # Test PyLong_AsSsize_t() and PyLong_FromSsize_t()
274 as_ssize_t = _testlimitedcapi.pylong_as_ssize_t
275 from _testcapi import PY_SSIZE_T_MIN, PY_SSIZE_T_MAX
276 self.check_long_asint(as_ssize_t, PY_SSIZE_T_MIN, PY_SSIZE_T_MAX,
277 use_index=False)
278
279 def test_long_as_size_t(self):
280 # Test PyLong_AsSize_t() and PyLong_FromSize_t()
281 as_size_t = _testlimitedcapi.pylong_as_size_t
282 from _testcapi import SIZE_MAX
283 self.check_long_asint(as_size_t, 0, SIZE_MAX, use_index=False)
284
285 def test_long_asdouble(self):
286 # Test PyLong_AsDouble()
287 asdouble = _testlimitedcapi.pylong_asdouble
288 MAX = int(sys.float_info.max)
289 for value in (-MAX, MAX, -1, 0, 1, 1234):
290 with self.subTest(value=value):
291 self.assertEqual(asdouble(value), float(value))
292 self.assertIsInstance(asdouble(value), float)
293
294 self.assertEqual(asdouble(IntSubclass(42)), 42.0)
295 self.assertRaises(TypeError, asdouble, Index(42))
296 self.assertRaises(TypeError, asdouble, MyIndexAndInt())
297
298 self.assertRaises(OverflowError, asdouble, 2 * MAX)
299 self.assertRaises(OverflowError, asdouble, -2 * MAX)
300 self.assertRaises(TypeError, asdouble, 1.0)
301 self.assertRaises(TypeError, asdouble, b'2')
302 self.assertRaises(TypeError, asdouble, '3')
303 self.assertRaises(SystemError, asdouble, NULL)
304
305 def test_long_asvoidptr(self):
306 # Test PyLong_AsVoidPtr()
307 fromvoidptr = _testlimitedcapi.pylong_fromvoidptr
308 asvoidptr = _testlimitedcapi.pylong_asvoidptr
309 obj = object()
310 x = fromvoidptr(obj)
311 y = fromvoidptr(NULL)
312 self.assertIs(asvoidptr(x), obj)
313 self.assertIs(asvoidptr(y), NULL)
314 self.assertIs(asvoidptr(IntSubclass(x)), obj)
315
316 # negative values
317 M = (1 << _testcapi.SIZEOF_VOID_P * 8)
318 if x >= M//2:
319 self.assertIs(asvoidptr(x - M), obj)
320 if y >= M//2:
321 self.assertIs(asvoidptr(y - M), NULL)
322
323 self.assertRaises(TypeError, asvoidptr, Index(x))
324 self.assertRaises(TypeError, asvoidptr, object())
325 self.assertRaises(OverflowError, asvoidptr, 2**1000)
326 self.assertRaises(OverflowError, asvoidptr, -2**1000)
327 # CRASHES asvoidptr(NULL)
328
329 def _test_long_aspid(self, aspid):
330 # Test PyLong_AsPid()
331 from _testcapi import SIZEOF_PID_T
332 bits = 8 * SIZEOF_PID_T
333 PID_T_MIN = -2**(bits-1)
334 PID_T_MAX = 2**(bits-1) - 1
335 self.check_long_asint(aspid, PID_T_MIN, PID_T_MAX)
336
337 def test_long_aspid(self):
338 self._test_long_aspid(_testcapi.pylong_aspid)
339
340 def test_long_aspid_limited(self):
341 self._test_long_aspid(_testlimitedcapi.pylong_aspid)
342
343 @support.bigmemtest(2**32, memuse=0.35)
344 def test_long_asnativebytes_huge(self, size):
345 asnativebytes = _testcapi.pylong_asnativebytes
346 v = 1 << size
347 buffer = bytearray(size * 2 // 15 + 10)
348 r = asnativebytes(v, buffer, 0, -1)
349 self.assertEqual(r, size // 8 + 1)
350 self.assertEqual(buffer.count(0), len(buffer))
351 r = asnativebytes(v, buffer, len(buffer), -1)
352 self.assertEqual(r, size // 8 + 1)
353 self.assertEqual(buffer.count(0), len(buffer) - 1)
354
355 def test_long_asnativebytes(self):
356 import math
357 from _testcapi import (
358 pylong_asnativebytes as asnativebytes,
359 SIZE_MAX,
360 )
361
362 # Abbreviate sizeof(Py_ssize_t) to SZ because we use it a lot
363 SZ = int(math.ceil(math.log(SIZE_MAX + 1) / math.log(2)) / 8)
364 MAX_SSIZE = 2 ** (SZ * 8 - 1) - 1
365 MAX_USIZE = 2 ** (SZ * 8) - 1
366 if support.verbose:
367 print(f"SIZEOF_SIZE={SZ}\n{MAX_SSIZE=:016X}\n{MAX_USIZE=:016X}")
368
369 # These tests check that the requested buffer size is correct.
370 # This matches our current implementation: We only specify that the
371 # return value is a size *sufficient* to hold the result when queried
372 # using n_bytes=0. If our implementation changes, feel free to update
373 # the expectations here -- or loosen them to be range checks.
374 # (i.e. 0 *could* be stored in 1 byte and 512 in 2)
375 for v, expect in [
376 (0, SZ),
377 (512, SZ),
378 (-512, SZ),
379 (MAX_SSIZE, SZ),
380 (MAX_USIZE, SZ + 1),
381 (-MAX_SSIZE, SZ),
382 (-MAX_USIZE, SZ + 1),
383 (2**255-1, 32),
384 (-(2**255-1), 32),
385 (2**255, 33),
386 (-(2**255), 33), # if you ask, we'll say 33, but 32 would do
387 (2**256-1, 33),
388 (-(2**256-1), 33),
389 (2**256, 33),
390 (-(2**256), 33),
391 ]:
392 with self.subTest(f"sizeof-{v:X}"):
393 buffer = bytearray(b"\x5a")
394 self.assertEqual(expect, asnativebytes(v, buffer, 0, -1),
395 "PyLong_AsNativeBytes(v, <unknown>, 0, -1)")
396 self.assertEqual(buffer, b"\x5a",
397 "buffer overwritten when it should not have been")
398 # Also check via the __index__ path.
399 # We pass Py_ASNATIVEBYTES_NATIVE_ENDIAN | ALLOW_INDEX
400 self.assertEqual(expect, asnativebytes(Index(v), buffer, 0, 3 | 16),
401 "PyLong_AsNativeBytes(Index(v), <unknown>, 0, -1)")
402 self.assertEqual(buffer, b"\x5a",
403 "buffer overwritten when it should not have been")
404
405 # Test that we populate n=2 bytes but do not overwrite more.
406 buffer = bytearray(b"\x99"*3)
407 self.assertEqual(2, asnativebytes(4, buffer, 2, 0), # BE
408 "PyLong_AsNativeBytes(v, <3 byte buffer>, 2, 0) // BE")
409 self.assertEqual(buffer, b"\x00\x04\x99")
410 self.assertEqual(2, asnativebytes(4, buffer, 2, 1), # LE
411 "PyLong_AsNativeBytes(v, <3 byte buffer>, 2, 1) // LE")
412 self.assertEqual(buffer, b"\x04\x00\x99")
413
414 # We request as many bytes as `expect_be` contains, and always check
415 # the result (both big and little endian). We check the return value
416 # independently, since the buffer should always be filled correctly even
417 # if we need more bytes
418 for v, expect_be, expect_n in [
419 (0, b'\x00', 1),
420 (0, b'\x00' * 2, 2),
421 (0, b'\x00' * 8, min(8, SZ)),
422 (1, b'\x01', 1),
423 (1, b'\x00' * 10 + b'\x01', min(11, SZ)),
424 (42, b'\x2a', 1),
425 (42, b'\x00' * 10 + b'\x2a', min(11, SZ)),
426 (-1, b'\xff', 1),
427 (-1, b'\xff' * 10, min(11, SZ)),
428 (-42, b'\xd6', 1),
429 (-42, b'\xff' * 10 + b'\xd6', min(11, SZ)),
430 # Extracts 255 into a single byte, but requests 2
431 # (this is currently a special case, and "should" request SZ)
432 (255, b'\xff', 2),
433 (255, b'\x00\xff', 2),
434 (256, b'\x01\x00', 2),
435 (0x80, b'\x00' * 7 + b'\x80', min(8, SZ)),
436 # Extracts successfully (unsigned), but requests 9 bytes
437 (2**63, b'\x80' + b'\x00' * 7, 9),
438 (2**63, b'\x00\x80' + b'\x00' * 7, 9),
439 # Extracts into 8 bytes, but if you provide 9 we'll say 9
440 (-2**63, b'\x80' + b'\x00' * 7, 8),
441 (-2**63, b'\xff\x80' + b'\x00' * 7, 9),
442
443 (2**255-1, b'\x7f' + b'\xff' * 31, 32),
444 (-(2**255-1), b'\x80' + b'\x00' * 30 + b'\x01', 32),
445 # Request extra bytes, but result says we only needed 32
446 (-(2**255-1), b'\xff\x80' + b'\x00' * 30 + b'\x01', 32),
447 (-(2**255-1), b'\xff\xff\x80' + b'\x00' * 30 + b'\x01', 32),
448
449 # Extracting 256 bits of integer will request 33 bytes, but still
450 # copy as many bits as possible into the buffer. So we *can* copy
451 # into a 32-byte buffer, though negative number may be unrecoverable
452 (2**256-1, b'\xff' * 32, 33),
453 (2**256-1, b'\x00' + b'\xff' * 32, 33),
454 (-(2**256-1), b'\x00' * 31 + b'\x01', 33),
455 (-(2**256-1), b'\xff' + b'\x00' * 31 + b'\x01', 33),
456 (-(2**256-1), b'\xff\xff' + b'\x00' * 31 + b'\x01', 33),
457 # However, -2**255 precisely will extract into 32 bytes and return
458 # success. For bigger buffers, it will still succeed, but will
459 # return 33
460 (-(2**255), b'\x80' + b'\x00' * 31, 32),
461 (-(2**255), b'\xff\x80' + b'\x00' * 31, 33),
462
463 # The classic "Windows HRESULT as negative number" case
464 # HRESULT hr;
465 # PyLong_AsNativeBytes(<-2147467259>, &hr, sizeof(HRESULT), -1)
466 # assert(hr == E_FAIL)
467 (-2147467259, b'\x80\x00\x40\x05', 4),
468 ]:
469 with self.subTest(f"{v:X}-{len(expect_be)}bytes"):
470 n = len(expect_be)
471 # Fill the buffer with dummy data to ensure all bytes
472 # are overwritten.
473 buffer = bytearray(b"\xa5"*n)
474 expect_le = expect_be[::-1]
475
476 self.assertEqual(expect_n, asnativebytes(v, buffer, n, 0),
477 f"PyLong_AsNativeBytes(v, buffer, {n}, <big>)")
478 self.assertEqual(expect_be, buffer[:n], "<big>")
479 self.assertEqual(expect_n, asnativebytes(v, buffer, n, 1),
480 f"PyLong_AsNativeBytes(v, buffer, {n}, <little>)")
481 self.assertEqual(expect_le, buffer[:n], "<little>")
482
483 # Test cases that do not request size for a sign bit when we pass the
484 # Py_ASNATIVEBYTES_UNSIGNED_BUFFER flag
485 for v, expect_be, expect_n in [
486 (255, b'\xff', 1),
487 # We pass a 2 byte buffer so it just uses the whole thing
488 (255, b'\x00\xff', 2),
489
490 (2**63, b'\x80' + b'\x00' * 7, 8),
491 # We pass a 9 byte buffer so it uses the whole thing
492 (2**63, b'\x00\x80' + b'\x00' * 7, 9),
493
494 (2**256-1, b'\xff' * 32, 32),
495 # We pass a 33 byte buffer so it uses the whole thing
496 (2**256-1, b'\x00' + b'\xff' * 32, 33),
497 ]:
498 with self.subTest(f"{v:X}-{len(expect_be)}bytes-unsigned"):
499 n = len(expect_be)
500 buffer = bytearray(b"\xa5"*n)
501 self.assertEqual(expect_n, asnativebytes(v, buffer, n, 4),
502 f"PyLong_AsNativeBytes(v, buffer, {n}, <big|unsigned>)")
503 self.assertEqual(expect_n, asnativebytes(v, buffer, n, 5),
504 f"PyLong_AsNativeBytes(v, buffer, {n}, <little|unsigned>)")
505
506 # Ensure Py_ASNATIVEBYTES_REJECT_NEGATIVE raises on negative value
507 with self.assertRaises(ValueError):
508 asnativebytes(-1, buffer, 0, 8)
509
510 # Ensure omitting Py_ASNATIVEBYTES_ALLOW_INDEX raises on __index__ value
511 with self.assertRaises(TypeError):
512 asnativebytes(Index(1), buffer, 0, -1)
513 with self.assertRaises(TypeError):
514 asnativebytes(Index(1), buffer, 0, 3)
515
516 # Check a few error conditions. These are validated in code, but are
517 # unspecified in docs, so if we make changes to the implementation, it's
518 # fine to just update these tests rather than preserve the behaviour.
519 with self.assertRaises(TypeError):
520 asnativebytes('not a number', buffer, 0, -1)
521
522 def test_long_asnativebytes_fuzz(self):
523 import math
524 from random import Random
525 from _testcapi import (
526 pylong_asnativebytes as asnativebytes,
527 SIZE_MAX,
528 )
529
530 # Abbreviate sizeof(Py_ssize_t) to SZ because we use it a lot
531 SZ = int(math.ceil(math.log(SIZE_MAX + 1) / math.log(2)) / 8)
532
533 rng = Random()
534 # Allocate bigger buffer than actual values are going to be
535 buffer = bytearray(260)
536
537 for _ in range(1000):
538 n = rng.randrange(1, 256)
539 bytes_be = bytes([
540 # Ensure the most significant byte is nonzero
541 rng.randrange(1, 256),
542 *[rng.randrange(256) for _ in range(n - 1)]
543 ])
544 bytes_le = bytes_be[::-1]
545 v = int.from_bytes(bytes_le, 'little')
546
547 expect_1 = expect_2 = (SZ, n)
548 if bytes_be[0] & 0x80:
549 # All values are positive, so if MSB is set, expect extra bit
550 # when we request the size or have a large enough buffer
551 expect_1 = (SZ, n + 1)
552 # When passing Py_ASNATIVEBYTES_UNSIGNED_BUFFER, we expect the
553 # return to be exactly the right size.
554 expect_2 = (n,)
555
556 try:
557 actual = asnativebytes(v, buffer, 0, -1)
558 self.assertIn(actual, expect_1)
559
560 actual = asnativebytes(v, buffer, len(buffer), 0)
561 self.assertIn(actual, expect_1)
562 self.assertEqual(bytes_be, buffer[-n:])
563
564 actual = asnativebytes(v, buffer, len(buffer), 1)
565 self.assertIn(actual, expect_1)
566 self.assertEqual(bytes_le, buffer[:n])
567
568 actual = asnativebytes(v, buffer, n, 4)
569 self.assertIn(actual, expect_2, bytes_be.hex())
570 actual = asnativebytes(v, buffer, n, 5)
571 self.assertIn(actual, expect_2, bytes_be.hex())
572 except AssertionError as ex:
573 value_hex = ''.join(reversed([
574 f'{b:02X}{"" if i % 8 else "_"}'
575 for i, b in enumerate(bytes_le, start=1)
576 ])).strip('_')
577 if support.verbose:
578 print()
579 print(n, 'bytes')
580 print('hex =', value_hex)
581 print('int =', v)
582 raise
583 raise AssertionError(f"Value: 0x{value_hex}") from ex
584
585 def test_long_fromnativebytes(self):
586 import math
587 from _testcapi import (
588 pylong_fromnativebytes as fromnativebytes,
589 SIZE_MAX,
590 )
591
592 # Abbreviate sizeof(Py_ssize_t) to SZ because we use it a lot
593 SZ = int(math.ceil(math.log(SIZE_MAX + 1) / math.log(2)) / 8)
594 MAX_SSIZE = 2 ** (SZ * 8 - 1) - 1
595 MAX_USIZE = 2 ** (SZ * 8) - 1
596
597 for v_be, expect_s, expect_u in [
598 (b'\x00', 0, 0),
599 (b'\x01', 1, 1),
600 (b'\xff', -1, 255),
601 (b'\x00\xff', 255, 255),
602 (b'\xff\xff', -1, 65535),
603 ]:
604 with self.subTest(f"{expect_s}-{expect_u:X}-{len(v_be)}bytes"):
605 n = len(v_be)
606 v_le = v_be[::-1]
607
608 self.assertEqual(expect_s, fromnativebytes(v_be, n, 0, 1),
609 f"PyLong_FromNativeBytes(buffer, {n}, <big>)")
610 self.assertEqual(expect_s, fromnativebytes(v_le, n, 1, 1),
611 f"PyLong_FromNativeBytes(buffer, {n}, <little>)")
612 self.assertEqual(expect_u, fromnativebytes(v_be, n, 0, 0),
613 f"PyLong_FromUnsignedNativeBytes(buffer, {n}, <big>)")
614 self.assertEqual(expect_u, fromnativebytes(v_le, n, 1, 0),
615 f"PyLong_FromUnsignedNativeBytes(buffer, {n}, <little>)")
616
617 # Check native endian when the result would be the same either
618 # way and we can test it.
619 if v_be == v_le:
620 self.assertEqual(expect_s, fromnativebytes(v_be, n, -1, 1),
621 f"PyLong_FromNativeBytes(buffer, {n}, <native>)")
622 self.assertEqual(expect_u, fromnativebytes(v_be, n, -1, 0),
623 f"PyLong_FromUnsignedNativeBytes(buffer, {n}, <native>)")
624
625 # Swap the unsigned request for tests and use the
626 # Py_ASNATIVEBYTES_UNSIGNED_BUFFER flag instead
627 self.assertEqual(expect_u, fromnativebytes(v_be, n, 4, 1),
628 f"PyLong_FromNativeBytes(buffer, {n}, <big|unsigned>)")
629
630 def test_long_getsign(self):
631 # Test PyLong_GetSign()
632 getsign = _testcapi.pylong_getsign
633 self.assertEqual(getsign(1), 1)
634 self.assertEqual(getsign(123456), 1)
635 self.assertEqual(getsign(-2), -1)
636 self.assertEqual(getsign(0), 0)
637 self.assertEqual(getsign(True), 1)
638 self.assertEqual(getsign(IntSubclass(-11)), -1)
639 self.assertEqual(getsign(False), 0)
640
641 self.assertRaises(TypeError, getsign, 1.0)
642 self.assertRaises(TypeError, getsign, Index(123))
643
644 # CRASHES getsign(NULL)
645
646 def test_long_ispositive(self):
647 # Test PyLong_IsPositive()
648 ispositive = _testcapi.pylong_ispositive
649 self.assertEqual(ispositive(1), 1)
650 self.assertEqual(ispositive(123), 1)
651 self.assertEqual(ispositive(-1), 0)
652 self.assertEqual(ispositive(0), 0)
653 self.assertEqual(ispositive(True), 1)
654 self.assertEqual(ispositive(False), 0)
655 self.assertEqual(ispositive(IntSubclass(-1)), 0)
656 self.assertRaises(TypeError, ispositive, 1.0)
657 self.assertRaises(TypeError, ispositive, Index(123))
658
659 # CRASHES ispositive(NULL)
660
661 def test_long_isnegative(self):
662 # Test PyLong_IsNegative()
663 isnegative = _testcapi.pylong_isnegative
664 self.assertEqual(isnegative(1), 0)
665 self.assertEqual(isnegative(123), 0)
666 self.assertEqual(isnegative(-1), 1)
667 self.assertEqual(isnegative(0), 0)
668 self.assertEqual(isnegative(True), 0)
669 self.assertEqual(isnegative(False), 0)
670 self.assertEqual(isnegative(IntSubclass(-1)), 1)
671 self.assertRaises(TypeError, isnegative, 1.0)
672 self.assertRaises(TypeError, isnegative, Index(123))
673
674 # CRASHES isnegative(NULL)
675
676 def test_long_iszero(self):
677 # Test PyLong_IsZero()
678 iszero = _testcapi.pylong_iszero
679 self.assertEqual(iszero(1), 0)
680 self.assertEqual(iszero(-1), 0)
681 self.assertEqual(iszero(0), 1)
682 self.assertEqual(iszero(True), 0)
683 self.assertEqual(iszero(False), 1)
684 self.assertEqual(iszero(IntSubclass(-1)), 0)
685 self.assertEqual(iszero(IntSubclass(0)), 1)
686 self.assertRaises(TypeError, iszero, 1.0)
687 self.assertRaises(TypeError, iszero, Index(123))
688
689 # CRASHES iszero(NULL)
690
691 def test_long_asint32(self):
692 # Test PyLong_AsInt32() and PyLong_FromInt32()
693 to_int32 = _testlimitedcapi.pylong_asint32
694 from _testcapi import INT32_MIN, INT32_MAX
695 self.check_long_asint(to_int32, INT32_MIN, INT32_MAX)
696
697 def test_long_asint64(self):
698 # Test PyLong_AsInt64() and PyLong_FromInt64()
699 as_int64 = _testlimitedcapi.pylong_asint64
700 from _testcapi import INT64_MIN, INT64_MAX
701 self.check_long_asint(as_int64, INT64_MIN, INT64_MAX)
702
703 def test_long_asuint32(self):
704 # Test PyLong_AsUInt32() and PyLong_FromUInt32()
705 as_uint32 = _testlimitedcapi.pylong_asuint32
706 from _testcapi import UINT32_MAX
707 self.check_long_asint(as_uint32, 0, UINT32_MAX,
708 negative_value_error=ValueError)
709
710 def test_long_asuint64(self):
711 # Test PyLong_AsUInt64() and PyLong_FromUInt64()
712 as_uint64 = _testlimitedcapi.pylong_asuint64
713 from _testcapi import UINT64_MAX
714 self.check_long_asint(as_uint64, 0, UINT64_MAX,
715 negative_value_error=ValueError)
716
717 def test_long_layout(self):
718 # Test PyLong_GetNativeLayout()
719 int_info = sys.int_info
720 layout = _testcapi.get_pylong_layout()
721 expected = {
722 'bits_per_digit': int_info.bits_per_digit,
723 'digit_size': int_info.sizeof_digit,
724 'digits_order': -1,
725 'digit_endianness': -1 if sys.byteorder == 'little' else 1,
726 }
727 self.assertEqual(layout, expected)
728
729 def test_long_export(self):
730 # Test PyLong_Export()
731 layout = _testcapi.get_pylong_layout()
732 base = 2 ** layout['bits_per_digit']
733
734 pylong_export = _testcapi.pylong_export
735
736 # value fits into int64_t
737 self.assertEqual(pylong_export(0), 0)
738 self.assertEqual(pylong_export(123), 123)
739 self.assertEqual(pylong_export(-123), -123)
740 self.assertEqual(pylong_export(IntSubclass(123)), 123)
741
742 # use an array, doesn't fit into int64_t
743 self.assertEqual(pylong_export(base**10 * 2 + 1),
744 (0, [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]))
745 self.assertEqual(pylong_export(-(base**10 * 2 + 1)),
746 (1, [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]))
747 self.assertEqual(pylong_export(IntSubclass(base**10 * 2 + 1)),
748 (0, [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]))
749
750 self.assertRaises(TypeError, pylong_export, 1.0)
751 self.assertRaises(TypeError, pylong_export, 0+1j)
752 self.assertRaises(TypeError, pylong_export, "abc")
753
754 def test_longwriter_create(self):
755 # Test PyLongWriter_Create()
756 layout = _testcapi.get_pylong_layout()
757 base = 2 ** layout['bits_per_digit']
758
759 pylongwriter_create = _testcapi.pylongwriter_create
760 self.assertRaises(ValueError, pylongwriter_create, 0, [])
761 self.assertRaises(ValueError, pylongwriter_create, -123, [])
762 self.assertEqual(pylongwriter_create(0, [0]), 0)
763 self.assertEqual(pylongwriter_create(0, [123]), 123)
764 self.assertEqual(pylongwriter_create(1, [123]), -123)
765 self.assertEqual(pylongwriter_create(1, [1, 2]),
766 -(base * 2 + 1))
767 self.assertEqual(pylongwriter_create(0, [1, 2, 3]),
768 base**2 * 3 + base * 2 + 1)
769 max_digit = base - 1
770 self.assertEqual(pylongwriter_create(0, [max_digit, max_digit, max_digit]),
771 base**2 * max_digit + base * max_digit + max_digit)
772
773 # normalize
774 self.assertEqual(pylongwriter_create(0, [123, 0, 0]), 123)
775
776 # test singletons + normalize
777 for num in (-2, 0, 1, 5, 42, 100):
778 self.assertIs(pylongwriter_create(bool(num < 0), [abs(num), 0]),
779 num)
780
781 def to_digits(num):
782 digits = []
783 while True:
784 num, digit = divmod(num, base)
785 digits.append(digit)
786 if not num:
787 break
788 return digits
789
790 # round trip: Python int -> export -> Python int
791 pylong_export = _testcapi.pylong_export
792 numbers = [*range(0, 10), 12345, 0xdeadbeef, 2**100, 2**100-1]
793 numbers.extend(-num for num in list(numbers))
794 for num in numbers:
795 with self.subTest(num=num):
796 data = pylong_export(num)
797 if isinstance(data, tuple):
798 negative, digits = data
799 else:
800 value = data
801 negative = int(value < 0)
802 digits = to_digits(abs(value))
803 self.assertEqual(pylongwriter_create(negative, digits), num,
804 (negative, digits))
805
806 def test_bug_143050(self):
807 with support.adjust_int_max_str_digits(0):
808 # Bug coming from using _pylong.int_from_string(), that
809 # currently requires > 6000 decimal digits.
810 int('-' + '0' * 7000, 10)
811 _testcapi.test_immortal_small_ints()
812 # Test also nonzero small int
813 int('-' + '0' * 7000 + '123', 10)
814 _testcapi.test_immortal_small_ints()
815
816
817 if __name__ == "__main__":
818 unittest.main()