annotate Lib/test/test_weakset.py @ 80661:94de77bd0b4b 2.7

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