test_tuple.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. from test import support, seq_tests
  2. import unittest
  3. import gc
  4. import pickle
  5. # For tuple hashes, we normally only run a test to ensure that we get
  6. # the same results across platforms in a handful of cases. If that's
  7. # so, there's no real point to running more. Set RUN_ALL_HASH_TESTS to
  8. # run more anyway. That's usually of real interest only when analyzing,
  9. # or changing, the hash algorithm. In which case it's usually also
  10. # most useful to set JUST_SHOW_HASH_RESULTS, to see all the results
  11. # instead of wrestling with test "failures". See the bottom of the
  12. # file for extensive notes on what we're testing here and why.
  13. RUN_ALL_HASH_TESTS = False
  14. JUST_SHOW_HASH_RESULTS = False # if RUN_ALL_HASH_TESTS, just display
  15. class TupleTest(seq_tests.CommonTest):
  16. type2test = tuple
  17. def test_getitem_error(self):
  18. t = ()
  19. msg = "tuple indices must be integers or slices"
  20. with self.assertRaisesRegex(TypeError, msg):
  21. t['a']
  22. def test_constructors(self):
  23. super().test_constructors()
  24. # calling built-in types without argument must return empty
  25. self.assertEqual(tuple(), ())
  26. t0_3 = (0, 1, 2, 3)
  27. t0_3_bis = tuple(t0_3)
  28. self.assertTrue(t0_3 is t0_3_bis)
  29. self.assertEqual(tuple([]), ())
  30. self.assertEqual(tuple([0, 1, 2, 3]), (0, 1, 2, 3))
  31. self.assertEqual(tuple(''), ())
  32. self.assertEqual(tuple('spam'), ('s', 'p', 'a', 'm'))
  33. self.assertEqual(tuple(x for x in range(10) if x % 2),
  34. (1, 3, 5, 7, 9))
  35. def test_keyword_args(self):
  36. with self.assertRaisesRegex(TypeError, 'keyword argument'):
  37. tuple(sequence=())
  38. def test_keywords_in_subclass(self):
  39. class subclass(tuple):
  40. pass
  41. u = subclass([1, 2])
  42. self.assertIs(type(u), subclass)
  43. self.assertEqual(list(u), [1, 2])
  44. with self.assertRaises(TypeError):
  45. subclass(sequence=())
  46. class subclass_with_init(tuple):
  47. def __init__(self, arg, newarg=None):
  48. self.newarg = newarg
  49. u = subclass_with_init([1, 2], newarg=3)
  50. self.assertIs(type(u), subclass_with_init)
  51. self.assertEqual(list(u), [1, 2])
  52. self.assertEqual(u.newarg, 3)
  53. class subclass_with_new(tuple):
  54. def __new__(cls, arg, newarg=None):
  55. self = super().__new__(cls, arg)
  56. self.newarg = newarg
  57. return self
  58. u = subclass_with_new([1, 2], newarg=3)
  59. self.assertIs(type(u), subclass_with_new)
  60. self.assertEqual(list(u), [1, 2])
  61. self.assertEqual(u.newarg, 3)
  62. def test_truth(self):
  63. super().test_truth()
  64. self.assertTrue(not ())
  65. self.assertTrue((42, ))
  66. def test_len(self):
  67. super().test_len()
  68. self.assertEqual(len(()), 0)
  69. self.assertEqual(len((0,)), 1)
  70. self.assertEqual(len((0, 1, 2)), 3)
  71. def test_iadd(self):
  72. super().test_iadd()
  73. u = (0, 1)
  74. u2 = u
  75. u += (2, 3)
  76. self.assertTrue(u is not u2)
  77. def test_imul(self):
  78. super().test_imul()
  79. u = (0, 1)
  80. u2 = u
  81. u *= 3
  82. self.assertTrue(u is not u2)
  83. def test_tupleresizebug(self):
  84. # Check that a specific bug in _PyTuple_Resize() is squashed.
  85. def f():
  86. for i in range(1000):
  87. yield i
  88. self.assertEqual(list(tuple(f())), list(range(1000)))
  89. # We expect tuples whose base components have deterministic hashes to
  90. # have deterministic hashes too - and, indeed, the same hashes across
  91. # platforms with hash codes of the same bit width.
  92. def test_hash_exact(self):
  93. def check_one_exact(t, e32, e64):
  94. got = hash(t)
  95. expected = e32 if support.NHASHBITS == 32 else e64
  96. if got != expected:
  97. msg = f"FAIL hash({t!r}) == {got} != {expected}"
  98. self.fail(msg)
  99. check_one_exact((), 750394483, 5740354900026072187)
  100. check_one_exact((0,), 1214856301, -8753497827991233192)
  101. check_one_exact((0, 0), -168982784, -8458139203682520985)
  102. check_one_exact((0.5,), 2077348973, -408149959306781352)
  103. check_one_exact((0.5, (), (-2, 3, (4, 6))), 714642271,
  104. -1845940830829704396)
  105. # Various tests for hashing of tuples to check that we get few collisions.
  106. # Does something only if RUN_ALL_HASH_TESTS is true.
  107. #
  108. # Earlier versions of the tuple hash algorithm had massive collisions
  109. # reported at:
  110. # - https://bugs.python.org/issue942952
  111. # - https://bugs.python.org/issue34751
  112. def test_hash_optional(self):
  113. from itertools import product
  114. if not RUN_ALL_HASH_TESTS:
  115. return
  116. # If specified, `expected` is a 2-tuple of expected
  117. # (number_of_collisions, pileup) values, and the test fails if
  118. # those aren't the values we get. Also if specified, the test
  119. # fails if z > `zlimit`.
  120. def tryone_inner(tag, nbins, hashes, expected=None, zlimit=None):
  121. from collections import Counter
  122. nballs = len(hashes)
  123. mean, sdev = support.collision_stats(nbins, nballs)
  124. c = Counter(hashes)
  125. collisions = nballs - len(c)
  126. z = (collisions - mean) / sdev
  127. pileup = max(c.values()) - 1
  128. del c
  129. got = (collisions, pileup)
  130. failed = False
  131. prefix = ""
  132. if zlimit is not None and z > zlimit:
  133. failed = True
  134. prefix = f"FAIL z > {zlimit}; "
  135. if expected is not None and got != expected:
  136. failed = True
  137. prefix += f"FAIL {got} != {expected}; "
  138. if failed or JUST_SHOW_HASH_RESULTS:
  139. msg = f"{prefix}{tag}; pileup {pileup:,} mean {mean:.1f} "
  140. msg += f"coll {collisions:,} z {z:+.1f}"
  141. if JUST_SHOW_HASH_RESULTS:
  142. import sys
  143. print(msg, file=sys.__stdout__)
  144. else:
  145. self.fail(msg)
  146. def tryone(tag, xs,
  147. native32=None, native64=None, hi32=None, lo32=None,
  148. zlimit=None):
  149. NHASHBITS = support.NHASHBITS
  150. hashes = list(map(hash, xs))
  151. tryone_inner(tag + f"; {NHASHBITS}-bit hash codes",
  152. 1 << NHASHBITS,
  153. hashes,
  154. native32 if NHASHBITS == 32 else native64,
  155. zlimit)
  156. if NHASHBITS > 32:
  157. shift = NHASHBITS - 32
  158. tryone_inner(tag + "; 32-bit upper hash codes",
  159. 1 << 32,
  160. [h >> shift for h in hashes],
  161. hi32,
  162. zlimit)
  163. mask = (1 << 32) - 1
  164. tryone_inner(tag + "; 32-bit lower hash codes",
  165. 1 << 32,
  166. [h & mask for h in hashes],
  167. lo32,
  168. zlimit)
  169. # Tuples of smallish positive integers are common - nice if we
  170. # get "better than random" for these.
  171. tryone("range(100) by 3", list(product(range(100), repeat=3)),
  172. (0, 0), (0, 0), (4, 1), (0, 0))
  173. # A previous hash had systematic problems when mixing integers of
  174. # similar magnitude but opposite sign, obscurely related to that
  175. # j ^ -2 == -j when j is odd.
  176. cands = list(range(-10, -1)) + list(range(9))
  177. # Note: -1 is omitted because hash(-1) == hash(-2) == -2, and
  178. # there's nothing the tuple hash can do to avoid collisions
  179. # inherited from collisions in the tuple components' hashes.
  180. tryone("-10 .. 8 by 4", list(product(cands, repeat=4)),
  181. (0, 0), (0, 0), (0, 0), (0, 0))
  182. del cands
  183. # The hashes here are a weird mix of values where all the
  184. # variation is in the lowest bits and across a single high-order
  185. # bit - the middle bits are all zeroes. A decent hash has to
  186. # both propagate low bits to the left and high bits to the
  187. # right. This is also complicated a bit in that there are
  188. # collisions among the hashes of the integers in L alone.
  189. L = [n << 60 for n in range(100)]
  190. tryone("0..99 << 60 by 3", list(product(L, repeat=3)),
  191. (0, 0), (0, 0), (0, 0), (324, 1))
  192. del L
  193. # Used to suffer a massive number of collisions.
  194. tryone("[-3, 3] by 18", list(product([-3, 3], repeat=18)),
  195. (7, 1), (0, 0), (7, 1), (6, 1))
  196. # And even worse. hash(0.5) has only a single bit set, at the
  197. # high end. A decent hash needs to propagate high bits right.
  198. tryone("[0, 0.5] by 18", list(product([0, 0.5], repeat=18)),
  199. (5, 1), (0, 0), (9, 1), (12, 1))
  200. # Hashes of ints and floats are the same across platforms.
  201. # String hashes vary even on a single platform across runs, due
  202. # to hash randomization for strings. So we can't say exactly
  203. # what this should do. Instead we insist that the # of
  204. # collisions is no more than 4 sdevs above the theoretically
  205. # random mean. Even if the tuple hash can't achieve that on its
  206. # own, the string hash is trying to be decently pseudo-random
  207. # (in all bit positions) on _its_ own. We can at least test
  208. # that the tuple hash doesn't systematically ruin that.
  209. tryone("4-char tuples",
  210. list(product("abcdefghijklmnopqrstuvwxyz", repeat=4)),
  211. zlimit=4.0)
  212. # The "old tuple test". See https://bugs.python.org/issue942952.
  213. # Ensures, for example, that the hash:
  214. # is non-commutative
  215. # spreads closely spaced values
  216. # doesn't exhibit cancellation in tuples like (x,(x,y))
  217. N = 50
  218. base = list(range(N))
  219. xp = list(product(base, repeat=2))
  220. inps = base + list(product(base, xp)) + \
  221. list(product(xp, base)) + xp + list(zip(base))
  222. tryone("old tuple test", inps,
  223. (2, 1), (0, 0), (52, 49), (7, 1))
  224. del base, xp, inps
  225. # The "new tuple test". See https://bugs.python.org/issue34751.
  226. # Even more tortured nesting, and a mix of signed ints of very
  227. # small magnitude.
  228. n = 5
  229. A = [x for x in range(-n, n+1) if x != -1]
  230. B = A + [(a,) for a in A]
  231. L2 = list(product(A, repeat=2))
  232. L3 = L2 + list(product(A, repeat=3))
  233. L4 = L3 + list(product(A, repeat=4))
  234. # T = list of testcases. These consist of all (possibly nested
  235. # at most 2 levels deep) tuples containing at most 4 items from
  236. # the set A.
  237. T = A
  238. T += [(a,) for a in B + L4]
  239. T += product(L3, B)
  240. T += product(L2, repeat=2)
  241. T += product(B, L3)
  242. T += product(B, B, L2)
  243. T += product(B, L2, B)
  244. T += product(L2, B, B)
  245. T += product(B, repeat=4)
  246. assert len(T) == 345130
  247. tryone("new tuple test", T,
  248. (9, 1), (0, 0), (21, 5), (6, 1))
  249. def test_repr(self):
  250. l0 = tuple()
  251. l2 = (0, 1, 2)
  252. a0 = self.type2test(l0)
  253. a2 = self.type2test(l2)
  254. self.assertEqual(str(a0), repr(l0))
  255. self.assertEqual(str(a2), repr(l2))
  256. self.assertEqual(repr(a0), "()")
  257. self.assertEqual(repr(a2), "(0, 1, 2)")
  258. def _not_tracked(self, t):
  259. # Nested tuples can take several collections to untrack
  260. gc.collect()
  261. gc.collect()
  262. self.assertFalse(gc.is_tracked(t), t)
  263. def _tracked(self, t):
  264. self.assertTrue(gc.is_tracked(t), t)
  265. gc.collect()
  266. gc.collect()
  267. self.assertTrue(gc.is_tracked(t), t)
  268. @support.cpython_only
  269. def test_track_literals(self):
  270. # Test GC-optimization of tuple literals
  271. x, y, z = 1.5, "a", []
  272. self._not_tracked(())
  273. self._not_tracked((1,))
  274. self._not_tracked((1, 2))
  275. self._not_tracked((1, 2, "a"))
  276. self._not_tracked((1, 2, (None, True, False, ()), int))
  277. self._not_tracked((object(),))
  278. self._not_tracked(((1, x), y, (2, 3)))
  279. # Tuples with mutable elements are always tracked, even if those
  280. # elements are not tracked right now.
  281. self._tracked(([],))
  282. self._tracked(([1],))
  283. self._tracked(({},))
  284. self._tracked((set(),))
  285. self._tracked((x, y, z))
  286. def check_track_dynamic(self, tp, always_track):
  287. x, y, z = 1.5, "a", []
  288. check = self._tracked if always_track else self._not_tracked
  289. check(tp())
  290. check(tp([]))
  291. check(tp(set()))
  292. check(tp([1, x, y]))
  293. check(tp(obj for obj in [1, x, y]))
  294. check(tp(set([1, x, y])))
  295. check(tp(tuple([obj]) for obj in [1, x, y]))
  296. check(tuple(tp([obj]) for obj in [1, x, y]))
  297. self._tracked(tp([z]))
  298. self._tracked(tp([[x, y]]))
  299. self._tracked(tp([{x: y}]))
  300. self._tracked(tp(obj for obj in [x, y, z]))
  301. self._tracked(tp(tuple([obj]) for obj in [x, y, z]))
  302. self._tracked(tuple(tp([obj]) for obj in [x, y, z]))
  303. @support.cpython_only
  304. def test_track_dynamic(self):
  305. # Test GC-optimization of dynamically constructed tuples.
  306. self.check_track_dynamic(tuple, False)
  307. @support.cpython_only
  308. def test_track_subtypes(self):
  309. # Tuple subtypes must always be tracked
  310. class MyTuple(tuple):
  311. pass
  312. self.check_track_dynamic(MyTuple, True)
  313. @support.cpython_only
  314. def test_bug7466(self):
  315. # Trying to untrack an unfinished tuple could crash Python
  316. self._not_tracked(tuple(gc.collect() for i in range(101)))
  317. def test_repr_large(self):
  318. # Check the repr of large list objects
  319. def check(n):
  320. l = (0,) * n
  321. s = repr(l)
  322. self.assertEqual(s,
  323. '(' + ', '.join(['0'] * n) + ')')
  324. check(10) # check our checking code
  325. check(1000000)
  326. def test_iterator_pickle(self):
  327. # Userlist iterators don't support pickling yet since
  328. # they are based on generators.
  329. data = self.type2test([4, 5, 6, 7])
  330. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  331. itorg = iter(data)
  332. d = pickle.dumps(itorg, proto)
  333. it = pickle.loads(d)
  334. self.assertEqual(type(itorg), type(it))
  335. self.assertEqual(self.type2test(it), self.type2test(data))
  336. it = pickle.loads(d)
  337. next(it)
  338. d = pickle.dumps(it, proto)
  339. self.assertEqual(self.type2test(it), self.type2test(data)[1:])
  340. def test_reversed_pickle(self):
  341. data = self.type2test([4, 5, 6, 7])
  342. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  343. itorg = reversed(data)
  344. d = pickle.dumps(itorg, proto)
  345. it = pickle.loads(d)
  346. self.assertEqual(type(itorg), type(it))
  347. self.assertEqual(self.type2test(it), self.type2test(reversed(data)))
  348. it = pickle.loads(d)
  349. next(it)
  350. d = pickle.dumps(it, proto)
  351. self.assertEqual(self.type2test(it), self.type2test(reversed(data))[1:])
  352. def test_no_comdat_folding(self):
  353. # Issue 8847: In the PGO build, the MSVC linker's COMDAT folding
  354. # optimization causes failures in code that relies on distinct
  355. # function addresses.
  356. class T(tuple): pass
  357. with self.assertRaises(TypeError):
  358. [3,] + T((1,2))
  359. def test_lexicographic_ordering(self):
  360. # Issue 21100
  361. a = self.type2test([1, 2])
  362. b = self.type2test([1, 2, 0])
  363. c = self.type2test([1, 3])
  364. self.assertLess(a, b)
  365. self.assertLess(b, c)
  366. # Notes on testing hash codes. The primary thing is that Python doesn't
  367. # care about "random" hash codes. To the contrary, we like them to be
  368. # very regular when possible, so that the low-order bits are as evenly
  369. # distributed as possible. For integers this is easy: hash(i) == i for
  370. # all not-huge i except i==-1.
  371. #
  372. # For tuples of mixed type there's really no hope of that, so we want
  373. # "randomish" here instead. But getting close to pseudo-random in all
  374. # bit positions is more expensive than we've been willing to pay for.
  375. #
  376. # We can tolerate large deviations from random - what we don't want is
  377. # catastrophic pileups on a relative handful of hash codes. The dict
  378. # and set lookup routines remain effective provided that full-width hash
  379. # codes for not-equal objects are distinct.
  380. #
  381. # So we compute various statistics here based on what a "truly random"
  382. # hash would do, but don't automate "pass or fail" based on those
  383. # results. Instead those are viewed as inputs to human judgment, and the
  384. # automated tests merely ensure we get the _same_ results across
  385. # platforms. In fact, we normally don't bother to run them at all -
  386. # set RUN_ALL_HASH_TESTS to force it.
  387. #
  388. # When global JUST_SHOW_HASH_RESULTS is True, the tuple hash statistics
  389. # are just displayed to stdout. A typical output line looks like:
  390. #
  391. # old tuple test; 32-bit upper hash codes; \
  392. # pileup 49 mean 7.4 coll 52 z +16.4
  393. #
  394. # "old tuple test" is just a string name for the test being run.
  395. #
  396. # "32-bit upper hash codes" means this was run under a 64-bit build and
  397. # we've shifted away the lower 32 bits of the hash codes.
  398. #
  399. # "pileup" is 0 if there were no collisions across those hash codes.
  400. # It's 1 less than the maximum number of times any single hash code was
  401. # seen. So in this case, there was (at least) one hash code that was
  402. # seen 50 times: that hash code "piled up" 49 more times than ideal.
  403. #
  404. # "mean" is the number of collisions a perfectly random hash function
  405. # would have yielded, on average.
  406. #
  407. # "coll" is the number of collisions actually seen.
  408. #
  409. # "z" is "coll - mean" divided by the standard deviation of the number
  410. # of collisions a perfectly random hash function would suffer. A
  411. # positive value is "worse than random", and negative value "better than
  412. # random". Anything of magnitude greater than 3 would be highly suspect
  413. # for a hash function that claimed to be random. It's essentially
  414. # impossible that a truly random function would deliver a result 16.4
  415. # sdevs "worse than random".
  416. #
  417. # But we don't care here! That's why the test isn't coded to fail.
  418. # Knowing something about how the high-order hash code bits behave
  419. # provides insight, but is irrelevant to how the dict and set lookup
  420. # code performs. The low-order bits are much more important to that,
  421. # and on the same test those did "just like random":
  422. #
  423. # old tuple test; 32-bit lower hash codes; \
  424. # pileup 1 mean 7.4 coll 7 z -0.2
  425. #
  426. # So there are always tradeoffs to consider. For another:
  427. #
  428. # 0..99 << 60 by 3; 32-bit hash codes; \
  429. # pileup 0 mean 116.4 coll 0 z -10.8
  430. #
  431. # That was run under a 32-bit build, and is spectacularly "better than
  432. # random". On a 64-bit build the wider hash codes are fine too:
  433. #
  434. # 0..99 << 60 by 3; 64-bit hash codes; \
  435. # pileup 0 mean 0.0 coll 0 z -0.0
  436. #
  437. # but their lower 32 bits are poor:
  438. #
  439. # 0..99 << 60 by 3; 32-bit lower hash codes; \
  440. # pileup 1 mean 116.4 coll 324 z +19.2
  441. #
  442. # In a statistical sense that's waaaaay too many collisions, but (a) 324
  443. # collisions out of a million hash codes isn't anywhere near being a
  444. # real problem; and, (b) the worst pileup on a single hash code is a measly
  445. # 1 extra. It's a relatively poor case for the tuple hash, but still
  446. # fine for practical use.
  447. #
  448. # This isn't, which is what Python 3.7.1 produced for the hashes of
  449. # itertools.product([0, 0.5], repeat=18). Even with a fat 64-bit
  450. # hashcode, the highest pileup was over 16,000 - making a dict/set
  451. # lookup on one of the colliding values thousands of times slower (on
  452. # average) than we expect.
  453. #
  454. # [0, 0.5] by 18; 64-bit hash codes; \
  455. # pileup 16,383 mean 0.0 coll 262,128 z +6073641856.9
  456. # [0, 0.5] by 18; 32-bit lower hash codes; \
  457. # pileup 262,143 mean 8.0 coll 262,143 z +92683.6
  458. if __name__ == "__main__":
  459. unittest.main()