blob: fe193730fe8488fba9fc771c17bcca7a48e39f46 [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
156print "Non-callable Proxy References"
157print "XXX -- tests not written!"
158
159
160def test_proxy(o, proxy):
161 o.foo = 1
162 verify(proxy.foo == 1,
163 "proxy does not reflect attribute addition")
164 o.foo = 2
165 verify(proxy.foo == 2,
166 "proxy does not reflect attribute modification")
167 del o.foo
168 verify(not hasattr(proxy, 'foo'),
169 "proxy does not reflect attribute removal")
170
171 proxy.foo = 1
172 verify(o.foo == 1,
173 "object does not reflect attribute addition via proxy")
174 proxy.foo = 2
175 verify(o.foo == 2,
176 "object does not reflect attribute modification via proxy")
177 del proxy.foo
178 verify(not hasattr(o, 'foo'),
179 "object does not reflect attribute removal via proxy")
180
181
182o = C()
183test_proxy(o, weakref.proxy(o))
184
185print
186print "Callable Proxy References"
187
188class Callable:
189 bar = None
190 def __call__(self, x):
191 self.bar = x
192
193o = Callable()
194ref1 = weakref.proxy(o)
195
196test_proxy(o, ref1)
197
198verify(type(ref1) is weakref.CallableProxyType,
199 "proxy is not of callable type")
200ref1('twinkies!')
201verify(o.bar == 'twinkies!',
202 "call through proxy not passed through to original")
203
204try:
205 ref1()
206except TypeError:
207 # expect due to too few args
208 pass
209else:
210 raise TestFailed("did not catch expected TypeError -- too few args")
211
212try:
213 ref1(1, 2, 3)
214except TypeError:
215 # expect due to too many args
216 pass
217else:
218 raise TestFailed("did not catch expected TypeError -- too many args")