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 |
Raymond Hettinger | 53dbe39 | 2008-02-12 20:03:09 +0000 | [diff] [blame] | 4 | import collections |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 5 | import weakref |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 6 | import operator |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 7 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 8 | from test import support |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 9 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 10 | # Used in ReferencesTestCase.test_ref_created_during_del() . |
| 11 | ref_from_del = None |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 12 | |
| 13 | class C: |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 14 | def method(self): |
| 15 | pass |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 16 | |
| 17 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 18 | class Callable: |
| 19 | bar = None |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 20 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 21 | def __call__(self, x): |
| 22 | self.bar = x |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 23 | |
| 24 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 25 | def create_function(): |
| 26 | def f(): pass |
| 27 | return f |
| 28 | |
| 29 | def create_bound_method(): |
| 30 | return C().method |
| 31 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 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) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 48 | |
Fred Drake | 43735da | 2002-04-11 03:59:42 +0000 | [diff] [blame] | 49 | # Just make sure the tp_repr handler doesn't raise an exception. |
| 50 | # Live reference: |
| 51 | o = C() |
| 52 | wr = weakref.ref(o) |
Brett Cannon | 0b70cca | 2006-08-25 02:59:59 +0000 | [diff] [blame] | 53 | repr(wr) |
Fred Drake | 43735da | 2002-04-11 03:59:42 +0000 | [diff] [blame] | 54 | # Dead reference: |
| 55 | del o |
Brett Cannon | 0b70cca | 2006-08-25 02:59:59 +0000 | [diff] [blame] | 56 | repr(wr) |
Fred Drake | 43735da | 2002-04-11 03:59:42 +0000 | [diff] [blame] | 57 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 58 | 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) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 62 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 63 | def test_multiple_callbacks(self): |
| 64 | o = C() |
| 65 | ref1 = weakref.ref(o, self.callback) |
| 66 | ref2 = weakref.ref(o, self.callback) |
| 67 | del o |
| 68 | self.assert_(ref1() is None, |
| 69 | "expected reference to be invalidated") |
| 70 | self.assert_(ref2() is None, |
| 71 | "expected reference to be invalidated") |
| 72 | self.assert_(self.cbcalled == 2, |
| 73 | "callback not called the right number of times") |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 74 | |
Fred Drake | 705088e | 2001-04-13 17:18:15 +0000 | [diff] [blame] | 75 | def test_multiple_selfref_callbacks(self): |
Guido van Rossum | 9eee554 | 2002-08-22 20:21:30 +0000 | [diff] [blame] | 76 | # Make sure all references are invalidated before callbacks are called |
Fred Drake | 705088e | 2001-04-13 17:18:15 +0000 | [diff] [blame] | 77 | # |
| 78 | # What's important here is that we're using the first |
| 79 | # reference in the callback invoked on the second reference |
| 80 | # (the most recently created ref is cleaned up first). This |
| 81 | # tests that all references to the object are invalidated |
| 82 | # before any of the callbacks are invoked, so that we only |
| 83 | # have one invocation of _weakref.c:cleanup_helper() active |
| 84 | # for a particular object at a time. |
| 85 | # |
| 86 | def callback(object, self=self): |
| 87 | self.ref() |
| 88 | c = C() |
| 89 | self.ref = weakref.ref(c, callback) |
| 90 | ref1 = weakref.ref(c, callback) |
| 91 | del c |
| 92 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 93 | def test_proxy_ref(self): |
| 94 | o = C() |
| 95 | o.bar = 1 |
| 96 | ref1 = weakref.proxy(o, self.callback) |
| 97 | ref2 = weakref.proxy(o, self.callback) |
| 98 | del o |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 99 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 100 | def check(proxy): |
| 101 | proxy.bar |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 102 | |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 103 | self.assertRaises(ReferenceError, check, ref1) |
| 104 | self.assertRaises(ReferenceError, check, ref2) |
| 105 | self.assertRaises(ReferenceError, bool, weakref.proxy(C())) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 106 | self.assertEqual(self.cbcalled, 2) |
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_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 Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 116 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 117 | def check_basic_callback(self, factory): |
| 118 | self.cbcalled = 0 |
| 119 | o = factory() |
| 120 | ref = weakref.ref(o, self.callback) |
| 121 | del o |
Fred Drake | 705088e | 2001-04-13 17:18:15 +0000 | [diff] [blame] | 122 | 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 Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 126 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 127 | 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 Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 136 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 137 | 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 Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 148 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 149 | 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 | |
Raymond Hettinger | 53dbe39 | 2008-02-12 20:03:09 +0000 | [diff] [blame] | 161 | L = collections.UserList() |
Fred Drake | 5935ff0 | 2001-12-19 16:54:23 +0000 | [diff] [blame] | 162 | p = weakref.proxy(L) |
| 163 | self.failIf(p, "proxy for empty UserList should be false") |
| 164 | p.append(12) |
| 165 | self.assertEqual(len(L), 1) |
| 166 | self.failUnless(p, "proxy for non-empty UserList should be true") |
| 167 | p[:] = [2, 3] |
| 168 | self.assertEqual(len(L), 2) |
| 169 | self.assertEqual(len(p), 2) |
Fred Drake | f425b1e | 2003-07-14 21:37:17 +0000 | [diff] [blame] | 170 | self.failUnless(3 in p, |
| 171 | "proxy didn't support __contains__() properly") |
Fred Drake | 5935ff0 | 2001-12-19 16:54:23 +0000 | [diff] [blame] | 172 | p[1] = 5 |
| 173 | self.assertEqual(L[1], 5) |
| 174 | self.assertEqual(p[1], 5) |
Raymond Hettinger | 53dbe39 | 2008-02-12 20:03:09 +0000 | [diff] [blame] | 175 | L2 = collections.UserList(L) |
Fred Drake | 5935ff0 | 2001-12-19 16:54:23 +0000 | [diff] [blame] | 176 | p2 = weakref.proxy(L2) |
| 177 | self.assertEqual(p, p2) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 178 | ## self.assertEqual(repr(L2), repr(p2)) |
Raymond Hettinger | 53dbe39 | 2008-02-12 20:03:09 +0000 | [diff] [blame] | 179 | L3 = collections.UserList(range(10)) |
Fred Drake | 43735da | 2002-04-11 03:59:42 +0000 | [diff] [blame] | 180 | p3 = weakref.proxy(L3) |
| 181 | self.assertEqual(L3[:], p3[:]) |
| 182 | self.assertEqual(L3[5:], p3[5:]) |
| 183 | self.assertEqual(L3[:5], p3[:5]) |
| 184 | self.assertEqual(L3[2:5], p3[2:5]) |
Fred Drake | 5935ff0 | 2001-12-19 16:54:23 +0000 | [diff] [blame] | 185 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 186 | def test_proxy_index(self): |
| 187 | class C: |
| 188 | def __index__(self): |
| 189 | return 10 |
| 190 | o = C() |
| 191 | p = weakref.proxy(o) |
| 192 | self.assertEqual(operator.index(p), 10) |
| 193 | |
| 194 | def test_proxy_div(self): |
| 195 | class C: |
| 196 | def __floordiv__(self, other): |
| 197 | return 42 |
| 198 | def __ifloordiv__(self, other): |
| 199 | return 21 |
| 200 | o = C() |
| 201 | p = weakref.proxy(o) |
| 202 | self.assertEqual(p // 5, 42) |
| 203 | p //= 5 |
| 204 | self.assertEqual(p, 21) |
| 205 | |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 206 | # The PyWeakref_* C API is documented as allowing either NULL or |
| 207 | # None as the value for the callback, where either means "no |
| 208 | # callback". The "no callback" ref and proxy objects are supposed |
| 209 | # to be shared so long as they exist by all callers so long as |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 210 | # they are active. In Python 2.3.3 and earlier, this guarantee |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 211 | # was not honored, and was broken in different ways for |
| 212 | # PyWeakref_NewRef() and PyWeakref_NewProxy(). (Two tests.) |
| 213 | |
| 214 | def test_shared_ref_without_callback(self): |
| 215 | self.check_shared_without_callback(weakref.ref) |
| 216 | |
| 217 | def test_shared_proxy_without_callback(self): |
| 218 | self.check_shared_without_callback(weakref.proxy) |
| 219 | |
| 220 | def check_shared_without_callback(self, makeref): |
| 221 | o = Object(1) |
| 222 | p1 = makeref(o, None) |
| 223 | p2 = makeref(o, None) |
| 224 | self.assert_(p1 is p2, "both callbacks were None in the C API") |
| 225 | del p1, p2 |
| 226 | p1 = makeref(o) |
| 227 | p2 = makeref(o, None) |
| 228 | self.assert_(p1 is p2, "callbacks were NULL, None in the C API") |
| 229 | del p1, p2 |
| 230 | p1 = makeref(o) |
| 231 | p2 = makeref(o) |
| 232 | self.assert_(p1 is p2, "both callbacks were NULL in the C API") |
| 233 | del p1, p2 |
| 234 | p1 = makeref(o, None) |
| 235 | p2 = makeref(o) |
| 236 | self.assert_(p1 is p2, "callbacks were None, NULL in the C API") |
| 237 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 238 | def test_callable_proxy(self): |
| 239 | o = Callable() |
| 240 | ref1 = weakref.proxy(o) |
| 241 | |
| 242 | self.check_proxy(o, ref1) |
| 243 | |
| 244 | self.assert_(type(ref1) is weakref.CallableProxyType, |
| 245 | "proxy is not of callable type") |
| 246 | ref1('twinkies!') |
| 247 | self.assert_(o.bar == 'twinkies!', |
| 248 | "call through proxy not passed through to original") |
Fred Drake | 3bb4d21 | 2001-10-18 19:28:29 +0000 | [diff] [blame] | 249 | ref1(x='Splat.') |
| 250 | self.assert_(o.bar == 'Splat.', |
| 251 | "call through proxy not passed through to original") |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 252 | |
| 253 | # expect due to too few args |
| 254 | self.assertRaises(TypeError, ref1) |
| 255 | |
| 256 | # expect due to too many args |
| 257 | self.assertRaises(TypeError, ref1, 1, 2, 3) |
| 258 | |
| 259 | def check_proxy(self, o, proxy): |
| 260 | o.foo = 1 |
| 261 | self.assert_(proxy.foo == 1, |
| 262 | "proxy does not reflect attribute addition") |
| 263 | o.foo = 2 |
| 264 | self.assert_(proxy.foo == 2, |
| 265 | "proxy does not reflect attribute modification") |
| 266 | del o.foo |
| 267 | self.assert_(not hasattr(proxy, 'foo'), |
| 268 | "proxy does not reflect attribute removal") |
| 269 | |
| 270 | proxy.foo = 1 |
| 271 | self.assert_(o.foo == 1, |
| 272 | "object does not reflect attribute addition via proxy") |
| 273 | proxy.foo = 2 |
| 274 | self.assert_( |
| 275 | o.foo == 2, |
| 276 | "object does not reflect attribute modification via proxy") |
| 277 | del proxy.foo |
| 278 | self.assert_(not hasattr(o, 'foo'), |
| 279 | "object does not reflect attribute removal via proxy") |
| 280 | |
Raymond Hettinger | d693a81 | 2003-06-30 04:18:48 +0000 | [diff] [blame] | 281 | def test_proxy_deletion(self): |
| 282 | # Test clearing of SF bug #762891 |
| 283 | class Foo: |
| 284 | result = None |
| 285 | def __delitem__(self, accessor): |
| 286 | self.result = accessor |
| 287 | g = Foo() |
| 288 | f = weakref.proxy(g) |
| 289 | del f[0] |
| 290 | self.assertEqual(f.result, 0) |
| 291 | |
Raymond Hettinger | e6c470f | 2005-03-27 03:04:54 +0000 | [diff] [blame] | 292 | def test_proxy_bool(self): |
| 293 | # Test clearing of SF bug #1170766 |
| 294 | class List(list): pass |
| 295 | lyst = List() |
| 296 | self.assertEqual(bool(weakref.proxy(lyst)), bool(lyst)) |
| 297 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 298 | def test_getweakrefcount(self): |
| 299 | o = C() |
| 300 | ref1 = weakref.ref(o) |
| 301 | ref2 = weakref.ref(o, self.callback) |
| 302 | self.assert_(weakref.getweakrefcount(o) == 2, |
| 303 | "got wrong number of weak reference objects") |
| 304 | |
| 305 | proxy1 = weakref.proxy(o) |
| 306 | proxy2 = weakref.proxy(o, self.callback) |
| 307 | self.assert_(weakref.getweakrefcount(o) == 4, |
| 308 | "got wrong number of weak reference objects") |
| 309 | |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 310 | del ref1, ref2, proxy1, proxy2 |
| 311 | self.assert_(weakref.getweakrefcount(o) == 0, |
| 312 | "weak reference objects not unlinked from" |
| 313 | " referent when discarded.") |
| 314 | |
Walter Dörwald | b167b04 | 2003-12-11 12:34:05 +0000 | [diff] [blame] | 315 | # assumes ints do not support weakrefs |
| 316 | self.assert_(weakref.getweakrefcount(1) == 0, |
| 317 | "got wrong number of weak reference objects for int") |
| 318 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 319 | def test_getweakrefs(self): |
| 320 | o = C() |
| 321 | ref1 = weakref.ref(o, self.callback) |
| 322 | ref2 = weakref.ref(o, self.callback) |
| 323 | del ref1 |
| 324 | self.assert_(weakref.getweakrefs(o) == [ref2], |
| 325 | "list of refs does not match") |
| 326 | |
| 327 | o = C() |
| 328 | ref1 = weakref.ref(o, self.callback) |
| 329 | ref2 = weakref.ref(o, self.callback) |
| 330 | del ref2 |
| 331 | self.assert_(weakref.getweakrefs(o) == [ref1], |
| 332 | "list of refs does not match") |
| 333 | |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 334 | del ref1 |
| 335 | self.assert_(weakref.getweakrefs(o) == [], |
| 336 | "list of refs not cleared") |
| 337 | |
Walter Dörwald | b167b04 | 2003-12-11 12:34:05 +0000 | [diff] [blame] | 338 | # assumes ints do not support weakrefs |
| 339 | self.assert_(weakref.getweakrefs(1) == [], |
| 340 | "list of refs does not match for int") |
| 341 | |
Fred Drake | 39c27f1 | 2001-10-18 18:06:05 +0000 | [diff] [blame] | 342 | def test_newstyle_number_ops(self): |
| 343 | class F(float): |
| 344 | pass |
| 345 | f = F(2.0) |
| 346 | p = weakref.proxy(f) |
| 347 | self.assert_(p + 1.0 == 3.0) |
| 348 | self.assert_(1.0 + p == 3.0) # this used to SEGV |
| 349 | |
Fred Drake | 2a64f46 | 2001-12-10 23:46:02 +0000 | [diff] [blame] | 350 | def test_callbacks_protected(self): |
Guido van Rossum | 9eee554 | 2002-08-22 20:21:30 +0000 | [diff] [blame] | 351 | # Callbacks protected from already-set exceptions? |
Fred Drake | 2a64f46 | 2001-12-10 23:46:02 +0000 | [diff] [blame] | 352 | # Regression test for SF bug #478534. |
| 353 | class BogusError(Exception): |
| 354 | pass |
| 355 | data = {} |
| 356 | def remove(k): |
| 357 | del data[k] |
| 358 | def encapsulate(): |
| 359 | f = lambda : () |
| 360 | data[weakref.ref(f, remove)] = None |
| 361 | raise BogusError |
| 362 | try: |
| 363 | encapsulate() |
| 364 | except BogusError: |
| 365 | pass |
| 366 | else: |
| 367 | self.fail("exception not properly restored") |
| 368 | try: |
| 369 | encapsulate() |
| 370 | except BogusError: |
| 371 | pass |
| 372 | else: |
| 373 | self.fail("exception not properly restored") |
| 374 | |
Tim Peters | add09b4 | 2003-11-12 20:43:28 +0000 | [diff] [blame] | 375 | def test_sf_bug_840829(self): |
| 376 | # "weakref callbacks and gc corrupt memory" |
| 377 | # subtype_dealloc erroneously exposed a new-style instance |
| 378 | # already in the process of getting deallocated to gc, |
| 379 | # causing double-deallocation if the instance had a weakref |
| 380 | # callback that triggered gc. |
| 381 | # If the bug exists, there probably won't be an obvious symptom |
| 382 | # in a release build. In a debug build, a segfault will occur |
| 383 | # when the second attempt to remove the instance from the "list |
| 384 | # of all objects" occurs. |
| 385 | |
| 386 | import gc |
| 387 | |
| 388 | class C(object): |
| 389 | pass |
| 390 | |
| 391 | c = C() |
| 392 | wr = weakref.ref(c, lambda ignore: gc.collect()) |
| 393 | del c |
| 394 | |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 395 | # There endeth the first part. It gets worse. |
| 396 | del wr |
| 397 | |
| 398 | c1 = C() |
| 399 | c1.i = C() |
| 400 | wr = weakref.ref(c1.i, lambda ignore: gc.collect()) |
| 401 | |
| 402 | c2 = C() |
| 403 | c2.c1 = c1 |
| 404 | del c1 # still alive because c2 points to it |
| 405 | |
| 406 | # Now when subtype_dealloc gets called on c2, it's not enough just |
| 407 | # that c2 is immune from gc while the weakref callbacks associated |
| 408 | # with c2 execute (there are none in this 2nd half of the test, btw). |
| 409 | # subtype_dealloc goes on to call the base classes' deallocs too, |
| 410 | # so any gc triggered by weakref callbacks associated with anything |
| 411 | # torn down by a base class dealloc can also trigger double |
| 412 | # deallocation of c2. |
| 413 | del c2 |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 414 | |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 415 | def test_callback_in_cycle_1(self): |
| 416 | import gc |
| 417 | |
| 418 | class J(object): |
| 419 | pass |
| 420 | |
| 421 | class II(object): |
| 422 | def acallback(self, ignore): |
| 423 | self.J |
| 424 | |
| 425 | I = II() |
| 426 | I.J = J |
| 427 | I.wr = weakref.ref(J, I.acallback) |
| 428 | |
| 429 | # Now J and II are each in a self-cycle (as all new-style class |
| 430 | # objects are, since their __mro__ points back to them). I holds |
| 431 | # both a weak reference (I.wr) and a strong reference (I.J) to class |
| 432 | # J. I is also in a cycle (I.wr points to a weakref that references |
| 433 | # I.acallback). When we del these three, they all become trash, but |
| 434 | # the cycles prevent any of them from getting cleaned up immediately. |
| 435 | # Instead they have to wait for cyclic gc to deduce that they're |
| 436 | # trash. |
| 437 | # |
| 438 | # gc used to call tp_clear on all of them, and the order in which |
| 439 | # it does that is pretty accidental. The exact order in which we |
| 440 | # built up these things manages to provoke gc into running tp_clear |
| 441 | # in just the right order (I last). Calling tp_clear on II leaves |
| 442 | # behind an insane class object (its __mro__ becomes NULL). Calling |
| 443 | # tp_clear on J breaks its self-cycle, but J doesn't get deleted |
| 444 | # just then because of the strong reference from I.J. Calling |
| 445 | # tp_clear on I starts to clear I's __dict__, and just happens to |
| 446 | # clear I.J first -- I.wr is still intact. That removes the last |
| 447 | # reference to J, which triggers the weakref callback. The callback |
| 448 | # tries to do "self.J", and instances of new-style classes look up |
| 449 | # attributes ("J") in the class dict first. The class (II) wants to |
| 450 | # search II.__mro__, but that's NULL. The result was a segfault in |
| 451 | # a release build, and an assert failure in a debug build. |
| 452 | del I, J, II |
| 453 | gc.collect() |
| 454 | |
| 455 | def test_callback_in_cycle_2(self): |
| 456 | import gc |
| 457 | |
| 458 | # This is just like test_callback_in_cycle_1, except that II is an |
| 459 | # old-style class. The symptom is different then: an instance of an |
| 460 | # old-style class looks in its own __dict__ first. 'J' happens to |
| 461 | # get cleared from I.__dict__ before 'wr', and 'J' was never in II's |
| 462 | # __dict__, so the attribute isn't found. The difference is that |
| 463 | # the old-style II doesn't have a NULL __mro__ (it doesn't have any |
| 464 | # __mro__), so no segfault occurs. Instead it got: |
| 465 | # test_callback_in_cycle_2 (__main__.ReferencesTestCase) ... |
| 466 | # Exception exceptions.AttributeError: |
| 467 | # "II instance has no attribute 'J'" in <bound method II.acallback |
| 468 | # of <?.II instance at 0x00B9B4B8>> ignored |
| 469 | |
| 470 | class J(object): |
| 471 | pass |
| 472 | |
| 473 | class II: |
| 474 | def acallback(self, ignore): |
| 475 | self.J |
| 476 | |
| 477 | I = II() |
| 478 | I.J = J |
| 479 | I.wr = weakref.ref(J, I.acallback) |
| 480 | |
| 481 | del I, J, II |
| 482 | gc.collect() |
| 483 | |
| 484 | def test_callback_in_cycle_3(self): |
| 485 | import gc |
| 486 | |
| 487 | # This one broke the first patch that fixed the last two. In this |
| 488 | # case, the objects reachable from the callback aren't also reachable |
| 489 | # from the object (c1) *triggering* the callback: you can get to |
| 490 | # c1 from c2, but not vice-versa. The result was that c2's __dict__ |
| 491 | # got tp_clear'ed by the time the c2.cb callback got invoked. |
| 492 | |
| 493 | class C: |
| 494 | def cb(self, ignore): |
| 495 | self.me |
| 496 | self.c1 |
| 497 | self.wr |
| 498 | |
| 499 | c1, c2 = C(), C() |
| 500 | |
| 501 | c2.me = c2 |
| 502 | c2.c1 = c1 |
| 503 | c2.wr = weakref.ref(c1, c2.cb) |
| 504 | |
| 505 | del c1, c2 |
| 506 | gc.collect() |
| 507 | |
| 508 | def test_callback_in_cycle_4(self): |
| 509 | import gc |
| 510 | |
| 511 | # Like test_callback_in_cycle_3, except c2 and c1 have different |
| 512 | # classes. c2's class (C) isn't reachable from c1 then, so protecting |
| 513 | # objects reachable from the dying object (c1) isn't enough to stop |
| 514 | # c2's class (C) from getting tp_clear'ed before c2.cb is invoked. |
| 515 | # The result was a segfault (C.__mro__ was NULL when the callback |
| 516 | # tried to look up self.me). |
| 517 | |
| 518 | class C(object): |
| 519 | def cb(self, ignore): |
| 520 | self.me |
| 521 | self.c1 |
| 522 | self.wr |
| 523 | |
| 524 | class D: |
| 525 | pass |
| 526 | |
| 527 | c1, c2 = D(), C() |
| 528 | |
| 529 | c2.me = c2 |
| 530 | c2.c1 = c1 |
| 531 | c2.wr = weakref.ref(c1, c2.cb) |
| 532 | |
| 533 | del c1, c2, C, D |
| 534 | gc.collect() |
| 535 | |
| 536 | def test_callback_in_cycle_resurrection(self): |
| 537 | import gc |
| 538 | |
| 539 | # Do something nasty in a weakref callback: resurrect objects |
| 540 | # from dead cycles. For this to be attempted, the weakref and |
| 541 | # its callback must also be part of the cyclic trash (else the |
| 542 | # objects reachable via the callback couldn't be in cyclic trash |
| 543 | # to begin with -- the callback would act like an external root). |
| 544 | # But gc clears trash weakrefs with callbacks early now, which |
| 545 | # disables the callbacks, so the callbacks shouldn't get called |
| 546 | # at all (and so nothing actually gets resurrected). |
| 547 | |
| 548 | alist = [] |
| 549 | class C(object): |
| 550 | def __init__(self, value): |
| 551 | self.attribute = value |
| 552 | |
| 553 | def acallback(self, ignore): |
| 554 | alist.append(self.c) |
| 555 | |
| 556 | c1, c2 = C(1), C(2) |
| 557 | c1.c = c2 |
| 558 | c2.c = c1 |
| 559 | c1.wr = weakref.ref(c2, c1.acallback) |
| 560 | c2.wr = weakref.ref(c1, c2.acallback) |
| 561 | |
| 562 | def C_went_away(ignore): |
| 563 | alist.append("C went away") |
| 564 | wr = weakref.ref(C, C_went_away) |
| 565 | |
| 566 | del c1, c2, C # make them all trash |
| 567 | self.assertEqual(alist, []) # del isn't enough to reclaim anything |
| 568 | |
| 569 | gc.collect() |
| 570 | # c1.wr and c2.wr were part of the cyclic trash, so should have |
| 571 | # been cleared without their callbacks executing. OTOH, the weakref |
| 572 | # to C is bound to a function local (wr), and wasn't trash, so that |
| 573 | # callback should have been invoked when C went away. |
| 574 | self.assertEqual(alist, ["C went away"]) |
| 575 | # The remaining weakref should be dead now (its callback ran). |
| 576 | self.assertEqual(wr(), None) |
| 577 | |
| 578 | del alist[:] |
| 579 | gc.collect() |
| 580 | self.assertEqual(alist, []) |
| 581 | |
| 582 | def test_callbacks_on_callback(self): |
| 583 | import gc |
| 584 | |
| 585 | # Set up weakref callbacks *on* weakref callbacks. |
| 586 | alist = [] |
| 587 | def safe_callback(ignore): |
| 588 | alist.append("safe_callback called") |
| 589 | |
| 590 | class C(object): |
| 591 | def cb(self, ignore): |
| 592 | alist.append("cb called") |
| 593 | |
| 594 | c, d = C(), C() |
| 595 | c.other = d |
| 596 | d.other = c |
| 597 | callback = c.cb |
| 598 | c.wr = weakref.ref(d, callback) # this won't trigger |
| 599 | d.wr = weakref.ref(callback, d.cb) # ditto |
| 600 | external_wr = weakref.ref(callback, safe_callback) # but this will |
| 601 | self.assert_(external_wr() is callback) |
| 602 | |
| 603 | # The weakrefs attached to c and d should get cleared, so that |
| 604 | # C.cb is never called. But external_wr isn't part of the cyclic |
| 605 | # trash, and no cyclic trash is reachable from it, so safe_callback |
| 606 | # should get invoked when the bound method object callback (c.cb) |
| 607 | # -- which is itself a callback, and also part of the cyclic trash -- |
| 608 | # gets reclaimed at the end of gc. |
| 609 | |
| 610 | del callback, c, d, C |
| 611 | self.assertEqual(alist, []) # del isn't enough to clean up cycles |
| 612 | gc.collect() |
| 613 | self.assertEqual(alist, ["safe_callback called"]) |
| 614 | self.assertEqual(external_wr(), None) |
| 615 | |
| 616 | del alist[:] |
| 617 | gc.collect() |
| 618 | self.assertEqual(alist, []) |
| 619 | |
Fred Drake | bc875f5 | 2004-02-04 23:14:14 +0000 | [diff] [blame] | 620 | def test_gc_during_ref_creation(self): |
| 621 | self.check_gc_during_creation(weakref.ref) |
| 622 | |
| 623 | def test_gc_during_proxy_creation(self): |
| 624 | self.check_gc_during_creation(weakref.proxy) |
| 625 | |
| 626 | def check_gc_during_creation(self, makeref): |
| 627 | thresholds = gc.get_threshold() |
| 628 | gc.set_threshold(1, 1, 1) |
| 629 | gc.collect() |
Fred Drake | 55cf434 | 2004-02-13 19:21:57 +0000 | [diff] [blame] | 630 | class A: |
| 631 | pass |
Fred Drake | bc875f5 | 2004-02-04 23:14:14 +0000 | [diff] [blame] | 632 | |
| 633 | def callback(*args): |
| 634 | pass |
| 635 | |
Fred Drake | 55cf434 | 2004-02-13 19:21:57 +0000 | [diff] [blame] | 636 | referenced = A() |
Fred Drake | bc875f5 | 2004-02-04 23:14:14 +0000 | [diff] [blame] | 637 | |
Fred Drake | 55cf434 | 2004-02-13 19:21:57 +0000 | [diff] [blame] | 638 | a = A() |
Fred Drake | bc875f5 | 2004-02-04 23:14:14 +0000 | [diff] [blame] | 639 | a.a = a |
| 640 | a.wr = makeref(referenced) |
| 641 | |
| 642 | try: |
| 643 | # now make sure the object and the ref get labeled as |
| 644 | # cyclic trash: |
Fred Drake | 55cf434 | 2004-02-13 19:21:57 +0000 | [diff] [blame] | 645 | a = A() |
| 646 | weakref.ref(referenced, callback) |
Fred Drake | bc875f5 | 2004-02-04 23:14:14 +0000 | [diff] [blame] | 647 | |
| 648 | finally: |
| 649 | gc.set_threshold(*thresholds) |
| 650 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 651 | def test_ref_created_during_del(self): |
| 652 | # Bug #1377858 |
| 653 | # A weakref created in an object's __del__() would crash the |
| 654 | # interpreter when the weakref was cleaned up since it would refer to |
| 655 | # non-existent memory. This test should not segfault the interpreter. |
| 656 | class Target(object): |
| 657 | def __del__(self): |
| 658 | global ref_from_del |
| 659 | ref_from_del = weakref.ref(self) |
| 660 | |
| 661 | w = Target() |
| 662 | |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 663 | |
| 664 | class SubclassableWeakrefTestCase(unittest.TestCase): |
| 665 | |
| 666 | def test_subclass_refs(self): |
| 667 | class MyRef(weakref.ref): |
| 668 | def __init__(self, ob, callback=None, value=42): |
| 669 | self.value = value |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 670 | super().__init__(ob, callback) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 671 | def __call__(self): |
| 672 | self.called = True |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 673 | return super().__call__() |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 674 | o = Object("foo") |
| 675 | mr = MyRef(o, value=24) |
| 676 | self.assert_(mr() is o) |
| 677 | self.assert_(mr.called) |
| 678 | self.assertEqual(mr.value, 24) |
| 679 | del o |
| 680 | self.assert_(mr() is None) |
| 681 | self.assert_(mr.called) |
| 682 | |
| 683 | def test_subclass_refs_dont_replace_standard_refs(self): |
| 684 | class MyRef(weakref.ref): |
| 685 | pass |
| 686 | o = Object(42) |
| 687 | r1 = MyRef(o) |
| 688 | r2 = weakref.ref(o) |
| 689 | self.assert_(r1 is not r2) |
| 690 | self.assertEqual(weakref.getweakrefs(o), [r2, r1]) |
| 691 | self.assertEqual(weakref.getweakrefcount(o), 2) |
| 692 | r3 = MyRef(o) |
| 693 | self.assertEqual(weakref.getweakrefcount(o), 3) |
| 694 | refs = weakref.getweakrefs(o) |
| 695 | self.assertEqual(len(refs), 3) |
| 696 | self.assert_(r2 is refs[0]) |
| 697 | self.assert_(r1 in refs[1:]) |
| 698 | self.assert_(r3 in refs[1:]) |
| 699 | |
| 700 | def test_subclass_refs_dont_conflate_callbacks(self): |
| 701 | class MyRef(weakref.ref): |
| 702 | pass |
| 703 | o = Object(42) |
| 704 | r1 = MyRef(o, id) |
| 705 | r2 = MyRef(o, str) |
| 706 | self.assert_(r1 is not r2) |
| 707 | refs = weakref.getweakrefs(o) |
| 708 | self.assert_(r1 in refs) |
| 709 | self.assert_(r2 in refs) |
| 710 | |
| 711 | def test_subclass_refs_with_slots(self): |
| 712 | class MyRef(weakref.ref): |
| 713 | __slots__ = "slot1", "slot2" |
| 714 | def __new__(type, ob, callback, slot1, slot2): |
| 715 | return weakref.ref.__new__(type, ob, callback) |
| 716 | def __init__(self, ob, callback, slot1, slot2): |
| 717 | self.slot1 = slot1 |
| 718 | self.slot2 = slot2 |
| 719 | def meth(self): |
| 720 | return self.slot1 + self.slot2 |
| 721 | o = Object(42) |
| 722 | r = MyRef(o, None, "abc", "def") |
| 723 | self.assertEqual(r.slot1, "abc") |
| 724 | self.assertEqual(r.slot2, "def") |
| 725 | self.assertEqual(r.meth(), "abcdef") |
| 726 | self.failIf(hasattr(r, "__dict__")) |
| 727 | |
| 728 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 729 | class Object: |
| 730 | def __init__(self, arg): |
| 731 | self.arg = arg |
| 732 | def __repr__(self): |
| 733 | return "<Object %r>" % self.arg |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 734 | def __lt__(self, other): |
| 735 | if isinstance(other, Object): |
| 736 | return self.arg < other.arg |
| 737 | return NotImplemented |
| 738 | def __hash__(self): |
| 739 | return hash(self.arg) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 740 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 741 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 742 | class MappingTestCase(TestBase): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 743 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 744 | COUNT = 10 |
| 745 | |
| 746 | def test_weak_values(self): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 747 | # |
| 748 | # This exercises d.copy(), d.items(), d[], del d[], len(d). |
| 749 | # |
| 750 | dict, objects = self.make_weak_valued_dict() |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 751 | for o in objects: |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 752 | self.assertEqual(weakref.getweakrefcount(o), 1) |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 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() |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 759 | self.assertEqual(items1, items2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 760 | "cloning of weak-valued dictionary did not work!") |
| 761 | del items1, items2 |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 762 | self.assertEqual(len(dict), self.COUNT) |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 763 | del objects[0] |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 764 | self.assertEqual(len(dict), self.COUNT - 1, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 765 | "deleting object did not cause dictionary update") |
| 766 | del objects, o |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 767 | self.assertEqual(len(dict), 0, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 768 | "deleting the values did not clear the dictionary") |
Fred Drake | 4fd06e0 | 2001-08-03 04:11:27 +0000 | [diff] [blame] | 769 | # 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 Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 774 | |
| 775 | def test_weak_keys(self): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 776 | # |
| 777 | # This exercises d.copy(), d.items(), d[] = v, d[], del d[], |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 778 | # len(d), k in d. |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 779 | # |
| 780 | dict, objects = self.make_weak_keyed_dict() |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 781 | 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() |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 788 | self.assertEqual(set(items1), set(items2), |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 789 | "cloning of weak-keyed dictionary did not work!") |
| 790 | del items1, items2 |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 791 | self.assertEqual(len(dict), self.COUNT) |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 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 Drake | 752eda4 | 2001-11-06 16:38:34 +0000 | [diff] [blame] | 798 | o = Object(42) |
| 799 | dict[o] = "What is the meaning of the universe?" |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 800 | self.assert_(o in dict) |
| 801 | self.assert_(34 not in dict) |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 802 | |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 803 | def test_weak_keyed_iters(self): |
| 804 | dict, objects = self.make_weak_keyed_dict() |
| 805 | self.check_iters(dict) |
| 806 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 807 | # 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() |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 813 | self.assert_(ob in dict) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 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() |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 824 | self.assert_(ob in dict) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 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 Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 830 | def test_weak_valued_iters(self): |
| 831 | dict, objects = self.make_weak_valued_dict() |
| 832 | self.check_iters(dict) |
| 833 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 834 | # 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 Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 855 | def check_iters(self, dict): |
| 856 | # item iterator: |
| 857 | items = dict.items() |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 858 | for item in dict.items(): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 859 | items.remove(item) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 860 | self.assert_(len(items) == 0, "items() did not touch all items") |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 861 | |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 862 | # key iterator, via __iter__(): |
Guido van Rossum | 07f2436 | 2007-02-11 22:59:48 +0000 | [diff] [blame] | 863 | keys = list(dict.keys()) |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 864 | for k in dict: |
| 865 | keys.remove(k) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 866 | self.assert_(len(keys) == 0, "__iter__() did not touch all keys") |
| 867 | |
| 868 | # key iterator, via iterkeys(): |
Guido van Rossum | 07f2436 | 2007-02-11 22:59:48 +0000 | [diff] [blame] | 869 | keys = list(dict.keys()) |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 870 | for k in dict.keys(): |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 871 | keys.remove(k) |
| 872 | self.assert_(len(keys) == 0, "iterkeys() did not touch all keys") |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 873 | |
| 874 | # value iterator: |
Guido van Rossum | 07f2436 | 2007-02-11 22:59:48 +0000 | [diff] [blame] | 875 | values = list(dict.values()) |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 876 | for v in dict.values(): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 877 | values.remove(v) |
Fred Drake | f425b1e | 2003-07-14 21:37:17 +0000 | [diff] [blame] | 878 | self.assert_(len(values) == 0, |
| 879 | "itervalues() did not touch all values") |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 880 | |
Guido van Rossum | 009afb7 | 2002-06-10 20:00:52 +0000 | [diff] [blame] | 881 | def test_make_weak_keyed_dict_from_dict(self): |
| 882 | o = Object(3) |
| 883 | dict = weakref.WeakKeyDictionary({o:364}) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 884 | self.assertEqual(dict[o], 364) |
Guido van Rossum | 009afb7 | 2002-06-10 20:00:52 +0000 | [diff] [blame] | 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) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 890 | self.assertEqual(dict[o], 364) |
Guido van Rossum | 009afb7 | 2002-06-10 20:00:52 +0000 | [diff] [blame] | 891 | |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 892 | def make_weak_keyed_dict(self): |
| 893 | dict = weakref.WeakKeyDictionary() |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 894 | objects = list(map(Object, range(self.COUNT))) |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 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() |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 901 | objects = list(map(Object, range(self.COUNT))) |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 902 | for o in objects: |
| 903 | dict[o.arg] = o |
| 904 | return dict, objects |
| 905 | |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 906 | def check_popitem(self, klass, key1, value1, key2, value2): |
| 907 | weakdict = klass() |
| 908 | weakdict[key1] = value1 |
| 909 | weakdict[key2] = value2 |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 910 | self.assertEqual(len(weakdict), 2) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 911 | k, v = weakdict.popitem() |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 912 | self.assertEqual(len(weakdict), 1) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 913 | if k is key1: |
| 914 | self.assert_(v is value1) |
| 915 | else: |
| 916 | self.assert_(v is value2) |
| 917 | k, v = weakdict.popitem() |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 918 | self.assertEqual(len(weakdict), 0) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 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) |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 939 | self.assert_(key in weakdict) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 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) |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 945 | self.assert_(key in weakdict) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 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 Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 957 | def check_update(self, klass, dict): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 958 | # |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 959 | # This exercises d.update(), len(d), d.keys(), k in d, |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 960 | # d.get(), d[]. |
| 961 | # |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 962 | weakdict = klass() |
| 963 | weakdict.update(dict) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 964 | self.assertEqual(len(weakdict), len(dict)) |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 965 | for k in weakdict.keys(): |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 966 | self.assert_(k in dict, |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 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(): |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 972 | self.assert_(k in weakdict, |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 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 Drake | ccc7562 | 2001-09-06 14:52:39 +0000 | [diff] [blame] | 986 | 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' |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 992 | self.assertEqual(len(d), 2) |
Fred Drake | ccc7562 | 2001-09-06 14:52:39 +0000 | [diff] [blame] | 993 | del d[o1] |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 994 | self.assertEqual(len(d), 1) |
| 995 | self.assertEqual(d.keys(), [o2]) |
Fred Drake | ccc7562 | 2001-09-06 14:52:39 +0000 | [diff] [blame] | 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 |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1003 | self.assertEqual(len(d), 2) |
Fred Drake | ccc7562 | 2001-09-06 14:52:39 +0000 | [diff] [blame] | 1004 | del d['something'] |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1005 | self.assertEqual(len(d), 1) |
Fred Drake | ccc7562 | 2001-09-06 14:52:39 +0000 | [diff] [blame] | 1006 | self.assert_(d.items() == [('something else', o2)]) |
| 1007 | |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 1008 | 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 Peters | 50d8b8b | 2003-05-25 17:44:31 +0000 | [diff] [blame] | 1012 | # KeyError. It didn't before 2.3. |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 1013 | self.assertRaises(KeyError, d.__delitem__, o) |
Tim Peters | 50d8b8b | 2003-05-25 17:44:31 +0000 | [diff] [blame] | 1014 | 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 Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 1021 | |
| 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 Peters | 50d8b8b | 2003-05-25 17:44:31 +0000 | [diff] [blame] | 1051 | |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 1052 | # 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 Peters | 50d8b8b | 2003-05-25 17:44:31 +0000 | [diff] [blame] | 1057 | # key. After this was fixed, it just deletes the last object *our* |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 1058 | # "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örwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 1067 | from test import mapping_tests |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 1068 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 1069 | class WeakValueDictionaryTestCase(mapping_tests.BasicTestMappingProtocol): |
Fred Drake | f425b1e | 2003-07-14 21:37:17 +0000 | [diff] [blame] | 1070 | """Check that WeakValueDictionary conforms to the mapping protocol""" |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 1071 | __ref = {"key1":Object(1), "key2":Object(2), "key3":Object(3)} |
Walter Dörwald | 118f931 | 2004-06-02 18:42:25 +0000 | [diff] [blame] | 1072 | type2test = weakref.WeakValueDictionary |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 1073 | def _reference(self): |
| 1074 | return self.__ref.copy() |
| 1075 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 1076 | class WeakKeyDictionaryTestCase(mapping_tests.BasicTestMappingProtocol): |
Fred Drake | f425b1e | 2003-07-14 21:37:17 +0000 | [diff] [blame] | 1077 | """Check that WeakKeyDictionary conforms to the mapping protocol""" |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 1078 | __ref = {Object("key1"):1, Object("key2"):2, Object("key3"):3} |
Walter Dörwald | 118f931 | 2004-06-02 18:42:25 +0000 | [diff] [blame] | 1079 | type2test = weakref.WeakKeyDictionary |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 1080 | def _reference(self): |
| 1081 | return self.__ref.copy() |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 1082 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1083 | libreftest = """ Doctest for examples in the library reference: weakref.rst |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1084 | |
| 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) |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1091 | >>> print(r() is obj) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1092 | True |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1093 | |
| 1094 | >>> import weakref |
| 1095 | >>> class Object: |
| 1096 | ... pass |
| 1097 | ... |
| 1098 | >>> o = Object() |
| 1099 | >>> r = weakref.ref(o) |
| 1100 | >>> o2 = r() |
| 1101 | >>> o is o2 |
| 1102 | True |
| 1103 | >>> del o, o2 |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1104 | >>> print(r()) |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1105 | None |
| 1106 | |
| 1107 | >>> import weakref |
| 1108 | >>> class ExtendedRef(weakref.ref): |
| 1109 | ... def __init__(self, ob, callback=None, **annotations): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1110 | ... super().__init__(ob, callback) |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1111 | ... self.__counter = 0 |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 1112 | ... for k, v in annotations.items(): |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 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 | ... ''' |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1118 | ... ob = super().__call__() |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1119 | ... if ob is not None: |
| 1120 | ... self.__counter += 1 |
| 1121 | ... ob = (ob, self.__counter) |
| 1122 | ... return ob |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1123 | ... |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 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 |
| 1130 | 1 |
| 1131 | >>> r.bar |
| 1132 | 'baz' |
| 1133 | >>> r()[1] |
| 1134 | 1 |
| 1135 | >>> r()[1] |
| 1136 | 2 |
| 1137 | >>> r()[0] is a |
| 1138 | True |
| 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 |
| 1154 | True |
| 1155 | >>> del a |
| 1156 | >>> try: |
| 1157 | ... id2obj(a_id) |
| 1158 | ... except KeyError: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1159 | ... print('OK') |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1160 | ... else: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1161 | ... print('WeakValueDictionary error') |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1162 | OK |
| 1163 | |
| 1164 | """ |
| 1165 | |
| 1166 | __test__ = {'libreftest' : libreftest} |
| 1167 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 1168 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1169 | support.run_unittest( |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1170 | ReferencesTestCase, |
| 1171 | MappingTestCase, |
| 1172 | WeakValueDictionaryTestCase, |
Fred Drake | f425b1e | 2003-07-14 21:37:17 +0000 | [diff] [blame] | 1173 | WeakKeyDictionaryTestCase, |
| 1174 | ) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1175 | support.run_doctest(sys.modules[__name__]) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 1176 | |
| 1177 | |
| 1178 | if __name__ == "__main__": |
| 1179 | test_main() |