Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_tuple.py @ 65035:87f4b32905df 2.7
Cosmetic fix to use print function.
| author | Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp> |
|---|---|
| date | Fri, 01 Oct 2010 10:48:47 +0000 |
| parents | 3670c66ee149 |
| children | 6270946115d0 |
| rev | line source |
|---|---|
|
31159
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
1 from test import test_support, seq_tests |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
2 |
|
52288
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
3 import gc |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
4 |
|
31159
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
5 class TupleTest(seq_tests.CommonTest): |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
6 type2test = tuple |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
7 |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
8 def test_constructors(self): |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
9 super(TupleTest, self).test_len() |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
10 # calling built-in types without argument must return empty |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
11 self.assertEqual(tuple(), ()) |
|
46649
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
12 t0_3 = (0, 1, 2, 3) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
13 t0_3_bis = tuple(t0_3) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52288
diff
changeset
|
14 self.assertTrue(t0_3 is t0_3_bis) |
|
46649
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
15 self.assertEqual(tuple([]), ()) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
16 self.assertEqual(tuple([0, 1, 2, 3]), (0, 1, 2, 3)) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
17 self.assertEqual(tuple(''), ()) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
18 self.assertEqual(tuple('spam'), ('s', 'p', 'a', 'm')) |
|
31159
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
19 |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
20 def test_truth(self): |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
21 super(TupleTest, self).test_truth() |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52288
diff
changeset
|
22 self.assertTrue(not ()) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52288
diff
changeset
|
23 self.assertTrue((42, )) |
|
31159
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
24 |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
25 def test_len(self): |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
26 super(TupleTest, self).test_len() |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
27 self.assertEqual(len(()), 0) |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
28 self.assertEqual(len((0,)), 1) |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
29 self.assertEqual(len((0, 1, 2)), 3) |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
30 |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
31 def test_iadd(self): |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
32 super(TupleTest, self).test_iadd() |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
33 u = (0, 1) |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
34 u2 = u |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
35 u += (2, 3) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52288
diff
changeset
|
36 self.assertTrue(u is not u2) |
|
31159
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
37 |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
38 def test_imul(self): |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
39 super(TupleTest, self).test_imul() |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
40 u = (0, 1) |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
41 u2 = u |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
42 u *= 3 |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52288
diff
changeset
|
43 self.assertTrue(u is not u2) |
|
31159
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
44 |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
45 def test_tupleresizebug(self): |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
46 # Check that a specific bug in _PyTuple_Resize() is squashed. |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
47 def f(): |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
48 for i in range(1000): |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
49 yield i |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
50 self.assertEqual(list(tuple(f())), range(1000)) |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
51 |
|
32171
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
52 def test_hash(self): |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
53 # See SF bug 942952: Weakness in tuple hash |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
54 # The hash should: |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
55 # be non-commutative |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
56 # should spread-out closely spaced values |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
57 # should not exhibit cancellation in tuples like (x,(x,y)) |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
58 # should be distinct from element hashes: hash(x)!=hash((x,)) |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
59 # This test exercises those cases. |
|
32178
041f89c98c35
test_hash(): The test here is different enough from the one in the bug
Tim Peters <tim.peters@gmail.com>
parents:
32171
diff
changeset
|
60 # For a pure random hash and N=50, the expected number of occupied |
|
041f89c98c35
test_hash(): The test here is different enough from the one in the bug
Tim Peters <tim.peters@gmail.com>
parents:
32171
diff
changeset
|
61 # buckets when tossing 252,600 balls into 2**32 buckets |
|
041f89c98c35
test_hash(): The test here is different enough from the one in the bug
Tim Peters <tim.peters@gmail.com>
parents:
32171
diff
changeset
|
62 # is 252,592.6, or about 7.4 expected collisions. The |
|
041f89c98c35
test_hash(): The test here is different enough from the one in the bug
Tim Peters <tim.peters@gmail.com>
parents:
32171
diff
changeset
|
63 # standard deviation is 2.73. On a box with 64-bit hash |
|
041f89c98c35
test_hash(): The test here is different enough from the one in the bug
Tim Peters <tim.peters@gmail.com>
parents:
32171
diff
changeset
|
64 # codes, no collisions are expected. Here we accept no |
|
041f89c98c35
test_hash(): The test here is different enough from the one in the bug
Tim Peters <tim.peters@gmail.com>
parents:
32171
diff
changeset
|
65 # more than 15 collisions. Any worse and the hash function |
|
041f89c98c35
test_hash(): The test here is different enough from the one in the bug
Tim Peters <tim.peters@gmail.com>
parents:
32171
diff
changeset
|
66 # is sorely suspect. |
|
32171
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
67 |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
68 N=50 |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
69 base = range(N) |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
70 xp = [(i, j) for i in base for j in base] |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
71 inps = base + [(i, j) for i in base for j in xp] + \ |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
72 [(i, j) for i in xp for j in base] + xp + zip(base) |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
73 collisions = len(inps) - len(set(map(hash, inps))) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52288
diff
changeset
|
74 self.assertTrue(collisions <= 15) |
|
31159
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
75 |
|
33629
c84dbe717d49
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
32178
diff
changeset
|
76 def test_repr(self): |
|
c84dbe717d49
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
32178
diff
changeset
|
77 l0 = tuple() |
|
c84dbe717d49
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
32178
diff
changeset
|
78 l2 = (0, 1, 2) |
|
c84dbe717d49
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
32178
diff
changeset
|
79 a0 = self.type2test(l0) |
|
c84dbe717d49
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
32178
diff
changeset
|
80 a2 = self.type2test(l2) |
|
c84dbe717d49
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
32178
diff
changeset
|
81 |
|
c84dbe717d49
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
32178
diff
changeset
|
82 self.assertEqual(str(a0), repr(l0)) |
|
c84dbe717d49
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
32178
diff
changeset
|
83 self.assertEqual(str(a2), repr(l2)) |
|
c84dbe717d49
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
32178
diff
changeset
|
84 self.assertEqual(repr(a0), "()") |
|
c84dbe717d49
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
32178
diff
changeset
|
85 self.assertEqual(repr(a2), "(0, 1, 2)") |
|
c84dbe717d49
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
32178
diff
changeset
|
86 |
|
52288
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
87 def _not_tracked(self, t): |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
88 # Nested tuples can take several collections to untrack |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
89 gc.collect() |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
90 gc.collect() |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
91 self.assertFalse(gc.is_tracked(t), t) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
92 |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
93 def _tracked(self, t): |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
94 self.assertTrue(gc.is_tracked(t), t) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
95 gc.collect() |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
96 gc.collect() |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
97 self.assertTrue(gc.is_tracked(t), t) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
98 |
|
62571
3670c66ee149
mark tracking tests as implementation details
Benjamin Peterson <benjamin@python.org>
parents:
57665
diff
changeset
|
99 @test_support.cpython_only |
|
52288
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
100 def test_track_literals(self): |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
101 # Test GC-optimization of tuple literals |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
102 x, y, z = 1.5, "a", [] |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
103 |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
104 self._not_tracked(()) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
105 self._not_tracked((1,)) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
106 self._not_tracked((1, 2)) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
107 self._not_tracked((1, 2, "a")) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
108 self._not_tracked((1, 2, (None, True, False, ()), int)) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
109 self._not_tracked((object(),)) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
110 self._not_tracked(((1, x), y, (2, 3))) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
111 |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
112 # Tuples with mutable elements are always tracked, even if those |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
113 # elements are not tracked right now. |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
114 self._tracked(([],)) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
115 self._tracked(([1],)) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
116 self._tracked(({},)) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
117 self._tracked((set(),)) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
118 self._tracked((x, y, z)) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
119 |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
120 def check_track_dynamic(self, tp, always_track): |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
121 x, y, z = 1.5, "a", [] |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
122 |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
123 check = self._tracked if always_track else self._not_tracked |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
124 check(tp()) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
125 check(tp([])) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
126 check(tp(set())) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
127 check(tp([1, x, y])) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
128 check(tp(obj for obj in [1, x, y])) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
129 check(tp(set([1, x, y]))) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
130 check(tp(tuple([obj]) for obj in [1, x, y])) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
131 check(tuple(tp([obj]) for obj in [1, x, y])) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
132 |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
133 self._tracked(tp([z])) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
134 self._tracked(tp([[x, y]])) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
135 self._tracked(tp([{x: y}])) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
136 self._tracked(tp(obj for obj in [x, y, z])) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
137 self._tracked(tp(tuple([obj]) for obj in [x, y, z])) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
138 self._tracked(tuple(tp([obj]) for obj in [x, y, z])) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
139 |
|
62571
3670c66ee149
mark tracking tests as implementation details
Benjamin Peterson <benjamin@python.org>
parents:
57665
diff
changeset
|
140 @test_support.cpython_only |
|
52288
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
141 def test_track_dynamic(self): |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
142 # Test GC-optimization of dynamically constructed tuples. |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
143 self.check_track_dynamic(tuple, False) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
144 |
|
62571
3670c66ee149
mark tracking tests as implementation details
Benjamin Peterson <benjamin@python.org>
parents:
57665
diff
changeset
|
145 @test_support.cpython_only |
|
52288
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
146 def test_track_subtypes(self): |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
147 # Tuple subtypes must always be tracked |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
148 class MyTuple(tuple): |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
149 pass |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
150 self.check_track_dynamic(MyTuple, True) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
151 |
|
62571
3670c66ee149
mark tracking tests as implementation details
Benjamin Peterson <benjamin@python.org>
parents:
57665
diff
changeset
|
152 @test_support.cpython_only |
|
57665
2ffd19a224d4
Issue #7466: segmentation fault when the garbage collector is called
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
153 def test_bug7466(self): |
|
2ffd19a224d4
Issue #7466: segmentation fault when the garbage collector is called
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
154 # Trying to untrack an unfinished tuple could crash Python |
|
2ffd19a224d4
Issue #7466: segmentation fault when the garbage collector is called
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
155 self._not_tracked(tuple(gc.collect() for i in range(101))) |
|
52288
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
46649
diff
changeset
|
156 |
|
31159
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
157 def test_main(): |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
158 test_support.run_unittest(TupleTest) |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
159 |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
160 if __name__=="__main__": |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
161 test_main() |
