Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_weakref.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | 86aa2c2da40e |
| children | bd349999cb08 |
| rev | line source |
|---|---|
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
1 import sys |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
2 import unittest |
|
21484
2d376d926da8
Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents:
21340
diff
changeset
|
3 import UserList |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
4 import weakref |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
5 |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19764
diff
changeset
|
6 import test_support |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
7 |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
8 |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
9 class C: |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
10 def method(self): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
11 pass |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
12 |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
13 |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
14 class Callable: |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
15 bar = None |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
16 |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
17 def __call__(self, x): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
18 self.bar = x |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
19 |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
20 |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
21 def create_function(): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
22 def f(): pass |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
23 return f |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
24 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
25 def create_bound_method(): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
26 return C().method |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
27 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
28 def create_unbound_method(): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
29 return C.method |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
30 |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
31 |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
32 class TestBase(unittest.TestCase): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
33 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
34 def setUp(self): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
35 self.cbcalled = 0 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
36 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
37 def callback(self, ref): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
38 self.cbcalled += 1 |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
39 |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
40 |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
41 class ReferencesTestCase(TestBase): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
42 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
43 def test_basic_ref(self): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
44 self.check_basic_ref(C) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
45 self.check_basic_ref(create_function) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
46 self.check_basic_ref(create_bound_method) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
47 self.check_basic_ref(create_unbound_method) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
48 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
49 def test_basic_callback(self): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
50 self.check_basic_callback(C) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
51 self.check_basic_callback(create_function) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
52 self.check_basic_callback(create_bound_method) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
53 self.check_basic_callback(create_unbound_method) |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
54 |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
55 def test_multiple_callbacks(self): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
56 o = C() |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
57 ref1 = weakref.ref(o, self.callback) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
58 ref2 = weakref.ref(o, self.callback) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
59 del o |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
60 self.assert_(ref1() is None, |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
61 "expected reference to be invalidated") |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
62 self.assert_(ref2() is None, |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
63 "expected reference to be invalidated") |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
64 self.assert_(self.cbcalled == 2, |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
65 "callback not called the right number of times") |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
66 |
|
17498
f72dcd67cccb
Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents:
17385
diff
changeset
|
67 def test_multiple_selfref_callbacks(self): |
|
f72dcd67cccb
Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents:
17385
diff
changeset
|
68 """Make sure all references are invalidated before callbacks |
|
f72dcd67cccb
Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents:
17385
diff
changeset
|
69 are called.""" |
|
f72dcd67cccb
Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents:
17385
diff
changeset
|
70 # |
|
f72dcd67cccb
Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents:
17385
diff
changeset
|
71 # What's important here is that we're using the first |
|
f72dcd67cccb
Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents:
17385
diff
changeset
|
72 # reference in the callback invoked on the second reference |
|
f72dcd67cccb
Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents:
17385
diff
changeset
|
73 # (the most recently created ref is cleaned up first). This |
|
f72dcd67cccb
Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents:
17385
diff
changeset
|
74 # tests that all references to the object are invalidated |
|
f72dcd67cccb
Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents:
17385
diff
changeset
|
75 # before any of the callbacks are invoked, so that we only |
|
f72dcd67cccb
Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents:
17385
diff
changeset
|
76 # have one invocation of _weakref.c:cleanup_helper() active |
|
f72dcd67cccb
Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents:
17385
diff
changeset
|
77 # for a particular object at a time. |
|
f72dcd67cccb
Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents:
17385
diff
changeset
|
78 # |
|
f72dcd67cccb
Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents:
17385
diff
changeset
|
79 def callback(object, self=self): |
|
f72dcd67cccb
Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents:
17385
diff
changeset
|
80 self.ref() |
|
f72dcd67cccb
Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents:
17385
diff
changeset
|
81 c = C() |
|
f72dcd67cccb
Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents:
17385
diff
changeset
|
82 self.ref = weakref.ref(c, callback) |
|
f72dcd67cccb
Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents:
17385
diff
changeset
|
83 ref1 = weakref.ref(c, callback) |
|
f72dcd67cccb
Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents:
17385
diff
changeset
|
84 del c |
|
f72dcd67cccb
Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents:
17385
diff
changeset
|
85 |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
86 def test_proxy_ref(self): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
87 o = C() |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
88 o.bar = 1 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
89 ref1 = weakref.proxy(o, self.callback) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
90 ref2 = weakref.proxy(o, self.callback) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
91 del o |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
92 |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
93 def check(proxy): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
94 proxy.bar |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
95 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
96 self.assertRaises(weakref.ReferenceError, check, ref1) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
97 self.assertRaises(weakref.ReferenceError, check, ref2) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
98 self.assert_(self.cbcalled == 2) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
99 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
100 def check_basic_ref(self, factory): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
101 o = factory() |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
102 ref = weakref.ref(o) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
103 self.assert_(ref() is not None, |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
104 "weak reference to live object should be live") |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
105 o2 = ref() |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
106 self.assert_(o is o2, |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
107 "<ref>() should return original object if live") |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
108 |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
109 def check_basic_callback(self, factory): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
110 self.cbcalled = 0 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
111 o = factory() |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
112 ref = weakref.ref(o, self.callback) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
113 del o |
|
17498
f72dcd67cccb
Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents:
17385
diff
changeset
|
114 self.assert_(self.cbcalled == 1, |
|
f72dcd67cccb
Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents:
17385
diff
changeset
|
115 "callback did not properly set 'cbcalled'") |
|
f72dcd67cccb
Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents:
17385
diff
changeset
|
116 self.assert_(ref() is None, |
|
f72dcd67cccb
Added regression test for SF bug #415660 (failure to invalidate all
Fred Drake <fdrake@acm.org>
parents:
17385
diff
changeset
|
117 "ref2 should be dead after deleting object reference") |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
118 |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
119 def test_ref_reuse(self): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
120 o = C() |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
121 ref1 = weakref.ref(o) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
122 # create a proxy to make sure that there's an intervening creation |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
123 # between these two; it should make no difference |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
124 proxy = weakref.proxy(o) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
125 ref2 = weakref.ref(o) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
126 self.assert_(ref1 is ref2, |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
127 "reference object w/out callback should be re-used") |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
128 |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
129 o = C() |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
130 proxy = weakref.proxy(o) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
131 ref1 = weakref.ref(o) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
132 ref2 = weakref.ref(o) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
133 self.assert_(ref1 is ref2, |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
134 "reference object w/out callback should be re-used") |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
135 self.assert_(weakref.getweakrefcount(o) == 2, |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
136 "wrong weak ref count for object") |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
137 del proxy |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
138 self.assert_(weakref.getweakrefcount(o) == 1, |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
139 "wrong weak ref count for object after deleting proxy") |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
140 |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
141 def test_proxy_reuse(self): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
142 o = C() |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
143 proxy1 = weakref.proxy(o) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
144 ref = weakref.ref(o) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
145 proxy2 = weakref.proxy(o) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
146 self.assert_(proxy1 is proxy2, |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
147 "proxy object w/out callback should have been re-used") |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
148 |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
149 def test_basic_proxy(self): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
150 o = C() |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
151 self.check_proxy(o, weakref.proxy(o)) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
152 |
|
21484
2d376d926da8
Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents:
21340
diff
changeset
|
153 L = UserList.UserList() |
|
2d376d926da8
Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents:
21340
diff
changeset
|
154 p = weakref.proxy(L) |
|
2d376d926da8
Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents:
21340
diff
changeset
|
155 self.failIf(p, "proxy for empty UserList should be false") |
|
2d376d926da8
Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents:
21340
diff
changeset
|
156 p.append(12) |
|
2d376d926da8
Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents:
21340
diff
changeset
|
157 self.assertEqual(len(L), 1) |
|
2d376d926da8
Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents:
21340
diff
changeset
|
158 self.failUnless(p, "proxy for non-empty UserList should be true") |
|
2d376d926da8
Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents:
21340
diff
changeset
|
159 p[:] = [2, 3] |
|
2d376d926da8
Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents:
21340
diff
changeset
|
160 self.assertEqual(len(L), 2) |
|
2d376d926da8
Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents:
21340
diff
changeset
|
161 self.assertEqual(len(p), 2) |
|
2d376d926da8
Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents:
21340
diff
changeset
|
162 self.failUnless(3 in p, "proxy didn't support __contains__() properly") |
|
2d376d926da8
Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents:
21340
diff
changeset
|
163 p[1] = 5 |
|
2d376d926da8
Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents:
21340
diff
changeset
|
164 self.assertEqual(L[1], 5) |
|
2d376d926da8
Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents:
21340
diff
changeset
|
165 self.assertEqual(p[1], 5) |
|
2d376d926da8
Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents:
21340
diff
changeset
|
166 L2 = UserList.UserList(L) |
|
2d376d926da8
Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents:
21340
diff
changeset
|
167 p2 = weakref.proxy(L2) |
|
2d376d926da8
Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents:
21340
diff
changeset
|
168 self.assertEqual(p, p2) |
|
2d376d926da8
Add some additional tests that check more proxy behaviors.
Fred Drake <fdrake@acm.org>
parents:
21340
diff
changeset
|
169 |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
170 def test_callable_proxy(self): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
171 o = Callable() |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
172 ref1 = weakref.proxy(o) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
173 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
174 self.check_proxy(o, ref1) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
175 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
176 self.assert_(type(ref1) is weakref.CallableProxyType, |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
177 "proxy is not of callable type") |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
178 ref1('twinkies!') |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
179 self.assert_(o.bar == 'twinkies!', |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
180 "call through proxy not passed through to original") |
|
20533
4f7befec35b0
Add a test for calling a weakref proxy with a dictionary of keyword args.
Fred Drake <fdrake@acm.org>
parents:
20522
diff
changeset
|
181 ref1(x='Splat.') |
|
4f7befec35b0
Add a test for calling a weakref proxy with a dictionary of keyword args.
Fred Drake <fdrake@acm.org>
parents:
20522
diff
changeset
|
182 self.assert_(o.bar == 'Splat.', |
|
4f7befec35b0
Add a test for calling a weakref proxy with a dictionary of keyword args.
Fred Drake <fdrake@acm.org>
parents:
20522
diff
changeset
|
183 "call through proxy not passed through to original") |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
184 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
185 # expect due to too few args |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
186 self.assertRaises(TypeError, ref1) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
187 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
188 # expect due to too many args |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
189 self.assertRaises(TypeError, ref1, 1, 2, 3) |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
190 |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
191 def check_proxy(self, o, proxy): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
192 o.foo = 1 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
193 self.assert_(proxy.foo == 1, |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
194 "proxy does not reflect attribute addition") |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
195 o.foo = 2 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
196 self.assert_(proxy.foo == 2, |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
197 "proxy does not reflect attribute modification") |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
198 del o.foo |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
199 self.assert_(not hasattr(proxy, 'foo'), |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
200 "proxy does not reflect attribute removal") |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
201 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
202 proxy.foo = 1 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
203 self.assert_(o.foo == 1, |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
204 "object does not reflect attribute addition via proxy") |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
205 proxy.foo = 2 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
206 self.assert_( |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
207 o.foo == 2, |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
208 "object does not reflect attribute modification via proxy") |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
209 del proxy.foo |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
210 self.assert_(not hasattr(o, 'foo'), |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
211 "object does not reflect attribute removal via proxy") |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
212 |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
213 def test_getweakrefcount(self): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
214 o = C() |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
215 ref1 = weakref.ref(o) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
216 ref2 = weakref.ref(o, self.callback) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
217 self.assert_(weakref.getweakrefcount(o) == 2, |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
218 "got wrong number of weak reference objects") |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
219 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
220 proxy1 = weakref.proxy(o) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
221 proxy2 = weakref.proxy(o, self.callback) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
222 self.assert_(weakref.getweakrefcount(o) == 4, |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
223 "got wrong number of weak reference objects") |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
224 |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
225 def test_getweakrefs(self): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
226 o = C() |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
227 ref1 = weakref.ref(o, self.callback) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
228 ref2 = weakref.ref(o, self.callback) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
229 del ref1 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
230 self.assert_(weakref.getweakrefs(o) == [ref2], |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
231 "list of refs does not match") |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
232 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
233 o = C() |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
234 ref1 = weakref.ref(o, self.callback) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
235 ref2 = weakref.ref(o, self.callback) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
236 del ref2 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
237 self.assert_(weakref.getweakrefs(o) == [ref1], |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
238 "list of refs does not match") |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
239 |
|
20522
3d13417a63e0
Make sure we do not core dump when using proxies with the binary slot
Fred Drake <fdrake@acm.org>
parents:
20013
diff
changeset
|
240 def test_newstyle_number_ops(self): |
|
3d13417a63e0
Make sure we do not core dump when using proxies with the binary slot
Fred Drake <fdrake@acm.org>
parents:
20013
diff
changeset
|
241 class F(float): |
|
3d13417a63e0
Make sure we do not core dump when using proxies with the binary slot
Fred Drake <fdrake@acm.org>
parents:
20013
diff
changeset
|
242 pass |
|
3d13417a63e0
Make sure we do not core dump when using proxies with the binary slot
Fred Drake <fdrake@acm.org>
parents:
20013
diff
changeset
|
243 f = F(2.0) |
|
3d13417a63e0
Make sure we do not core dump when using proxies with the binary slot
Fred Drake <fdrake@acm.org>
parents:
20013
diff
changeset
|
244 p = weakref.proxy(f) |
|
3d13417a63e0
Make sure we do not core dump when using proxies with the binary slot
Fred Drake <fdrake@acm.org>
parents:
20013
diff
changeset
|
245 self.assert_(p + 1.0 == 3.0) |
|
3d13417a63e0
Make sure we do not core dump when using proxies with the binary slot
Fred Drake <fdrake@acm.org>
parents:
20013
diff
changeset
|
246 self.assert_(1.0 + p == 3.0) # this used to SEGV |
|
3d13417a63e0
Make sure we do not core dump when using proxies with the binary slot
Fred Drake <fdrake@acm.org>
parents:
20013
diff
changeset
|
247 |
|
21340
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
248 def test_callbacks_protected(self): |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
249 """Callbacks protected from already-set exceptions?""" |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
250 # Regression test for SF bug #478534. |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
251 class BogusError(Exception): |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
252 pass |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
253 data = {} |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
254 def remove(k): |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
255 del data[k] |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
256 def encapsulate(): |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
257 f = lambda : () |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
258 data[weakref.ref(f, remove)] = None |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
259 raise BogusError |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
260 try: |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
261 encapsulate() |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
262 except BogusError: |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
263 pass |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
264 else: |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
265 self.fail("exception not properly restored") |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
266 try: |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
267 encapsulate() |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
268 except BogusError: |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
269 pass |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
270 else: |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
271 self.fail("exception not properly restored") |
|
3d68e3110e1f
Regression test for SF bug #478534 -- exceptions could "leak" into a weakref
Fred Drake <fdrake@acm.org>
parents:
20903
diff
changeset
|
272 |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
273 |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
274 class Object: |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
275 def __init__(self, arg): |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
276 self.arg = arg |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
277 def __repr__(self): |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
278 return "<Object %r>" % self.arg |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
279 |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
280 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
281 class MappingTestCase(TestBase): |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
282 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
283 COUNT = 10 |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
284 |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
285 def test_weak_values(self): |
|
17651
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
286 # |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
287 # This exercises d.copy(), d.items(), d[], del d[], len(d). |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
288 # |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
289 dict, objects = self.make_weak_valued_dict() |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
290 for o in objects: |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
291 self.assert_(weakref.getweakrefcount(o) == 1, |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
292 "wrong number of weak references to %r!" % o) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
293 self.assert_(o is dict[o.arg], |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
294 "wrong object returned by weak dict!") |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
295 items1 = dict.items() |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
296 items2 = dict.copy().items() |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
297 items1.sort() |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
298 items2.sort() |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
299 self.assert_(items1 == items2, |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
300 "cloning of weak-valued dictionary did not work!") |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
301 del items1, items2 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
302 self.assert_(len(dict) == self.COUNT) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
303 del objects[0] |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
304 self.assert_(len(dict) == (self.COUNT - 1), |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
305 "deleting object did not cause dictionary update") |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
306 del objects, o |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
307 self.assert_(len(dict) == 0, |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
308 "deleting the values did not clear the dictionary") |
|
18952
e90a658a58d4
Make sure that WeakValueDictionary[] raises KeyError instead of TypeError
Fred Drake <fdrake@acm.org>
parents:
17767
diff
changeset
|
309 # regression on SF bug #447152: |
|
e90a658a58d4
Make sure that WeakValueDictionary[] raises KeyError instead of TypeError
Fred Drake <fdrake@acm.org>
parents:
17767
diff
changeset
|
310 dict = weakref.WeakValueDictionary() |
|
e90a658a58d4
Make sure that WeakValueDictionary[] raises KeyError instead of TypeError
Fred Drake <fdrake@acm.org>
parents:
17767
diff
changeset
|
311 self.assertRaises(KeyError, dict.__getitem__, 1) |
|
e90a658a58d4
Make sure that WeakValueDictionary[] raises KeyError instead of TypeError
Fred Drake <fdrake@acm.org>
parents:
17767
diff
changeset
|
312 dict[2] = C() |
|
e90a658a58d4
Make sure that WeakValueDictionary[] raises KeyError instead of TypeError
Fred Drake <fdrake@acm.org>
parents:
17767
diff
changeset
|
313 self.assertRaises(KeyError, dict.__getitem__, 2) |
|
16794
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16385
diff
changeset
|
314 |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
315 def test_weak_keys(self): |
|
17651
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
316 # |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
317 # This exercises d.copy(), d.items(), d[] = v, d[], del d[], |
|
20903
bfcc2ed9caba
Add a regression test for SF bug #478536: If a value cannot be weakly
Fred Drake <fdrake@acm.org>
parents:
20533
diff
changeset
|
318 # len(d), d.has_key(). |
|
17651
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
319 # |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
320 dict, objects = self.make_weak_keyed_dict() |
|
17172
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
321 for o in objects: |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
322 self.assert_(weakref.getweakrefcount(o) == 1, |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
323 "wrong number of weak references to %r!" % o) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
324 self.assert_(o.arg is dict[o], |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
325 "wrong object returned by weak dict!") |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
326 items1 = dict.items() |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
327 items2 = dict.copy().items() |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
328 items1.sort() |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
329 items2.sort() |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
330 self.assert_(items1 == items2, |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
331 "cloning of weak-keyed dictionary did not work!") |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
332 del items1, items2 |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
333 self.assert_(len(dict) == self.COUNT) |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
334 del objects[0] |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
335 self.assert_(len(dict) == (self.COUNT - 1), |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
336 "deleting object did not cause dictionary update") |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
337 del objects, o |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
338 self.assert_(len(dict) == 0, |
|
10902ecabca9
Convert the weakref test suite to PyUNIT, and add tests that exercise weak
Fred Drake <fdrake@acm.org>
parents:
16885
diff
changeset
|
339 "deleting the keys did not clear the dictionary") |
|
20903
bfcc2ed9caba
Add a regression test for SF bug #478536: If a value cannot be weakly
Fred Drake <fdrake@acm.org>
parents:
20533
diff
changeset
|
340 o = Object(42) |
|
bfcc2ed9caba
Add a regression test for SF bug #478536: If a value cannot be weakly
Fred Drake <fdrake@acm.org>
parents:
20533
diff
changeset
|
341 dict[o] = "What is the meaning of the universe?" |
|
bfcc2ed9caba
Add a regression test for SF bug #478536: If a value cannot be weakly
Fred Drake <fdrake@acm.org>
parents:
20533
diff
changeset
|
342 self.assert_(dict.has_key(o)) |
|
bfcc2ed9caba
Add a regression test for SF bug #478536: If a value cannot be weakly
Fred Drake <fdrake@acm.org>
parents:
20533
diff
changeset
|
343 self.assert_(not dict.has_key(34)) |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
344 |
|
17651
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
345 def test_weak_keyed_iters(self): |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
346 dict, objects = self.make_weak_keyed_dict() |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
347 self.check_iters(dict) |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
348 |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
349 def test_weak_valued_iters(self): |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
350 dict, objects = self.make_weak_valued_dict() |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
351 self.check_iters(dict) |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
352 |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
353 def check_iters(self, dict): |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
354 # item iterator: |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
355 items = dict.items() |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
356 for item in dict.iteritems(): |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
357 items.remove(item) |
|
17767
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
358 self.assert_(len(items) == 0, "iteritems() did not touch all items") |
|
17651
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
359 |
|
17767
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
360 # key iterator, via __iter__(): |
|
17651
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
361 keys = dict.keys() |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
362 for k in dict: |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
363 keys.remove(k) |
|
17767
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
364 self.assert_(len(keys) == 0, "__iter__() did not touch all keys") |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
365 |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
366 # key iterator, via iterkeys(): |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
367 keys = dict.keys() |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
368 for k in dict.iterkeys(): |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
369 keys.remove(k) |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
370 self.assert_(len(keys) == 0, "iterkeys() did not touch all keys") |
|
17651
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
371 |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
372 # value iterator: |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
373 values = dict.values() |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
374 for v in dict.itervalues(): |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
375 values.remove(v) |
|
17767
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
376 self.assert_(len(values) == 0, "itervalues() did not touch all values") |
|
17651
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
377 |
|
24995
86aa2c2da40e
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21484
diff
changeset
|
378 def test_make_weak_keyed_dict_from_dict(self): |
|
86aa2c2da40e
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21484
diff
changeset
|
379 o = Object(3) |
|
86aa2c2da40e
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21484
diff
changeset
|
380 dict = weakref.WeakKeyDictionary({o:364}) |
|
86aa2c2da40e
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21484
diff
changeset
|
381 self.assert_(dict[o] == 364) |
|
86aa2c2da40e
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21484
diff
changeset
|
382 |
|
86aa2c2da40e
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21484
diff
changeset
|
383 def test_make_weak_keyed_dict_from_weak_keyed_dict(self): |
|
86aa2c2da40e
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21484
diff
changeset
|
384 o = Object(3) |
|
86aa2c2da40e
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21484
diff
changeset
|
385 dict = weakref.WeakKeyDictionary({o:364}) |
|
86aa2c2da40e
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21484
diff
changeset
|
386 dict2 = weakref.WeakKeyDictionary(dict) |
|
86aa2c2da40e
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21484
diff
changeset
|
387 self.assert_(dict[o] == 364) |
|
86aa2c2da40e
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21484
diff
changeset
|
388 |
|
17651
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
389 def make_weak_keyed_dict(self): |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
390 dict = weakref.WeakKeyDictionary() |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
391 objects = map(Object, range(self.COUNT)) |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
392 for o in objects: |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
393 dict[o] = o.arg |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
394 return dict, objects |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
395 |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
396 def make_weak_valued_dict(self): |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
397 dict = weakref.WeakValueDictionary() |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
398 objects = map(Object, range(self.COUNT)) |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
399 for o in objects: |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
400 dict[o.arg] = o |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
401 return dict, objects |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
402 |
|
17767
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
403 def check_popitem(self, klass, key1, value1, key2, value2): |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
404 weakdict = klass() |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
405 weakdict[key1] = value1 |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
406 weakdict[key2] = value2 |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
407 self.assert_(len(weakdict) == 2) |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
408 k, v = weakdict.popitem() |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
409 self.assert_(len(weakdict) == 1) |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
410 if k is key1: |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
411 self.assert_(v is value1) |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
412 else: |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
413 self.assert_(v is value2) |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
414 k, v = weakdict.popitem() |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
415 self.assert_(len(weakdict) == 0) |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
416 if k is key1: |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
417 self.assert_(v is value1) |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
418 else: |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
419 self.assert_(v is value2) |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
420 |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
421 def test_weak_valued_dict_popitem(self): |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
422 self.check_popitem(weakref.WeakValueDictionary, |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
423 "key1", C(), "key2", C()) |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
424 |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
425 def test_weak_keyed_dict_popitem(self): |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
426 self.check_popitem(weakref.WeakKeyDictionary, |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
427 C(), "value 1", C(), "value 2") |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
428 |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
429 def check_setdefault(self, klass, key, value1, value2): |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
430 self.assert_(value1 is not value2, |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
431 "invalid test" |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
432 " -- value parameters must be distinct objects") |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
433 weakdict = klass() |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
434 o = weakdict.setdefault(key, value1) |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
435 self.assert_(o is value1) |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
436 self.assert_(weakdict.has_key(key)) |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
437 self.assert_(weakdict.get(key) is value1) |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
438 self.assert_(weakdict[key] is value1) |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
439 |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
440 o = weakdict.setdefault(key, value2) |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
441 self.assert_(o is value1) |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
442 self.assert_(weakdict.has_key(key)) |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
443 self.assert_(weakdict.get(key) is value1) |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
444 self.assert_(weakdict[key] is value1) |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
445 |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
446 def test_weak_valued_dict_setdefault(self): |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
447 self.check_setdefault(weakref.WeakValueDictionary, |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
448 "key", C(), C()) |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
449 |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
450 def test_weak_keyed_dict_setdefault(self): |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
451 self.check_setdefault(weakref.WeakKeyDictionary, |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
452 C(), "value 1", "value 2") |
|
47f4d0568c7c
Extend the weakref test suite to cover the complete mapping interface for
Fred Drake <fdrake@acm.org>
parents:
17651
diff
changeset
|
453 |
|
17547
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
454 def check_update(self, klass, dict): |
|
17651
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
455 # |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
456 # This exercises d.update(), len(d), d.keys(), d.has_key(), |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
457 # d.get(), d[]. |
|
fa8326cc761c
Added tests for Weak*Dictionary iterator support.
Fred Drake <fdrake@acm.org>
parents:
17547
diff
changeset
|
458 # |
|
17547
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
459 weakdict = klass() |
|
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
460 weakdict.update(dict) |
|
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
461 self.assert_(len(weakdict) == len(dict)) |
|
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
462 for k in weakdict.keys(): |
|
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
463 self.assert_(dict.has_key(k), |
|
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
464 "mysterious new key appeared in weak dict") |
|
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
465 v = dict.get(k) |
|
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
466 self.assert_(v is weakdict[k]) |
|
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
467 self.assert_(v is weakdict.get(k)) |
|
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
468 for k in dict.keys(): |
|
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
469 self.assert_(weakdict.has_key(k), |
|
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
470 "original key disappeared in weak dict") |
|
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
471 v = dict[k] |
|
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
472 self.assert_(v is weakdict[k]) |
|
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
473 self.assert_(v is weakdict.get(k)) |
|
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
474 |
|
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
475 def test_weak_valued_dict_update(self): |
|
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
476 self.check_update(weakref.WeakValueDictionary, |
|
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
477 {1: C(), 'a': C(), C(): C()}) |
|
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
478 |
|
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
479 def test_weak_keyed_dict_update(self): |
|
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
480 self.check_update(weakref.WeakKeyDictionary, |
|
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
481 {C(): 1, C(): 2, C(): 3}) |
|
6252693afd9a
Add a test case for Weak*Dictionary.update() that would have caught a
Fred Drake <fdrake@acm.org>
parents:
17498
diff
changeset
|
482 |
|
19764
578ef36f8fde
Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
483 def test_weak_keyed_delitem(self): |
|
578ef36f8fde
Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
484 d = weakref.WeakKeyDictionary() |
|
578ef36f8fde
Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
485 o1 = Object('1') |
|
578ef36f8fde
Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
486 o2 = Object('2') |
|
578ef36f8fde
Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
487 d[o1] = 'something' |
|
578ef36f8fde
Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
488 d[o2] = 'something' |
|
578ef36f8fde
Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
489 self.assert_(len(d) == 2) |
|
578ef36f8fde
Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
490 del d[o1] |
|
578ef36f8fde
Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
491 self.assert_(len(d) == 1) |
|
578ef36f8fde
Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
492 self.assert_(d.keys() == [o2]) |
|
578ef36f8fde
Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
493 |
|
578ef36f8fde
Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
494 def test_weak_valued_delitem(self): |
|
578ef36f8fde
Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
495 d = weakref.WeakValueDictionary() |
|
578ef36f8fde
Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
496 o1 = Object('1') |
|
578ef36f8fde
Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
497 o2 = Object('2') |
|
578ef36f8fde
Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
498 d['something'] = o1 |
|
578ef36f8fde
Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
499 d['something else'] = o2 |
|
578ef36f8fde
Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
500 self.assert_(len(d) == 2) |
|
578ef36f8fde
Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
501 del d['something'] |
|
578ef36f8fde
Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
502 self.assert_(len(d) == 1) |
|
578ef36f8fde
Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
503 self.assert_(d.items() == [('something else', o2)]) |
|
578ef36f8fde
Added tests for key deletion for both Weak*Dictionary flavors.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
504 |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
505 |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19764
diff
changeset
|
506 def test_main(): |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19764
diff
changeset
|
507 loader = unittest.TestLoader() |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19764
diff
changeset
|
508 suite = unittest.TestSuite() |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19764
diff
changeset
|
509 suite.addTest(loader.loadTestsFromTestCase(ReferencesTestCase)) |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19764
diff
changeset
|
510 suite.addTest(loader.loadTestsFromTestCase(MappingTestCase)) |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19764
diff
changeset
|
511 test_support.run_suite(suite) |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19764
diff
changeset
|
512 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19764
diff
changeset
|
513 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19764
diff
changeset
|
514 if __name__ == "__main__": |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19764
diff
changeset
|
515 test_main() |
