Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_weakset.py @ 78069:a8aa77458e12 3.2
Add c_size_t and c_ssize_t to the ctypes summary table.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Thu, 12 Jul 2012 20:31:50 +0200 |
| parents | 5b88475aae96 |
| children | f911a89d2f1d |
| rev | line source |
|---|---|
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
1 import unittest |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
47166
diff
changeset
|
2 from test import support |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
3 from weakref import proxy, ref, WeakSet |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
4 import operator |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
5 import copy |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
6 import string |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
7 import os |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
8 from random import randrange, shuffle |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
9 import sys |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
10 import warnings |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
11 import collections |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
12 from collections import UserString as ustr |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
13 import gc |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
14 import contextlib |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
15 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
16 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
17 class Foo: |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
18 pass |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
19 |
|
75356
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
20 class RefCycle: |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
21 def __init__(self): |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
22 self.cycle = self |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
23 |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
24 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
25 class TestWeakSet(unittest.TestCase): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
26 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
27 def setUp(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
28 # need to keep references to them |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
29 self.items = [ustr(c) for c in ('a', 'b', 'c')] |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
30 self.items2 = [ustr(c) for c in ('x', 'y', 'z')] |
|
75405
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
31 self.ab_items = [ustr(c) for c in 'ab'] |
|
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
32 self.abcde_items = [ustr(c) for c in 'abcde'] |
|
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
33 self.def_items = [ustr(c) for c in 'def'] |
|
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
34 self.ab_weakset = WeakSet(self.ab_items) |
|
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
35 self.abcde_weakset = WeakSet(self.abcde_items) |
|
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
36 self.def_weakset = WeakSet(self.def_items) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
37 self.letters = [ustr(c) for c in string.ascii_letters] |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
38 self.s = WeakSet(self.items) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
39 self.d = dict.fromkeys(self.items) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
40 self.obj = ustr('F') |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
41 self.fs = WeakSet([self.obj]) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
42 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
43 def test_methods(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
44 weaksetmethods = dir(WeakSet) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
45 for method in dir(set): |
|
47166
12ca1e820282
Ignore test_c_api when checking for set methods. Thanks Brett!
Georg Brandl <georg@python.org>
parents:
47161
diff
changeset
|
46 if method == 'test_c_api' or method.startswith('_'): |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
47 continue |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58415
diff
changeset
|
48 self.assertIn(method, weaksetmethods, |
| 47161 | 49 "WeakSet missing method " + method) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
50 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
51 def test_new_or_init(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
52 self.assertRaises(TypeError, WeakSet, [], 2) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
53 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
54 def test_len(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
55 self.assertEqual(len(self.s), len(self.d)) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
56 self.assertEqual(len(self.fs), 1) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
57 del self.obj |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
58 self.assertEqual(len(self.fs), 0) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
59 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
60 def test_contains(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
61 for c in self.letters: |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
62 self.assertEqual(c in self.s, c in self.d) |
|
68007
b245d69b4f0a
Merged revisions 86537,86867-86868,86881,86887,86913-86915,86931-86933,86960,86964,86974,86980,86996,87008,87050 via svnmerge from
Georg Brandl <georg@python.org>
parents:
58194
diff
changeset
|
63 # 1 is not weakref'able, but that TypeError is caught by __contains__ |
|
b245d69b4f0a
Merged revisions 86537,86867-86868,86881,86887,86913-86915,86931-86933,86960,86964,86974,86980,86996,87008,87050 via svnmerge from
Georg Brandl <georg@python.org>
parents:
58194
diff
changeset
|
64 self.assertNotIn(1, self.s) |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58194
diff
changeset
|
65 self.assertIn(self.obj, self.fs) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
66 del self.obj |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58194
diff
changeset
|
67 self.assertNotIn(ustr('F'), self.fs) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
68 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
69 def test_union(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
70 u = self.s.union(self.items2) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
71 for c in self.letters: |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
72 self.assertEqual(c in u, c in self.d or c in self.items2) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
73 self.assertEqual(self.s, WeakSet(self.items)) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
74 self.assertEqual(type(u), WeakSet) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
75 self.assertRaises(TypeError, self.s.union, [[]]) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
76 for C in set, frozenset, dict.fromkeys, list, tuple: |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
77 x = WeakSet(self.items + self.items2) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
78 c = C(self.items2) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
79 self.assertEqual(self.s.union(c), x) |
|
75402
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75356
diff
changeset
|
80 del c |
|
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75356
diff
changeset
|
81 self.assertEqual(len(u), len(self.items) + len(self.items2)) |
|
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75356
diff
changeset
|
82 self.items2.pop() |
|
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75356
diff
changeset
|
83 gc.collect() |
|
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75356
diff
changeset
|
84 self.assertEqual(len(u), len(self.items) + len(self.items2)) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
85 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
86 def test_or(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
87 i = self.s.union(self.items2) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
88 self.assertEqual(self.s | set(self.items2), i) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
89 self.assertEqual(self.s | frozenset(self.items2), i) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
90 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
91 def test_intersection(self): |
|
75402
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75356
diff
changeset
|
92 s = WeakSet(self.letters) |
|
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75356
diff
changeset
|
93 i = s.intersection(self.items2) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
94 for c in self.letters: |
|
75402
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75356
diff
changeset
|
95 self.assertEqual(c in i, c in self.items2 and c in self.letters) |
|
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75356
diff
changeset
|
96 self.assertEqual(s, WeakSet(self.letters)) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
97 self.assertEqual(type(i), WeakSet) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
98 for C in set, frozenset, dict.fromkeys, list, tuple: |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
99 x = WeakSet([]) |
|
75402
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75356
diff
changeset
|
100 self.assertEqual(i.intersection(C(self.items)), x) |
|
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75356
diff
changeset
|
101 self.assertEqual(len(i), len(self.items2)) |
|
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75356
diff
changeset
|
102 self.items2.pop() |
|
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75356
diff
changeset
|
103 gc.collect() |
|
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75356
diff
changeset
|
104 self.assertEqual(len(i), len(self.items2)) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
105 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
106 def test_isdisjoint(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54179
diff
changeset
|
107 self.assertTrue(self.s.isdisjoint(WeakSet(self.items2))) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54179
diff
changeset
|
108 self.assertTrue(not self.s.isdisjoint(WeakSet(self.letters))) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
109 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
110 def test_and(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
111 i = self.s.intersection(self.items2) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
112 self.assertEqual(self.s & set(self.items2), i) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
113 self.assertEqual(self.s & frozenset(self.items2), i) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
114 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
115 def test_difference(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
116 i = self.s.difference(self.items2) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
117 for c in self.letters: |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
118 self.assertEqual(c in i, c in self.d and c not in self.items2) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
119 self.assertEqual(self.s, WeakSet(self.items)) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
120 self.assertEqual(type(i), WeakSet) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
121 self.assertRaises(TypeError, self.s.difference, [[]]) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
122 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
123 def test_sub(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
124 i = self.s.difference(self.items2) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
125 self.assertEqual(self.s - set(self.items2), i) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
126 self.assertEqual(self.s - frozenset(self.items2), i) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
127 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
128 def test_symmetric_difference(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
129 i = self.s.symmetric_difference(self.items2) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
130 for c in self.letters: |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
131 self.assertEqual(c in i, (c in self.d) ^ (c in self.items2)) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
132 self.assertEqual(self.s, WeakSet(self.items)) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
133 self.assertEqual(type(i), WeakSet) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
134 self.assertRaises(TypeError, self.s.symmetric_difference, [[]]) |
|
75402
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75356
diff
changeset
|
135 self.assertEqual(len(i), len(self.items) + len(self.items2)) |
|
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75356
diff
changeset
|
136 self.items2.pop() |
|
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75356
diff
changeset
|
137 gc.collect() |
|
d19d9ecbe42b
Fix some set algebra methods of WeakSet objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75356
diff
changeset
|
138 self.assertEqual(len(i), len(self.items) + len(self.items2)) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
139 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
140 def test_xor(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
141 i = self.s.symmetric_difference(self.items2) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
142 self.assertEqual(self.s ^ set(self.items2), i) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
143 self.assertEqual(self.s ^ frozenset(self.items2), i) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
144 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
145 def test_sub_and_super(self): |
|
75405
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
146 self.assertTrue(self.ab_weakset <= self.abcde_weakset) |
|
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
147 self.assertTrue(self.abcde_weakset <= self.abcde_weakset) |
|
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
148 self.assertTrue(self.abcde_weakset >= self.ab_weakset) |
|
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
149 self.assertFalse(self.abcde_weakset <= self.def_weakset) |
|
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
150 self.assertFalse(self.abcde_weakset >= self.def_weakset) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54179
diff
changeset
|
151 self.assertTrue(set('a').issubset('abc')) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54179
diff
changeset
|
152 self.assertTrue(set('abc').issuperset('a')) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54179
diff
changeset
|
153 self.assertFalse(set('a').issubset('cbs')) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54179
diff
changeset
|
154 self.assertFalse(set('cbs').issuperset('a')) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
155 |
|
75405
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
156 def test_lt(self): |
|
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
157 self.assertTrue(self.ab_weakset < self.abcde_weakset) |
|
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
158 self.assertFalse(self.abcde_weakset < self.def_weakset) |
|
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
159 self.assertFalse(self.ab_weakset < self.ab_weakset) |
|
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
160 self.assertFalse(WeakSet() < WeakSet()) |
|
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
161 |
|
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
162 def test_gt(self): |
|
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
163 self.assertTrue(self.abcde_weakset > self.ab_weakset) |
|
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
164 self.assertFalse(self.abcde_weakset > self.def_weakset) |
|
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
165 self.assertFalse(self.ab_weakset > self.ab_weakset) |
|
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
166 self.assertFalse(WeakSet() > WeakSet()) |
|
5b88475aae96
Issue #14195: Make WeakSet.__lt__ and WeakSet.__gt__ irreflexive.
Meador Inge <meadori@gmail.com>
parents:
75402
diff
changeset
|
167 |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
168 def test_gc(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
169 # Create a nest of cycles to exercise overall ref count check |
|
54179
a7ad1696866b
Merged revisions 72751 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47201
diff
changeset
|
170 s = WeakSet(Foo() for i in range(1000)) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
171 for elem in s: |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
172 elem.cycle = s |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
173 elem.sub = elem |
|
54179
a7ad1696866b
Merged revisions 72751 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47201
diff
changeset
|
174 elem.set = WeakSet([elem]) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
175 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
176 def test_subclass_with_custom_hash(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
177 # Bug #1257731 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
178 class H(WeakSet): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
179 def __hash__(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
180 return int(id(self) & 0x7fffffff) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
181 s=H() |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
182 f=set() |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
183 f.add(s) |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58194
diff
changeset
|
184 self.assertIn(s, f) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
185 f.remove(s) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
186 f.add(s) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
187 f.discard(s) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
188 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
189 def test_init(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
190 s = WeakSet() |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
191 s.__init__(self.items) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
192 self.assertEqual(s, self.s) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
193 s.__init__(self.items2) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
194 self.assertEqual(s, WeakSet(self.items2)) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
195 self.assertRaises(TypeError, s.__init__, s, 2); |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
196 self.assertRaises(TypeError, s.__init__, 1); |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
197 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
198 def test_constructor_identity(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
199 s = WeakSet(self.items) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
200 t = WeakSet(s) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
201 self.assertNotEqual(id(s), id(t)) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
202 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
203 def test_hash(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
204 self.assertRaises(TypeError, hash, self.s) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
205 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
206 def test_clear(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
207 self.s.clear() |
|
54179
a7ad1696866b
Merged revisions 72751 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47201
diff
changeset
|
208 self.assertEqual(self.s, WeakSet([])) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
209 self.assertEqual(len(self.s), 0) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
210 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
211 def test_copy(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
212 dup = self.s.copy() |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
213 self.assertEqual(self.s, dup) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
214 self.assertNotEqual(id(self.s), id(dup)) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
215 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
216 def test_add(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
217 x = ustr('Q') |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
218 self.s.add(x) |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58194
diff
changeset
|
219 self.assertIn(x, self.s) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
220 dup = self.s.copy() |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
221 self.s.add(x) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
222 self.assertEqual(self.s, dup) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
223 self.assertRaises(TypeError, self.s.add, []) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
224 self.fs.add(Foo()) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54179
diff
changeset
|
225 self.assertTrue(len(self.fs) == 1) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
226 self.fs.add(self.obj) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54179
diff
changeset
|
227 self.assertTrue(len(self.fs) == 1) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
228 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
229 def test_remove(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
230 x = ustr('a') |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
231 self.s.remove(x) |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58194
diff
changeset
|
232 self.assertNotIn(x, self.s) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
233 self.assertRaises(KeyError, self.s.remove, x) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
234 self.assertRaises(TypeError, self.s.remove, []) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
235 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
236 def test_discard(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
237 a, q = ustr('a'), ustr('Q') |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
238 self.s.discard(a) |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58194
diff
changeset
|
239 self.assertNotIn(a, self.s) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
240 self.s.discard(q) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
241 self.assertRaises(TypeError, self.s.discard, []) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
242 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
243 def test_pop(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
244 for i in range(len(self.s)): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
245 elem = self.s.pop() |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58194
diff
changeset
|
246 self.assertNotIn(elem, self.s) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
247 self.assertRaises(KeyError, self.s.pop) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
248 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
249 def test_update(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
250 retval = self.s.update(self.items2) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
251 self.assertEqual(retval, None) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
252 for c in (self.items + self.items2): |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58194
diff
changeset
|
253 self.assertIn(c, self.s) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
254 self.assertRaises(TypeError, self.s.update, [[]]) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
255 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
256 def test_update_set(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
257 self.s.update(set(self.items2)) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
258 for c in (self.items + self.items2): |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58194
diff
changeset
|
259 self.assertIn(c, self.s) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
260 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
261 def test_ior(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
262 self.s |= set(self.items2) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
263 for c in (self.items + self.items2): |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58194
diff
changeset
|
264 self.assertIn(c, self.s) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
265 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
266 def test_intersection_update(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
267 retval = self.s.intersection_update(self.items2) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
268 self.assertEqual(retval, None) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
269 for c in (self.items + self.items2): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
270 if c in self.items2 and c in self.items: |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58194
diff
changeset
|
271 self.assertIn(c, self.s) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
272 else: |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58194
diff
changeset
|
273 self.assertNotIn(c, self.s) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
274 self.assertRaises(TypeError, self.s.intersection_update, [[]]) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
275 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
276 def test_iand(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
277 self.s &= set(self.items2) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
278 for c in (self.items + self.items2): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
279 if c in self.items2 and c in self.items: |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58194
diff
changeset
|
280 self.assertIn(c, self.s) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
281 else: |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58194
diff
changeset
|
282 self.assertNotIn(c, self.s) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
283 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
284 def test_difference_update(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
285 retval = self.s.difference_update(self.items2) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
286 self.assertEqual(retval, None) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
287 for c in (self.items + self.items2): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
288 if c in self.items and c not in self.items2: |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58194
diff
changeset
|
289 self.assertIn(c, self.s) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
290 else: |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58194
diff
changeset
|
291 self.assertNotIn(c, self.s) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
292 self.assertRaises(TypeError, self.s.difference_update, [[]]) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
293 self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]]) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
294 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
295 def test_isub(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
296 self.s -= set(self.items2) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
297 for c in (self.items + self.items2): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
298 if c in self.items and c not in self.items2: |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58194
diff
changeset
|
299 self.assertIn(c, self.s) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
300 else: |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58194
diff
changeset
|
301 self.assertNotIn(c, self.s) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
302 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
303 def test_symmetric_difference_update(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
304 retval = self.s.symmetric_difference_update(self.items2) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
305 self.assertEqual(retval, None) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
306 for c in (self.items + self.items2): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
307 if (c in self.items) ^ (c in self.items2): |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58194
diff
changeset
|
308 self.assertIn(c, self.s) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
309 else: |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58194
diff
changeset
|
310 self.assertNotIn(c, self.s) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
311 self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]]) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
312 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
313 def test_ixor(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
314 self.s ^= set(self.items2) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
315 for c in (self.items + self.items2): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
316 if (c in self.items) ^ (c in self.items2): |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58194
diff
changeset
|
317 self.assertIn(c, self.s) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
318 else: |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58194
diff
changeset
|
319 self.assertNotIn(c, self.s) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
320 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
321 def test_inplace_on_self(self): |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
322 t = self.s.copy() |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
323 t |= t |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
324 self.assertEqual(t, self.s) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
325 t &= t |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
326 self.assertEqual(t, self.s) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
327 t -= t |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
328 self.assertEqual(t, WeakSet()) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
329 t = self.s.copy() |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
330 t ^= t |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
331 self.assertEqual(t, WeakSet()) |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
332 |
|
54179
a7ad1696866b
Merged revisions 72751 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47201
diff
changeset
|
333 def test_eq(self): |
|
a7ad1696866b
Merged revisions 72751 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47201
diff
changeset
|
334 # issue 5964 |
|
a7ad1696866b
Merged revisions 72751 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47201
diff
changeset
|
335 self.assertTrue(self.s == self.s) |
|
a7ad1696866b
Merged revisions 72751 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47201
diff
changeset
|
336 self.assertTrue(self.s == WeakSet(self.items)) |
|
a7ad1696866b
Merged revisions 72751 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47201
diff
changeset
|
337 self.assertFalse(self.s == set(self.items)) |
|
a7ad1696866b
Merged revisions 72751 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47201
diff
changeset
|
338 self.assertFalse(self.s == list(self.items)) |
|
a7ad1696866b
Merged revisions 72751 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47201
diff
changeset
|
339 self.assertFalse(self.s == tuple(self.items)) |
|
a7ad1696866b
Merged revisions 72751 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47201
diff
changeset
|
340 self.assertFalse(self.s == WeakSet([Foo])) |
|
a7ad1696866b
Merged revisions 72751 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47201
diff
changeset
|
341 self.assertFalse(self.s == 1) |
|
a7ad1696866b
Merged revisions 72751 via svnmerge from
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47201
diff
changeset
|
342 |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
343 def test_weak_destroy_while_iterating(self): |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
344 # Issue #7105: iterators shouldn't crash when a key is implicitly removed |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
345 # Create new items to be sure no-one else holds a reference |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
346 items = [ustr(c) for c in ('a', 'b', 'c')] |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
347 s = WeakSet(items) |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
348 it = iter(s) |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
349 next(it) # Trigger internal iteration |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
350 # Destroy an item |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
351 del items[-1] |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
352 gc.collect() # just in case |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
353 # We have removed either the first consumed items, or another one |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
354 self.assertIn(len(list(it)), [len(items), len(items) - 1]) |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
355 del it |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
356 # The removal has been committed |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
357 self.assertEqual(len(s), len(items)) |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
358 |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
359 def test_weak_destroy_and_mutate_while_iterating(self): |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
360 # Issue #7105: iterators shouldn't crash when a key is implicitly removed |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
361 items = [ustr(c) for c in string.ascii_letters] |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
362 s = WeakSet(items) |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
363 @contextlib.contextmanager |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
364 def testcontext(): |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
365 try: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
366 it = iter(s) |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
367 next(it) |
|
75095
f189da5bda26
Delete the iterator, which could accidentally keep a temporary reference to the yielded element.
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
368 del it |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
369 # Schedule an item for removal and recreate it |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
370 u = ustr(str(items.pop())) |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
371 gc.collect() # just in case |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
372 yield u |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
373 finally: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
374 it = None # should commit all removals |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
375 |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
376 with testcontext() as u: |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58415
diff
changeset
|
377 self.assertNotIn(u, s) |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
378 with testcontext() as u: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
379 self.assertRaises(KeyError, s.remove, u) |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58415
diff
changeset
|
380 self.assertNotIn(u, s) |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
381 with testcontext() as u: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
382 s.add(u) |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58194
diff
changeset
|
383 self.assertIn(u, s) |
|
58194
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
384 t = s.copy() |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
385 with testcontext() as u: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
386 s.update(t) |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
387 self.assertEqual(len(s), len(t)) |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
388 with testcontext() as u: |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
389 s.clear() |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
390 self.assertEqual(len(s), 0) |
|
84caac71d79b
Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
391 |
|
75356
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
392 def test_len_cycles(self): |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
393 N = 20 |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
394 items = [RefCycle() for i in range(N)] |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
395 s = WeakSet(items) |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
396 del items |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
397 it = iter(s) |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
398 try: |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
399 next(it) |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
400 except StopIteration: |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
401 pass |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
402 gc.collect() |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
403 n1 = len(s) |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
404 del it |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
405 gc.collect() |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
406 n2 = len(s) |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
407 # one item may be kept alive inside the iterator |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
408 self.assertIn(n1, (0, 1)) |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
409 self.assertEqual(n2, 0) |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
410 |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
411 def test_len_race(self): |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
412 # Extended sanity checks for len() in the face of cyclic collection |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
413 self.addCleanup(gc.set_threshold, *gc.get_threshold()) |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
414 for th in range(1, 100): |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
415 N = 20 |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
416 gc.collect(0) |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
417 gc.set_threshold(th, th, th) |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
418 items = [RefCycle() for i in range(N)] |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
419 s = WeakSet(items) |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
420 del items |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
421 # All items will be collected at next garbage collection pass |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
422 it = iter(s) |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
423 try: |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
424 next(it) |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
425 except StopIteration: |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
426 pass |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
427 n1 = len(s) |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
428 del it |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
429 n2 = len(s) |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
430 self.assertGreaterEqual(n1, 0) |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
431 self.assertLessEqual(n1, N) |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
432 self.assertGreaterEqual(n2, 0) |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
433 self.assertLessEqual(n2, n1) |
|
1cd0688ff004
Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary, WeakValueDictionary) to return a better approximation when some objects are dead or dying.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75095
diff
changeset
|
434 |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
435 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
436 def test_main(verbose=None): |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
47166
diff
changeset
|
437 support.run_unittest(TestWeakSet) |
|
47159
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
438 |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
439 if __name__ == "__main__": |
|
37499287f758
Add a test suite for WeakSet mostly derived from test_set and fix some
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
440 test_main(verbose=True) |
