4 from test_support
import TestFailed
, verify
11 print "Basic Weak References"
13 print "-- Liveness and referent identity"
17 verify(ref() is not None, "weak reference to live object should be live")
19 verify(ref() is not None, "weak ref should still be live")
20 verify(o
is o2
, "<ref>() should return original object if live")
30 ref2
= weakref
.ref(o
, callback
)
33 "callback did not properly set 'cbcalled'")
34 verify(ref2() is None,
35 "ref2 should be dead after deleting object reference")
39 print "-- Reference objects with callbacks"
42 ref1
= weakref
.ref(o
, id)
43 ref2
= weakref
.ref(o
, id)
45 verify(ref1() is None,
46 "expected reference to be invalidated")
47 verify(ref2() is None,
48 "expected reference to be invalidated")
51 print "-- Proxy objects with callbacks"
54 ref1
= weakref
.proxy(o
, id)
55 ref2
= weakref
.proxy(o
, id)
59 except weakref
.ReferenceError:
62 raise TestFailed("expected ReferenceError exception")
65 except weakref
.ReferenceError:
68 raise TestFailed("expected ReferenceError exception")
71 print "-- Re-use of weak reference objects"
72 print " reference objects"
76 # create a proxy to make sure that there's an intervening creation
77 # between these two; it should make no difference
78 proxy
= weakref
.proxy(o
)
81 "reference object w/out callback should have been re-used")
84 proxy
= weakref
.proxy(o
)
88 "reference object w/out callback should have been re-used")
89 verify(weakref
.getweakrefcount(o
) == 2,
90 "wrong weak ref count for object")
92 verify(weakref
.getweakrefcount(o
) == 1,
93 "wrong weak ref count for object after deleting proxy")
95 print " proxy objects"
98 ref3
= weakref
.proxy(o
)
99 ref4
= weakref
.proxy(o
)
101 "proxy object w/out callback should have been re-used")
105 print "clearing ref 1"
108 print "clearing ref 2"
111 ref1
= weakref
.ref(o
, clearing1
)
112 ref2
= weakref
.ref(o
, clearing2
)
113 verify(weakref
.getweakrefcount(o
) == 2,
114 "got wrong number of weak reference objects")
118 ref1
= weakref
.ref(o
, clearing1
)
119 ref2
= weakref
.ref(o
, clearing2
)
121 verify(weakref
.getweakrefs(o
) == [ref2
],
122 "list of refs does not match")
126 ref1
= weakref
.ref(o
, clearing1
)
127 ref2
= weakref
.ref(o
, clearing2
)
129 verify(weakref
.getweakrefs(o
) == [ref1
],
130 "list of refs does not match")
134 print "Weak Valued Dictionaries"
137 def __init__(self
, arg
):
140 return "<Object %r>" % self
.arg
142 dict = weakref
.mapping()
143 objects
= map(Object
, range(10))
146 print "objects are stored in weak dict"
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!")
152 items1
= dict.items()
153 items2
= dict.copy().items()
156 verify(items1
== items2
,
157 "cloning of weak-valued dictionary did not work!")
160 print "weak dict test complete"
163 print "Weak Keyed Dictionaries"
165 dict = weakref
.mapping(weakkeys
=1)
166 objects
= map(Object
, range(10))
169 print "objects are stored in weak dict"
171 verify(weakref
.getweakrefcount(o
) == 1,
172 "wrong number of weak references to %r!" % o
)
173 verify(o
.arg
is dict[o
],
174 "wrong object returned by weak dict!")
175 items1
= dict.items()
176 items2
= dict.copy().items()
179 verify(items1
== items2
,
180 "cloning of weak-keyed dictionary did not work!")
183 verify(len(dict)==0, "deleting the keys did not clear the dictionary")
184 print "weak key dict test complete"
188 print "Non-callable Proxy References"
189 print "XXX -- tests not written!"
192 def test_proxy(o
, proxy
):
194 verify(proxy
.foo
== 1,
195 "proxy does not reflect attribute addition")
197 verify(proxy
.foo
== 2,
198 "proxy does not reflect attribute modification")
200 verify(not hasattr(proxy
, 'foo'),
201 "proxy does not reflect attribute removal")
205 "object does not reflect attribute addition via proxy")
208 "object does not reflect attribute modification via proxy")
210 verify(not hasattr(o
, 'foo'),
211 "object does not reflect attribute removal via proxy")
215 test_proxy(o
, weakref
.proxy(o
))
218 print "Callable Proxy References"
222 def __call__(self
, x
):
226 ref1
= weakref
.proxy(o
)
230 verify(type(ref1
) is weakref
.CallableProxyType
,
231 "proxy is not of callable type")
233 verify(o
.bar
== 'twinkies!',
234 "call through proxy not passed through to original")
239 # expect due to too few args
242 raise TestFailed("did not catch expected TypeError -- too few args")
247 # expect due to too many args
250 raise TestFailed("did not catch expected TypeError -- too many args")