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