blob: f732d7ba81a86faeb9149f0f9d9a02d2ddd107ca [file] [log] [blame]
Fred Drake41deb1e2001-02-01 05:27:45 +00001import sys
Fred Drakeb0fefc52001-03-23 04:22:45 +00002import unittest
Fred Drake5935ff02001-12-19 16:54:23 +00003import UserList
Fred Drake41deb1e2001-02-01 05:27:45 +00004import weakref
5
Barry Warsaw04f357c2002-07-23 19:04:11 +00006from test import test_support
Fred Drake41deb1e2001-02-01 05:27:45 +00007
8
9class C:
Fred Drakeb0fefc52001-03-23 04:22:45 +000010 def method(self):
11 pass
Fred Drake41deb1e2001-02-01 05:27:45 +000012
13
Fred Drakeb0fefc52001-03-23 04:22:45 +000014class Callable:
15 bar = None
Fred Drake41deb1e2001-02-01 05:27:45 +000016
Fred Drakeb0fefc52001-03-23 04:22:45 +000017 def __call__(self, x):
18 self.bar = x
Fred Drake41deb1e2001-02-01 05:27:45 +000019
20
Fred Drakeb0fefc52001-03-23 04:22:45 +000021def create_function():
22 def f(): pass
23 return f
24
25def create_bound_method():
26 return C().method
27
28def create_unbound_method():
29 return C.method
Fred Drake41deb1e2001-02-01 05:27:45 +000030
31
Fred Drakeb0fefc52001-03-23 04:22:45 +000032class TestBase(unittest.TestCase):
33
34 def setUp(self):
35 self.cbcalled = 0
36
37 def callback(self, ref):
38 self.cbcalled += 1
Fred Drake41deb1e2001-02-01 05:27:45 +000039
40
Fred Drakeb0fefc52001-03-23 04:22:45 +000041class ReferencesTestCase(TestBase):
Fred Drake41deb1e2001-02-01 05:27:45 +000042
Fred Drakeb0fefc52001-03-23 04:22:45 +000043 def test_basic_ref(self):
44 self.check_basic_ref(C)
45 self.check_basic_ref(create_function)
46 self.check_basic_ref(create_bound_method)
47 self.check_basic_ref(create_unbound_method)
Fred Drake41deb1e2001-02-01 05:27:45 +000048
Fred Drake43735da2002-04-11 03:59:42 +000049 # Just make sure the tp_repr handler doesn't raise an exception.
50 # Live reference:
51 o = C()
52 wr = weakref.ref(o)
53 `wr`
54 # Dead reference:
55 del o
56 `wr`
57
Fred Drakeb0fefc52001-03-23 04:22:45 +000058 def test_basic_callback(self):
59 self.check_basic_callback(C)
60 self.check_basic_callback(create_function)
61 self.check_basic_callback(create_bound_method)
62 self.check_basic_callback(create_unbound_method)
Fred Drake41deb1e2001-02-01 05:27:45 +000063
Fred Drakeb0fefc52001-03-23 04:22:45 +000064 def test_multiple_callbacks(self):
65 o = C()
66 ref1 = weakref.ref(o, self.callback)
67 ref2 = weakref.ref(o, self.callback)
68 del o
69 self.assert_(ref1() is None,
70 "expected reference to be invalidated")
71 self.assert_(ref2() is None,
72 "expected reference to be invalidated")
73 self.assert_(self.cbcalled == 2,
74 "callback not called the right number of times")
Fred Drake41deb1e2001-02-01 05:27:45 +000075
Fred Drake705088e2001-04-13 17:18:15 +000076 def test_multiple_selfref_callbacks(self):
Guido van Rossum9eee5542002-08-22 20:21:30 +000077 # Make sure all references are invalidated before callbacks are called
Fred Drake705088e2001-04-13 17:18:15 +000078 #
79 # What's important here is that we're using the first
80 # reference in the callback invoked on the second reference
81 # (the most recently created ref is cleaned up first). This
82 # tests that all references to the object are invalidated
83 # before any of the callbacks are invoked, so that we only
84 # have one invocation of _weakref.c:cleanup_helper() active
85 # for a particular object at a time.
86 #
87 def callback(object, self=self):
88 self.ref()
89 c = C()
90 self.ref = weakref.ref(c, callback)
91 ref1 = weakref.ref(c, callback)
92 del c
93
Fred Drakeb0fefc52001-03-23 04:22:45 +000094 def test_proxy_ref(self):
95 o = C()
96 o.bar = 1
97 ref1 = weakref.proxy(o, self.callback)
98 ref2 = weakref.proxy(o, self.callback)
99 del o
Fred Drake41deb1e2001-02-01 05:27:45 +0000100
Fred Drakeb0fefc52001-03-23 04:22:45 +0000101 def check(proxy):
102 proxy.bar
Fred Drake41deb1e2001-02-01 05:27:45 +0000103
Fred Drakeb0fefc52001-03-23 04:22:45 +0000104 self.assertRaises(weakref.ReferenceError, check, ref1)
105 self.assertRaises(weakref.ReferenceError, check, ref2)
106 self.assert_(self.cbcalled == 2)
Fred Drake41deb1e2001-02-01 05:27:45 +0000107
Fred Drakeb0fefc52001-03-23 04:22:45 +0000108 def check_basic_ref(self, factory):
109 o = factory()
110 ref = weakref.ref(o)
111 self.assert_(ref() is not None,
112 "weak reference to live object should be live")
113 o2 = ref()
114 self.assert_(o is o2,
115 "<ref>() should return original object if live")
Fred Drake41deb1e2001-02-01 05:27:45 +0000116
Fred Drakeb0fefc52001-03-23 04:22:45 +0000117 def check_basic_callback(self, factory):
118 self.cbcalled = 0
119 o = factory()
120 ref = weakref.ref(o, self.callback)
121 del o
Fred Drake705088e2001-04-13 17:18:15 +0000122 self.assert_(self.cbcalled == 1,
123 "callback did not properly set 'cbcalled'")
124 self.assert_(ref() is None,
125 "ref2 should be dead after deleting object reference")
Fred Drake41deb1e2001-02-01 05:27:45 +0000126
Fred Drakeb0fefc52001-03-23 04:22:45 +0000127 def test_ref_reuse(self):
128 o = C()
129 ref1 = weakref.ref(o)
130 # create a proxy to make sure that there's an intervening creation
131 # between these two; it should make no difference
132 proxy = weakref.proxy(o)
133 ref2 = weakref.ref(o)
134 self.assert_(ref1 is ref2,
135 "reference object w/out callback should be re-used")
Fred Drake41deb1e2001-02-01 05:27:45 +0000136
Fred Drakeb0fefc52001-03-23 04:22:45 +0000137 o = C()
138 proxy = weakref.proxy(o)
139 ref1 = weakref.ref(o)
140 ref2 = weakref.ref(o)
141 self.assert_(ref1 is ref2,
142 "reference object w/out callback should be re-used")
143 self.assert_(weakref.getweakrefcount(o) == 2,
144 "wrong weak ref count for object")
145 del proxy
146 self.assert_(weakref.getweakrefcount(o) == 1,
147 "wrong weak ref count for object after deleting proxy")
Fred Drake41deb1e2001-02-01 05:27:45 +0000148
Fred Drakeb0fefc52001-03-23 04:22:45 +0000149 def test_proxy_reuse(self):
150 o = C()
151 proxy1 = weakref.proxy(o)
152 ref = weakref.ref(o)
153 proxy2 = weakref.proxy(o)
154 self.assert_(proxy1 is proxy2,
155 "proxy object w/out callback should have been re-used")
156
157 def test_basic_proxy(self):
158 o = C()
159 self.check_proxy(o, weakref.proxy(o))
160
Fred Drake5935ff02001-12-19 16:54:23 +0000161 L = UserList.UserList()
162 p = weakref.proxy(L)
163 self.failIf(p, "proxy for empty UserList should be false")
164 p.append(12)
165 self.assertEqual(len(L), 1)
166 self.failUnless(p, "proxy for non-empty UserList should be true")
167 p[:] = [2, 3]
168 self.assertEqual(len(L), 2)
169 self.assertEqual(len(p), 2)
Fred Drakef425b1e2003-07-14 21:37:17 +0000170 self.failUnless(3 in p,
171 "proxy didn't support __contains__() properly")
Fred Drake5935ff02001-12-19 16:54:23 +0000172 p[1] = 5
173 self.assertEqual(L[1], 5)
174 self.assertEqual(p[1], 5)
175 L2 = UserList.UserList(L)
176 p2 = weakref.proxy(L2)
177 self.assertEqual(p, p2)
Fred Drake43735da2002-04-11 03:59:42 +0000178 ## self.assertEqual(`L2`, `p2`)
179 L3 = UserList.UserList(range(10))
180 p3 = weakref.proxy(L3)
181 self.assertEqual(L3[:], p3[:])
182 self.assertEqual(L3[5:], p3[5:])
183 self.assertEqual(L3[:5], p3[:5])
184 self.assertEqual(L3[2:5], p3[2:5])
Fred Drake5935ff02001-12-19 16:54:23 +0000185
Fred Drakeea2adc92004-02-03 19:56:46 +0000186 # The PyWeakref_* C API is documented as allowing either NULL or
187 # None as the value for the callback, where either means "no
188 # callback". The "no callback" ref and proxy objects are supposed
189 # to be shared so long as they exist by all callers so long as
190 # they are active. In Python 2.3.3 and earlier, this guaranttee
191 # was not honored, and was broken in different ways for
192 # PyWeakref_NewRef() and PyWeakref_NewProxy(). (Two tests.)
193
194 def test_shared_ref_without_callback(self):
195 self.check_shared_without_callback(weakref.ref)
196
197 def test_shared_proxy_without_callback(self):
198 self.check_shared_without_callback(weakref.proxy)
199
200 def check_shared_without_callback(self, makeref):
201 o = Object(1)
202 p1 = makeref(o, None)
203 p2 = makeref(o, None)
204 self.assert_(p1 is p2, "both callbacks were None in the C API")
205 del p1, p2
206 p1 = makeref(o)
207 p2 = makeref(o, None)
208 self.assert_(p1 is p2, "callbacks were NULL, None in the C API")
209 del p1, p2
210 p1 = makeref(o)
211 p2 = makeref(o)
212 self.assert_(p1 is p2, "both callbacks were NULL in the C API")
213 del p1, p2
214 p1 = makeref(o, None)
215 p2 = makeref(o)
216 self.assert_(p1 is p2, "callbacks were None, NULL in the C API")
217
Fred Drakeb0fefc52001-03-23 04:22:45 +0000218 def test_callable_proxy(self):
219 o = Callable()
220 ref1 = weakref.proxy(o)
221
222 self.check_proxy(o, ref1)
223
224 self.assert_(type(ref1) is weakref.CallableProxyType,
225 "proxy is not of callable type")
226 ref1('twinkies!')
227 self.assert_(o.bar == 'twinkies!',
228 "call through proxy not passed through to original")
Fred Drake3bb4d212001-10-18 19:28:29 +0000229 ref1(x='Splat.')
230 self.assert_(o.bar == 'Splat.',
231 "call through proxy not passed through to original")
Fred Drakeb0fefc52001-03-23 04:22:45 +0000232
233 # expect due to too few args
234 self.assertRaises(TypeError, ref1)
235
236 # expect due to too many args
237 self.assertRaises(TypeError, ref1, 1, 2, 3)
238
239 def check_proxy(self, o, proxy):
240 o.foo = 1
241 self.assert_(proxy.foo == 1,
242 "proxy does not reflect attribute addition")
243 o.foo = 2
244 self.assert_(proxy.foo == 2,
245 "proxy does not reflect attribute modification")
246 del o.foo
247 self.assert_(not hasattr(proxy, 'foo'),
248 "proxy does not reflect attribute removal")
249
250 proxy.foo = 1
251 self.assert_(o.foo == 1,
252 "object does not reflect attribute addition via proxy")
253 proxy.foo = 2
254 self.assert_(
255 o.foo == 2,
256 "object does not reflect attribute modification via proxy")
257 del proxy.foo
258 self.assert_(not hasattr(o, 'foo'),
259 "object does not reflect attribute removal via proxy")
260
Raymond Hettingerd693a812003-06-30 04:18:48 +0000261 def test_proxy_deletion(self):
262 # Test clearing of SF bug #762891
263 class Foo:
264 result = None
265 def __delitem__(self, accessor):
266 self.result = accessor
267 g = Foo()
268 f = weakref.proxy(g)
269 del f[0]
270 self.assertEqual(f.result, 0)
271
Fred Drakeb0fefc52001-03-23 04:22:45 +0000272 def test_getweakrefcount(self):
273 o = C()
274 ref1 = weakref.ref(o)
275 ref2 = weakref.ref(o, self.callback)
276 self.assert_(weakref.getweakrefcount(o) == 2,
277 "got wrong number of weak reference objects")
278
279 proxy1 = weakref.proxy(o)
280 proxy2 = weakref.proxy(o, self.callback)
281 self.assert_(weakref.getweakrefcount(o) == 4,
282 "got wrong number of weak reference objects")
283
Fred Drakeea2adc92004-02-03 19:56:46 +0000284 del ref1, ref2, proxy1, proxy2
285 self.assert_(weakref.getweakrefcount(o) == 0,
286 "weak reference objects not unlinked from"
287 " referent when discarded.")
288
Walter Dörwaldb167b042003-12-11 12:34:05 +0000289 # assumes ints do not support weakrefs
290 self.assert_(weakref.getweakrefcount(1) == 0,
291 "got wrong number of weak reference objects for int")
292
Fred Drakeb0fefc52001-03-23 04:22:45 +0000293 def test_getweakrefs(self):
294 o = C()
295 ref1 = weakref.ref(o, self.callback)
296 ref2 = weakref.ref(o, self.callback)
297 del ref1
298 self.assert_(weakref.getweakrefs(o) == [ref2],
299 "list of refs does not match")
300
301 o = C()
302 ref1 = weakref.ref(o, self.callback)
303 ref2 = weakref.ref(o, self.callback)
304 del ref2
305 self.assert_(weakref.getweakrefs(o) == [ref1],
306 "list of refs does not match")
307
Fred Drakeea2adc92004-02-03 19:56:46 +0000308 del ref1
309 self.assert_(weakref.getweakrefs(o) == [],
310 "list of refs not cleared")
311
Walter Dörwaldb167b042003-12-11 12:34:05 +0000312 # assumes ints do not support weakrefs
313 self.assert_(weakref.getweakrefs(1) == [],
314 "list of refs does not match for int")
315
Fred Drake39c27f12001-10-18 18:06:05 +0000316 def test_newstyle_number_ops(self):
317 class F(float):
318 pass
319 f = F(2.0)
320 p = weakref.proxy(f)
321 self.assert_(p + 1.0 == 3.0)
322 self.assert_(1.0 + p == 3.0) # this used to SEGV
323
Fred Drake2a64f462001-12-10 23:46:02 +0000324 def test_callbacks_protected(self):
Guido van Rossum9eee5542002-08-22 20:21:30 +0000325 # Callbacks protected from already-set exceptions?
Fred Drake2a64f462001-12-10 23:46:02 +0000326 # Regression test for SF bug #478534.
327 class BogusError(Exception):
328 pass
329 data = {}
330 def remove(k):
331 del data[k]
332 def encapsulate():
333 f = lambda : ()
334 data[weakref.ref(f, remove)] = None
335 raise BogusError
336 try:
337 encapsulate()
338 except BogusError:
339 pass
340 else:
341 self.fail("exception not properly restored")
342 try:
343 encapsulate()
344 except BogusError:
345 pass
346 else:
347 self.fail("exception not properly restored")
348
Tim Petersadd09b42003-11-12 20:43:28 +0000349 def test_sf_bug_840829(self):
350 # "weakref callbacks and gc corrupt memory"
351 # subtype_dealloc erroneously exposed a new-style instance
352 # already in the process of getting deallocated to gc,
353 # causing double-deallocation if the instance had a weakref
354 # callback that triggered gc.
355 # If the bug exists, there probably won't be an obvious symptom
356 # in a release build. In a debug build, a segfault will occur
357 # when the second attempt to remove the instance from the "list
358 # of all objects" occurs.
359
360 import gc
361
362 class C(object):
363 pass
364
365 c = C()
366 wr = weakref.ref(c, lambda ignore: gc.collect())
367 del c
368
Tim Petersf7f9e992003-11-13 21:59:32 +0000369 # There endeth the first part. It gets worse.
370 del wr
371
372 c1 = C()
373 c1.i = C()
374 wr = weakref.ref(c1.i, lambda ignore: gc.collect())
375
376 c2 = C()
377 c2.c1 = c1
378 del c1 # still alive because c2 points to it
379
380 # Now when subtype_dealloc gets called on c2, it's not enough just
381 # that c2 is immune from gc while the weakref callbacks associated
382 # with c2 execute (there are none in this 2nd half of the test, btw).
383 # subtype_dealloc goes on to call the base classes' deallocs too,
384 # so any gc triggered by weakref callbacks associated with anything
385 # torn down by a base class dealloc can also trigger double
386 # deallocation of c2.
387 del c2
Fred Drake41deb1e2001-02-01 05:27:45 +0000388
Tim Peters403a2032003-11-20 21:21:46 +0000389 def test_callback_in_cycle_1(self):
390 import gc
391
392 class J(object):
393 pass
394
395 class II(object):
396 def acallback(self, ignore):
397 self.J
398
399 I = II()
400 I.J = J
401 I.wr = weakref.ref(J, I.acallback)
402
403 # Now J and II are each in a self-cycle (as all new-style class
404 # objects are, since their __mro__ points back to them). I holds
405 # both a weak reference (I.wr) and a strong reference (I.J) to class
406 # J. I is also in a cycle (I.wr points to a weakref that references
407 # I.acallback). When we del these three, they all become trash, but
408 # the cycles prevent any of them from getting cleaned up immediately.
409 # Instead they have to wait for cyclic gc to deduce that they're
410 # trash.
411 #
412 # gc used to call tp_clear on all of them, and the order in which
413 # it does that is pretty accidental. The exact order in which we
414 # built up these things manages to provoke gc into running tp_clear
415 # in just the right order (I last). Calling tp_clear on II leaves
416 # behind an insane class object (its __mro__ becomes NULL). Calling
417 # tp_clear on J breaks its self-cycle, but J doesn't get deleted
418 # just then because of the strong reference from I.J. Calling
419 # tp_clear on I starts to clear I's __dict__, and just happens to
420 # clear I.J first -- I.wr is still intact. That removes the last
421 # reference to J, which triggers the weakref callback. The callback
422 # tries to do "self.J", and instances of new-style classes look up
423 # attributes ("J") in the class dict first. The class (II) wants to
424 # search II.__mro__, but that's NULL. The result was a segfault in
425 # a release build, and an assert failure in a debug build.
426 del I, J, II
427 gc.collect()
428
429 def test_callback_in_cycle_2(self):
430 import gc
431
432 # This is just like test_callback_in_cycle_1, except that II is an
433 # old-style class. The symptom is different then: an instance of an
434 # old-style class looks in its own __dict__ first. 'J' happens to
435 # get cleared from I.__dict__ before 'wr', and 'J' was never in II's
436 # __dict__, so the attribute isn't found. The difference is that
437 # the old-style II doesn't have a NULL __mro__ (it doesn't have any
438 # __mro__), so no segfault occurs. Instead it got:
439 # test_callback_in_cycle_2 (__main__.ReferencesTestCase) ...
440 # Exception exceptions.AttributeError:
441 # "II instance has no attribute 'J'" in <bound method II.acallback
442 # of <?.II instance at 0x00B9B4B8>> ignored
443
444 class J(object):
445 pass
446
447 class II:
448 def acallback(self, ignore):
449 self.J
450
451 I = II()
452 I.J = J
453 I.wr = weakref.ref(J, I.acallback)
454
455 del I, J, II
456 gc.collect()
457
458 def test_callback_in_cycle_3(self):
459 import gc
460
461 # This one broke the first patch that fixed the last two. In this
462 # case, the objects reachable from the callback aren't also reachable
463 # from the object (c1) *triggering* the callback: you can get to
464 # c1 from c2, but not vice-versa. The result was that c2's __dict__
465 # got tp_clear'ed by the time the c2.cb callback got invoked.
466
467 class C:
468 def cb(self, ignore):
469 self.me
470 self.c1
471 self.wr
472
473 c1, c2 = C(), C()
474
475 c2.me = c2
476 c2.c1 = c1
477 c2.wr = weakref.ref(c1, c2.cb)
478
479 del c1, c2
480 gc.collect()
481
482 def test_callback_in_cycle_4(self):
483 import gc
484
485 # Like test_callback_in_cycle_3, except c2 and c1 have different
486 # classes. c2's class (C) isn't reachable from c1 then, so protecting
487 # objects reachable from the dying object (c1) isn't enough to stop
488 # c2's class (C) from getting tp_clear'ed before c2.cb is invoked.
489 # The result was a segfault (C.__mro__ was NULL when the callback
490 # tried to look up self.me).
491
492 class C(object):
493 def cb(self, ignore):
494 self.me
495 self.c1
496 self.wr
497
498 class D:
499 pass
500
501 c1, c2 = D(), C()
502
503 c2.me = c2
504 c2.c1 = c1
505 c2.wr = weakref.ref(c1, c2.cb)
506
507 del c1, c2, C, D
508 gc.collect()
509
510 def test_callback_in_cycle_resurrection(self):
511 import gc
512
513 # Do something nasty in a weakref callback: resurrect objects
514 # from dead cycles. For this to be attempted, the weakref and
515 # its callback must also be part of the cyclic trash (else the
516 # objects reachable via the callback couldn't be in cyclic trash
517 # to begin with -- the callback would act like an external root).
518 # But gc clears trash weakrefs with callbacks early now, which
519 # disables the callbacks, so the callbacks shouldn't get called
520 # at all (and so nothing actually gets resurrected).
521
522 alist = []
523 class C(object):
524 def __init__(self, value):
525 self.attribute = value
526
527 def acallback(self, ignore):
528 alist.append(self.c)
529
530 c1, c2 = C(1), C(2)
531 c1.c = c2
532 c2.c = c1
533 c1.wr = weakref.ref(c2, c1.acallback)
534 c2.wr = weakref.ref(c1, c2.acallback)
535
536 def C_went_away(ignore):
537 alist.append("C went away")
538 wr = weakref.ref(C, C_went_away)
539
540 del c1, c2, C # make them all trash
541 self.assertEqual(alist, []) # del isn't enough to reclaim anything
542
543 gc.collect()
544 # c1.wr and c2.wr were part of the cyclic trash, so should have
545 # been cleared without their callbacks executing. OTOH, the weakref
546 # to C is bound to a function local (wr), and wasn't trash, so that
547 # callback should have been invoked when C went away.
548 self.assertEqual(alist, ["C went away"])
549 # The remaining weakref should be dead now (its callback ran).
550 self.assertEqual(wr(), None)
551
552 del alist[:]
553 gc.collect()
554 self.assertEqual(alist, [])
555
556 def test_callbacks_on_callback(self):
557 import gc
558
559 # Set up weakref callbacks *on* weakref callbacks.
560 alist = []
561 def safe_callback(ignore):
562 alist.append("safe_callback called")
563
564 class C(object):
565 def cb(self, ignore):
566 alist.append("cb called")
567
568 c, d = C(), C()
569 c.other = d
570 d.other = c
571 callback = c.cb
572 c.wr = weakref.ref(d, callback) # this won't trigger
573 d.wr = weakref.ref(callback, d.cb) # ditto
574 external_wr = weakref.ref(callback, safe_callback) # but this will
575 self.assert_(external_wr() is callback)
576
577 # The weakrefs attached to c and d should get cleared, so that
578 # C.cb is never called. But external_wr isn't part of the cyclic
579 # trash, and no cyclic trash is reachable from it, so safe_callback
580 # should get invoked when the bound method object callback (c.cb)
581 # -- which is itself a callback, and also part of the cyclic trash --
582 # gets reclaimed at the end of gc.
583
584 del callback, c, d, C
585 self.assertEqual(alist, []) # del isn't enough to clean up cycles
586 gc.collect()
587 self.assertEqual(alist, ["safe_callback called"])
588 self.assertEqual(external_wr(), None)
589
590 del alist[:]
591 gc.collect()
592 self.assertEqual(alist, [])
593
Fred Drake41deb1e2001-02-01 05:27:45 +0000594class Object:
595 def __init__(self, arg):
596 self.arg = arg
597 def __repr__(self):
598 return "<Object %r>" % self.arg
599
Fred Drake41deb1e2001-02-01 05:27:45 +0000600
Fred Drakeb0fefc52001-03-23 04:22:45 +0000601class MappingTestCase(TestBase):
Martin v. Löwis5e163332001-02-27 18:36:56 +0000602
Fred Drakeb0fefc52001-03-23 04:22:45 +0000603 COUNT = 10
604
605 def test_weak_values(self):
Fred Drake0e540c32001-05-02 05:44:22 +0000606 #
607 # This exercises d.copy(), d.items(), d[], del d[], len(d).
608 #
609 dict, objects = self.make_weak_valued_dict()
Fred Drakeb0fefc52001-03-23 04:22:45 +0000610 for o in objects:
611 self.assert_(weakref.getweakrefcount(o) == 1,
612 "wrong number of weak references to %r!" % o)
613 self.assert_(o is dict[o.arg],
614 "wrong object returned by weak dict!")
615 items1 = dict.items()
616 items2 = dict.copy().items()
617 items1.sort()
618 items2.sort()
619 self.assert_(items1 == items2,
620 "cloning of weak-valued dictionary did not work!")
621 del items1, items2
622 self.assert_(len(dict) == self.COUNT)
623 del objects[0]
624 self.assert_(len(dict) == (self.COUNT - 1),
625 "deleting object did not cause dictionary update")
626 del objects, o
627 self.assert_(len(dict) == 0,
628 "deleting the values did not clear the dictionary")
Fred Drake4fd06e02001-08-03 04:11:27 +0000629 # regression on SF bug #447152:
630 dict = weakref.WeakValueDictionary()
631 self.assertRaises(KeyError, dict.__getitem__, 1)
632 dict[2] = C()
633 self.assertRaises(KeyError, dict.__getitem__, 2)
Fred Drakeb0fefc52001-03-23 04:22:45 +0000634
635 def test_weak_keys(self):
Fred Drake0e540c32001-05-02 05:44:22 +0000636 #
637 # This exercises d.copy(), d.items(), d[] = v, d[], del d[],
Fred Drake752eda42001-11-06 16:38:34 +0000638 # len(d), d.has_key().
Fred Drake0e540c32001-05-02 05:44:22 +0000639 #
640 dict, objects = self.make_weak_keyed_dict()
Fred Drakeb0fefc52001-03-23 04:22:45 +0000641 for o in objects:
642 self.assert_(weakref.getweakrefcount(o) == 1,
643 "wrong number of weak references to %r!" % o)
644 self.assert_(o.arg is dict[o],
645 "wrong object returned by weak dict!")
646 items1 = dict.items()
647 items2 = dict.copy().items()
Raymond Hettingera690a992003-11-16 16:17:49 +0000648 self.assert_(set(items1) == set(items2),
Fred Drakeb0fefc52001-03-23 04:22:45 +0000649 "cloning of weak-keyed dictionary did not work!")
650 del items1, items2
651 self.assert_(len(dict) == self.COUNT)
652 del objects[0]
653 self.assert_(len(dict) == (self.COUNT - 1),
654 "deleting object did not cause dictionary update")
655 del objects, o
656 self.assert_(len(dict) == 0,
657 "deleting the keys did not clear the dictionary")
Fred Drake752eda42001-11-06 16:38:34 +0000658 o = Object(42)
659 dict[o] = "What is the meaning of the universe?"
660 self.assert_(dict.has_key(o))
661 self.assert_(not dict.has_key(34))
Martin v. Löwis5e163332001-02-27 18:36:56 +0000662
Fred Drake0e540c32001-05-02 05:44:22 +0000663 def test_weak_keyed_iters(self):
664 dict, objects = self.make_weak_keyed_dict()
665 self.check_iters(dict)
666
667 def test_weak_valued_iters(self):
668 dict, objects = self.make_weak_valued_dict()
669 self.check_iters(dict)
670
671 def check_iters(self, dict):
672 # item iterator:
673 items = dict.items()
674 for item in dict.iteritems():
675 items.remove(item)
Fred Drakeaaa48ff2001-05-10 17:16:38 +0000676 self.assert_(len(items) == 0, "iteritems() did not touch all items")
Fred Drake0e540c32001-05-02 05:44:22 +0000677
Fred Drakeaaa48ff2001-05-10 17:16:38 +0000678 # key iterator, via __iter__():
Fred Drake0e540c32001-05-02 05:44:22 +0000679 keys = dict.keys()
680 for k in dict:
681 keys.remove(k)
Fred Drakeaaa48ff2001-05-10 17:16:38 +0000682 self.assert_(len(keys) == 0, "__iter__() did not touch all keys")
683
684 # key iterator, via iterkeys():
685 keys = dict.keys()
686 for k in dict.iterkeys():
687 keys.remove(k)
688 self.assert_(len(keys) == 0, "iterkeys() did not touch all keys")
Fred Drake0e540c32001-05-02 05:44:22 +0000689
690 # value iterator:
691 values = dict.values()
692 for v in dict.itervalues():
693 values.remove(v)
Fred Drakef425b1e2003-07-14 21:37:17 +0000694 self.assert_(len(values) == 0,
695 "itervalues() did not touch all values")
Fred Drake0e540c32001-05-02 05:44:22 +0000696
Guido van Rossum009afb72002-06-10 20:00:52 +0000697 def test_make_weak_keyed_dict_from_dict(self):
698 o = Object(3)
699 dict = weakref.WeakKeyDictionary({o:364})
700 self.assert_(dict[o] == 364)
701
702 def test_make_weak_keyed_dict_from_weak_keyed_dict(self):
703 o = Object(3)
704 dict = weakref.WeakKeyDictionary({o:364})
705 dict2 = weakref.WeakKeyDictionary(dict)
706 self.assert_(dict[o] == 364)
707
Fred Drake0e540c32001-05-02 05:44:22 +0000708 def make_weak_keyed_dict(self):
709 dict = weakref.WeakKeyDictionary()
710 objects = map(Object, range(self.COUNT))
711 for o in objects:
712 dict[o] = o.arg
713 return dict, objects
714
715 def make_weak_valued_dict(self):
716 dict = weakref.WeakValueDictionary()
717 objects = map(Object, range(self.COUNT))
718 for o in objects:
719 dict[o.arg] = o
720 return dict, objects
721
Fred Drakeaaa48ff2001-05-10 17:16:38 +0000722 def check_popitem(self, klass, key1, value1, key2, value2):
723 weakdict = klass()
724 weakdict[key1] = value1
725 weakdict[key2] = value2
726 self.assert_(len(weakdict) == 2)
727 k, v = weakdict.popitem()
728 self.assert_(len(weakdict) == 1)
729 if k is key1:
730 self.assert_(v is value1)
731 else:
732 self.assert_(v is value2)
733 k, v = weakdict.popitem()
734 self.assert_(len(weakdict) == 0)
735 if k is key1:
736 self.assert_(v is value1)
737 else:
738 self.assert_(v is value2)
739
740 def test_weak_valued_dict_popitem(self):
741 self.check_popitem(weakref.WeakValueDictionary,
742 "key1", C(), "key2", C())
743
744 def test_weak_keyed_dict_popitem(self):
745 self.check_popitem(weakref.WeakKeyDictionary,
746 C(), "value 1", C(), "value 2")
747
748 def check_setdefault(self, klass, key, value1, value2):
749 self.assert_(value1 is not value2,
750 "invalid test"
751 " -- value parameters must be distinct objects")
752 weakdict = klass()
753 o = weakdict.setdefault(key, value1)
754 self.assert_(o is value1)
755 self.assert_(weakdict.has_key(key))
756 self.assert_(weakdict.get(key) is value1)
757 self.assert_(weakdict[key] is value1)
758
759 o = weakdict.setdefault(key, value2)
760 self.assert_(o is value1)
761 self.assert_(weakdict.has_key(key))
762 self.assert_(weakdict.get(key) is value1)
763 self.assert_(weakdict[key] is value1)
764
765 def test_weak_valued_dict_setdefault(self):
766 self.check_setdefault(weakref.WeakValueDictionary,
767 "key", C(), C())
768
769 def test_weak_keyed_dict_setdefault(self):
770 self.check_setdefault(weakref.WeakKeyDictionary,
771 C(), "value 1", "value 2")
772
Fred Drakea0a4ab12001-04-16 17:37:27 +0000773 def check_update(self, klass, dict):
Fred Drake0e540c32001-05-02 05:44:22 +0000774 #
775 # This exercises d.update(), len(d), d.keys(), d.has_key(),
776 # d.get(), d[].
777 #
Fred Drakea0a4ab12001-04-16 17:37:27 +0000778 weakdict = klass()
779 weakdict.update(dict)
780 self.assert_(len(weakdict) == len(dict))
781 for k in weakdict.keys():
782 self.assert_(dict.has_key(k),
783 "mysterious new key appeared in weak dict")
784 v = dict.get(k)
785 self.assert_(v is weakdict[k])
786 self.assert_(v is weakdict.get(k))
787 for k in dict.keys():
788 self.assert_(weakdict.has_key(k),
789 "original key disappeared in weak dict")
790 v = dict[k]
791 self.assert_(v is weakdict[k])
792 self.assert_(v is weakdict.get(k))
793
794 def test_weak_valued_dict_update(self):
795 self.check_update(weakref.WeakValueDictionary,
796 {1: C(), 'a': C(), C(): C()})
797
798 def test_weak_keyed_dict_update(self):
799 self.check_update(weakref.WeakKeyDictionary,
800 {C(): 1, C(): 2, C(): 3})
801
Fred Drakeccc75622001-09-06 14:52:39 +0000802 def test_weak_keyed_delitem(self):
803 d = weakref.WeakKeyDictionary()
804 o1 = Object('1')
805 o2 = Object('2')
806 d[o1] = 'something'
807 d[o2] = 'something'
808 self.assert_(len(d) == 2)
809 del d[o1]
810 self.assert_(len(d) == 1)
811 self.assert_(d.keys() == [o2])
812
813 def test_weak_valued_delitem(self):
814 d = weakref.WeakValueDictionary()
815 o1 = Object('1')
816 o2 = Object('2')
817 d['something'] = o1
818 d['something else'] = o2
819 self.assert_(len(d) == 2)
820 del d['something']
821 self.assert_(len(d) == 1)
822 self.assert_(d.items() == [('something else', o2)])
823
Tim Peters886128f2003-05-25 01:45:11 +0000824 def test_weak_keyed_bad_delitem(self):
825 d = weakref.WeakKeyDictionary()
826 o = Object('1')
827 # An attempt to delete an object that isn't there should raise
Tim Peters50d8b8b2003-05-25 17:44:31 +0000828 # KeyError. It didn't before 2.3.
Tim Peters886128f2003-05-25 01:45:11 +0000829 self.assertRaises(KeyError, d.__delitem__, o)
Tim Peters50d8b8b2003-05-25 17:44:31 +0000830 self.assertRaises(KeyError, d.__getitem__, o)
831
832 # If a key isn't of a weakly referencable type, __getitem__ and
833 # __setitem__ raise TypeError. __delitem__ should too.
834 self.assertRaises(TypeError, d.__delitem__, 13)
835 self.assertRaises(TypeError, d.__getitem__, 13)
836 self.assertRaises(TypeError, d.__setitem__, 13, 13)
Tim Peters886128f2003-05-25 01:45:11 +0000837
838 def test_weak_keyed_cascading_deletes(self):
839 # SF bug 742860. For some reason, before 2.3 __delitem__ iterated
840 # over the keys via self.data.iterkeys(). If things vanished from
841 # the dict during this (or got added), that caused a RuntimeError.
842
843 d = weakref.WeakKeyDictionary()
844 mutate = False
845
846 class C(object):
847 def __init__(self, i):
848 self.value = i
849 def __hash__(self):
850 return hash(self.value)
851 def __eq__(self, other):
852 if mutate:
853 # Side effect that mutates the dict, by removing the
854 # last strong reference to a key.
855 del objs[-1]
856 return self.value == other.value
857
858 objs = [C(i) for i in range(4)]
859 for o in objs:
860 d[o] = o.value
861 del o # now the only strong references to keys are in objs
862 # Find the order in which iterkeys sees the keys.
863 objs = d.keys()
864 # Reverse it, so that the iteration implementation of __delitem__
865 # has to keep looping to find the first object we delete.
866 objs.reverse()
Tim Peters50d8b8b2003-05-25 17:44:31 +0000867
Tim Peters886128f2003-05-25 01:45:11 +0000868 # Turn on mutation in C.__eq__. The first time thru the loop,
869 # under the iterkeys() business the first comparison will delete
870 # the last item iterkeys() would see, and that causes a
871 # RuntimeError: dictionary changed size during iteration
872 # when the iterkeys() loop goes around to try comparing the next
Tim Peters50d8b8b2003-05-25 17:44:31 +0000873 # key. After this was fixed, it just deletes the last object *our*
Tim Peters886128f2003-05-25 01:45:11 +0000874 # "for o in obj" loop would have gotten to.
875 mutate = True
876 count = 0
877 for o in objs:
878 count += 1
879 del d[o]
880 self.assertEqual(len(d), 0)
881 self.assertEqual(count, 2)
882
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000883from test_userdict import TestMappingProtocol
884
885class WeakValueDictionaryTestCase(TestMappingProtocol):
Fred Drakef425b1e2003-07-14 21:37:17 +0000886 """Check that WeakValueDictionary conforms to the mapping protocol"""
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000887 __ref = {"key1":Object(1), "key2":Object(2), "key3":Object(3)}
888 _tested_class = weakref.WeakValueDictionary
889 def _reference(self):
890 return self.__ref.copy()
891
892class WeakKeyDictionaryTestCase(TestMappingProtocol):
Fred Drakef425b1e2003-07-14 21:37:17 +0000893 """Check that WeakKeyDictionary conforms to the mapping protocol"""
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000894 __ref = {Object("key1"):1, Object("key2"):2, Object("key3"):3}
895 _tested_class = weakref.WeakKeyDictionary
896 def _reference(self):
897 return self.__ref.copy()
Martin v. Löwis5e163332001-02-27 18:36:56 +0000898
Fred Drake2e2be372001-09-20 21:33:42 +0000899def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000900 test_support.run_unittest(
901 ReferencesTestCase,
902 MappingTestCase,
903 WeakValueDictionaryTestCase,
Fred Drakef425b1e2003-07-14 21:37:17 +0000904 WeakKeyDictionaryTestCase,
905 )
Fred Drake2e2be372001-09-20 21:33:42 +0000906
907
908if __name__ == "__main__":
909 test_main()