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