blob: 3a548cdfd749dc447c57cf0ea918ea19c3dfcada [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)
170 self.failUnless(3 in p, "proxy didn't support __contains__() properly")
171 p[1] = 5
172 self.assertEqual(L[1], 5)
173 self.assertEqual(p[1], 5)
174 L2 = UserList.UserList(L)
175 p2 = weakref.proxy(L2)
176 self.assertEqual(p, p2)
Fred Drake43735da2002-04-11 03:59:42 +0000177 ## self.assertEqual(`L2`, `p2`)
178 L3 = UserList.UserList(range(10))
179 p3 = weakref.proxy(L3)
180 self.assertEqual(L3[:], p3[:])
181 self.assertEqual(L3[5:], p3[5:])
182 self.assertEqual(L3[:5], p3[:5])
183 self.assertEqual(L3[2:5], p3[2:5])
Fred Drake5935ff02001-12-19 16:54:23 +0000184
Fred Drakeb0fefc52001-03-23 04:22:45 +0000185 def test_callable_proxy(self):
186 o = Callable()
187 ref1 = weakref.proxy(o)
188
189 self.check_proxy(o, ref1)
190
191 self.assert_(type(ref1) is weakref.CallableProxyType,
192 "proxy is not of callable type")
193 ref1('twinkies!')
194 self.assert_(o.bar == 'twinkies!',
195 "call through proxy not passed through to original")
Fred Drake3bb4d212001-10-18 19:28:29 +0000196 ref1(x='Splat.')
197 self.assert_(o.bar == 'Splat.',
198 "call through proxy not passed through to original")
Fred Drakeb0fefc52001-03-23 04:22:45 +0000199
200 # expect due to too few args
201 self.assertRaises(TypeError, ref1)
202
203 # expect due to too many args
204 self.assertRaises(TypeError, ref1, 1, 2, 3)
205
206 def check_proxy(self, o, proxy):
207 o.foo = 1
208 self.assert_(proxy.foo == 1,
209 "proxy does not reflect attribute addition")
210 o.foo = 2
211 self.assert_(proxy.foo == 2,
212 "proxy does not reflect attribute modification")
213 del o.foo
214 self.assert_(not hasattr(proxy, 'foo'),
215 "proxy does not reflect attribute removal")
216
217 proxy.foo = 1
218 self.assert_(o.foo == 1,
219 "object does not reflect attribute addition via proxy")
220 proxy.foo = 2
221 self.assert_(
222 o.foo == 2,
223 "object does not reflect attribute modification via proxy")
224 del proxy.foo
225 self.assert_(not hasattr(o, 'foo'),
226 "object does not reflect attribute removal via proxy")
227
228 def test_getweakrefcount(self):
229 o = C()
230 ref1 = weakref.ref(o)
231 ref2 = weakref.ref(o, self.callback)
232 self.assert_(weakref.getweakrefcount(o) == 2,
233 "got wrong number of weak reference objects")
234
235 proxy1 = weakref.proxy(o)
236 proxy2 = weakref.proxy(o, self.callback)
237 self.assert_(weakref.getweakrefcount(o) == 4,
238 "got wrong number of weak reference objects")
239
240 def test_getweakrefs(self):
241 o = C()
242 ref1 = weakref.ref(o, self.callback)
243 ref2 = weakref.ref(o, self.callback)
244 del ref1
245 self.assert_(weakref.getweakrefs(o) == [ref2],
246 "list of refs does not match")
247
248 o = C()
249 ref1 = weakref.ref(o, self.callback)
250 ref2 = weakref.ref(o, self.callback)
251 del ref2
252 self.assert_(weakref.getweakrefs(o) == [ref1],
253 "list of refs does not match")
254
Fred Drake39c27f12001-10-18 18:06:05 +0000255 def test_newstyle_number_ops(self):
256 class F(float):
257 pass
258 f = F(2.0)
259 p = weakref.proxy(f)
260 self.assert_(p + 1.0 == 3.0)
261 self.assert_(1.0 + p == 3.0) # this used to SEGV
262
Fred Drake2a64f462001-12-10 23:46:02 +0000263 def test_callbacks_protected(self):
Guido van Rossum9eee5542002-08-22 20:21:30 +0000264 # Callbacks protected from already-set exceptions?
Fred Drake2a64f462001-12-10 23:46:02 +0000265 # Regression test for SF bug #478534.
266 class BogusError(Exception):
267 pass
268 data = {}
269 def remove(k):
270 del data[k]
271 def encapsulate():
272 f = lambda : ()
273 data[weakref.ref(f, remove)] = None
274 raise BogusError
275 try:
276 encapsulate()
277 except BogusError:
278 pass
279 else:
280 self.fail("exception not properly restored")
281 try:
282 encapsulate()
283 except BogusError:
284 pass
285 else:
286 self.fail("exception not properly restored")
287
Fred Drake41deb1e2001-02-01 05:27:45 +0000288
289class Object:
290 def __init__(self, arg):
291 self.arg = arg
292 def __repr__(self):
293 return "<Object %r>" % self.arg
294
Fred Drake41deb1e2001-02-01 05:27:45 +0000295
Fred Drakeb0fefc52001-03-23 04:22:45 +0000296class MappingTestCase(TestBase):
Martin v. Löwis5e163332001-02-27 18:36:56 +0000297
Fred Drakeb0fefc52001-03-23 04:22:45 +0000298 COUNT = 10
299
300 def test_weak_values(self):
Fred Drake0e540c32001-05-02 05:44:22 +0000301 #
302 # This exercises d.copy(), d.items(), d[], del d[], len(d).
303 #
304 dict, objects = self.make_weak_valued_dict()
Fred Drakeb0fefc52001-03-23 04:22:45 +0000305 for o in objects:
306 self.assert_(weakref.getweakrefcount(o) == 1,
307 "wrong number of weak references to %r!" % o)
308 self.assert_(o is dict[o.arg],
309 "wrong object returned by weak dict!")
310 items1 = dict.items()
311 items2 = dict.copy().items()
312 items1.sort()
313 items2.sort()
314 self.assert_(items1 == items2,
315 "cloning of weak-valued dictionary did not work!")
316 del items1, items2
317 self.assert_(len(dict) == self.COUNT)
318 del objects[0]
319 self.assert_(len(dict) == (self.COUNT - 1),
320 "deleting object did not cause dictionary update")
321 del objects, o
322 self.assert_(len(dict) == 0,
323 "deleting the values did not clear the dictionary")
Fred Drake4fd06e02001-08-03 04:11:27 +0000324 # regression on SF bug #447152:
325 dict = weakref.WeakValueDictionary()
326 self.assertRaises(KeyError, dict.__getitem__, 1)
327 dict[2] = C()
328 self.assertRaises(KeyError, dict.__getitem__, 2)
Fred Drakeb0fefc52001-03-23 04:22:45 +0000329
330 def test_weak_keys(self):
Fred Drake0e540c32001-05-02 05:44:22 +0000331 #
332 # This exercises d.copy(), d.items(), d[] = v, d[], del d[],
Fred Drake752eda42001-11-06 16:38:34 +0000333 # len(d), d.has_key().
Fred Drake0e540c32001-05-02 05:44:22 +0000334 #
335 dict, objects = self.make_weak_keyed_dict()
Fred Drakeb0fefc52001-03-23 04:22:45 +0000336 for o in objects:
337 self.assert_(weakref.getweakrefcount(o) == 1,
338 "wrong number of weak references to %r!" % o)
339 self.assert_(o.arg is dict[o],
340 "wrong object returned by weak dict!")
341 items1 = dict.items()
342 items2 = dict.copy().items()
343 items1.sort()
344 items2.sort()
345 self.assert_(items1 == items2,
346 "cloning of weak-keyed dictionary did not work!")
347 del items1, items2
348 self.assert_(len(dict) == self.COUNT)
349 del objects[0]
350 self.assert_(len(dict) == (self.COUNT - 1),
351 "deleting object did not cause dictionary update")
352 del objects, o
353 self.assert_(len(dict) == 0,
354 "deleting the keys did not clear the dictionary")
Fred Drake752eda42001-11-06 16:38:34 +0000355 o = Object(42)
356 dict[o] = "What is the meaning of the universe?"
357 self.assert_(dict.has_key(o))
358 self.assert_(not dict.has_key(34))
Martin v. Löwis5e163332001-02-27 18:36:56 +0000359
Fred Drake0e540c32001-05-02 05:44:22 +0000360 def test_weak_keyed_iters(self):
361 dict, objects = self.make_weak_keyed_dict()
362 self.check_iters(dict)
363
364 def test_weak_valued_iters(self):
365 dict, objects = self.make_weak_valued_dict()
366 self.check_iters(dict)
367
368 def check_iters(self, dict):
369 # item iterator:
370 items = dict.items()
371 for item in dict.iteritems():
372 items.remove(item)
Fred Drakeaaa48ff2001-05-10 17:16:38 +0000373 self.assert_(len(items) == 0, "iteritems() did not touch all items")
Fred Drake0e540c32001-05-02 05:44:22 +0000374
Fred Drakeaaa48ff2001-05-10 17:16:38 +0000375 # key iterator, via __iter__():
Fred Drake0e540c32001-05-02 05:44:22 +0000376 keys = dict.keys()
377 for k in dict:
378 keys.remove(k)
Fred Drakeaaa48ff2001-05-10 17:16:38 +0000379 self.assert_(len(keys) == 0, "__iter__() did not touch all keys")
380
381 # key iterator, via iterkeys():
382 keys = dict.keys()
383 for k in dict.iterkeys():
384 keys.remove(k)
385 self.assert_(len(keys) == 0, "iterkeys() did not touch all keys")
Fred Drake0e540c32001-05-02 05:44:22 +0000386
387 # value iterator:
388 values = dict.values()
389 for v in dict.itervalues():
390 values.remove(v)
Fred Drakeaaa48ff2001-05-10 17:16:38 +0000391 self.assert_(len(values) == 0, "itervalues() did not touch all values")
Fred Drake0e540c32001-05-02 05:44:22 +0000392
Guido van Rossum009afb72002-06-10 20:00:52 +0000393 def test_make_weak_keyed_dict_from_dict(self):
394 o = Object(3)
395 dict = weakref.WeakKeyDictionary({o:364})
396 self.assert_(dict[o] == 364)
397
398 def test_make_weak_keyed_dict_from_weak_keyed_dict(self):
399 o = Object(3)
400 dict = weakref.WeakKeyDictionary({o:364})
401 dict2 = weakref.WeakKeyDictionary(dict)
402 self.assert_(dict[o] == 364)
403
Fred Drake0e540c32001-05-02 05:44:22 +0000404 def make_weak_keyed_dict(self):
405 dict = weakref.WeakKeyDictionary()
406 objects = map(Object, range(self.COUNT))
407 for o in objects:
408 dict[o] = o.arg
409 return dict, objects
410
411 def make_weak_valued_dict(self):
412 dict = weakref.WeakValueDictionary()
413 objects = map(Object, range(self.COUNT))
414 for o in objects:
415 dict[o.arg] = o
416 return dict, objects
417
Fred Drakeaaa48ff2001-05-10 17:16:38 +0000418 def check_popitem(self, klass, key1, value1, key2, value2):
419 weakdict = klass()
420 weakdict[key1] = value1
421 weakdict[key2] = value2
422 self.assert_(len(weakdict) == 2)
423 k, v = weakdict.popitem()
424 self.assert_(len(weakdict) == 1)
425 if k is key1:
426 self.assert_(v is value1)
427 else:
428 self.assert_(v is value2)
429 k, v = weakdict.popitem()
430 self.assert_(len(weakdict) == 0)
431 if k is key1:
432 self.assert_(v is value1)
433 else:
434 self.assert_(v is value2)
435
436 def test_weak_valued_dict_popitem(self):
437 self.check_popitem(weakref.WeakValueDictionary,
438 "key1", C(), "key2", C())
439
440 def test_weak_keyed_dict_popitem(self):
441 self.check_popitem(weakref.WeakKeyDictionary,
442 C(), "value 1", C(), "value 2")
443
444 def check_setdefault(self, klass, key, value1, value2):
445 self.assert_(value1 is not value2,
446 "invalid test"
447 " -- value parameters must be distinct objects")
448 weakdict = klass()
449 o = weakdict.setdefault(key, value1)
450 self.assert_(o is value1)
451 self.assert_(weakdict.has_key(key))
452 self.assert_(weakdict.get(key) is value1)
453 self.assert_(weakdict[key] is value1)
454
455 o = weakdict.setdefault(key, value2)
456 self.assert_(o is value1)
457 self.assert_(weakdict.has_key(key))
458 self.assert_(weakdict.get(key) is value1)
459 self.assert_(weakdict[key] is value1)
460
461 def test_weak_valued_dict_setdefault(self):
462 self.check_setdefault(weakref.WeakValueDictionary,
463 "key", C(), C())
464
465 def test_weak_keyed_dict_setdefault(self):
466 self.check_setdefault(weakref.WeakKeyDictionary,
467 C(), "value 1", "value 2")
468
Fred Drakea0a4ab12001-04-16 17:37:27 +0000469 def check_update(self, klass, dict):
Fred Drake0e540c32001-05-02 05:44:22 +0000470 #
471 # This exercises d.update(), len(d), d.keys(), d.has_key(),
472 # d.get(), d[].
473 #
Fred Drakea0a4ab12001-04-16 17:37:27 +0000474 weakdict = klass()
475 weakdict.update(dict)
476 self.assert_(len(weakdict) == len(dict))
477 for k in weakdict.keys():
478 self.assert_(dict.has_key(k),
479 "mysterious new key appeared in weak dict")
480 v = dict.get(k)
481 self.assert_(v is weakdict[k])
482 self.assert_(v is weakdict.get(k))
483 for k in dict.keys():
484 self.assert_(weakdict.has_key(k),
485 "original key disappeared in weak dict")
486 v = dict[k]
487 self.assert_(v is weakdict[k])
488 self.assert_(v is weakdict.get(k))
489
490 def test_weak_valued_dict_update(self):
491 self.check_update(weakref.WeakValueDictionary,
492 {1: C(), 'a': C(), C(): C()})
493
494 def test_weak_keyed_dict_update(self):
495 self.check_update(weakref.WeakKeyDictionary,
496 {C(): 1, C(): 2, C(): 3})
497
Fred Drakeccc75622001-09-06 14:52:39 +0000498 def test_weak_keyed_delitem(self):
499 d = weakref.WeakKeyDictionary()
500 o1 = Object('1')
501 o2 = Object('2')
502 d[o1] = 'something'
503 d[o2] = 'something'
504 self.assert_(len(d) == 2)
505 del d[o1]
506 self.assert_(len(d) == 1)
507 self.assert_(d.keys() == [o2])
508
509 def test_weak_valued_delitem(self):
510 d = weakref.WeakValueDictionary()
511 o1 = Object('1')
512 o2 = Object('2')
513 d['something'] = o1
514 d['something else'] = o2
515 self.assert_(len(d) == 2)
516 del d['something']
517 self.assert_(len(d) == 1)
518 self.assert_(d.items() == [('something else', o2)])
519
Martin v. Löwis5e163332001-02-27 18:36:56 +0000520
Fred Drake2e2be372001-09-20 21:33:42 +0000521def test_main():
Fred Drake2e2be372001-09-20 21:33:42 +0000522 suite = unittest.TestSuite()
Guido van Rossum9eee5542002-08-22 20:21:30 +0000523 suite.addTest(unittest.makeSuite(ReferencesTestCase))
524 suite.addTest(unittest.makeSuite(MappingTestCase))
Fred Drake2e2be372001-09-20 21:33:42 +0000525 test_support.run_suite(suite)
526
527
528if __name__ == "__main__":
529 test_main()