Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 1 | import sys |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 2 | import unittest |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 3 | import weakref |
| 4 | |
Fred Drake | 705088e | 2001-04-13 17:18:15 +0000 | [diff] [blame] | 5 | from test_support import run_unittest |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 6 | |
| 7 | |
| 8 | class C: |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 9 | def method(self): |
| 10 | pass |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 11 | |
| 12 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 13 | class Callable: |
| 14 | bar = None |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 15 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 16 | def __call__(self, x): |
| 17 | self.bar = x |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 18 | |
| 19 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 20 | def create_function(): |
| 21 | def f(): pass |
| 22 | return f |
| 23 | |
| 24 | def create_bound_method(): |
| 25 | return C().method |
| 26 | |
| 27 | def create_unbound_method(): |
| 28 | return C.method |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 29 | |
| 30 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 31 | class TestBase(unittest.TestCase): |
| 32 | |
| 33 | def setUp(self): |
| 34 | self.cbcalled = 0 |
| 35 | |
| 36 | def callback(self, ref): |
| 37 | self.cbcalled += 1 |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 38 | |
| 39 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 40 | class ReferencesTestCase(TestBase): |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 41 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 42 | def test_basic_ref(self): |
| 43 | self.check_basic_ref(C) |
| 44 | self.check_basic_ref(create_function) |
| 45 | self.check_basic_ref(create_bound_method) |
| 46 | self.check_basic_ref(create_unbound_method) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 47 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 48 | def test_basic_callback(self): |
| 49 | self.check_basic_callback(C) |
| 50 | self.check_basic_callback(create_function) |
| 51 | self.check_basic_callback(create_bound_method) |
| 52 | self.check_basic_callback(create_unbound_method) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 53 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 54 | def test_multiple_callbacks(self): |
| 55 | o = C() |
| 56 | ref1 = weakref.ref(o, self.callback) |
| 57 | ref2 = weakref.ref(o, self.callback) |
| 58 | del o |
| 59 | self.assert_(ref1() is None, |
| 60 | "expected reference to be invalidated") |
| 61 | self.assert_(ref2() is None, |
| 62 | "expected reference to be invalidated") |
| 63 | self.assert_(self.cbcalled == 2, |
| 64 | "callback not called the right number of times") |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 65 | |
Fred Drake | 705088e | 2001-04-13 17:18:15 +0000 | [diff] [blame] | 66 | def test_multiple_selfref_callbacks(self): |
| 67 | """Make sure all references are invalidated before callbacks |
| 68 | are called.""" |
| 69 | # |
| 70 | # What's important here is that we're using the first |
| 71 | # reference in the callback invoked on the second reference |
| 72 | # (the most recently created ref is cleaned up first). This |
| 73 | # tests that all references to the object are invalidated |
| 74 | # before any of the callbacks are invoked, so that we only |
| 75 | # have one invocation of _weakref.c:cleanup_helper() active |
| 76 | # for a particular object at a time. |
| 77 | # |
| 78 | def callback(object, self=self): |
| 79 | self.ref() |
| 80 | c = C() |
| 81 | self.ref = weakref.ref(c, callback) |
| 82 | ref1 = weakref.ref(c, callback) |
| 83 | del c |
| 84 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 85 | def test_proxy_ref(self): |
| 86 | o = C() |
| 87 | o.bar = 1 |
| 88 | ref1 = weakref.proxy(o, self.callback) |
| 89 | ref2 = weakref.proxy(o, self.callback) |
| 90 | del o |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 91 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 92 | def check(proxy): |
| 93 | proxy.bar |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 94 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 95 | self.assertRaises(weakref.ReferenceError, check, ref1) |
| 96 | self.assertRaises(weakref.ReferenceError, check, ref2) |
| 97 | self.assert_(self.cbcalled == 2) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 98 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 99 | def check_basic_ref(self, factory): |
| 100 | o = factory() |
| 101 | ref = weakref.ref(o) |
| 102 | self.assert_(ref() is not None, |
| 103 | "weak reference to live object should be live") |
| 104 | o2 = ref() |
| 105 | self.assert_(o is o2, |
| 106 | "<ref>() should return original object if live") |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 107 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 108 | def check_basic_callback(self, factory): |
| 109 | self.cbcalled = 0 |
| 110 | o = factory() |
| 111 | ref = weakref.ref(o, self.callback) |
| 112 | del o |
Fred Drake | 705088e | 2001-04-13 17:18:15 +0000 | [diff] [blame] | 113 | self.assert_(self.cbcalled == 1, |
| 114 | "callback did not properly set 'cbcalled'") |
| 115 | self.assert_(ref() is None, |
| 116 | "ref2 should be dead after deleting object reference") |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 117 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 118 | def test_ref_reuse(self): |
| 119 | o = C() |
| 120 | ref1 = weakref.ref(o) |
| 121 | # create a proxy to make sure that there's an intervening creation |
| 122 | # between these two; it should make no difference |
| 123 | proxy = weakref.proxy(o) |
| 124 | ref2 = weakref.ref(o) |
| 125 | self.assert_(ref1 is ref2, |
| 126 | "reference object w/out callback should be re-used") |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 127 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 128 | o = C() |
| 129 | proxy = weakref.proxy(o) |
| 130 | ref1 = weakref.ref(o) |
| 131 | ref2 = weakref.ref(o) |
| 132 | self.assert_(ref1 is ref2, |
| 133 | "reference object w/out callback should be re-used") |
| 134 | self.assert_(weakref.getweakrefcount(o) == 2, |
| 135 | "wrong weak ref count for object") |
| 136 | del proxy |
| 137 | self.assert_(weakref.getweakrefcount(o) == 1, |
| 138 | "wrong weak ref count for object after deleting proxy") |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 139 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 140 | def test_proxy_reuse(self): |
| 141 | o = C() |
| 142 | proxy1 = weakref.proxy(o) |
| 143 | ref = weakref.ref(o) |
| 144 | proxy2 = weakref.proxy(o) |
| 145 | self.assert_(proxy1 is proxy2, |
| 146 | "proxy object w/out callback should have been re-used") |
| 147 | |
| 148 | def test_basic_proxy(self): |
| 149 | o = C() |
| 150 | self.check_proxy(o, weakref.proxy(o)) |
| 151 | |
| 152 | def test_callable_proxy(self): |
| 153 | o = Callable() |
| 154 | ref1 = weakref.proxy(o) |
| 155 | |
| 156 | self.check_proxy(o, ref1) |
| 157 | |
| 158 | self.assert_(type(ref1) is weakref.CallableProxyType, |
| 159 | "proxy is not of callable type") |
| 160 | ref1('twinkies!') |
| 161 | self.assert_(o.bar == 'twinkies!', |
| 162 | "call through proxy not passed through to original") |
| 163 | |
| 164 | # expect due to too few args |
| 165 | self.assertRaises(TypeError, ref1) |
| 166 | |
| 167 | # expect due to too many args |
| 168 | self.assertRaises(TypeError, ref1, 1, 2, 3) |
| 169 | |
| 170 | def check_proxy(self, o, proxy): |
| 171 | o.foo = 1 |
| 172 | self.assert_(proxy.foo == 1, |
| 173 | "proxy does not reflect attribute addition") |
| 174 | o.foo = 2 |
| 175 | self.assert_(proxy.foo == 2, |
| 176 | "proxy does not reflect attribute modification") |
| 177 | del o.foo |
| 178 | self.assert_(not hasattr(proxy, 'foo'), |
| 179 | "proxy does not reflect attribute removal") |
| 180 | |
| 181 | proxy.foo = 1 |
| 182 | self.assert_(o.foo == 1, |
| 183 | "object does not reflect attribute addition via proxy") |
| 184 | proxy.foo = 2 |
| 185 | self.assert_( |
| 186 | o.foo == 2, |
| 187 | "object does not reflect attribute modification via proxy") |
| 188 | del proxy.foo |
| 189 | self.assert_(not hasattr(o, 'foo'), |
| 190 | "object does not reflect attribute removal via proxy") |
| 191 | |
| 192 | def test_getweakrefcount(self): |
| 193 | o = C() |
| 194 | ref1 = weakref.ref(o) |
| 195 | ref2 = weakref.ref(o, self.callback) |
| 196 | self.assert_(weakref.getweakrefcount(o) == 2, |
| 197 | "got wrong number of weak reference objects") |
| 198 | |
| 199 | proxy1 = weakref.proxy(o) |
| 200 | proxy2 = weakref.proxy(o, self.callback) |
| 201 | self.assert_(weakref.getweakrefcount(o) == 4, |
| 202 | "got wrong number of weak reference objects") |
| 203 | |
| 204 | def test_getweakrefs(self): |
| 205 | o = C() |
| 206 | ref1 = weakref.ref(o, self.callback) |
| 207 | ref2 = weakref.ref(o, self.callback) |
| 208 | del ref1 |
| 209 | self.assert_(weakref.getweakrefs(o) == [ref2], |
| 210 | "list of refs does not match") |
| 211 | |
| 212 | o = C() |
| 213 | ref1 = weakref.ref(o, self.callback) |
| 214 | ref2 = weakref.ref(o, self.callback) |
| 215 | del ref2 |
| 216 | self.assert_(weakref.getweakrefs(o) == [ref1], |
| 217 | "list of refs does not match") |
| 218 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 219 | |
| 220 | class Object: |
| 221 | def __init__(self, arg): |
| 222 | self.arg = arg |
| 223 | def __repr__(self): |
| 224 | return "<Object %r>" % self.arg |
| 225 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 226 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 227 | class MappingTestCase(TestBase): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 228 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 229 | COUNT = 10 |
| 230 | |
| 231 | def test_weak_values(self): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 232 | # |
| 233 | # This exercises d.copy(), d.items(), d[], del d[], len(d). |
| 234 | # |
| 235 | dict, objects = self.make_weak_valued_dict() |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 236 | for o in objects: |
| 237 | self.assert_(weakref.getweakrefcount(o) == 1, |
| 238 | "wrong number of weak references to %r!" % o) |
| 239 | self.assert_(o is dict[o.arg], |
| 240 | "wrong object returned by weak dict!") |
| 241 | items1 = dict.items() |
| 242 | items2 = dict.copy().items() |
| 243 | items1.sort() |
| 244 | items2.sort() |
| 245 | self.assert_(items1 == items2, |
| 246 | "cloning of weak-valued dictionary did not work!") |
| 247 | del items1, items2 |
| 248 | self.assert_(len(dict) == self.COUNT) |
| 249 | del objects[0] |
| 250 | self.assert_(len(dict) == (self.COUNT - 1), |
| 251 | "deleting object did not cause dictionary update") |
| 252 | del objects, o |
| 253 | self.assert_(len(dict) == 0, |
| 254 | "deleting the values did not clear the dictionary") |
Fred Drake | 4fd06e0 | 2001-08-03 04:11:27 +0000 | [diff] [blame] | 255 | # regression on SF bug #447152: |
| 256 | dict = weakref.WeakValueDictionary() |
| 257 | self.assertRaises(KeyError, dict.__getitem__, 1) |
| 258 | dict[2] = C() |
| 259 | self.assertRaises(KeyError, dict.__getitem__, 2) |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 260 | |
| 261 | def test_weak_keys(self): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 262 | # |
| 263 | # This exercises d.copy(), d.items(), d[] = v, d[], del d[], |
| 264 | # len(d). |
| 265 | # |
| 266 | dict, objects = self.make_weak_keyed_dict() |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 267 | for o in objects: |
| 268 | self.assert_(weakref.getweakrefcount(o) == 1, |
| 269 | "wrong number of weak references to %r!" % o) |
| 270 | self.assert_(o.arg is dict[o], |
| 271 | "wrong object returned by weak dict!") |
| 272 | items1 = dict.items() |
| 273 | items2 = dict.copy().items() |
| 274 | items1.sort() |
| 275 | items2.sort() |
| 276 | self.assert_(items1 == items2, |
| 277 | "cloning of weak-keyed dictionary did not work!") |
| 278 | del items1, items2 |
| 279 | self.assert_(len(dict) == self.COUNT) |
| 280 | del objects[0] |
| 281 | self.assert_(len(dict) == (self.COUNT - 1), |
| 282 | "deleting object did not cause dictionary update") |
| 283 | del objects, o |
| 284 | self.assert_(len(dict) == 0, |
| 285 | "deleting the keys did not clear the dictionary") |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 286 | |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 287 | def test_weak_keyed_iters(self): |
| 288 | dict, objects = self.make_weak_keyed_dict() |
| 289 | self.check_iters(dict) |
| 290 | |
| 291 | def test_weak_valued_iters(self): |
| 292 | dict, objects = self.make_weak_valued_dict() |
| 293 | self.check_iters(dict) |
| 294 | |
| 295 | def check_iters(self, dict): |
| 296 | # item iterator: |
| 297 | items = dict.items() |
| 298 | for item in dict.iteritems(): |
| 299 | items.remove(item) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 300 | self.assert_(len(items) == 0, "iteritems() did not touch all items") |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 301 | |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 302 | # key iterator, via __iter__(): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 303 | keys = dict.keys() |
| 304 | for k in dict: |
| 305 | keys.remove(k) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 306 | self.assert_(len(keys) == 0, "__iter__() did not touch all keys") |
| 307 | |
| 308 | # key iterator, via iterkeys(): |
| 309 | keys = dict.keys() |
| 310 | for k in dict.iterkeys(): |
| 311 | keys.remove(k) |
| 312 | self.assert_(len(keys) == 0, "iterkeys() did not touch all keys") |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 313 | |
| 314 | # value iterator: |
| 315 | values = dict.values() |
| 316 | for v in dict.itervalues(): |
| 317 | values.remove(v) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 318 | self.assert_(len(values) == 0, "itervalues() did not touch all values") |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 319 | |
| 320 | def make_weak_keyed_dict(self): |
| 321 | dict = weakref.WeakKeyDictionary() |
| 322 | objects = map(Object, range(self.COUNT)) |
| 323 | for o in objects: |
| 324 | dict[o] = o.arg |
| 325 | return dict, objects |
| 326 | |
| 327 | def make_weak_valued_dict(self): |
| 328 | dict = weakref.WeakValueDictionary() |
| 329 | objects = map(Object, range(self.COUNT)) |
| 330 | for o in objects: |
| 331 | dict[o.arg] = o |
| 332 | return dict, objects |
| 333 | |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 334 | def check_popitem(self, klass, key1, value1, key2, value2): |
| 335 | weakdict = klass() |
| 336 | weakdict[key1] = value1 |
| 337 | weakdict[key2] = value2 |
| 338 | self.assert_(len(weakdict) == 2) |
| 339 | k, v = weakdict.popitem() |
| 340 | self.assert_(len(weakdict) == 1) |
| 341 | if k is key1: |
| 342 | self.assert_(v is value1) |
| 343 | else: |
| 344 | self.assert_(v is value2) |
| 345 | k, v = weakdict.popitem() |
| 346 | self.assert_(len(weakdict) == 0) |
| 347 | if k is key1: |
| 348 | self.assert_(v is value1) |
| 349 | else: |
| 350 | self.assert_(v is value2) |
| 351 | |
| 352 | def test_weak_valued_dict_popitem(self): |
| 353 | self.check_popitem(weakref.WeakValueDictionary, |
| 354 | "key1", C(), "key2", C()) |
| 355 | |
| 356 | def test_weak_keyed_dict_popitem(self): |
| 357 | self.check_popitem(weakref.WeakKeyDictionary, |
| 358 | C(), "value 1", C(), "value 2") |
| 359 | |
| 360 | def check_setdefault(self, klass, key, value1, value2): |
| 361 | self.assert_(value1 is not value2, |
| 362 | "invalid test" |
| 363 | " -- value parameters must be distinct objects") |
| 364 | weakdict = klass() |
| 365 | o = weakdict.setdefault(key, value1) |
| 366 | self.assert_(o is value1) |
| 367 | self.assert_(weakdict.has_key(key)) |
| 368 | self.assert_(weakdict.get(key) is value1) |
| 369 | self.assert_(weakdict[key] is value1) |
| 370 | |
| 371 | o = weakdict.setdefault(key, value2) |
| 372 | self.assert_(o is value1) |
| 373 | self.assert_(weakdict.has_key(key)) |
| 374 | self.assert_(weakdict.get(key) is value1) |
| 375 | self.assert_(weakdict[key] is value1) |
| 376 | |
| 377 | def test_weak_valued_dict_setdefault(self): |
| 378 | self.check_setdefault(weakref.WeakValueDictionary, |
| 379 | "key", C(), C()) |
| 380 | |
| 381 | def test_weak_keyed_dict_setdefault(self): |
| 382 | self.check_setdefault(weakref.WeakKeyDictionary, |
| 383 | C(), "value 1", "value 2") |
| 384 | |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 385 | def check_update(self, klass, dict): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 386 | # |
| 387 | # This exercises d.update(), len(d), d.keys(), d.has_key(), |
| 388 | # d.get(), d[]. |
| 389 | # |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 390 | weakdict = klass() |
| 391 | weakdict.update(dict) |
| 392 | self.assert_(len(weakdict) == len(dict)) |
| 393 | for k in weakdict.keys(): |
| 394 | self.assert_(dict.has_key(k), |
| 395 | "mysterious new key appeared in weak dict") |
| 396 | v = dict.get(k) |
| 397 | self.assert_(v is weakdict[k]) |
| 398 | self.assert_(v is weakdict.get(k)) |
| 399 | for k in dict.keys(): |
| 400 | self.assert_(weakdict.has_key(k), |
| 401 | "original key disappeared in weak dict") |
| 402 | v = dict[k] |
| 403 | self.assert_(v is weakdict[k]) |
| 404 | self.assert_(v is weakdict.get(k)) |
| 405 | |
| 406 | def test_weak_valued_dict_update(self): |
| 407 | self.check_update(weakref.WeakValueDictionary, |
| 408 | {1: C(), 'a': C(), C(): C()}) |
| 409 | |
| 410 | def test_weak_keyed_dict_update(self): |
| 411 | self.check_update(weakref.WeakKeyDictionary, |
| 412 | {C(): 1, C(): 2, C(): 3}) |
| 413 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 414 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 415 | run_unittest(ReferencesTestCase) |
| 416 | run_unittest(MappingTestCase) |