blob: b7c72bd5e1b063409124f65e4a09d862c295d1e6 [file] [log] [blame]
Fred Drakebc875f52004-02-04 23:14:14 +00001import gc
Fred Drake41deb1e2001-02-01 05:27:45 +00002import sys
Fred Drakeb0fefc52001-03-23 04:22:45 +00003import unittest
Fred Drake5935ff02001-12-19 16:54:23 +00004import UserList
Fred Drake41deb1e2001-02-01 05:27:45 +00005import weakref
Georg Brandl88659b02008-05-20 08:40:43 +00006import operator
Fred Drake41deb1e2001-02-01 05:27:45 +00007
Barry Warsaw04f357c2002-07-23 19:04:11 +00008from test import test_support
Fred Drake41deb1e2001-02-01 05:27:45 +00009
Brett Cannonf5bee302007-01-23 23:21:22 +000010# Used in ReferencesTestCase.test_ref_created_during_del() .
11ref_from_del = None
Fred Drake41deb1e2001-02-01 05:27:45 +000012
13class C:
Fred Drakeb0fefc52001-03-23 04:22:45 +000014 def method(self):
15 pass
Fred Drake41deb1e2001-02-01 05:27:45 +000016
17
Fred Drakeb0fefc52001-03-23 04:22:45 +000018class Callable:
19 bar = None
Fred Drake41deb1e2001-02-01 05:27:45 +000020
Fred Drakeb0fefc52001-03-23 04:22:45 +000021 def __call__(self, x):
22 self.bar = x
Fred Drake41deb1e2001-02-01 05:27:45 +000023
24
Fred Drakeb0fefc52001-03-23 04:22:45 +000025def create_function():
26 def f(): pass
27 return f
28
29def create_bound_method():
30 return C().method
31
32def create_unbound_method():
33 return C.method
Fred Drake41deb1e2001-02-01 05:27:45 +000034
35
Antoine Pitroub704eab2012-11-11 19:36:51 +010036class Object:
37 def __init__(self, arg):
38 self.arg = arg
39 def __repr__(self):
40 return "<Object %r>" % self.arg
41 def __eq__(self, other):
42 if isinstance(other, Object):
43 return self.arg == other.arg
44 return NotImplemented
45 def __ne__(self, other):
46 if isinstance(other, Object):
47 return self.arg != other.arg
48 return NotImplemented
49 def __hash__(self):
50 return hash(self.arg)
51
52class RefCycle:
53 def __init__(self):
54 self.cycle = self
55
56
Fred Drakeb0fefc52001-03-23 04:22:45 +000057class TestBase(unittest.TestCase):
58
59 def setUp(self):
60 self.cbcalled = 0
61
62 def callback(self, ref):
63 self.cbcalled += 1
Fred Drake41deb1e2001-02-01 05:27:45 +000064
65
Fred Drakeb0fefc52001-03-23 04:22:45 +000066class ReferencesTestCase(TestBase):
Fred Drake41deb1e2001-02-01 05:27:45 +000067
Fred Drakeb0fefc52001-03-23 04:22:45 +000068 def test_basic_ref(self):
69 self.check_basic_ref(C)
70 self.check_basic_ref(create_function)
71 self.check_basic_ref(create_bound_method)
72 self.check_basic_ref(create_unbound_method)
Fred Drake41deb1e2001-02-01 05:27:45 +000073
Fred Drake43735da2002-04-11 03:59:42 +000074 # Just make sure the tp_repr handler doesn't raise an exception.
75 # Live reference:
76 o = C()
77 wr = weakref.ref(o)
Florent Xicluna07627882010-03-21 01:14:24 +000078 repr(wr)
Fred Drake43735da2002-04-11 03:59:42 +000079 # Dead reference:
80 del o
Florent Xicluna07627882010-03-21 01:14:24 +000081 repr(wr)
Fred Drake43735da2002-04-11 03:59:42 +000082
Fred Drakeb0fefc52001-03-23 04:22:45 +000083 def test_basic_callback(self):
84 self.check_basic_callback(C)
85 self.check_basic_callback(create_function)
86 self.check_basic_callback(create_bound_method)
87 self.check_basic_callback(create_unbound_method)
Fred Drake41deb1e2001-02-01 05:27:45 +000088
Fred Drakeb0fefc52001-03-23 04:22:45 +000089 def test_multiple_callbacks(self):
90 o = C()
91 ref1 = weakref.ref(o, self.callback)
92 ref2 = weakref.ref(o, self.callback)
93 del o
Benjamin Peterson5c8da862009-06-30 22:57:08 +000094 self.assertTrue(ref1() is None,
Fred Drakeb0fefc52001-03-23 04:22:45 +000095 "expected reference to be invalidated")
Benjamin Peterson5c8da862009-06-30 22:57:08 +000096 self.assertTrue(ref2() is None,
Fred Drakeb0fefc52001-03-23 04:22:45 +000097 "expected reference to be invalidated")
Benjamin Peterson5c8da862009-06-30 22:57:08 +000098 self.assertTrue(self.cbcalled == 2,
Fred Drakeb0fefc52001-03-23 04:22:45 +000099 "callback not called the right number of times")
Fred Drake41deb1e2001-02-01 05:27:45 +0000100
Fred Drake705088e2001-04-13 17:18:15 +0000101 def test_multiple_selfref_callbacks(self):
Guido van Rossum9eee5542002-08-22 20:21:30 +0000102 # Make sure all references are invalidated before callbacks are called
Fred Drake705088e2001-04-13 17:18:15 +0000103 #
104 # What's important here is that we're using the first
105 # reference in the callback invoked on the second reference
106 # (the most recently created ref is cleaned up first). This
107 # tests that all references to the object are invalidated
108 # before any of the callbacks are invoked, so that we only
109 # have one invocation of _weakref.c:cleanup_helper() active
110 # for a particular object at a time.
111 #
112 def callback(object, self=self):
113 self.ref()
114 c = C()
115 self.ref = weakref.ref(c, callback)
116 ref1 = weakref.ref(c, callback)
117 del c
118
Fred Drakeb0fefc52001-03-23 04:22:45 +0000119 def test_proxy_ref(self):
120 o = C()
121 o.bar = 1
122 ref1 = weakref.proxy(o, self.callback)
123 ref2 = weakref.proxy(o, self.callback)
124 del o
Fred Drake41deb1e2001-02-01 05:27:45 +0000125
Fred Drakeb0fefc52001-03-23 04:22:45 +0000126 def check(proxy):
127 proxy.bar
Fred Drake41deb1e2001-02-01 05:27:45 +0000128
Fred Drakeb0fefc52001-03-23 04:22:45 +0000129 self.assertRaises(weakref.ReferenceError, check, ref1)
130 self.assertRaises(weakref.ReferenceError, check, ref2)
Neal Norwitzbdcb9412004-07-08 01:22:31 +0000131 self.assertRaises(weakref.ReferenceError, bool, weakref.proxy(C()))
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000132 self.assertTrue(self.cbcalled == 2)
Fred Drake41deb1e2001-02-01 05:27:45 +0000133
Fred Drakeb0fefc52001-03-23 04:22:45 +0000134 def check_basic_ref(self, factory):
135 o = factory()
136 ref = weakref.ref(o)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000137 self.assertTrue(ref() is not None,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000138 "weak reference to live object should be live")
139 o2 = ref()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000140 self.assertTrue(o is o2,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000141 "<ref>() should return original object if live")
Fred Drake41deb1e2001-02-01 05:27:45 +0000142
Fred Drakeb0fefc52001-03-23 04:22:45 +0000143 def check_basic_callback(self, factory):
144 self.cbcalled = 0
145 o = factory()
146 ref = weakref.ref(o, self.callback)
147 del o
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000148 self.assertTrue(self.cbcalled == 1,
Fred Drake705088e2001-04-13 17:18:15 +0000149 "callback did not properly set 'cbcalled'")
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000150 self.assertTrue(ref() is None,
Fred Drake705088e2001-04-13 17:18:15 +0000151 "ref2 should be dead after deleting object reference")
Fred Drake41deb1e2001-02-01 05:27:45 +0000152
Fred Drakeb0fefc52001-03-23 04:22:45 +0000153 def test_ref_reuse(self):
154 o = C()
155 ref1 = weakref.ref(o)
156 # create a proxy to make sure that there's an intervening creation
157 # between these two; it should make no difference
158 proxy = weakref.proxy(o)
159 ref2 = weakref.ref(o)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000160 self.assertTrue(ref1 is ref2,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000161 "reference object w/out callback should be re-used")
Fred Drake41deb1e2001-02-01 05:27:45 +0000162
Fred Drakeb0fefc52001-03-23 04:22:45 +0000163 o = C()
164 proxy = weakref.proxy(o)
165 ref1 = weakref.ref(o)
166 ref2 = weakref.ref(o)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000167 self.assertTrue(ref1 is ref2,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000168 "reference object w/out callback should be re-used")
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000169 self.assertTrue(weakref.getweakrefcount(o) == 2,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000170 "wrong weak ref count for object")
171 del proxy
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000172 self.assertTrue(weakref.getweakrefcount(o) == 1,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000173 "wrong weak ref count for object after deleting proxy")
Fred Drake41deb1e2001-02-01 05:27:45 +0000174
Fred Drakeb0fefc52001-03-23 04:22:45 +0000175 def test_proxy_reuse(self):
176 o = C()
177 proxy1 = weakref.proxy(o)
178 ref = weakref.ref(o)
179 proxy2 = weakref.proxy(o)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000180 self.assertTrue(proxy1 is proxy2,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000181 "proxy object w/out callback should have been re-used")
182
183 def test_basic_proxy(self):
184 o = C()
185 self.check_proxy(o, weakref.proxy(o))
186
Fred Drake5935ff02001-12-19 16:54:23 +0000187 L = UserList.UserList()
188 p = weakref.proxy(L)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000189 self.assertFalse(p, "proxy for empty UserList should be false")
Fred Drake5935ff02001-12-19 16:54:23 +0000190 p.append(12)
191 self.assertEqual(len(L), 1)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000192 self.assertTrue(p, "proxy for non-empty UserList should be true")
Florent Xicluna07627882010-03-21 01:14:24 +0000193 with test_support.check_py3k_warnings():
194 p[:] = [2, 3]
Fred Drake5935ff02001-12-19 16:54:23 +0000195 self.assertEqual(len(L), 2)
196 self.assertEqual(len(p), 2)
Ezio Melottiaa980582010-01-23 23:04:36 +0000197 self.assertIn(3, p, "proxy didn't support __contains__() properly")
Fred Drake5935ff02001-12-19 16:54:23 +0000198 p[1] = 5
199 self.assertEqual(L[1], 5)
200 self.assertEqual(p[1], 5)
201 L2 = UserList.UserList(L)
202 p2 = weakref.proxy(L2)
203 self.assertEqual(p, p2)
Walter Dörwald70a6b492004-02-12 17:35:32 +0000204 ## self.assertEqual(repr(L2), repr(p2))
Fred Drake43735da2002-04-11 03:59:42 +0000205 L3 = UserList.UserList(range(10))
206 p3 = weakref.proxy(L3)
Florent Xicluna07627882010-03-21 01:14:24 +0000207 with test_support.check_py3k_warnings():
208 self.assertEqual(L3[:], p3[:])
209 self.assertEqual(L3[5:], p3[5:])
210 self.assertEqual(L3[:5], p3[:5])
211 self.assertEqual(L3[2:5], p3[2:5])
Fred Drake5935ff02001-12-19 16:54:23 +0000212
Benjamin Petersondc3c2392009-11-19 03:00:02 +0000213 def test_proxy_unicode(self):
214 # See bug 5037
215 class C(object):
216 def __str__(self):
217 return "string"
218 def __unicode__(self):
219 return u"unicode"
220 instance = C()
Ezio Melottiaa980582010-01-23 23:04:36 +0000221 self.assertIn("__unicode__", dir(weakref.proxy(instance)))
Benjamin Petersondc3c2392009-11-19 03:00:02 +0000222 self.assertEqual(unicode(weakref.proxy(instance)), u"unicode")
223
Georg Brandl88659b02008-05-20 08:40:43 +0000224 def test_proxy_index(self):
225 class C:
226 def __index__(self):
227 return 10
228 o = C()
229 p = weakref.proxy(o)
230 self.assertEqual(operator.index(p), 10)
231
232 def test_proxy_div(self):
233 class C:
234 def __floordiv__(self, other):
235 return 42
236 def __ifloordiv__(self, other):
237 return 21
238 o = C()
239 p = weakref.proxy(o)
240 self.assertEqual(p // 5, 42)
241 p //= 5
242 self.assertEqual(p, 21)
243
Fred Drakeea2adc92004-02-03 19:56:46 +0000244 # The PyWeakref_* C API is documented as allowing either NULL or
245 # None as the value for the callback, where either means "no
246 # callback". The "no callback" ref and proxy objects are supposed
247 # to be shared so long as they exist by all callers so long as
Walter Dörwaldda1ad322006-12-12 21:55:31 +0000248 # they are active. In Python 2.3.3 and earlier, this guarantee
Fred Drakeea2adc92004-02-03 19:56:46 +0000249 # was not honored, and was broken in different ways for
250 # PyWeakref_NewRef() and PyWeakref_NewProxy(). (Two tests.)
251
252 def test_shared_ref_without_callback(self):
253 self.check_shared_without_callback(weakref.ref)
254
255 def test_shared_proxy_without_callback(self):
256 self.check_shared_without_callback(weakref.proxy)
257
258 def check_shared_without_callback(self, makeref):
259 o = Object(1)
260 p1 = makeref(o, None)
261 p2 = makeref(o, None)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000262 self.assertTrue(p1 is p2, "both callbacks were None in the C API")
Fred Drakeea2adc92004-02-03 19:56:46 +0000263 del p1, p2
264 p1 = makeref(o)
265 p2 = makeref(o, None)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000266 self.assertTrue(p1 is p2, "callbacks were NULL, None in the C API")
Fred Drakeea2adc92004-02-03 19:56:46 +0000267 del p1, p2
268 p1 = makeref(o)
269 p2 = makeref(o)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000270 self.assertTrue(p1 is p2, "both callbacks were NULL in the C API")
Fred Drakeea2adc92004-02-03 19:56:46 +0000271 del p1, p2
272 p1 = makeref(o, None)
273 p2 = makeref(o)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000274 self.assertTrue(p1 is p2, "callbacks were None, NULL in the C API")
Fred Drakeea2adc92004-02-03 19:56:46 +0000275
Fred Drakeb0fefc52001-03-23 04:22:45 +0000276 def test_callable_proxy(self):
277 o = Callable()
278 ref1 = weakref.proxy(o)
279
280 self.check_proxy(o, ref1)
281
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000282 self.assertTrue(type(ref1) is weakref.CallableProxyType,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000283 "proxy is not of callable type")
284 ref1('twinkies!')
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000285 self.assertTrue(o.bar == 'twinkies!',
Fred Drakeb0fefc52001-03-23 04:22:45 +0000286 "call through proxy not passed through to original")
Fred Drake3bb4d212001-10-18 19:28:29 +0000287 ref1(x='Splat.')
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000288 self.assertTrue(o.bar == 'Splat.',
Fred Drake3bb4d212001-10-18 19:28:29 +0000289 "call through proxy not passed through to original")
Fred Drakeb0fefc52001-03-23 04:22:45 +0000290
291 # expect due to too few args
292 self.assertRaises(TypeError, ref1)
293
294 # expect due to too many args
295 self.assertRaises(TypeError, ref1, 1, 2, 3)
296
297 def check_proxy(self, o, proxy):
298 o.foo = 1
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000299 self.assertTrue(proxy.foo == 1,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000300 "proxy does not reflect attribute addition")
301 o.foo = 2
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000302 self.assertTrue(proxy.foo == 2,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000303 "proxy does not reflect attribute modification")
304 del o.foo
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000305 self.assertTrue(not hasattr(proxy, 'foo'),
Fred Drakeb0fefc52001-03-23 04:22:45 +0000306 "proxy does not reflect attribute removal")
307
308 proxy.foo = 1
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000309 self.assertTrue(o.foo == 1,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000310 "object does not reflect attribute addition via proxy")
311 proxy.foo = 2
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000312 self.assertTrue(
Fred Drakeb0fefc52001-03-23 04:22:45 +0000313 o.foo == 2,
314 "object does not reflect attribute modification via proxy")
315 del proxy.foo
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000316 self.assertTrue(not hasattr(o, 'foo'),
Fred Drakeb0fefc52001-03-23 04:22:45 +0000317 "object does not reflect attribute removal via proxy")
318
Raymond Hettingerd693a812003-06-30 04:18:48 +0000319 def test_proxy_deletion(self):
320 # Test clearing of SF bug #762891
321 class Foo:
322 result = None
323 def __delitem__(self, accessor):
324 self.result = accessor
325 g = Foo()
326 f = weakref.proxy(g)
327 del f[0]
328 self.assertEqual(f.result, 0)
329
Raymond Hettingere6c470f2005-03-27 03:04:54 +0000330 def test_proxy_bool(self):
331 # Test clearing of SF bug #1170766
332 class List(list): pass
333 lyst = List()
334 self.assertEqual(bool(weakref.proxy(lyst)), bool(lyst))
335
Fred Drakeb0fefc52001-03-23 04:22:45 +0000336 def test_getweakrefcount(self):
337 o = C()
338 ref1 = weakref.ref(o)
339 ref2 = weakref.ref(o, self.callback)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000340 self.assertTrue(weakref.getweakrefcount(o) == 2,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000341 "got wrong number of weak reference objects")
342
343 proxy1 = weakref.proxy(o)
344 proxy2 = weakref.proxy(o, self.callback)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000345 self.assertTrue(weakref.getweakrefcount(o) == 4,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000346 "got wrong number of weak reference objects")
347
Fred Drakeea2adc92004-02-03 19:56:46 +0000348 del ref1, ref2, proxy1, proxy2
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000349 self.assertTrue(weakref.getweakrefcount(o) == 0,
Fred Drakeea2adc92004-02-03 19:56:46 +0000350 "weak reference objects not unlinked from"
351 " referent when discarded.")
352
Walter Dörwaldb167b042003-12-11 12:34:05 +0000353 # assumes ints do not support weakrefs
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000354 self.assertTrue(weakref.getweakrefcount(1) == 0,
Walter Dörwaldb167b042003-12-11 12:34:05 +0000355 "got wrong number of weak reference objects for int")
356
Fred Drakeb0fefc52001-03-23 04:22:45 +0000357 def test_getweakrefs(self):
358 o = C()
359 ref1 = weakref.ref(o, self.callback)
360 ref2 = weakref.ref(o, self.callback)
361 del ref1
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000362 self.assertTrue(weakref.getweakrefs(o) == [ref2],
Fred Drakeb0fefc52001-03-23 04:22:45 +0000363 "list of refs does not match")
364
365 o = C()
366 ref1 = weakref.ref(o, self.callback)
367 ref2 = weakref.ref(o, self.callback)
368 del ref2
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000369 self.assertTrue(weakref.getweakrefs(o) == [ref1],
Fred Drakeb0fefc52001-03-23 04:22:45 +0000370 "list of refs does not match")
371
Fred Drakeea2adc92004-02-03 19:56:46 +0000372 del ref1
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000373 self.assertTrue(weakref.getweakrefs(o) == [],
Fred Drakeea2adc92004-02-03 19:56:46 +0000374 "list of refs not cleared")
375
Walter Dörwaldb167b042003-12-11 12:34:05 +0000376 # assumes ints do not support weakrefs
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000377 self.assertTrue(weakref.getweakrefs(1) == [],
Walter Dörwaldb167b042003-12-11 12:34:05 +0000378 "list of refs does not match for int")
379
Fred Drake39c27f12001-10-18 18:06:05 +0000380 def test_newstyle_number_ops(self):
381 class F(float):
382 pass
383 f = F(2.0)
384 p = weakref.proxy(f)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000385 self.assertTrue(p + 1.0 == 3.0)
386 self.assertTrue(1.0 + p == 3.0) # this used to SEGV
Fred Drake39c27f12001-10-18 18:06:05 +0000387
Fred Drake2a64f462001-12-10 23:46:02 +0000388 def test_callbacks_protected(self):
Guido van Rossum9eee5542002-08-22 20:21:30 +0000389 # Callbacks protected from already-set exceptions?
Fred Drake2a64f462001-12-10 23:46:02 +0000390 # Regression test for SF bug #478534.
391 class BogusError(Exception):
392 pass
393 data = {}
394 def remove(k):
395 del data[k]
396 def encapsulate():
397 f = lambda : ()
398 data[weakref.ref(f, remove)] = None
399 raise BogusError
400 try:
401 encapsulate()
402 except BogusError:
403 pass
404 else:
405 self.fail("exception not properly restored")
406 try:
407 encapsulate()
408 except BogusError:
409 pass
410 else:
411 self.fail("exception not properly restored")
412
Tim Petersadd09b42003-11-12 20:43:28 +0000413 def test_sf_bug_840829(self):
414 # "weakref callbacks and gc corrupt memory"
415 # subtype_dealloc erroneously exposed a new-style instance
416 # already in the process of getting deallocated to gc,
417 # causing double-deallocation if the instance had a weakref
418 # callback that triggered gc.
419 # If the bug exists, there probably won't be an obvious symptom
420 # in a release build. In a debug build, a segfault will occur
421 # when the second attempt to remove the instance from the "list
422 # of all objects" occurs.
423
424 import gc
425
426 class C(object):
427 pass
428
429 c = C()
430 wr = weakref.ref(c, lambda ignore: gc.collect())
431 del c
432
Tim Petersf7f9e992003-11-13 21:59:32 +0000433 # There endeth the first part. It gets worse.
434 del wr
435
436 c1 = C()
437 c1.i = C()
438 wr = weakref.ref(c1.i, lambda ignore: gc.collect())
439
440 c2 = C()
441 c2.c1 = c1
442 del c1 # still alive because c2 points to it
443
444 # Now when subtype_dealloc gets called on c2, it's not enough just
445 # that c2 is immune from gc while the weakref callbacks associated
446 # with c2 execute (there are none in this 2nd half of the test, btw).
447 # subtype_dealloc goes on to call the base classes' deallocs too,
448 # so any gc triggered by weakref callbacks associated with anything
449 # torn down by a base class dealloc can also trigger double
450 # deallocation of c2.
451 del c2
Fred Drake41deb1e2001-02-01 05:27:45 +0000452
Tim Peters403a2032003-11-20 21:21:46 +0000453 def test_callback_in_cycle_1(self):
454 import gc
455
456 class J(object):
457 pass
458
459 class II(object):
460 def acallback(self, ignore):
461 self.J
462
463 I = II()
464 I.J = J
465 I.wr = weakref.ref(J, I.acallback)
466
467 # Now J and II are each in a self-cycle (as all new-style class
468 # objects are, since their __mro__ points back to them). I holds
469 # both a weak reference (I.wr) and a strong reference (I.J) to class
470 # J. I is also in a cycle (I.wr points to a weakref that references
471 # I.acallback). When we del these three, they all become trash, but
472 # the cycles prevent any of them from getting cleaned up immediately.
473 # Instead they have to wait for cyclic gc to deduce that they're
474 # trash.
475 #
476 # gc used to call tp_clear on all of them, and the order in which
477 # it does that is pretty accidental. The exact order in which we
478 # built up these things manages to provoke gc into running tp_clear
479 # in just the right order (I last). Calling tp_clear on II leaves
480 # behind an insane class object (its __mro__ becomes NULL). Calling
481 # tp_clear on J breaks its self-cycle, but J doesn't get deleted
482 # just then because of the strong reference from I.J. Calling
483 # tp_clear on I starts to clear I's __dict__, and just happens to
484 # clear I.J first -- I.wr is still intact. That removes the last
485 # reference to J, which triggers the weakref callback. The callback
486 # tries to do "self.J", and instances of new-style classes look up
487 # attributes ("J") in the class dict first. The class (II) wants to
488 # search II.__mro__, but that's NULL. The result was a segfault in
489 # a release build, and an assert failure in a debug build.
490 del I, J, II
491 gc.collect()
492
493 def test_callback_in_cycle_2(self):
494 import gc
495
496 # This is just like test_callback_in_cycle_1, except that II is an
497 # old-style class. The symptom is different then: an instance of an
498 # old-style class looks in its own __dict__ first. 'J' happens to
499 # get cleared from I.__dict__ before 'wr', and 'J' was never in II's
500 # __dict__, so the attribute isn't found. The difference is that
501 # the old-style II doesn't have a NULL __mro__ (it doesn't have any
502 # __mro__), so no segfault occurs. Instead it got:
503 # test_callback_in_cycle_2 (__main__.ReferencesTestCase) ...
504 # Exception exceptions.AttributeError:
505 # "II instance has no attribute 'J'" in <bound method II.acallback
506 # of <?.II instance at 0x00B9B4B8>> ignored
507
508 class J(object):
509 pass
510
511 class II:
512 def acallback(self, ignore):
513 self.J
514
515 I = II()
516 I.J = J
517 I.wr = weakref.ref(J, I.acallback)
518
519 del I, J, II
520 gc.collect()
521
522 def test_callback_in_cycle_3(self):
523 import gc
524
525 # This one broke the first patch that fixed the last two. In this
526 # case, the objects reachable from the callback aren't also reachable
527 # from the object (c1) *triggering* the callback: you can get to
528 # c1 from c2, but not vice-versa. The result was that c2's __dict__
529 # got tp_clear'ed by the time the c2.cb callback got invoked.
530
531 class C:
532 def cb(self, ignore):
533 self.me
534 self.c1
535 self.wr
536
537 c1, c2 = C(), C()
538
539 c2.me = c2
540 c2.c1 = c1
541 c2.wr = weakref.ref(c1, c2.cb)
542
543 del c1, c2
544 gc.collect()
545
546 def test_callback_in_cycle_4(self):
547 import gc
548
549 # Like test_callback_in_cycle_3, except c2 and c1 have different
550 # classes. c2's class (C) isn't reachable from c1 then, so protecting
551 # objects reachable from the dying object (c1) isn't enough to stop
552 # c2's class (C) from getting tp_clear'ed before c2.cb is invoked.
553 # The result was a segfault (C.__mro__ was NULL when the callback
554 # tried to look up self.me).
555
556 class C(object):
557 def cb(self, ignore):
558 self.me
559 self.c1
560 self.wr
561
562 class D:
563 pass
564
565 c1, c2 = D(), C()
566
567 c2.me = c2
568 c2.c1 = c1
569 c2.wr = weakref.ref(c1, c2.cb)
570
571 del c1, c2, C, D
572 gc.collect()
573
574 def test_callback_in_cycle_resurrection(self):
575 import gc
576
577 # Do something nasty in a weakref callback: resurrect objects
578 # from dead cycles. For this to be attempted, the weakref and
579 # its callback must also be part of the cyclic trash (else the
580 # objects reachable via the callback couldn't be in cyclic trash
581 # to begin with -- the callback would act like an external root).
582 # But gc clears trash weakrefs with callbacks early now, which
583 # disables the callbacks, so the callbacks shouldn't get called
584 # at all (and so nothing actually gets resurrected).
585
586 alist = []
587 class C(object):
588 def __init__(self, value):
589 self.attribute = value
590
591 def acallback(self, ignore):
592 alist.append(self.c)
593
594 c1, c2 = C(1), C(2)
595 c1.c = c2
596 c2.c = c1
597 c1.wr = weakref.ref(c2, c1.acallback)
598 c2.wr = weakref.ref(c1, c2.acallback)
599
600 def C_went_away(ignore):
601 alist.append("C went away")
602 wr = weakref.ref(C, C_went_away)
603
604 del c1, c2, C # make them all trash
605 self.assertEqual(alist, []) # del isn't enough to reclaim anything
606
607 gc.collect()
608 # c1.wr and c2.wr were part of the cyclic trash, so should have
609 # been cleared without their callbacks executing. OTOH, the weakref
610 # to C is bound to a function local (wr), and wasn't trash, so that
611 # callback should have been invoked when C went away.
612 self.assertEqual(alist, ["C went away"])
613 # The remaining weakref should be dead now (its callback ran).
614 self.assertEqual(wr(), None)
615
616 del alist[:]
617 gc.collect()
618 self.assertEqual(alist, [])
619
620 def test_callbacks_on_callback(self):
621 import gc
622
623 # Set up weakref callbacks *on* weakref callbacks.
624 alist = []
625 def safe_callback(ignore):
626 alist.append("safe_callback called")
627
628 class C(object):
629 def cb(self, ignore):
630 alist.append("cb called")
631
632 c, d = C(), C()
633 c.other = d
634 d.other = c
635 callback = c.cb
636 c.wr = weakref.ref(d, callback) # this won't trigger
637 d.wr = weakref.ref(callback, d.cb) # ditto
638 external_wr = weakref.ref(callback, safe_callback) # but this will
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000639 self.assertTrue(external_wr() is callback)
Tim Peters403a2032003-11-20 21:21:46 +0000640
641 # The weakrefs attached to c and d should get cleared, so that
642 # C.cb is never called. But external_wr isn't part of the cyclic
643 # trash, and no cyclic trash is reachable from it, so safe_callback
644 # should get invoked when the bound method object callback (c.cb)
645 # -- which is itself a callback, and also part of the cyclic trash --
646 # gets reclaimed at the end of gc.
647
648 del callback, c, d, C
649 self.assertEqual(alist, []) # del isn't enough to clean up cycles
650 gc.collect()
651 self.assertEqual(alist, ["safe_callback called"])
652 self.assertEqual(external_wr(), None)
653
654 del alist[:]
655 gc.collect()
656 self.assertEqual(alist, [])
657
Fred Drakebc875f52004-02-04 23:14:14 +0000658 def test_gc_during_ref_creation(self):
659 self.check_gc_during_creation(weakref.ref)
660
661 def test_gc_during_proxy_creation(self):
662 self.check_gc_during_creation(weakref.proxy)
663
664 def check_gc_during_creation(self, makeref):
665 thresholds = gc.get_threshold()
666 gc.set_threshold(1, 1, 1)
667 gc.collect()
Fred Drake55cf4342004-02-13 19:21:57 +0000668 class A:
669 pass
Fred Drakebc875f52004-02-04 23:14:14 +0000670
671 def callback(*args):
672 pass
673
Fred Drake55cf4342004-02-13 19:21:57 +0000674 referenced = A()
Fred Drakebc875f52004-02-04 23:14:14 +0000675
Fred Drake55cf4342004-02-13 19:21:57 +0000676 a = A()
Fred Drakebc875f52004-02-04 23:14:14 +0000677 a.a = a
678 a.wr = makeref(referenced)
679
680 try:
681 # now make sure the object and the ref get labeled as
682 # cyclic trash:
Fred Drake55cf4342004-02-13 19:21:57 +0000683 a = A()
684 weakref.ref(referenced, callback)
Fred Drakebc875f52004-02-04 23:14:14 +0000685
686 finally:
687 gc.set_threshold(*thresholds)
688
Brett Cannonf5bee302007-01-23 23:21:22 +0000689 def test_ref_created_during_del(self):
690 # Bug #1377858
691 # A weakref created in an object's __del__() would crash the
692 # interpreter when the weakref was cleaned up since it would refer to
693 # non-existent memory. This test should not segfault the interpreter.
694 class Target(object):
695 def __del__(self):
696 global ref_from_del
697 ref_from_del = weakref.ref(self)
698
699 w = Target()
700
Benjamin Peterson97179b02008-09-09 20:55:01 +0000701 def test_init(self):
702 # Issue 3634
703 # <weakref to class>.__init__() doesn't check errors correctly
704 r = weakref.ref(Exception)
705 self.assertRaises(TypeError, r.__init__, 0, 0, 0, 0, 0)
706 # No exception should be raised here
707 gc.collect()
708
Antoine Pitroua57df2c2010-03-31 21:32:15 +0000709 def test_classes(self):
710 # Check that both old-style classes and new-style classes
711 # are weakrefable.
712 class A(object):
713 pass
714 class B:
715 pass
716 l = []
717 weakref.ref(int)
718 a = weakref.ref(A, l.append)
719 A = None
720 gc.collect()
721 self.assertEqual(a(), None)
722 self.assertEqual(l, [a])
723 b = weakref.ref(B, l.append)
724 B = None
725 gc.collect()
726 self.assertEqual(b(), None)
727 self.assertEqual(l, [a, b])
728
Antoine Pitroub704eab2012-11-11 19:36:51 +0100729 def test_equality(self):
730 # Alive weakrefs defer equality testing to their underlying object.
731 x = Object(1)
732 y = Object(1)
733 z = Object(2)
734 a = weakref.ref(x)
735 b = weakref.ref(y)
736 c = weakref.ref(z)
737 d = weakref.ref(x)
738 # Note how we directly test the operators here, to stress both
739 # __eq__ and __ne__.
740 self.assertTrue(a == b)
741 self.assertFalse(a != b)
742 self.assertFalse(a == c)
743 self.assertTrue(a != c)
744 self.assertTrue(a == d)
745 self.assertFalse(a != d)
746 del x, y, z
747 gc.collect()
748 for r in a, b, c:
749 # Sanity check
750 self.assertIs(r(), None)
751 # Dead weakrefs compare by identity: whether `a` and `d` are the
752 # same weakref object is an implementation detail, since they pointed
753 # to the same original object and didn't have a callback.
754 # (see issue #16453).
755 self.assertFalse(a == b)
756 self.assertTrue(a != b)
757 self.assertFalse(a == c)
758 self.assertTrue(a != c)
759 self.assertEqual(a == d, a is d)
760 self.assertEqual(a != d, a is not d)
761
762 def test_hashing(self):
763 # Alive weakrefs hash the same as the underlying object
764 x = Object(42)
765 y = Object(42)
766 a = weakref.ref(x)
767 b = weakref.ref(y)
768 self.assertEqual(hash(a), hash(42))
769 del x, y
770 gc.collect()
771 # Dead weakrefs:
772 # - retain their hash is they were hashed when alive;
773 # - otherwise, cannot be hashed.
774 self.assertEqual(hash(a), hash(42))
775 self.assertRaises(TypeError, hash, b)
776
Fred Drake0a4dd392004-07-02 18:57:45 +0000777
Amaury Forgeot d'Arca8919fe2008-06-16 19:12:42 +0000778class SubclassableWeakrefTestCase(TestBase):
Fred Drake0a4dd392004-07-02 18:57:45 +0000779
780 def test_subclass_refs(self):
781 class MyRef(weakref.ref):
782 def __init__(self, ob, callback=None, value=42):
783 self.value = value
784 super(MyRef, self).__init__(ob, callback)
785 def __call__(self):
786 self.called = True
787 return super(MyRef, self).__call__()
788 o = Object("foo")
789 mr = MyRef(o, value=24)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000790 self.assertTrue(mr() is o)
791 self.assertTrue(mr.called)
Fred Drake0a4dd392004-07-02 18:57:45 +0000792 self.assertEqual(mr.value, 24)
793 del o
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000794 self.assertTrue(mr() is None)
795 self.assertTrue(mr.called)
Fred Drake0a4dd392004-07-02 18:57:45 +0000796
797 def test_subclass_refs_dont_replace_standard_refs(self):
798 class MyRef(weakref.ref):
799 pass
800 o = Object(42)
801 r1 = MyRef(o)
802 r2 = weakref.ref(o)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000803 self.assertTrue(r1 is not r2)
Fred Drake0a4dd392004-07-02 18:57:45 +0000804 self.assertEqual(weakref.getweakrefs(o), [r2, r1])
805 self.assertEqual(weakref.getweakrefcount(o), 2)
806 r3 = MyRef(o)
807 self.assertEqual(weakref.getweakrefcount(o), 3)
808 refs = weakref.getweakrefs(o)
809 self.assertEqual(len(refs), 3)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000810 self.assertTrue(r2 is refs[0])
Ezio Melottiaa980582010-01-23 23:04:36 +0000811 self.assertIn(r1, refs[1:])
812 self.assertIn(r3, refs[1:])
Fred Drake0a4dd392004-07-02 18:57:45 +0000813
814 def test_subclass_refs_dont_conflate_callbacks(self):
815 class MyRef(weakref.ref):
816 pass
817 o = Object(42)
818 r1 = MyRef(o, id)
819 r2 = MyRef(o, str)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000820 self.assertTrue(r1 is not r2)
Fred Drake0a4dd392004-07-02 18:57:45 +0000821 refs = weakref.getweakrefs(o)
Ezio Melottiaa980582010-01-23 23:04:36 +0000822 self.assertIn(r1, refs)
823 self.assertIn(r2, refs)
Fred Drake0a4dd392004-07-02 18:57:45 +0000824
825 def test_subclass_refs_with_slots(self):
826 class MyRef(weakref.ref):
827 __slots__ = "slot1", "slot2"
828 def __new__(type, ob, callback, slot1, slot2):
829 return weakref.ref.__new__(type, ob, callback)
830 def __init__(self, ob, callback, slot1, slot2):
831 self.slot1 = slot1
832 self.slot2 = slot2
833 def meth(self):
834 return self.slot1 + self.slot2
835 o = Object(42)
836 r = MyRef(o, None, "abc", "def")
837 self.assertEqual(r.slot1, "abc")
838 self.assertEqual(r.slot2, "def")
839 self.assertEqual(r.meth(), "abcdef")
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000840 self.assertFalse(hasattr(r, "__dict__"))
Fred Drake0a4dd392004-07-02 18:57:45 +0000841
Amaury Forgeot d'Arca8919fe2008-06-16 19:12:42 +0000842 def test_subclass_refs_with_cycle(self):
843 # Bug #3110
844 # An instance of a weakref subclass can have attributes.
845 # If such a weakref holds the only strong reference to the object,
846 # deleting the weakref will delete the object. In this case,
847 # the callback must not be called, because the ref object is
848 # being deleted.
849 class MyRef(weakref.ref):
850 pass
851
852 # Use a local callback, for "regrtest -R::"
853 # to detect refcounting problems
854 def callback(w):
855 self.cbcalled += 1
856
857 o = C()
858 r1 = MyRef(o, callback)
859 r1.o = o
860 del o
861
862 del r1 # Used to crash here
863
864 self.assertEqual(self.cbcalled, 0)
865
866 # Same test, with two weakrefs to the same object
867 # (since code paths are different)
868 o = C()
869 r1 = MyRef(o, callback)
870 r2 = MyRef(o, callback)
871 r1.r = r2
872 r2.o = o
873 del o
874 del r2
875
876 del r1 # Used to crash here
877
878 self.assertEqual(self.cbcalled, 0)
879
Fred Drake0a4dd392004-07-02 18:57:45 +0000880
Fred Drakeb0fefc52001-03-23 04:22:45 +0000881class MappingTestCase(TestBase):
Martin v. Löwis5e163332001-02-27 18:36:56 +0000882
Fred Drakeb0fefc52001-03-23 04:22:45 +0000883 COUNT = 10
884
Antoine Pitrouc56bca32012-03-01 16:26:35 +0100885 def check_len_cycles(self, dict_type, cons):
886 N = 20
887 items = [RefCycle() for i in range(N)]
888 dct = dict_type(cons(o) for o in items)
889 # Keep an iterator alive
890 it = dct.iteritems()
891 try:
892 next(it)
893 except StopIteration:
894 pass
895 del items
896 gc.collect()
897 n1 = len(dct)
898 del it
899 gc.collect()
900 n2 = len(dct)
901 # one item may be kept alive inside the iterator
902 self.assertIn(n1, (0, 1))
903 self.assertEqual(n2, 0)
904
905 def test_weak_keyed_len_cycles(self):
906 self.check_len_cycles(weakref.WeakKeyDictionary, lambda k: (k, 1))
907
908 def test_weak_valued_len_cycles(self):
909 self.check_len_cycles(weakref.WeakValueDictionary, lambda k: (1, k))
910
911 def check_len_race(self, dict_type, cons):
912 # Extended sanity checks for len() in the face of cyclic collection
913 self.addCleanup(gc.set_threshold, *gc.get_threshold())
914 for th in range(1, 100):
915 N = 20
916 gc.collect(0)
917 gc.set_threshold(th, th, th)
918 items = [RefCycle() for i in range(N)]
919 dct = dict_type(cons(o) for o in items)
920 del items
921 # All items will be collected at next garbage collection pass
922 it = dct.iteritems()
923 try:
924 next(it)
925 except StopIteration:
926 pass
927 n1 = len(dct)
928 del it
929 n2 = len(dct)
930 self.assertGreaterEqual(n1, 0)
931 self.assertLessEqual(n1, N)
932 self.assertGreaterEqual(n2, 0)
933 self.assertLessEqual(n2, n1)
934
935 def test_weak_keyed_len_race(self):
936 self.check_len_race(weakref.WeakKeyDictionary, lambda k: (k, 1))
937
938 def test_weak_valued_len_race(self):
939 self.check_len_race(weakref.WeakValueDictionary, lambda k: (1, k))
940
Fred Drakeb0fefc52001-03-23 04:22:45 +0000941 def test_weak_values(self):
Fred Drake0e540c32001-05-02 05:44:22 +0000942 #
943 # This exercises d.copy(), d.items(), d[], del d[], len(d).
944 #
945 dict, objects = self.make_weak_valued_dict()
Fred Drakeb0fefc52001-03-23 04:22:45 +0000946 for o in objects:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000947 self.assertTrue(weakref.getweakrefcount(o) == 1,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000948 "wrong number of weak references to %r!" % o)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000949 self.assertTrue(o is dict[o.arg],
Fred Drakeb0fefc52001-03-23 04:22:45 +0000950 "wrong object returned by weak dict!")
951 items1 = dict.items()
952 items2 = dict.copy().items()
953 items1.sort()
954 items2.sort()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000955 self.assertTrue(items1 == items2,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000956 "cloning of weak-valued dictionary did not work!")
957 del items1, items2
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000958 self.assertTrue(len(dict) == self.COUNT)
Fred Drakeb0fefc52001-03-23 04:22:45 +0000959 del objects[0]
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000960 self.assertTrue(len(dict) == (self.COUNT - 1),
Fred Drakeb0fefc52001-03-23 04:22:45 +0000961 "deleting object did not cause dictionary update")
962 del objects, o
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000963 self.assertTrue(len(dict) == 0,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000964 "deleting the values did not clear the dictionary")
Fred Drake4fd06e02001-08-03 04:11:27 +0000965 # regression on SF bug #447152:
966 dict = weakref.WeakValueDictionary()
967 self.assertRaises(KeyError, dict.__getitem__, 1)
968 dict[2] = C()
969 self.assertRaises(KeyError, dict.__getitem__, 2)
Fred Drakeb0fefc52001-03-23 04:22:45 +0000970
971 def test_weak_keys(self):
Fred Drake0e540c32001-05-02 05:44:22 +0000972 #
973 # This exercises d.copy(), d.items(), d[] = v, d[], del d[],
Florent Xicluna07627882010-03-21 01:14:24 +0000974 # len(d), in d.
Fred Drake0e540c32001-05-02 05:44:22 +0000975 #
976 dict, objects = self.make_weak_keyed_dict()
Fred Drakeb0fefc52001-03-23 04:22:45 +0000977 for o in objects:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000978 self.assertTrue(weakref.getweakrefcount(o) == 1,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000979 "wrong number of weak references to %r!" % o)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000980 self.assertTrue(o.arg is dict[o],
Fred Drakeb0fefc52001-03-23 04:22:45 +0000981 "wrong object returned by weak dict!")
982 items1 = dict.items()
983 items2 = dict.copy().items()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000984 self.assertTrue(set(items1) == set(items2),
Fred Drakeb0fefc52001-03-23 04:22:45 +0000985 "cloning of weak-keyed dictionary did not work!")
986 del items1, items2
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000987 self.assertTrue(len(dict) == self.COUNT)
Fred Drakeb0fefc52001-03-23 04:22:45 +0000988 del objects[0]
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000989 self.assertTrue(len(dict) == (self.COUNT - 1),
Fred Drakeb0fefc52001-03-23 04:22:45 +0000990 "deleting object did not cause dictionary update")
991 del objects, o
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000992 self.assertTrue(len(dict) == 0,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000993 "deleting the keys did not clear the dictionary")
Fred Drake752eda42001-11-06 16:38:34 +0000994 o = Object(42)
995 dict[o] = "What is the meaning of the universe?"
Florent Xicluna07627882010-03-21 01:14:24 +0000996 self.assertIn(o, dict)
997 self.assertNotIn(34, dict)
Martin v. Löwis5e163332001-02-27 18:36:56 +0000998
Fred Drake0e540c32001-05-02 05:44:22 +0000999 def test_weak_keyed_iters(self):
1000 dict, objects = self.make_weak_keyed_dict()
1001 self.check_iters(dict)
1002
Fred Drake017e68c2006-05-02 06:53:59 +00001003 # Test keyrefs()
1004 refs = dict.keyrefs()
1005 self.assertEqual(len(refs), len(objects))
1006 objects2 = list(objects)
1007 for wr in refs:
1008 ob = wr()
Ezio Melottiaa980582010-01-23 23:04:36 +00001009 self.assertIn(ob, dict)
Fred Drake017e68c2006-05-02 06:53:59 +00001010 self.assertEqual(ob.arg, dict[ob])
1011 objects2.remove(ob)
1012 self.assertEqual(len(objects2), 0)
1013
1014 # Test iterkeyrefs()
1015 objects2 = list(objects)
1016 self.assertEqual(len(list(dict.iterkeyrefs())), len(objects))
1017 for wr in dict.iterkeyrefs():
1018 ob = wr()
Ezio Melottiaa980582010-01-23 23:04:36 +00001019 self.assertIn(ob, dict)
Fred Drake017e68c2006-05-02 06:53:59 +00001020 self.assertEqual(ob.arg, dict[ob])
1021 objects2.remove(ob)
1022 self.assertEqual(len(objects2), 0)
1023
Fred Drake0e540c32001-05-02 05:44:22 +00001024 def test_weak_valued_iters(self):
1025 dict, objects = self.make_weak_valued_dict()
1026 self.check_iters(dict)
1027
Fred Drake017e68c2006-05-02 06:53:59 +00001028 # Test valuerefs()
1029 refs = dict.valuerefs()
1030 self.assertEqual(len(refs), len(objects))
1031 objects2 = list(objects)
1032 for wr in refs:
1033 ob = wr()
1034 self.assertEqual(ob, dict[ob.arg])
1035 self.assertEqual(ob.arg, dict[ob.arg].arg)
1036 objects2.remove(ob)
1037 self.assertEqual(len(objects2), 0)
1038
1039 # Test itervaluerefs()
1040 objects2 = list(objects)
1041 self.assertEqual(len(list(dict.itervaluerefs())), len(objects))
1042 for wr in dict.itervaluerefs():
1043 ob = wr()
1044 self.assertEqual(ob, dict[ob.arg])
1045 self.assertEqual(ob.arg, dict[ob.arg].arg)
1046 objects2.remove(ob)
1047 self.assertEqual(len(objects2), 0)
1048
Fred Drake0e540c32001-05-02 05:44:22 +00001049 def check_iters(self, dict):
1050 # item iterator:
1051 items = dict.items()
1052 for item in dict.iteritems():
1053 items.remove(item)
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001054 self.assertTrue(len(items) == 0, "iteritems() did not touch all items")
Fred Drake0e540c32001-05-02 05:44:22 +00001055
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001056 # key iterator, via __iter__():
Fred Drake0e540c32001-05-02 05:44:22 +00001057 keys = dict.keys()
1058 for k in dict:
1059 keys.remove(k)
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001060 self.assertTrue(len(keys) == 0, "__iter__() did not touch all keys")
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001061
1062 # key iterator, via iterkeys():
1063 keys = dict.keys()
1064 for k in dict.iterkeys():
1065 keys.remove(k)
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001066 self.assertTrue(len(keys) == 0, "iterkeys() did not touch all keys")
Fred Drake0e540c32001-05-02 05:44:22 +00001067
1068 # value iterator:
1069 values = dict.values()
1070 for v in dict.itervalues():
1071 values.remove(v)
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001072 self.assertTrue(len(values) == 0,
Fred Drakef425b1e2003-07-14 21:37:17 +00001073 "itervalues() did not touch all values")
Fred Drake0e540c32001-05-02 05:44:22 +00001074
Guido van Rossum009afb72002-06-10 20:00:52 +00001075 def test_make_weak_keyed_dict_from_dict(self):
1076 o = Object(3)
1077 dict = weakref.WeakKeyDictionary({o:364})
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001078 self.assertTrue(dict[o] == 364)
Guido van Rossum009afb72002-06-10 20:00:52 +00001079
1080 def test_make_weak_keyed_dict_from_weak_keyed_dict(self):
1081 o = Object(3)
1082 dict = weakref.WeakKeyDictionary({o:364})
1083 dict2 = weakref.WeakKeyDictionary(dict)
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001084 self.assertTrue(dict[o] == 364)
Guido van Rossum009afb72002-06-10 20:00:52 +00001085
Fred Drake0e540c32001-05-02 05:44:22 +00001086 def make_weak_keyed_dict(self):
1087 dict = weakref.WeakKeyDictionary()
1088 objects = map(Object, range(self.COUNT))
1089 for o in objects:
1090 dict[o] = o.arg
1091 return dict, objects
1092
1093 def make_weak_valued_dict(self):
1094 dict = weakref.WeakValueDictionary()
1095 objects = map(Object, range(self.COUNT))
1096 for o in objects:
1097 dict[o.arg] = o
1098 return dict, objects
1099
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001100 def check_popitem(self, klass, key1, value1, key2, value2):
1101 weakdict = klass()
1102 weakdict[key1] = value1
1103 weakdict[key2] = value2
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001104 self.assertTrue(len(weakdict) == 2)
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001105 k, v = weakdict.popitem()
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001106 self.assertTrue(len(weakdict) == 1)
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001107 if k is key1:
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001108 self.assertTrue(v is value1)
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001109 else:
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001110 self.assertTrue(v is value2)
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001111 k, v = weakdict.popitem()
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001112 self.assertTrue(len(weakdict) == 0)
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001113 if k is key1:
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001114 self.assertTrue(v is value1)
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001115 else:
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001116 self.assertTrue(v is value2)
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001117
1118 def test_weak_valued_dict_popitem(self):
1119 self.check_popitem(weakref.WeakValueDictionary,
1120 "key1", C(), "key2", C())
1121
1122 def test_weak_keyed_dict_popitem(self):
1123 self.check_popitem(weakref.WeakKeyDictionary,
1124 C(), "value 1", C(), "value 2")
1125
1126 def check_setdefault(self, klass, key, value1, value2):
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001127 self.assertTrue(value1 is not value2,
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001128 "invalid test"
1129 " -- value parameters must be distinct objects")
1130 weakdict = klass()
1131 o = weakdict.setdefault(key, value1)
Florent Xicluna07627882010-03-21 01:14:24 +00001132 self.assertIs(o, value1)
1133 self.assertIn(key, weakdict)
1134 self.assertIs(weakdict.get(key), value1)
1135 self.assertIs(weakdict[key], value1)
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001136
1137 o = weakdict.setdefault(key, value2)
Florent Xicluna07627882010-03-21 01:14:24 +00001138 self.assertIs(o, value1)
1139 self.assertIn(key, weakdict)
1140 self.assertIs(weakdict.get(key), value1)
1141 self.assertIs(weakdict[key], value1)
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001142
1143 def test_weak_valued_dict_setdefault(self):
1144 self.check_setdefault(weakref.WeakValueDictionary,
1145 "key", C(), C())
1146
1147 def test_weak_keyed_dict_setdefault(self):
1148 self.check_setdefault(weakref.WeakKeyDictionary,
1149 C(), "value 1", "value 2")
1150
Fred Drakea0a4ab12001-04-16 17:37:27 +00001151 def check_update(self, klass, dict):
Fred Drake0e540c32001-05-02 05:44:22 +00001152 #
Florent Xicluna07627882010-03-21 01:14:24 +00001153 # This exercises d.update(), len(d), d.keys(), in d,
Fred Drake0e540c32001-05-02 05:44:22 +00001154 # d.get(), d[].
1155 #
Fred Drakea0a4ab12001-04-16 17:37:27 +00001156 weakdict = klass()
1157 weakdict.update(dict)
Florent Xicluna07627882010-03-21 01:14:24 +00001158 self.assertEqual(len(weakdict), len(dict))
Fred Drakea0a4ab12001-04-16 17:37:27 +00001159 for k in weakdict.keys():
Florent Xicluna07627882010-03-21 01:14:24 +00001160 self.assertIn(k, dict,
Fred Drakea0a4ab12001-04-16 17:37:27 +00001161 "mysterious new key appeared in weak dict")
1162 v = dict.get(k)
Florent Xicluna07627882010-03-21 01:14:24 +00001163 self.assertIs(v, weakdict[k])
1164 self.assertIs(v, weakdict.get(k))
Fred Drakea0a4ab12001-04-16 17:37:27 +00001165 for k in dict.keys():
Florent Xicluna07627882010-03-21 01:14:24 +00001166 self.assertIn(k, weakdict,
Fred Drakea0a4ab12001-04-16 17:37:27 +00001167 "original key disappeared in weak dict")
1168 v = dict[k]
Florent Xicluna07627882010-03-21 01:14:24 +00001169 self.assertIs(v, weakdict[k])
1170 self.assertIs(v, weakdict.get(k))
Fred Drakea0a4ab12001-04-16 17:37:27 +00001171
1172 def test_weak_valued_dict_update(self):
1173 self.check_update(weakref.WeakValueDictionary,
1174 {1: C(), 'a': C(), C(): C()})
1175
1176 def test_weak_keyed_dict_update(self):
1177 self.check_update(weakref.WeakKeyDictionary,
1178 {C(): 1, C(): 2, C(): 3})
1179
Fred Drakeccc75622001-09-06 14:52:39 +00001180 def test_weak_keyed_delitem(self):
1181 d = weakref.WeakKeyDictionary()
1182 o1 = Object('1')
1183 o2 = Object('2')
1184 d[o1] = 'something'
1185 d[o2] = 'something'
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001186 self.assertTrue(len(d) == 2)
Fred Drakeccc75622001-09-06 14:52:39 +00001187 del d[o1]
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001188 self.assertTrue(len(d) == 1)
1189 self.assertTrue(d.keys() == [o2])
Fred Drakeccc75622001-09-06 14:52:39 +00001190
1191 def test_weak_valued_delitem(self):
1192 d = weakref.WeakValueDictionary()
1193 o1 = Object('1')
1194 o2 = Object('2')
1195 d['something'] = o1
1196 d['something else'] = o2
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001197 self.assertTrue(len(d) == 2)
Fred Drakeccc75622001-09-06 14:52:39 +00001198 del d['something']
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001199 self.assertTrue(len(d) == 1)
1200 self.assertTrue(d.items() == [('something else', o2)])
Fred Drakeccc75622001-09-06 14:52:39 +00001201
Tim Peters886128f2003-05-25 01:45:11 +00001202 def test_weak_keyed_bad_delitem(self):
1203 d = weakref.WeakKeyDictionary()
1204 o = Object('1')
1205 # An attempt to delete an object that isn't there should raise
Tim Peters50d8b8b2003-05-25 17:44:31 +00001206 # KeyError. It didn't before 2.3.
Tim Peters886128f2003-05-25 01:45:11 +00001207 self.assertRaises(KeyError, d.__delitem__, o)
Tim Peters50d8b8b2003-05-25 17:44:31 +00001208 self.assertRaises(KeyError, d.__getitem__, o)
1209
1210 # If a key isn't of a weakly referencable type, __getitem__ and
1211 # __setitem__ raise TypeError. __delitem__ should too.
1212 self.assertRaises(TypeError, d.__delitem__, 13)
1213 self.assertRaises(TypeError, d.__getitem__, 13)
1214 self.assertRaises(TypeError, d.__setitem__, 13, 13)
Tim Peters886128f2003-05-25 01:45:11 +00001215
1216 def test_weak_keyed_cascading_deletes(self):
1217 # SF bug 742860. For some reason, before 2.3 __delitem__ iterated
1218 # over the keys via self.data.iterkeys(). If things vanished from
1219 # the dict during this (or got added), that caused a RuntimeError.
1220
1221 d = weakref.WeakKeyDictionary()
1222 mutate = False
1223
1224 class C(object):
1225 def __init__(self, i):
1226 self.value = i
1227 def __hash__(self):
1228 return hash(self.value)
1229 def __eq__(self, other):
1230 if mutate:
1231 # Side effect that mutates the dict, by removing the
1232 # last strong reference to a key.
1233 del objs[-1]
1234 return self.value == other.value
1235
1236 objs = [C(i) for i in range(4)]
1237 for o in objs:
1238 d[o] = o.value
1239 del o # now the only strong references to keys are in objs
1240 # Find the order in which iterkeys sees the keys.
1241 objs = d.keys()
1242 # Reverse it, so that the iteration implementation of __delitem__
1243 # has to keep looping to find the first object we delete.
1244 objs.reverse()
Tim Peters50d8b8b2003-05-25 17:44:31 +00001245
Tim Peters886128f2003-05-25 01:45:11 +00001246 # Turn on mutation in C.__eq__. The first time thru the loop,
1247 # under the iterkeys() business the first comparison will delete
1248 # the last item iterkeys() would see, and that causes a
1249 # RuntimeError: dictionary changed size during iteration
1250 # when the iterkeys() loop goes around to try comparing the next
Tim Peters50d8b8b2003-05-25 17:44:31 +00001251 # key. After this was fixed, it just deletes the last object *our*
Tim Peters886128f2003-05-25 01:45:11 +00001252 # "for o in obj" loop would have gotten to.
1253 mutate = True
1254 count = 0
1255 for o in objs:
1256 count += 1
1257 del d[o]
1258 self.assertEqual(len(d), 0)
1259 self.assertEqual(count, 2)
1260
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +00001261from test import mapping_tests
Raymond Hettinger2c2d3222003-03-09 07:05:43 +00001262
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +00001263class WeakValueDictionaryTestCase(mapping_tests.BasicTestMappingProtocol):
Fred Drakef425b1e2003-07-14 21:37:17 +00001264 """Check that WeakValueDictionary conforms to the mapping protocol"""
Raymond Hettinger2c2d3222003-03-09 07:05:43 +00001265 __ref = {"key1":Object(1), "key2":Object(2), "key3":Object(3)}
Walter Dörwald118f9312004-06-02 18:42:25 +00001266 type2test = weakref.WeakValueDictionary
Raymond Hettinger2c2d3222003-03-09 07:05:43 +00001267 def _reference(self):
1268 return self.__ref.copy()
1269
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +00001270class WeakKeyDictionaryTestCase(mapping_tests.BasicTestMappingProtocol):
Fred Drakef425b1e2003-07-14 21:37:17 +00001271 """Check that WeakKeyDictionary conforms to the mapping protocol"""
Raymond Hettinger2c2d3222003-03-09 07:05:43 +00001272 __ref = {Object("key1"):1, Object("key2"):2, Object("key3"):3}
Walter Dörwald118f9312004-06-02 18:42:25 +00001273 type2test = weakref.WeakKeyDictionary
Raymond Hettinger2c2d3222003-03-09 07:05:43 +00001274 def _reference(self):
1275 return self.__ref.copy()
Martin v. Löwis5e163332001-02-27 18:36:56 +00001276
Georg Brandl88659b02008-05-20 08:40:43 +00001277libreftest = """ Doctest for examples in the library reference: weakref.rst
Georg Brandl9a65d582005-07-02 19:07:30 +00001278
1279>>> import weakref
1280>>> class Dict(dict):
1281... pass
1282...
1283>>> obj = Dict(red=1, green=2, blue=3) # this object is weak referencable
1284>>> r = weakref.ref(obj)
Armin Rigoa3f09272006-05-28 19:13:17 +00001285>>> print r() is obj
1286True
Georg Brandl9a65d582005-07-02 19:07:30 +00001287
1288>>> import weakref
1289>>> class Object:
1290... pass
1291...
1292>>> o = Object()
1293>>> r = weakref.ref(o)
1294>>> o2 = r()
1295>>> o is o2
1296True
1297>>> del o, o2
1298>>> print r()
1299None
1300
1301>>> import weakref
1302>>> class ExtendedRef(weakref.ref):
1303... def __init__(self, ob, callback=None, **annotations):
1304... super(ExtendedRef, self).__init__(ob, callback)
1305... self.__counter = 0
1306... for k, v in annotations.iteritems():
1307... setattr(self, k, v)
1308... def __call__(self):
1309... '''Return a pair containing the referent and the number of
1310... times the reference has been called.
1311... '''
1312... ob = super(ExtendedRef, self).__call__()
1313... if ob is not None:
1314... self.__counter += 1
1315... ob = (ob, self.__counter)
1316... return ob
1317...
1318>>> class A: # not in docs from here, just testing the ExtendedRef
1319... pass
1320...
1321>>> a = A()
1322>>> r = ExtendedRef(a, foo=1, bar="baz")
1323>>> r.foo
13241
1325>>> r.bar
1326'baz'
1327>>> r()[1]
13281
1329>>> r()[1]
13302
1331>>> r()[0] is a
1332True
1333
1334
1335>>> import weakref
1336>>> _id2obj_dict = weakref.WeakValueDictionary()
1337>>> def remember(obj):
1338... oid = id(obj)
1339... _id2obj_dict[oid] = obj
1340... return oid
1341...
1342>>> def id2obj(oid):
1343... return _id2obj_dict[oid]
1344...
1345>>> a = A() # from here, just testing
1346>>> a_id = remember(a)
1347>>> id2obj(a_id) is a
1348True
1349>>> del a
1350>>> try:
1351... id2obj(a_id)
1352... except KeyError:
1353... print 'OK'
1354... else:
1355... print 'WeakValueDictionary error'
1356OK
1357
1358"""
1359
1360__test__ = {'libreftest' : libreftest}
1361
Fred Drake2e2be372001-09-20 21:33:42 +00001362def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +00001363 test_support.run_unittest(
1364 ReferencesTestCase,
1365 MappingTestCase,
1366 WeakValueDictionaryTestCase,
Fred Drakef425b1e2003-07-14 21:37:17 +00001367 WeakKeyDictionaryTestCase,
Amaury Forgeot d'Arca8919fe2008-06-16 19:12:42 +00001368 SubclassableWeakrefTestCase,
Fred Drakef425b1e2003-07-14 21:37:17 +00001369 )
Georg Brandl9a65d582005-07-02 19:07:30 +00001370 test_support.run_doctest(sys.modules[__name__])
Fred Drake2e2be372001-09-20 21:33:42 +00001371
1372
1373if __name__ == "__main__":
1374 test_main()