blob: 185105b260aed475280156d2a98aa3d09d534a5d [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
Fred Drakeb0fefc52001-03-23 04:22:45 +000036class TestBase(unittest.TestCase):
37
38 def setUp(self):
39 self.cbcalled = 0
40
41 def callback(self, ref):
42 self.cbcalled += 1
Fred Drake41deb1e2001-02-01 05:27:45 +000043
44
Fred Drakeb0fefc52001-03-23 04:22:45 +000045class ReferencesTestCase(TestBase):
Fred Drake41deb1e2001-02-01 05:27:45 +000046
Fred Drakeb0fefc52001-03-23 04:22:45 +000047 def test_basic_ref(self):
48 self.check_basic_ref(C)
49 self.check_basic_ref(create_function)
50 self.check_basic_ref(create_bound_method)
51 self.check_basic_ref(create_unbound_method)
Fred Drake41deb1e2001-02-01 05:27:45 +000052
Fred Drake43735da2002-04-11 03:59:42 +000053 # Just make sure the tp_repr handler doesn't raise an exception.
54 # Live reference:
55 o = C()
56 wr = weakref.ref(o)
57 `wr`
58 # Dead reference:
59 del o
60 `wr`
61
Fred Drakeb0fefc52001-03-23 04:22:45 +000062 def test_basic_callback(self):
63 self.check_basic_callback(C)
64 self.check_basic_callback(create_function)
65 self.check_basic_callback(create_bound_method)
66 self.check_basic_callback(create_unbound_method)
Fred Drake41deb1e2001-02-01 05:27:45 +000067
Fred Drakeb0fefc52001-03-23 04:22:45 +000068 def test_multiple_callbacks(self):
69 o = C()
70 ref1 = weakref.ref(o, self.callback)
71 ref2 = weakref.ref(o, self.callback)
72 del o
73 self.assert_(ref1() is None,
74 "expected reference to be invalidated")
75 self.assert_(ref2() is None,
76 "expected reference to be invalidated")
77 self.assert_(self.cbcalled == 2,
78 "callback not called the right number of times")
Fred Drake41deb1e2001-02-01 05:27:45 +000079
Fred Drake705088e2001-04-13 17:18:15 +000080 def test_multiple_selfref_callbacks(self):
Guido van Rossum9eee5542002-08-22 20:21:30 +000081 # Make sure all references are invalidated before callbacks are called
Fred Drake705088e2001-04-13 17:18:15 +000082 #
83 # What's important here is that we're using the first
84 # reference in the callback invoked on the second reference
85 # (the most recently created ref is cleaned up first). This
86 # tests that all references to the object are invalidated
87 # before any of the callbacks are invoked, so that we only
88 # have one invocation of _weakref.c:cleanup_helper() active
89 # for a particular object at a time.
90 #
91 def callback(object, self=self):
92 self.ref()
93 c = C()
94 self.ref = weakref.ref(c, callback)
95 ref1 = weakref.ref(c, callback)
96 del c
97
Fred Drakeb0fefc52001-03-23 04:22:45 +000098 def test_proxy_ref(self):
99 o = C()
100 o.bar = 1
101 ref1 = weakref.proxy(o, self.callback)
102 ref2 = weakref.proxy(o, self.callback)
103 del o
Fred Drake41deb1e2001-02-01 05:27:45 +0000104
Fred Drakeb0fefc52001-03-23 04:22:45 +0000105 def check(proxy):
106 proxy.bar
Fred Drake41deb1e2001-02-01 05:27:45 +0000107
Fred Drakeb0fefc52001-03-23 04:22:45 +0000108 self.assertRaises(weakref.ReferenceError, check, ref1)
109 self.assertRaises(weakref.ReferenceError, check, ref2)
Neal Norwitzbdcb9412004-07-08 01:22:31 +0000110 self.assertRaises(weakref.ReferenceError, bool, weakref.proxy(C()))
Fred Drakeb0fefc52001-03-23 04:22:45 +0000111 self.assert_(self.cbcalled == 2)
Fred Drake41deb1e2001-02-01 05:27:45 +0000112
Fred Drakeb0fefc52001-03-23 04:22:45 +0000113 def check_basic_ref(self, factory):
114 o = factory()
115 ref = weakref.ref(o)
116 self.assert_(ref() is not None,
117 "weak reference to live object should be live")
118 o2 = ref()
119 self.assert_(o is o2,
120 "<ref>() should return original object if live")
Fred Drake41deb1e2001-02-01 05:27:45 +0000121
Fred Drakeb0fefc52001-03-23 04:22:45 +0000122 def check_basic_callback(self, factory):
123 self.cbcalled = 0
124 o = factory()
125 ref = weakref.ref(o, self.callback)
126 del o
Fred Drake705088e2001-04-13 17:18:15 +0000127 self.assert_(self.cbcalled == 1,
128 "callback did not properly set 'cbcalled'")
129 self.assert_(ref() is None,
130 "ref2 should be dead after deleting object reference")
Fred Drake41deb1e2001-02-01 05:27:45 +0000131
Fred Drakeb0fefc52001-03-23 04:22:45 +0000132 def test_ref_reuse(self):
133 o = C()
134 ref1 = weakref.ref(o)
135 # create a proxy to make sure that there's an intervening creation
136 # between these two; it should make no difference
137 proxy = weakref.proxy(o)
138 ref2 = weakref.ref(o)
139 self.assert_(ref1 is ref2,
140 "reference object w/out callback should be re-used")
Fred Drake41deb1e2001-02-01 05:27:45 +0000141
Fred Drakeb0fefc52001-03-23 04:22:45 +0000142 o = C()
143 proxy = weakref.proxy(o)
144 ref1 = weakref.ref(o)
145 ref2 = weakref.ref(o)
146 self.assert_(ref1 is ref2,
147 "reference object w/out callback should be re-used")
148 self.assert_(weakref.getweakrefcount(o) == 2,
149 "wrong weak ref count for object")
150 del proxy
151 self.assert_(weakref.getweakrefcount(o) == 1,
152 "wrong weak ref count for object after deleting proxy")
Fred Drake41deb1e2001-02-01 05:27:45 +0000153
Fred Drakeb0fefc52001-03-23 04:22:45 +0000154 def test_proxy_reuse(self):
155 o = C()
156 proxy1 = weakref.proxy(o)
157 ref = weakref.ref(o)
158 proxy2 = weakref.proxy(o)
159 self.assert_(proxy1 is proxy2,
160 "proxy object w/out callback should have been re-used")
161
162 def test_basic_proxy(self):
163 o = C()
164 self.check_proxy(o, weakref.proxy(o))
165
Fred Drake5935ff02001-12-19 16:54:23 +0000166 L = UserList.UserList()
167 p = weakref.proxy(L)
168 self.failIf(p, "proxy for empty UserList should be false")
169 p.append(12)
170 self.assertEqual(len(L), 1)
171 self.failUnless(p, "proxy for non-empty UserList should be true")
172 p[:] = [2, 3]
173 self.assertEqual(len(L), 2)
174 self.assertEqual(len(p), 2)
Fred Drakef425b1e2003-07-14 21:37:17 +0000175 self.failUnless(3 in p,
176 "proxy didn't support __contains__() properly")
Fred Drake5935ff02001-12-19 16:54:23 +0000177 p[1] = 5
178 self.assertEqual(L[1], 5)
179 self.assertEqual(p[1], 5)
180 L2 = UserList.UserList(L)
181 p2 = weakref.proxy(L2)
182 self.assertEqual(p, p2)
Walter Dörwald70a6b492004-02-12 17:35:32 +0000183 ## self.assertEqual(repr(L2), repr(p2))
Fred Drake43735da2002-04-11 03:59:42 +0000184 L3 = UserList.UserList(range(10))
185 p3 = weakref.proxy(L3)
186 self.assertEqual(L3[:], p3[:])
187 self.assertEqual(L3[5:], p3[5:])
188 self.assertEqual(L3[:5], p3[:5])
189 self.assertEqual(L3[2:5], p3[2:5])
Fred Drake5935ff02001-12-19 16:54:23 +0000190
Georg Brandl88659b02008-05-20 08:40:43 +0000191 def test_proxy_index(self):
192 class C:
193 def __index__(self):
194 return 10
195 o = C()
196 p = weakref.proxy(o)
197 self.assertEqual(operator.index(p), 10)
198
199 def test_proxy_div(self):
200 class C:
201 def __floordiv__(self, other):
202 return 42
203 def __ifloordiv__(self, other):
204 return 21
205 o = C()
206 p = weakref.proxy(o)
207 self.assertEqual(p // 5, 42)
208 p //= 5
209 self.assertEqual(p, 21)
210
Fred Drakeea2adc92004-02-03 19:56:46 +0000211 # The PyWeakref_* C API is documented as allowing either NULL or
212 # None as the value for the callback, where either means "no
213 # callback". The "no callback" ref and proxy objects are supposed
214 # to be shared so long as they exist by all callers so long as
Walter Dörwaldda1ad322006-12-12 21:55:31 +0000215 # they are active. In Python 2.3.3 and earlier, this guarantee
Fred Drakeea2adc92004-02-03 19:56:46 +0000216 # was not honored, and was broken in different ways for
217 # PyWeakref_NewRef() and PyWeakref_NewProxy(). (Two tests.)
218
219 def test_shared_ref_without_callback(self):
220 self.check_shared_without_callback(weakref.ref)
221
222 def test_shared_proxy_without_callback(self):
223 self.check_shared_without_callback(weakref.proxy)
224
225 def check_shared_without_callback(self, makeref):
226 o = Object(1)
227 p1 = makeref(o, None)
228 p2 = makeref(o, None)
229 self.assert_(p1 is p2, "both callbacks were None in the C API")
230 del p1, p2
231 p1 = makeref(o)
232 p2 = makeref(o, None)
233 self.assert_(p1 is p2, "callbacks were NULL, None in the C API")
234 del p1, p2
235 p1 = makeref(o)
236 p2 = makeref(o)
237 self.assert_(p1 is p2, "both callbacks were NULL in the C API")
238 del p1, p2
239 p1 = makeref(o, None)
240 p2 = makeref(o)
241 self.assert_(p1 is p2, "callbacks were None, NULL in the C API")
242
Fred Drakeb0fefc52001-03-23 04:22:45 +0000243 def test_callable_proxy(self):
244 o = Callable()
245 ref1 = weakref.proxy(o)
246
247 self.check_proxy(o, ref1)
248
249 self.assert_(type(ref1) is weakref.CallableProxyType,
250 "proxy is not of callable type")
251 ref1('twinkies!')
252 self.assert_(o.bar == 'twinkies!',
253 "call through proxy not passed through to original")
Fred Drake3bb4d212001-10-18 19:28:29 +0000254 ref1(x='Splat.')
255 self.assert_(o.bar == 'Splat.',
256 "call through proxy not passed through to original")
Fred Drakeb0fefc52001-03-23 04:22:45 +0000257
258 # expect due to too few args
259 self.assertRaises(TypeError, ref1)
260
261 # expect due to too many args
262 self.assertRaises(TypeError, ref1, 1, 2, 3)
263
264 def check_proxy(self, o, proxy):
265 o.foo = 1
266 self.assert_(proxy.foo == 1,
267 "proxy does not reflect attribute addition")
268 o.foo = 2
269 self.assert_(proxy.foo == 2,
270 "proxy does not reflect attribute modification")
271 del o.foo
272 self.assert_(not hasattr(proxy, 'foo'),
273 "proxy does not reflect attribute removal")
274
275 proxy.foo = 1
276 self.assert_(o.foo == 1,
277 "object does not reflect attribute addition via proxy")
278 proxy.foo = 2
279 self.assert_(
280 o.foo == 2,
281 "object does not reflect attribute modification via proxy")
282 del proxy.foo
283 self.assert_(not hasattr(o, 'foo'),
284 "object does not reflect attribute removal via proxy")
285
Raymond Hettingerd693a812003-06-30 04:18:48 +0000286 def test_proxy_deletion(self):
287 # Test clearing of SF bug #762891
288 class Foo:
289 result = None
290 def __delitem__(self, accessor):
291 self.result = accessor
292 g = Foo()
293 f = weakref.proxy(g)
294 del f[0]
295 self.assertEqual(f.result, 0)
296
Raymond Hettingere6c470f2005-03-27 03:04:54 +0000297 def test_proxy_bool(self):
298 # Test clearing of SF bug #1170766
299 class List(list): pass
300 lyst = List()
301 self.assertEqual(bool(weakref.proxy(lyst)), bool(lyst))
302
Fred Drakeb0fefc52001-03-23 04:22:45 +0000303 def test_getweakrefcount(self):
304 o = C()
305 ref1 = weakref.ref(o)
306 ref2 = weakref.ref(o, self.callback)
307 self.assert_(weakref.getweakrefcount(o) == 2,
308 "got wrong number of weak reference objects")
309
310 proxy1 = weakref.proxy(o)
311 proxy2 = weakref.proxy(o, self.callback)
312 self.assert_(weakref.getweakrefcount(o) == 4,
313 "got wrong number of weak reference objects")
314
Fred Drakeea2adc92004-02-03 19:56:46 +0000315 del ref1, ref2, proxy1, proxy2
316 self.assert_(weakref.getweakrefcount(o) == 0,
317 "weak reference objects not unlinked from"
318 " referent when discarded.")
319
Walter Dörwaldb167b042003-12-11 12:34:05 +0000320 # assumes ints do not support weakrefs
321 self.assert_(weakref.getweakrefcount(1) == 0,
322 "got wrong number of weak reference objects for int")
323
Fred Drakeb0fefc52001-03-23 04:22:45 +0000324 def test_getweakrefs(self):
325 o = C()
326 ref1 = weakref.ref(o, self.callback)
327 ref2 = weakref.ref(o, self.callback)
328 del ref1
329 self.assert_(weakref.getweakrefs(o) == [ref2],
330 "list of refs does not match")
331
332 o = C()
333 ref1 = weakref.ref(o, self.callback)
334 ref2 = weakref.ref(o, self.callback)
335 del ref2
336 self.assert_(weakref.getweakrefs(o) == [ref1],
337 "list of refs does not match")
338
Fred Drakeea2adc92004-02-03 19:56:46 +0000339 del ref1
340 self.assert_(weakref.getweakrefs(o) == [],
341 "list of refs not cleared")
342
Walter Dörwaldb167b042003-12-11 12:34:05 +0000343 # assumes ints do not support weakrefs
344 self.assert_(weakref.getweakrefs(1) == [],
345 "list of refs does not match for int")
346
Fred Drake39c27f12001-10-18 18:06:05 +0000347 def test_newstyle_number_ops(self):
348 class F(float):
349 pass
350 f = F(2.0)
351 p = weakref.proxy(f)
352 self.assert_(p + 1.0 == 3.0)
353 self.assert_(1.0 + p == 3.0) # this used to SEGV
354
Fred Drake2a64f462001-12-10 23:46:02 +0000355 def test_callbacks_protected(self):
Guido van Rossum9eee5542002-08-22 20:21:30 +0000356 # Callbacks protected from already-set exceptions?
Fred Drake2a64f462001-12-10 23:46:02 +0000357 # Regression test for SF bug #478534.
358 class BogusError(Exception):
359 pass
360 data = {}
361 def remove(k):
362 del data[k]
363 def encapsulate():
364 f = lambda : ()
365 data[weakref.ref(f, remove)] = None
366 raise BogusError
367 try:
368 encapsulate()
369 except BogusError:
370 pass
371 else:
372 self.fail("exception not properly restored")
373 try:
374 encapsulate()
375 except BogusError:
376 pass
377 else:
378 self.fail("exception not properly restored")
379
Tim Petersadd09b42003-11-12 20:43:28 +0000380 def test_sf_bug_840829(self):
381 # "weakref callbacks and gc corrupt memory"
382 # subtype_dealloc erroneously exposed a new-style instance
383 # already in the process of getting deallocated to gc,
384 # causing double-deallocation if the instance had a weakref
385 # callback that triggered gc.
386 # If the bug exists, there probably won't be an obvious symptom
387 # in a release build. In a debug build, a segfault will occur
388 # when the second attempt to remove the instance from the "list
389 # of all objects" occurs.
390
391 import gc
392
393 class C(object):
394 pass
395
396 c = C()
397 wr = weakref.ref(c, lambda ignore: gc.collect())
398 del c
399
Tim Petersf7f9e992003-11-13 21:59:32 +0000400 # There endeth the first part. It gets worse.
401 del wr
402
403 c1 = C()
404 c1.i = C()
405 wr = weakref.ref(c1.i, lambda ignore: gc.collect())
406
407 c2 = C()
408 c2.c1 = c1
409 del c1 # still alive because c2 points to it
410
411 # Now when subtype_dealloc gets called on c2, it's not enough just
412 # that c2 is immune from gc while the weakref callbacks associated
413 # with c2 execute (there are none in this 2nd half of the test, btw).
414 # subtype_dealloc goes on to call the base classes' deallocs too,
415 # so any gc triggered by weakref callbacks associated with anything
416 # torn down by a base class dealloc can also trigger double
417 # deallocation of c2.
418 del c2
Fred Drake41deb1e2001-02-01 05:27:45 +0000419
Tim Peters403a2032003-11-20 21:21:46 +0000420 def test_callback_in_cycle_1(self):
421 import gc
422
423 class J(object):
424 pass
425
426 class II(object):
427 def acallback(self, ignore):
428 self.J
429
430 I = II()
431 I.J = J
432 I.wr = weakref.ref(J, I.acallback)
433
434 # Now J and II are each in a self-cycle (as all new-style class
435 # objects are, since their __mro__ points back to them). I holds
436 # both a weak reference (I.wr) and a strong reference (I.J) to class
437 # J. I is also in a cycle (I.wr points to a weakref that references
438 # I.acallback). When we del these three, they all become trash, but
439 # the cycles prevent any of them from getting cleaned up immediately.
440 # Instead they have to wait for cyclic gc to deduce that they're
441 # trash.
442 #
443 # gc used to call tp_clear on all of them, and the order in which
444 # it does that is pretty accidental. The exact order in which we
445 # built up these things manages to provoke gc into running tp_clear
446 # in just the right order (I last). Calling tp_clear on II leaves
447 # behind an insane class object (its __mro__ becomes NULL). Calling
448 # tp_clear on J breaks its self-cycle, but J doesn't get deleted
449 # just then because of the strong reference from I.J. Calling
450 # tp_clear on I starts to clear I's __dict__, and just happens to
451 # clear I.J first -- I.wr is still intact. That removes the last
452 # reference to J, which triggers the weakref callback. The callback
453 # tries to do "self.J", and instances of new-style classes look up
454 # attributes ("J") in the class dict first. The class (II) wants to
455 # search II.__mro__, but that's NULL. The result was a segfault in
456 # a release build, and an assert failure in a debug build.
457 del I, J, II
458 gc.collect()
459
460 def test_callback_in_cycle_2(self):
461 import gc
462
463 # This is just like test_callback_in_cycle_1, except that II is an
464 # old-style class. The symptom is different then: an instance of an
465 # old-style class looks in its own __dict__ first. 'J' happens to
466 # get cleared from I.__dict__ before 'wr', and 'J' was never in II's
467 # __dict__, so the attribute isn't found. The difference is that
468 # the old-style II doesn't have a NULL __mro__ (it doesn't have any
469 # __mro__), so no segfault occurs. Instead it got:
470 # test_callback_in_cycle_2 (__main__.ReferencesTestCase) ...
471 # Exception exceptions.AttributeError:
472 # "II instance has no attribute 'J'" in <bound method II.acallback
473 # of <?.II instance at 0x00B9B4B8>> ignored
474
475 class J(object):
476 pass
477
478 class II:
479 def acallback(self, ignore):
480 self.J
481
482 I = II()
483 I.J = J
484 I.wr = weakref.ref(J, I.acallback)
485
486 del I, J, II
487 gc.collect()
488
489 def test_callback_in_cycle_3(self):
490 import gc
491
492 # This one broke the first patch that fixed the last two. In this
493 # case, the objects reachable from the callback aren't also reachable
494 # from the object (c1) *triggering* the callback: you can get to
495 # c1 from c2, but not vice-versa. The result was that c2's __dict__
496 # got tp_clear'ed by the time the c2.cb callback got invoked.
497
498 class C:
499 def cb(self, ignore):
500 self.me
501 self.c1
502 self.wr
503
504 c1, c2 = C(), C()
505
506 c2.me = c2
507 c2.c1 = c1
508 c2.wr = weakref.ref(c1, c2.cb)
509
510 del c1, c2
511 gc.collect()
512
513 def test_callback_in_cycle_4(self):
514 import gc
515
516 # Like test_callback_in_cycle_3, except c2 and c1 have different
517 # classes. c2's class (C) isn't reachable from c1 then, so protecting
518 # objects reachable from the dying object (c1) isn't enough to stop
519 # c2's class (C) from getting tp_clear'ed before c2.cb is invoked.
520 # The result was a segfault (C.__mro__ was NULL when the callback
521 # tried to look up self.me).
522
523 class C(object):
524 def cb(self, ignore):
525 self.me
526 self.c1
527 self.wr
528
529 class D:
530 pass
531
532 c1, c2 = D(), C()
533
534 c2.me = c2
535 c2.c1 = c1
536 c2.wr = weakref.ref(c1, c2.cb)
537
538 del c1, c2, C, D
539 gc.collect()
540
541 def test_callback_in_cycle_resurrection(self):
542 import gc
543
544 # Do something nasty in a weakref callback: resurrect objects
545 # from dead cycles. For this to be attempted, the weakref and
546 # its callback must also be part of the cyclic trash (else the
547 # objects reachable via the callback couldn't be in cyclic trash
548 # to begin with -- the callback would act like an external root).
549 # But gc clears trash weakrefs with callbacks early now, which
550 # disables the callbacks, so the callbacks shouldn't get called
551 # at all (and so nothing actually gets resurrected).
552
553 alist = []
554 class C(object):
555 def __init__(self, value):
556 self.attribute = value
557
558 def acallback(self, ignore):
559 alist.append(self.c)
560
561 c1, c2 = C(1), C(2)
562 c1.c = c2
563 c2.c = c1
564 c1.wr = weakref.ref(c2, c1.acallback)
565 c2.wr = weakref.ref(c1, c2.acallback)
566
567 def C_went_away(ignore):
568 alist.append("C went away")
569 wr = weakref.ref(C, C_went_away)
570
571 del c1, c2, C # make them all trash
572 self.assertEqual(alist, []) # del isn't enough to reclaim anything
573
574 gc.collect()
575 # c1.wr and c2.wr were part of the cyclic trash, so should have
576 # been cleared without their callbacks executing. OTOH, the weakref
577 # to C is bound to a function local (wr), and wasn't trash, so that
578 # callback should have been invoked when C went away.
579 self.assertEqual(alist, ["C went away"])
580 # The remaining weakref should be dead now (its callback ran).
581 self.assertEqual(wr(), None)
582
583 del alist[:]
584 gc.collect()
585 self.assertEqual(alist, [])
586
587 def test_callbacks_on_callback(self):
588 import gc
589
590 # Set up weakref callbacks *on* weakref callbacks.
591 alist = []
592 def safe_callback(ignore):
593 alist.append("safe_callback called")
594
595 class C(object):
596 def cb(self, ignore):
597 alist.append("cb called")
598
599 c, d = C(), C()
600 c.other = d
601 d.other = c
602 callback = c.cb
603 c.wr = weakref.ref(d, callback) # this won't trigger
604 d.wr = weakref.ref(callback, d.cb) # ditto
605 external_wr = weakref.ref(callback, safe_callback) # but this will
606 self.assert_(external_wr() is callback)
607
608 # The weakrefs attached to c and d should get cleared, so that
609 # C.cb is never called. But external_wr isn't part of the cyclic
610 # trash, and no cyclic trash is reachable from it, so safe_callback
611 # should get invoked when the bound method object callback (c.cb)
612 # -- which is itself a callback, and also part of the cyclic trash --
613 # gets reclaimed at the end of gc.
614
615 del callback, c, d, C
616 self.assertEqual(alist, []) # del isn't enough to clean up cycles
617 gc.collect()
618 self.assertEqual(alist, ["safe_callback called"])
619 self.assertEqual(external_wr(), None)
620
621 del alist[:]
622 gc.collect()
623 self.assertEqual(alist, [])
624
Fred Drakebc875f52004-02-04 23:14:14 +0000625 def test_gc_during_ref_creation(self):
626 self.check_gc_during_creation(weakref.ref)
627
628 def test_gc_during_proxy_creation(self):
629 self.check_gc_during_creation(weakref.proxy)
630
631 def check_gc_during_creation(self, makeref):
632 thresholds = gc.get_threshold()
633 gc.set_threshold(1, 1, 1)
634 gc.collect()
Fred Drake55cf4342004-02-13 19:21:57 +0000635 class A:
636 pass
Fred Drakebc875f52004-02-04 23:14:14 +0000637
638 def callback(*args):
639 pass
640
Fred Drake55cf4342004-02-13 19:21:57 +0000641 referenced = A()
Fred Drakebc875f52004-02-04 23:14:14 +0000642
Fred Drake55cf4342004-02-13 19:21:57 +0000643 a = A()
Fred Drakebc875f52004-02-04 23:14:14 +0000644 a.a = a
645 a.wr = makeref(referenced)
646
647 try:
648 # now make sure the object and the ref get labeled as
649 # cyclic trash:
Fred Drake55cf4342004-02-13 19:21:57 +0000650 a = A()
651 weakref.ref(referenced, callback)
Fred Drakebc875f52004-02-04 23:14:14 +0000652
653 finally:
654 gc.set_threshold(*thresholds)
655
Brett Cannonf5bee302007-01-23 23:21:22 +0000656 def test_ref_created_during_del(self):
657 # Bug #1377858
658 # A weakref created in an object's __del__() would crash the
659 # interpreter when the weakref was cleaned up since it would refer to
660 # non-existent memory. This test should not segfault the interpreter.
661 class Target(object):
662 def __del__(self):
663 global ref_from_del
664 ref_from_del = weakref.ref(self)
665
666 w = Target()
667
Fred Drake0a4dd392004-07-02 18:57:45 +0000668
669class SubclassableWeakrefTestCase(unittest.TestCase):
670
671 def test_subclass_refs(self):
672 class MyRef(weakref.ref):
673 def __init__(self, ob, callback=None, value=42):
674 self.value = value
675 super(MyRef, self).__init__(ob, callback)
676 def __call__(self):
677 self.called = True
678 return super(MyRef, self).__call__()
679 o = Object("foo")
680 mr = MyRef(o, value=24)
681 self.assert_(mr() is o)
682 self.assert_(mr.called)
683 self.assertEqual(mr.value, 24)
684 del o
685 self.assert_(mr() is None)
686 self.assert_(mr.called)
687
688 def test_subclass_refs_dont_replace_standard_refs(self):
689 class MyRef(weakref.ref):
690 pass
691 o = Object(42)
692 r1 = MyRef(o)
693 r2 = weakref.ref(o)
694 self.assert_(r1 is not r2)
695 self.assertEqual(weakref.getweakrefs(o), [r2, r1])
696 self.assertEqual(weakref.getweakrefcount(o), 2)
697 r3 = MyRef(o)
698 self.assertEqual(weakref.getweakrefcount(o), 3)
699 refs = weakref.getweakrefs(o)
700 self.assertEqual(len(refs), 3)
701 self.assert_(r2 is refs[0])
702 self.assert_(r1 in refs[1:])
703 self.assert_(r3 in refs[1:])
704
705 def test_subclass_refs_dont_conflate_callbacks(self):
706 class MyRef(weakref.ref):
707 pass
708 o = Object(42)
709 r1 = MyRef(o, id)
710 r2 = MyRef(o, str)
711 self.assert_(r1 is not r2)
712 refs = weakref.getweakrefs(o)
713 self.assert_(r1 in refs)
714 self.assert_(r2 in refs)
715
716 def test_subclass_refs_with_slots(self):
717 class MyRef(weakref.ref):
718 __slots__ = "slot1", "slot2"
719 def __new__(type, ob, callback, slot1, slot2):
720 return weakref.ref.__new__(type, ob, callback)
721 def __init__(self, ob, callback, slot1, slot2):
722 self.slot1 = slot1
723 self.slot2 = slot2
724 def meth(self):
725 return self.slot1 + self.slot2
726 o = Object(42)
727 r = MyRef(o, None, "abc", "def")
728 self.assertEqual(r.slot1, "abc")
729 self.assertEqual(r.slot2, "def")
730 self.assertEqual(r.meth(), "abcdef")
731 self.failIf(hasattr(r, "__dict__"))
732
733
Fred Drake41deb1e2001-02-01 05:27:45 +0000734class Object:
735 def __init__(self, arg):
736 self.arg = arg
737 def __repr__(self):
738 return "<Object %r>" % self.arg
739
Fred Drake41deb1e2001-02-01 05:27:45 +0000740
Fred Drakeb0fefc52001-03-23 04:22:45 +0000741class MappingTestCase(TestBase):
Martin v. Löwis5e163332001-02-27 18:36:56 +0000742
Fred Drakeb0fefc52001-03-23 04:22:45 +0000743 COUNT = 10
744
745 def test_weak_values(self):
Fred Drake0e540c32001-05-02 05:44:22 +0000746 #
747 # This exercises d.copy(), d.items(), d[], del d[], len(d).
748 #
749 dict, objects = self.make_weak_valued_dict()
Fred Drakeb0fefc52001-03-23 04:22:45 +0000750 for o in objects:
751 self.assert_(weakref.getweakrefcount(o) == 1,
752 "wrong number of weak references to %r!" % o)
753 self.assert_(o is dict[o.arg],
754 "wrong object returned by weak dict!")
755 items1 = dict.items()
756 items2 = dict.copy().items()
757 items1.sort()
758 items2.sort()
759 self.assert_(items1 == items2,
760 "cloning of weak-valued dictionary did not work!")
761 del items1, items2
762 self.assert_(len(dict) == self.COUNT)
763 del objects[0]
764 self.assert_(len(dict) == (self.COUNT - 1),
765 "deleting object did not cause dictionary update")
766 del objects, o
767 self.assert_(len(dict) == 0,
768 "deleting the values did not clear the dictionary")
Fred Drake4fd06e02001-08-03 04:11:27 +0000769 # regression on SF bug #447152:
770 dict = weakref.WeakValueDictionary()
771 self.assertRaises(KeyError, dict.__getitem__, 1)
772 dict[2] = C()
773 self.assertRaises(KeyError, dict.__getitem__, 2)
Fred Drakeb0fefc52001-03-23 04:22:45 +0000774
775 def test_weak_keys(self):
Fred Drake0e540c32001-05-02 05:44:22 +0000776 #
777 # This exercises d.copy(), d.items(), d[] = v, d[], del d[],
Fred Drake752eda42001-11-06 16:38:34 +0000778 # len(d), d.has_key().
Fred Drake0e540c32001-05-02 05:44:22 +0000779 #
780 dict, objects = self.make_weak_keyed_dict()
Fred Drakeb0fefc52001-03-23 04:22:45 +0000781 for o in objects:
782 self.assert_(weakref.getweakrefcount(o) == 1,
783 "wrong number of weak references to %r!" % o)
784 self.assert_(o.arg is dict[o],
785 "wrong object returned by weak dict!")
786 items1 = dict.items()
787 items2 = dict.copy().items()
Raymond Hettingera690a992003-11-16 16:17:49 +0000788 self.assert_(set(items1) == set(items2),
Fred Drakeb0fefc52001-03-23 04:22:45 +0000789 "cloning of weak-keyed dictionary did not work!")
790 del items1, items2
791 self.assert_(len(dict) == self.COUNT)
792 del objects[0]
793 self.assert_(len(dict) == (self.COUNT - 1),
794 "deleting object did not cause dictionary update")
795 del objects, o
796 self.assert_(len(dict) == 0,
797 "deleting the keys did not clear the dictionary")
Fred Drake752eda42001-11-06 16:38:34 +0000798 o = Object(42)
799 dict[o] = "What is the meaning of the universe?"
800 self.assert_(dict.has_key(o))
801 self.assert_(not dict.has_key(34))
Martin v. Löwis5e163332001-02-27 18:36:56 +0000802
Fred Drake0e540c32001-05-02 05:44:22 +0000803 def test_weak_keyed_iters(self):
804 dict, objects = self.make_weak_keyed_dict()
805 self.check_iters(dict)
806
Fred Drake017e68c2006-05-02 06:53:59 +0000807 # Test keyrefs()
808 refs = dict.keyrefs()
809 self.assertEqual(len(refs), len(objects))
810 objects2 = list(objects)
811 for wr in refs:
812 ob = wr()
813 self.assert_(dict.has_key(ob))
814 self.assert_(ob in dict)
815 self.assertEqual(ob.arg, dict[ob])
816 objects2.remove(ob)
817 self.assertEqual(len(objects2), 0)
818
819 # Test iterkeyrefs()
820 objects2 = list(objects)
821 self.assertEqual(len(list(dict.iterkeyrefs())), len(objects))
822 for wr in dict.iterkeyrefs():
823 ob = wr()
824 self.assert_(dict.has_key(ob))
825 self.assert_(ob in dict)
826 self.assertEqual(ob.arg, dict[ob])
827 objects2.remove(ob)
828 self.assertEqual(len(objects2), 0)
829
Fred Drake0e540c32001-05-02 05:44:22 +0000830 def test_weak_valued_iters(self):
831 dict, objects = self.make_weak_valued_dict()
832 self.check_iters(dict)
833
Fred Drake017e68c2006-05-02 06:53:59 +0000834 # Test valuerefs()
835 refs = dict.valuerefs()
836 self.assertEqual(len(refs), len(objects))
837 objects2 = list(objects)
838 for wr in refs:
839 ob = wr()
840 self.assertEqual(ob, dict[ob.arg])
841 self.assertEqual(ob.arg, dict[ob.arg].arg)
842 objects2.remove(ob)
843 self.assertEqual(len(objects2), 0)
844
845 # Test itervaluerefs()
846 objects2 = list(objects)
847 self.assertEqual(len(list(dict.itervaluerefs())), len(objects))
848 for wr in dict.itervaluerefs():
849 ob = wr()
850 self.assertEqual(ob, dict[ob.arg])
851 self.assertEqual(ob.arg, dict[ob.arg].arg)
852 objects2.remove(ob)
853 self.assertEqual(len(objects2), 0)
854
Fred Drake0e540c32001-05-02 05:44:22 +0000855 def check_iters(self, dict):
856 # item iterator:
857 items = dict.items()
858 for item in dict.iteritems():
859 items.remove(item)
Fred Drakeaaa48ff2001-05-10 17:16:38 +0000860 self.assert_(len(items) == 0, "iteritems() did not touch all items")
Fred Drake0e540c32001-05-02 05:44:22 +0000861
Fred Drakeaaa48ff2001-05-10 17:16:38 +0000862 # key iterator, via __iter__():
Fred Drake0e540c32001-05-02 05:44:22 +0000863 keys = dict.keys()
864 for k in dict:
865 keys.remove(k)
Fred Drakeaaa48ff2001-05-10 17:16:38 +0000866 self.assert_(len(keys) == 0, "__iter__() did not touch all keys")
867
868 # key iterator, via iterkeys():
869 keys = dict.keys()
870 for k in dict.iterkeys():
871 keys.remove(k)
872 self.assert_(len(keys) == 0, "iterkeys() did not touch all keys")
Fred Drake0e540c32001-05-02 05:44:22 +0000873
874 # value iterator:
875 values = dict.values()
876 for v in dict.itervalues():
877 values.remove(v)
Fred Drakef425b1e2003-07-14 21:37:17 +0000878 self.assert_(len(values) == 0,
879 "itervalues() did not touch all values")
Fred Drake0e540c32001-05-02 05:44:22 +0000880
Guido van Rossum009afb72002-06-10 20:00:52 +0000881 def test_make_weak_keyed_dict_from_dict(self):
882 o = Object(3)
883 dict = weakref.WeakKeyDictionary({o:364})
884 self.assert_(dict[o] == 364)
885
886 def test_make_weak_keyed_dict_from_weak_keyed_dict(self):
887 o = Object(3)
888 dict = weakref.WeakKeyDictionary({o:364})
889 dict2 = weakref.WeakKeyDictionary(dict)
890 self.assert_(dict[o] == 364)
891
Fred Drake0e540c32001-05-02 05:44:22 +0000892 def make_weak_keyed_dict(self):
893 dict = weakref.WeakKeyDictionary()
894 objects = map(Object, range(self.COUNT))
895 for o in objects:
896 dict[o] = o.arg
897 return dict, objects
898
899 def make_weak_valued_dict(self):
900 dict = weakref.WeakValueDictionary()
901 objects = map(Object, range(self.COUNT))
902 for o in objects:
903 dict[o.arg] = o
904 return dict, objects
905
Fred Drakeaaa48ff2001-05-10 17:16:38 +0000906 def check_popitem(self, klass, key1, value1, key2, value2):
907 weakdict = klass()
908 weakdict[key1] = value1
909 weakdict[key2] = value2
910 self.assert_(len(weakdict) == 2)
911 k, v = weakdict.popitem()
912 self.assert_(len(weakdict) == 1)
913 if k is key1:
914 self.assert_(v is value1)
915 else:
916 self.assert_(v is value2)
917 k, v = weakdict.popitem()
918 self.assert_(len(weakdict) == 0)
919 if k is key1:
920 self.assert_(v is value1)
921 else:
922 self.assert_(v is value2)
923
924 def test_weak_valued_dict_popitem(self):
925 self.check_popitem(weakref.WeakValueDictionary,
926 "key1", C(), "key2", C())
927
928 def test_weak_keyed_dict_popitem(self):
929 self.check_popitem(weakref.WeakKeyDictionary,
930 C(), "value 1", C(), "value 2")
931
932 def check_setdefault(self, klass, key, value1, value2):
933 self.assert_(value1 is not value2,
934 "invalid test"
935 " -- value parameters must be distinct objects")
936 weakdict = klass()
937 o = weakdict.setdefault(key, value1)
938 self.assert_(o is value1)
939 self.assert_(weakdict.has_key(key))
940 self.assert_(weakdict.get(key) is value1)
941 self.assert_(weakdict[key] is value1)
942
943 o = weakdict.setdefault(key, value2)
944 self.assert_(o is value1)
945 self.assert_(weakdict.has_key(key))
946 self.assert_(weakdict.get(key) is value1)
947 self.assert_(weakdict[key] is value1)
948
949 def test_weak_valued_dict_setdefault(self):
950 self.check_setdefault(weakref.WeakValueDictionary,
951 "key", C(), C())
952
953 def test_weak_keyed_dict_setdefault(self):
954 self.check_setdefault(weakref.WeakKeyDictionary,
955 C(), "value 1", "value 2")
956
Fred Drakea0a4ab12001-04-16 17:37:27 +0000957 def check_update(self, klass, dict):
Fred Drake0e540c32001-05-02 05:44:22 +0000958 #
959 # This exercises d.update(), len(d), d.keys(), d.has_key(),
960 # d.get(), d[].
961 #
Fred Drakea0a4ab12001-04-16 17:37:27 +0000962 weakdict = klass()
963 weakdict.update(dict)
964 self.assert_(len(weakdict) == len(dict))
965 for k in weakdict.keys():
966 self.assert_(dict.has_key(k),
967 "mysterious new key appeared in weak dict")
968 v = dict.get(k)
969 self.assert_(v is weakdict[k])
970 self.assert_(v is weakdict.get(k))
971 for k in dict.keys():
972 self.assert_(weakdict.has_key(k),
973 "original key disappeared in weak dict")
974 v = dict[k]
975 self.assert_(v is weakdict[k])
976 self.assert_(v is weakdict.get(k))
977
978 def test_weak_valued_dict_update(self):
979 self.check_update(weakref.WeakValueDictionary,
980 {1: C(), 'a': C(), C(): C()})
981
982 def test_weak_keyed_dict_update(self):
983 self.check_update(weakref.WeakKeyDictionary,
984 {C(): 1, C(): 2, C(): 3})
985
Fred Drakeccc75622001-09-06 14:52:39 +0000986 def test_weak_keyed_delitem(self):
987 d = weakref.WeakKeyDictionary()
988 o1 = Object('1')
989 o2 = Object('2')
990 d[o1] = 'something'
991 d[o2] = 'something'
992 self.assert_(len(d) == 2)
993 del d[o1]
994 self.assert_(len(d) == 1)
995 self.assert_(d.keys() == [o2])
996
997 def test_weak_valued_delitem(self):
998 d = weakref.WeakValueDictionary()
999 o1 = Object('1')
1000 o2 = Object('2')
1001 d['something'] = o1
1002 d['something else'] = o2
1003 self.assert_(len(d) == 2)
1004 del d['something']
1005 self.assert_(len(d) == 1)
1006 self.assert_(d.items() == [('something else', o2)])
1007
Tim Peters886128f2003-05-25 01:45:11 +00001008 def test_weak_keyed_bad_delitem(self):
1009 d = weakref.WeakKeyDictionary()
1010 o = Object('1')
1011 # An attempt to delete an object that isn't there should raise
Tim Peters50d8b8b2003-05-25 17:44:31 +00001012 # KeyError. It didn't before 2.3.
Tim Peters886128f2003-05-25 01:45:11 +00001013 self.assertRaises(KeyError, d.__delitem__, o)
Tim Peters50d8b8b2003-05-25 17:44:31 +00001014 self.assertRaises(KeyError, d.__getitem__, o)
1015
1016 # If a key isn't of a weakly referencable type, __getitem__ and
1017 # __setitem__ raise TypeError. __delitem__ should too.
1018 self.assertRaises(TypeError, d.__delitem__, 13)
1019 self.assertRaises(TypeError, d.__getitem__, 13)
1020 self.assertRaises(TypeError, d.__setitem__, 13, 13)
Tim Peters886128f2003-05-25 01:45:11 +00001021
1022 def test_weak_keyed_cascading_deletes(self):
1023 # SF bug 742860. For some reason, before 2.3 __delitem__ iterated
1024 # over the keys via self.data.iterkeys(). If things vanished from
1025 # the dict during this (or got added), that caused a RuntimeError.
1026
1027 d = weakref.WeakKeyDictionary()
1028 mutate = False
1029
1030 class C(object):
1031 def __init__(self, i):
1032 self.value = i
1033 def __hash__(self):
1034 return hash(self.value)
1035 def __eq__(self, other):
1036 if mutate:
1037 # Side effect that mutates the dict, by removing the
1038 # last strong reference to a key.
1039 del objs[-1]
1040 return self.value == other.value
1041
1042 objs = [C(i) for i in range(4)]
1043 for o in objs:
1044 d[o] = o.value
1045 del o # now the only strong references to keys are in objs
1046 # Find the order in which iterkeys sees the keys.
1047 objs = d.keys()
1048 # Reverse it, so that the iteration implementation of __delitem__
1049 # has to keep looping to find the first object we delete.
1050 objs.reverse()
Tim Peters50d8b8b2003-05-25 17:44:31 +00001051
Tim Peters886128f2003-05-25 01:45:11 +00001052 # Turn on mutation in C.__eq__. The first time thru the loop,
1053 # under the iterkeys() business the first comparison will delete
1054 # the last item iterkeys() would see, and that causes a
1055 # RuntimeError: dictionary changed size during iteration
1056 # when the iterkeys() loop goes around to try comparing the next
Tim Peters50d8b8b2003-05-25 17:44:31 +00001057 # key. After this was fixed, it just deletes the last object *our*
Tim Peters886128f2003-05-25 01:45:11 +00001058 # "for o in obj" loop would have gotten to.
1059 mutate = True
1060 count = 0
1061 for o in objs:
1062 count += 1
1063 del d[o]
1064 self.assertEqual(len(d), 0)
1065 self.assertEqual(count, 2)
1066
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +00001067from test import mapping_tests
Raymond Hettinger2c2d3222003-03-09 07:05:43 +00001068
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +00001069class WeakValueDictionaryTestCase(mapping_tests.BasicTestMappingProtocol):
Fred Drakef425b1e2003-07-14 21:37:17 +00001070 """Check that WeakValueDictionary conforms to the mapping protocol"""
Raymond Hettinger2c2d3222003-03-09 07:05:43 +00001071 __ref = {"key1":Object(1), "key2":Object(2), "key3":Object(3)}
Walter Dörwald118f9312004-06-02 18:42:25 +00001072 type2test = weakref.WeakValueDictionary
Raymond Hettinger2c2d3222003-03-09 07:05:43 +00001073 def _reference(self):
1074 return self.__ref.copy()
1075
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +00001076class WeakKeyDictionaryTestCase(mapping_tests.BasicTestMappingProtocol):
Fred Drakef425b1e2003-07-14 21:37:17 +00001077 """Check that WeakKeyDictionary conforms to the mapping protocol"""
Raymond Hettinger2c2d3222003-03-09 07:05:43 +00001078 __ref = {Object("key1"):1, Object("key2"):2, Object("key3"):3}
Walter Dörwald118f9312004-06-02 18:42:25 +00001079 type2test = weakref.WeakKeyDictionary
Raymond Hettinger2c2d3222003-03-09 07:05:43 +00001080 def _reference(self):
1081 return self.__ref.copy()
Martin v. Löwis5e163332001-02-27 18:36:56 +00001082
Georg Brandl88659b02008-05-20 08:40:43 +00001083libreftest = """ Doctest for examples in the library reference: weakref.rst
Georg Brandl9a65d582005-07-02 19:07:30 +00001084
1085>>> import weakref
1086>>> class Dict(dict):
1087... pass
1088...
1089>>> obj = Dict(red=1, green=2, blue=3) # this object is weak referencable
1090>>> r = weakref.ref(obj)
Armin Rigoa3f09272006-05-28 19:13:17 +00001091>>> print r() is obj
1092True
Georg Brandl9a65d582005-07-02 19:07:30 +00001093
1094>>> import weakref
1095>>> class Object:
1096... pass
1097...
1098>>> o = Object()
1099>>> r = weakref.ref(o)
1100>>> o2 = r()
1101>>> o is o2
1102True
1103>>> del o, o2
1104>>> print r()
1105None
1106
1107>>> import weakref
1108>>> class ExtendedRef(weakref.ref):
1109... def __init__(self, ob, callback=None, **annotations):
1110... super(ExtendedRef, self).__init__(ob, callback)
1111... self.__counter = 0
1112... for k, v in annotations.iteritems():
1113... setattr(self, k, v)
1114... def __call__(self):
1115... '''Return a pair containing the referent and the number of
1116... times the reference has been called.
1117... '''
1118... ob = super(ExtendedRef, self).__call__()
1119... if ob is not None:
1120... self.__counter += 1
1121... ob = (ob, self.__counter)
1122... return ob
1123...
1124>>> class A: # not in docs from here, just testing the ExtendedRef
1125... pass
1126...
1127>>> a = A()
1128>>> r = ExtendedRef(a, foo=1, bar="baz")
1129>>> r.foo
11301
1131>>> r.bar
1132'baz'
1133>>> r()[1]
11341
1135>>> r()[1]
11362
1137>>> r()[0] is a
1138True
1139
1140
1141>>> import weakref
1142>>> _id2obj_dict = weakref.WeakValueDictionary()
1143>>> def remember(obj):
1144... oid = id(obj)
1145... _id2obj_dict[oid] = obj
1146... return oid
1147...
1148>>> def id2obj(oid):
1149... return _id2obj_dict[oid]
1150...
1151>>> a = A() # from here, just testing
1152>>> a_id = remember(a)
1153>>> id2obj(a_id) is a
1154True
1155>>> del a
1156>>> try:
1157... id2obj(a_id)
1158... except KeyError:
1159... print 'OK'
1160... else:
1161... print 'WeakValueDictionary error'
1162OK
1163
1164"""
1165
1166__test__ = {'libreftest' : libreftest}
1167
Fred Drake2e2be372001-09-20 21:33:42 +00001168def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +00001169 test_support.run_unittest(
1170 ReferencesTestCase,
1171 MappingTestCase,
1172 WeakValueDictionaryTestCase,
Fred Drakef425b1e2003-07-14 21:37:17 +00001173 WeakKeyDictionaryTestCase,
1174 )
Georg Brandl9a65d582005-07-02 19:07:30 +00001175 test_support.run_doctest(sys.modules[__name__])
Fred Drake2e2be372001-09-20 21:33:42 +00001176
1177
1178if __name__ == "__main__":
1179 test_main()