blob: befa70dc53440ed3e73feede0ac5c0a4968712a6 [file] [log] [blame]
Fred Drake41deb1e2001-02-01 05:27:45 +00001import sys
2import weakref
3
4from test_support import TestFailed, verify
5
6
7class C:
8 pass
9
10
11print "Basic Weak References"
12
13print "-- Liveness and referent identity"
14
15o = C()
16ref = weakref.ref(o)
17verify(ref() is not None, "weak reference to live object should be live")
18o2 = ref()
19verify(ref() is not None, "weak ref should still be live")
20verify(o is o2, "<ref>() should return original object if live")
21del o, o2
22del ref
23
24cbcalled = 0
25def callback(o):
26 global cbcalled
27 cbcalled = 1
28
29o = C()
30ref2 = weakref.ref(o, callback)
31del o
32verify(cbcalled,
33 "callback did not properly set 'cbcalled'")
34verify(ref2() is None,
35 "ref2 should be dead after deleting object reference")
36del ref2
37
38
39print "-- Reference objects with callbacks"
40o = C()
41o.bar = 1
42ref1 = weakref.ref(o, id)
43ref2 = weakref.ref(o, id)
44del o
45verify(ref1() is None,
46 "expected reference to be invalidated")
47verify(ref2() is None,
48 "expected reference to be invalidated")
49
50
51print "-- Proxy objects with callbacks"
52o = C()
53o.bar = 1
54ref1 = weakref.proxy(o, id)
55ref2 = weakref.proxy(o, id)
56del o
57try:
58 ref1.bar
59except weakref.ReferenceError:
60 pass
61else:
62 raise TestFailed("expected ReferenceError exception")
63try:
64 ref2.bar
65except weakref.ReferenceError:
66 pass
67else:
68 raise TestFailed("expected ReferenceError exception")
69
70
71print "-- Re-use of weak reference objects"
72print " reference objects"
73
74o = C()
75ref1 = weakref.ref(o)
76# create a proxy to make sure that there's an intervening creation
77# between these two; it should make no difference
78proxy = weakref.proxy(o)
79ref2 = weakref.ref(o)
80verify(ref1 is ref2,
81 "reference object w/out callback should have been re-used")
82
83o = C()
84proxy = weakref.proxy(o)
85ref1 = weakref.ref(o)
86ref2 = weakref.ref(o)
87verify(ref1 is ref2,
88 "reference object w/out callback should have been re-used")
89verify(weakref.getweakrefcount(o) == 2,
90 "wrong weak ref count for object")
91del proxy
92verify(weakref.getweakrefcount(o) == 1,
93 "wrong weak ref count for object after deleting proxy")
94
95print " proxy objects"
96
97o = C()
98ref3 = weakref.proxy(o)
99ref4 = weakref.proxy(o)
100verify(ref3 is ref4,
101 "proxy object w/out callback should have been re-used")
102
103
104def clearing1(r):
105 print "clearing ref 1"
106
107def clearing2(r):
108 print "clearing ref 2"
109
110o = C()
111ref1 = weakref.ref(o, clearing1)
112ref2 = weakref.ref(o, clearing2)
113verify(weakref.getweakrefcount(o) == 2,
114 "got wrong number of weak reference objects")
115del o
116
117o = C()
118ref1 = weakref.ref(o, clearing1)
119ref2 = weakref.ref(o, clearing2)
120del ref1
121verify(weakref.getweakrefs(o) == [ref2],
122 "list of refs does not match")
123del o
124
125o = C()
126ref1 = weakref.ref(o, clearing1)
127ref2 = weakref.ref(o, clearing2)
128del ref2
129verify(weakref.getweakrefs(o) == [ref1],
130 "list of refs does not match")
131del o
132
133print
134print "Weak Valued Dictionaries"
135
136class Object:
137 def __init__(self, arg):
138 self.arg = arg
139 def __repr__(self):
140 return "<Object %r>" % self.arg
141
142dict = weakref.mapping()
143objects = map(Object, range(10))
144for o in objects:
145 dict[o.arg] = o
146print "objects are stored in weak dict"
147for o in objects:
148 verify(weakref.getweakrefcount(o) == 1,
149 "wrong number of weak references to %r!" % o)
150 verify(o is dict[o.arg],
151 "wrong object returned by weak dict!")
152dict.clear()
153print "weak dict test complete"
154
155print
Martin v. Löwis5e163332001-02-27 18:36:56 +0000156print "Weak Keyed Dictionaries"
157
158dict = weakref.mapping(weakkeys=1)
159objects = map(Object, range(10))
160for o in objects:
161 dict[o] = o.arg
162print "objects are stored in weak dict"
163for o in objects:
164 verify(weakref.getweakrefcount(o) == 1,
165 "wrong number of weak references to %r!" % o)
166 verify(o.arg is dict[o],
167 "wrong object returned by weak dict!")
168del objects,o
169verify(len(dict)==0, "deleting the keys did not clear the dictionary")
170print "weak key dict test complete"
171
172
173print
Fred Drake41deb1e2001-02-01 05:27:45 +0000174print "Non-callable Proxy References"
175print "XXX -- tests not written!"
176
177
178def test_proxy(o, proxy):
179 o.foo = 1
180 verify(proxy.foo == 1,
181 "proxy does not reflect attribute addition")
182 o.foo = 2
183 verify(proxy.foo == 2,
184 "proxy does not reflect attribute modification")
185 del o.foo
186 verify(not hasattr(proxy, 'foo'),
187 "proxy does not reflect attribute removal")
188
189 proxy.foo = 1
190 verify(o.foo == 1,
191 "object does not reflect attribute addition via proxy")
192 proxy.foo = 2
193 verify(o.foo == 2,
194 "object does not reflect attribute modification via proxy")
195 del proxy.foo
196 verify(not hasattr(o, 'foo'),
197 "object does not reflect attribute removal via proxy")
198
199
200o = C()
201test_proxy(o, weakref.proxy(o))
202
203print
204print "Callable Proxy References"
205
206class Callable:
207 bar = None
208 def __call__(self, x):
209 self.bar = x
210
211o = Callable()
212ref1 = weakref.proxy(o)
213
214test_proxy(o, ref1)
215
216verify(type(ref1) is weakref.CallableProxyType,
217 "proxy is not of callable type")
218ref1('twinkies!')
219verify(o.bar == 'twinkies!',
220 "call through proxy not passed through to original")
221
222try:
223 ref1()
224except TypeError:
225 # expect due to too few args
226 pass
227else:
228 raise TestFailed("did not catch expected TypeError -- too few args")
229
230try:
231 ref1(1, 2, 3)
232except TypeError:
233 # expect due to too many args
234 pass
235else:
236 raise TestFailed("did not catch expected TypeError -- too many args")