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 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 68 | self.assertTrue(ref1() is None, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 69 | "expected reference to be invalidated") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 70 | self.assertTrue(ref2() is None, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 71 | "expected reference to be invalidated") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 72 | self.assertTrue(self.cbcalled == 2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 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) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 111 | self.assertTrue(ref() is not None, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 112 | "weak reference to live object should be live") |
| 113 | o2 = ref() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 114 | self.assertTrue(o is o2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 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 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 122 | self.assertTrue(self.cbcalled == 1, |
Fred Drake | 705088e | 2001-04-13 17:18:15 +0000 | [diff] [blame] | 123 | "callback did not properly set 'cbcalled'") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 124 | self.assertTrue(ref() is None, |
Fred Drake | 705088e | 2001-04-13 17:18:15 +0000 | [diff] [blame] | 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) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 134 | self.assertTrue(ref1 is ref2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 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) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 141 | self.assertTrue(ref1 is ref2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 142 | "reference object w/out callback should be re-used") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 143 | self.assertTrue(weakref.getweakrefcount(o) == 2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 144 | "wrong weak ref count for object") |
| 145 | del proxy |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 146 | self.assertTrue(weakref.getweakrefcount(o) == 1, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 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) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 154 | self.assertTrue(proxy1 is proxy2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 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) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 163 | self.assertFalse(p, "proxy for empty UserList should be false") |
Fred Drake | 5935ff0 | 2001-12-19 16:54:23 +0000 | [diff] [blame] | 164 | p.append(12) |
| 165 | self.assertEqual(len(L), 1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 166 | self.assertTrue(p, "proxy for non-empty UserList should be true") |
Fred Drake | 5935ff0 | 2001-12-19 16:54:23 +0000 | [diff] [blame] | 167 | p[:] = [2, 3] |
| 168 | self.assertEqual(len(L), 2) |
| 169 | self.assertEqual(len(p), 2) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 170 | self.assertTrue(3 in p, |
Fred Drake | f425b1e | 2003-07-14 21:37:17 +0000 | [diff] [blame] | 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) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 224 | self.assertTrue(p1 is p2, "both callbacks were None in the C API") |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 225 | del p1, p2 |
| 226 | p1 = makeref(o) |
| 227 | p2 = makeref(o, None) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 228 | self.assertTrue(p1 is p2, "callbacks were NULL, None in the C API") |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 229 | del p1, p2 |
| 230 | p1 = makeref(o) |
| 231 | p2 = makeref(o) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 232 | self.assertTrue(p1 is p2, "both callbacks were NULL in the C API") |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 233 | del p1, p2 |
| 234 | p1 = makeref(o, None) |
| 235 | p2 = makeref(o) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 236 | self.assertTrue(p1 is p2, "callbacks were None, NULL in the C API") |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 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 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 244 | self.assertTrue(type(ref1) is weakref.CallableProxyType, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 245 | "proxy is not of callable type") |
| 246 | ref1('twinkies!') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 247 | self.assertTrue(o.bar == 'twinkies!', |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 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.') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 250 | self.assertTrue(o.bar == 'Splat.', |
Fred Drake | 3bb4d21 | 2001-10-18 19:28:29 +0000 | [diff] [blame] | 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 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 261 | self.assertTrue(proxy.foo == 1, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 262 | "proxy does not reflect attribute addition") |
| 263 | o.foo = 2 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 264 | self.assertTrue(proxy.foo == 2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 265 | "proxy does not reflect attribute modification") |
| 266 | del o.foo |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 267 | self.assertTrue(not hasattr(proxy, 'foo'), |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 268 | "proxy does not reflect attribute removal") |
| 269 | |
| 270 | proxy.foo = 1 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 271 | self.assertTrue(o.foo == 1, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 272 | "object does not reflect attribute addition via proxy") |
| 273 | proxy.foo = 2 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 274 | self.assertTrue( |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 275 | o.foo == 2, |
| 276 | "object does not reflect attribute modification via proxy") |
| 277 | del proxy.foo |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 278 | self.assertTrue(not hasattr(o, 'foo'), |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 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) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 302 | self.assertTrue(weakref.getweakrefcount(o) == 2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 303 | "got wrong number of weak reference objects") |
| 304 | |
| 305 | proxy1 = weakref.proxy(o) |
| 306 | proxy2 = weakref.proxy(o, self.callback) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 307 | self.assertTrue(weakref.getweakrefcount(o) == 4, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 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 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 311 | self.assertTrue(weakref.getweakrefcount(o) == 0, |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 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 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 316 | self.assertTrue(weakref.getweakrefcount(1) == 0, |
Walter Dörwald | b167b04 | 2003-12-11 12:34:05 +0000 | [diff] [blame] | 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 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 324 | self.assertTrue(weakref.getweakrefs(o) == [ref2], |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 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 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 331 | self.assertTrue(weakref.getweakrefs(o) == [ref1], |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 332 | "list of refs does not match") |
| 333 | |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 334 | del ref1 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 335 | self.assertTrue(weakref.getweakrefs(o) == [], |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 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 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 339 | self.assertTrue(weakref.getweakrefs(1) == [], |
Walter Dörwald | b167b04 | 2003-12-11 12:34:05 +0000 | [diff] [blame] | 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) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 347 | self.assertTrue(p + 1.0 == 3.0) |
| 348 | self.assertTrue(1.0 + p == 3.0) # this used to SEGV |
Fred Drake | 39c27f1 | 2001-10-18 18:06:05 +0000 | [diff] [blame] | 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 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 601 | self.assertTrue(external_wr() is callback) |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 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 | |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 663 | def test_init(self): |
| 664 | # Issue 3634 |
| 665 | # <weakref to class>.__init__() doesn't check errors correctly |
| 666 | r = weakref.ref(Exception) |
| 667 | self.assertRaises(TypeError, r.__init__, 0, 0, 0, 0, 0) |
| 668 | # No exception should be raised here |
| 669 | gc.collect() |
| 670 | |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 671 | |
Amaury Forgeot d'Arc | c856c7a | 2008-06-16 19:50:09 +0000 | [diff] [blame] | 672 | class SubclassableWeakrefTestCase(TestBase): |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 673 | |
| 674 | def test_subclass_refs(self): |
| 675 | class MyRef(weakref.ref): |
| 676 | def __init__(self, ob, callback=None, value=42): |
| 677 | self.value = value |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 678 | super().__init__(ob, callback) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 679 | def __call__(self): |
| 680 | self.called = True |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 681 | return super().__call__() |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 682 | o = Object("foo") |
| 683 | mr = MyRef(o, value=24) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 684 | self.assertTrue(mr() is o) |
| 685 | self.assertTrue(mr.called) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 686 | self.assertEqual(mr.value, 24) |
| 687 | del o |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 688 | self.assertTrue(mr() is None) |
| 689 | self.assertTrue(mr.called) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 690 | |
| 691 | def test_subclass_refs_dont_replace_standard_refs(self): |
| 692 | class MyRef(weakref.ref): |
| 693 | pass |
| 694 | o = Object(42) |
| 695 | r1 = MyRef(o) |
| 696 | r2 = weakref.ref(o) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 697 | self.assertTrue(r1 is not r2) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 698 | self.assertEqual(weakref.getweakrefs(o), [r2, r1]) |
| 699 | self.assertEqual(weakref.getweakrefcount(o), 2) |
| 700 | r3 = MyRef(o) |
| 701 | self.assertEqual(weakref.getweakrefcount(o), 3) |
| 702 | refs = weakref.getweakrefs(o) |
| 703 | self.assertEqual(len(refs), 3) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 704 | self.assertTrue(r2 is refs[0]) |
| 705 | self.assertTrue(r1 in refs[1:]) |
| 706 | self.assertTrue(r3 in refs[1:]) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 707 | |
| 708 | def test_subclass_refs_dont_conflate_callbacks(self): |
| 709 | class MyRef(weakref.ref): |
| 710 | pass |
| 711 | o = Object(42) |
| 712 | r1 = MyRef(o, id) |
| 713 | r2 = MyRef(o, str) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 714 | self.assertTrue(r1 is not r2) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 715 | refs = weakref.getweakrefs(o) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 716 | self.assertTrue(r1 in refs) |
| 717 | self.assertTrue(r2 in refs) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 718 | |
| 719 | def test_subclass_refs_with_slots(self): |
| 720 | class MyRef(weakref.ref): |
| 721 | __slots__ = "slot1", "slot2" |
| 722 | def __new__(type, ob, callback, slot1, slot2): |
| 723 | return weakref.ref.__new__(type, ob, callback) |
| 724 | def __init__(self, ob, callback, slot1, slot2): |
| 725 | self.slot1 = slot1 |
| 726 | self.slot2 = slot2 |
| 727 | def meth(self): |
| 728 | return self.slot1 + self.slot2 |
| 729 | o = Object(42) |
| 730 | r = MyRef(o, None, "abc", "def") |
| 731 | self.assertEqual(r.slot1, "abc") |
| 732 | self.assertEqual(r.slot2, "def") |
| 733 | self.assertEqual(r.meth(), "abcdef") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 734 | self.assertFalse(hasattr(r, "__dict__")) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 735 | |
Amaury Forgeot d'Arc | c856c7a | 2008-06-16 19:50:09 +0000 | [diff] [blame] | 736 | def test_subclass_refs_with_cycle(self): |
| 737 | # Bug #3110 |
| 738 | # An instance of a weakref subclass can have attributes. |
| 739 | # If such a weakref holds the only strong reference to the object, |
| 740 | # deleting the weakref will delete the object. In this case, |
| 741 | # the callback must not be called, because the ref object is |
| 742 | # being deleted. |
| 743 | class MyRef(weakref.ref): |
| 744 | pass |
| 745 | |
| 746 | # Use a local callback, for "regrtest -R::" |
| 747 | # to detect refcounting problems |
| 748 | def callback(w): |
| 749 | self.cbcalled += 1 |
| 750 | |
| 751 | o = C() |
| 752 | r1 = MyRef(o, callback) |
| 753 | r1.o = o |
| 754 | del o |
| 755 | |
| 756 | del r1 # Used to crash here |
| 757 | |
| 758 | self.assertEqual(self.cbcalled, 0) |
| 759 | |
| 760 | # Same test, with two weakrefs to the same object |
| 761 | # (since code paths are different) |
| 762 | o = C() |
| 763 | r1 = MyRef(o, callback) |
| 764 | r2 = MyRef(o, callback) |
| 765 | r1.r = r2 |
| 766 | r2.o = o |
| 767 | del o |
| 768 | del r2 |
| 769 | |
| 770 | del r1 # Used to crash here |
| 771 | |
| 772 | self.assertEqual(self.cbcalled, 0) |
| 773 | |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 774 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 775 | class Object: |
| 776 | def __init__(self, arg): |
| 777 | self.arg = arg |
| 778 | def __repr__(self): |
| 779 | return "<Object %r>" % self.arg |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 780 | def __lt__(self, other): |
| 781 | if isinstance(other, Object): |
| 782 | return self.arg < other.arg |
| 783 | return NotImplemented |
| 784 | def __hash__(self): |
| 785 | return hash(self.arg) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 786 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 787 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 788 | class MappingTestCase(TestBase): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 789 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 790 | COUNT = 10 |
| 791 | |
| 792 | def test_weak_values(self): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 793 | # |
| 794 | # This exercises d.copy(), d.items(), d[], del d[], len(d). |
| 795 | # |
| 796 | dict, objects = self.make_weak_valued_dict() |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 797 | for o in objects: |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 798 | self.assertEqual(weakref.getweakrefcount(o), 1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 799 | self.assertTrue(o is dict[o.arg], |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 800 | "wrong object returned by weak dict!") |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 801 | items1 = list(dict.items()) |
| 802 | items2 = list(dict.copy().items()) |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 803 | items1.sort() |
| 804 | items2.sort() |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 805 | self.assertEqual(items1, items2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 806 | "cloning of weak-valued dictionary did not work!") |
| 807 | del items1, items2 |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 808 | self.assertEqual(len(dict), self.COUNT) |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 809 | del objects[0] |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 810 | self.assertEqual(len(dict), self.COUNT - 1, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 811 | "deleting object did not cause dictionary update") |
| 812 | del objects, o |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 813 | self.assertEqual(len(dict), 0, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 814 | "deleting the values did not clear the dictionary") |
Fred Drake | 4fd06e0 | 2001-08-03 04:11:27 +0000 | [diff] [blame] | 815 | # regression on SF bug #447152: |
| 816 | dict = weakref.WeakValueDictionary() |
| 817 | self.assertRaises(KeyError, dict.__getitem__, 1) |
| 818 | dict[2] = C() |
| 819 | self.assertRaises(KeyError, dict.__getitem__, 2) |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 820 | |
| 821 | def test_weak_keys(self): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 822 | # |
| 823 | # This exercises d.copy(), d.items(), d[] = v, d[], del d[], |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 824 | # len(d), k in d. |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 825 | # |
| 826 | dict, objects = self.make_weak_keyed_dict() |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 827 | for o in objects: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 828 | self.assertTrue(weakref.getweakrefcount(o) == 1, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 829 | "wrong number of weak references to %r!" % o) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 830 | self.assertTrue(o.arg is dict[o], |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 831 | "wrong object returned by weak dict!") |
| 832 | items1 = dict.items() |
| 833 | items2 = dict.copy().items() |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 834 | self.assertEqual(set(items1), set(items2), |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 835 | "cloning of weak-keyed dictionary did not work!") |
| 836 | del items1, items2 |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 837 | self.assertEqual(len(dict), self.COUNT) |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 838 | del objects[0] |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 839 | self.assertTrue(len(dict) == (self.COUNT - 1), |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 840 | "deleting object did not cause dictionary update") |
| 841 | del objects, o |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 842 | self.assertTrue(len(dict) == 0, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 843 | "deleting the keys did not clear the dictionary") |
Fred Drake | 752eda4 | 2001-11-06 16:38:34 +0000 | [diff] [blame] | 844 | o = Object(42) |
| 845 | dict[o] = "What is the meaning of the universe?" |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 846 | self.assertTrue(o in dict) |
| 847 | self.assertTrue(34 not in dict) |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 848 | |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 849 | def test_weak_keyed_iters(self): |
| 850 | dict, objects = self.make_weak_keyed_dict() |
| 851 | self.check_iters(dict) |
| 852 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 853 | # Test keyrefs() |
| 854 | refs = dict.keyrefs() |
| 855 | self.assertEqual(len(refs), len(objects)) |
| 856 | objects2 = list(objects) |
| 857 | for wr in refs: |
| 858 | ob = wr() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 859 | self.assertTrue(ob in dict) |
| 860 | self.assertTrue(ob in dict) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 861 | self.assertEqual(ob.arg, dict[ob]) |
| 862 | objects2.remove(ob) |
| 863 | self.assertEqual(len(objects2), 0) |
| 864 | |
| 865 | # Test iterkeyrefs() |
| 866 | objects2 = list(objects) |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 867 | self.assertEqual(len(list(dict.keyrefs())), len(objects)) |
| 868 | for wr in dict.keyrefs(): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 869 | ob = wr() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 870 | self.assertTrue(ob in dict) |
| 871 | self.assertTrue(ob in dict) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 872 | self.assertEqual(ob.arg, dict[ob]) |
| 873 | objects2.remove(ob) |
| 874 | self.assertEqual(len(objects2), 0) |
| 875 | |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 876 | def test_weak_valued_iters(self): |
| 877 | dict, objects = self.make_weak_valued_dict() |
| 878 | self.check_iters(dict) |
| 879 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 880 | # Test valuerefs() |
| 881 | refs = dict.valuerefs() |
| 882 | self.assertEqual(len(refs), len(objects)) |
| 883 | objects2 = list(objects) |
| 884 | for wr in refs: |
| 885 | ob = wr() |
| 886 | self.assertEqual(ob, dict[ob.arg]) |
| 887 | self.assertEqual(ob.arg, dict[ob.arg].arg) |
| 888 | objects2.remove(ob) |
| 889 | self.assertEqual(len(objects2), 0) |
| 890 | |
| 891 | # Test itervaluerefs() |
| 892 | objects2 = list(objects) |
| 893 | self.assertEqual(len(list(dict.itervaluerefs())), len(objects)) |
| 894 | for wr in dict.itervaluerefs(): |
| 895 | ob = wr() |
| 896 | self.assertEqual(ob, dict[ob.arg]) |
| 897 | self.assertEqual(ob.arg, dict[ob.arg].arg) |
| 898 | objects2.remove(ob) |
| 899 | self.assertEqual(len(objects2), 0) |
| 900 | |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 901 | def check_iters(self, dict): |
| 902 | # item iterator: |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 903 | items = list(dict.items()) |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 904 | for item in dict.items(): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 905 | items.remove(item) |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 906 | self.assertFalse(items, "items() did not touch all items") |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 907 | |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 908 | # key iterator, via __iter__(): |
Guido van Rossum | 07f2436 | 2007-02-11 22:59:48 +0000 | [diff] [blame] | 909 | keys = list(dict.keys()) |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 910 | for k in dict: |
| 911 | keys.remove(k) |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 912 | self.assertFalse(keys, "__iter__() did not touch all keys") |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 913 | |
| 914 | # key iterator, via iterkeys(): |
Guido van Rossum | 07f2436 | 2007-02-11 22:59:48 +0000 | [diff] [blame] | 915 | keys = list(dict.keys()) |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 916 | for k in dict.keys(): |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 917 | keys.remove(k) |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 918 | self.assertFalse(keys, "iterkeys() did not touch all keys") |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 919 | |
| 920 | # value iterator: |
Guido van Rossum | 07f2436 | 2007-02-11 22:59:48 +0000 | [diff] [blame] | 921 | values = list(dict.values()) |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 922 | for v in dict.values(): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 923 | values.remove(v) |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 924 | self.assertFalse(values, |
Fred Drake | f425b1e | 2003-07-14 21:37:17 +0000 | [diff] [blame] | 925 | "itervalues() did not touch all values") |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 926 | |
Guido van Rossum | 009afb7 | 2002-06-10 20:00:52 +0000 | [diff] [blame] | 927 | def test_make_weak_keyed_dict_from_dict(self): |
| 928 | o = Object(3) |
| 929 | dict = weakref.WeakKeyDictionary({o:364}) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 930 | self.assertEqual(dict[o], 364) |
Guido van Rossum | 009afb7 | 2002-06-10 20:00:52 +0000 | [diff] [blame] | 931 | |
| 932 | def test_make_weak_keyed_dict_from_weak_keyed_dict(self): |
| 933 | o = Object(3) |
| 934 | dict = weakref.WeakKeyDictionary({o:364}) |
| 935 | dict2 = weakref.WeakKeyDictionary(dict) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 936 | self.assertEqual(dict[o], 364) |
Guido van Rossum | 009afb7 | 2002-06-10 20:00:52 +0000 | [diff] [blame] | 937 | |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 938 | def make_weak_keyed_dict(self): |
| 939 | dict = weakref.WeakKeyDictionary() |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 940 | objects = list(map(Object, range(self.COUNT))) |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 941 | for o in objects: |
| 942 | dict[o] = o.arg |
| 943 | return dict, objects |
| 944 | |
Antoine Pitrou | c06de47 | 2009-05-30 21:04:26 +0000 | [diff] [blame] | 945 | def test_make_weak_valued_dict_from_dict(self): |
| 946 | o = Object(3) |
| 947 | dict = weakref.WeakValueDictionary({364:o}) |
| 948 | self.assertEqual(dict[364], o) |
| 949 | |
| 950 | def test_make_weak_valued_dict_from_weak_valued_dict(self): |
| 951 | o = Object(3) |
| 952 | dict = weakref.WeakValueDictionary({364:o}) |
| 953 | dict2 = weakref.WeakValueDictionary(dict) |
| 954 | self.assertEqual(dict[364], o) |
| 955 | |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 956 | def make_weak_valued_dict(self): |
| 957 | dict = weakref.WeakValueDictionary() |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 958 | objects = list(map(Object, range(self.COUNT))) |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 959 | for o in objects: |
| 960 | dict[o.arg] = o |
| 961 | return dict, objects |
| 962 | |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 963 | def check_popitem(self, klass, key1, value1, key2, value2): |
| 964 | weakdict = klass() |
| 965 | weakdict[key1] = value1 |
| 966 | weakdict[key2] = value2 |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 967 | self.assertEqual(len(weakdict), 2) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 968 | k, v = weakdict.popitem() |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 969 | self.assertEqual(len(weakdict), 1) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 970 | if k is key1: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 971 | self.assertTrue(v is value1) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 972 | else: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 973 | self.assertTrue(v is value2) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 974 | k, v = weakdict.popitem() |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 975 | self.assertEqual(len(weakdict), 0) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 976 | if k is key1: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 977 | self.assertTrue(v is value1) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 978 | else: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 979 | self.assertTrue(v is value2) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 980 | |
| 981 | def test_weak_valued_dict_popitem(self): |
| 982 | self.check_popitem(weakref.WeakValueDictionary, |
| 983 | "key1", C(), "key2", C()) |
| 984 | |
| 985 | def test_weak_keyed_dict_popitem(self): |
| 986 | self.check_popitem(weakref.WeakKeyDictionary, |
| 987 | C(), "value 1", C(), "value 2") |
| 988 | |
| 989 | def check_setdefault(self, klass, key, value1, value2): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 990 | self.assertTrue(value1 is not value2, |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 991 | "invalid test" |
| 992 | " -- value parameters must be distinct objects") |
| 993 | weakdict = klass() |
| 994 | o = weakdict.setdefault(key, value1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 995 | self.assertTrue(o is value1) |
| 996 | self.assertTrue(key in weakdict) |
| 997 | self.assertTrue(weakdict.get(key) is value1) |
| 998 | self.assertTrue(weakdict[key] is value1) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 999 | |
| 1000 | o = weakdict.setdefault(key, value2) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1001 | self.assertTrue(o is value1) |
| 1002 | self.assertTrue(key in weakdict) |
| 1003 | self.assertTrue(weakdict.get(key) is value1) |
| 1004 | self.assertTrue(weakdict[key] is value1) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1005 | |
| 1006 | def test_weak_valued_dict_setdefault(self): |
| 1007 | self.check_setdefault(weakref.WeakValueDictionary, |
| 1008 | "key", C(), C()) |
| 1009 | |
| 1010 | def test_weak_keyed_dict_setdefault(self): |
| 1011 | self.check_setdefault(weakref.WeakKeyDictionary, |
| 1012 | C(), "value 1", "value 2") |
| 1013 | |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 1014 | def check_update(self, klass, dict): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1015 | # |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 1016 | # This exercises d.update(), len(d), d.keys(), k in d, |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1017 | # d.get(), d[]. |
| 1018 | # |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 1019 | weakdict = klass() |
| 1020 | weakdict.update(dict) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1021 | self.assertEqual(len(weakdict), len(dict)) |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 1022 | for k in weakdict.keys(): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1023 | self.assertTrue(k in dict, |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 1024 | "mysterious new key appeared in weak dict") |
| 1025 | v = dict.get(k) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1026 | self.assertTrue(v is weakdict[k]) |
| 1027 | self.assertTrue(v is weakdict.get(k)) |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 1028 | for k in dict.keys(): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1029 | self.assertTrue(k in weakdict, |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 1030 | "original key disappeared in weak dict") |
| 1031 | v = dict[k] |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1032 | self.assertTrue(v is weakdict[k]) |
| 1033 | self.assertTrue(v is weakdict.get(k)) |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 1034 | |
| 1035 | def test_weak_valued_dict_update(self): |
| 1036 | self.check_update(weakref.WeakValueDictionary, |
| 1037 | {1: C(), 'a': C(), C(): C()}) |
| 1038 | |
| 1039 | def test_weak_keyed_dict_update(self): |
| 1040 | self.check_update(weakref.WeakKeyDictionary, |
| 1041 | {C(): 1, C(): 2, C(): 3}) |
| 1042 | |
Fred Drake | ccc7562 | 2001-09-06 14:52:39 +0000 | [diff] [blame] | 1043 | def test_weak_keyed_delitem(self): |
| 1044 | d = weakref.WeakKeyDictionary() |
| 1045 | o1 = Object('1') |
| 1046 | o2 = Object('2') |
| 1047 | d[o1] = 'something' |
| 1048 | d[o2] = 'something' |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1049 | self.assertEqual(len(d), 2) |
Fred Drake | ccc7562 | 2001-09-06 14:52:39 +0000 | [diff] [blame] | 1050 | del d[o1] |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1051 | self.assertEqual(len(d), 1) |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 1052 | self.assertEqual(list(d.keys()), [o2]) |
Fred Drake | ccc7562 | 2001-09-06 14:52:39 +0000 | [diff] [blame] | 1053 | |
| 1054 | def test_weak_valued_delitem(self): |
| 1055 | d = weakref.WeakValueDictionary() |
| 1056 | o1 = Object('1') |
| 1057 | o2 = Object('2') |
| 1058 | d['something'] = o1 |
| 1059 | d['something else'] = o2 |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1060 | self.assertEqual(len(d), 2) |
Fred Drake | ccc7562 | 2001-09-06 14:52:39 +0000 | [diff] [blame] | 1061 | del d['something'] |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1062 | self.assertEqual(len(d), 1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1063 | self.assertTrue(list(d.items()) == [('something else', o2)]) |
Fred Drake | ccc7562 | 2001-09-06 14:52:39 +0000 | [diff] [blame] | 1064 | |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 1065 | def test_weak_keyed_bad_delitem(self): |
| 1066 | d = weakref.WeakKeyDictionary() |
| 1067 | o = Object('1') |
| 1068 | # An attempt to delete an object that isn't there should raise |
Tim Peters | 50d8b8b | 2003-05-25 17:44:31 +0000 | [diff] [blame] | 1069 | # KeyError. It didn't before 2.3. |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 1070 | self.assertRaises(KeyError, d.__delitem__, o) |
Tim Peters | 50d8b8b | 2003-05-25 17:44:31 +0000 | [diff] [blame] | 1071 | self.assertRaises(KeyError, d.__getitem__, o) |
| 1072 | |
| 1073 | # If a key isn't of a weakly referencable type, __getitem__ and |
| 1074 | # __setitem__ raise TypeError. __delitem__ should too. |
| 1075 | self.assertRaises(TypeError, d.__delitem__, 13) |
| 1076 | self.assertRaises(TypeError, d.__getitem__, 13) |
| 1077 | self.assertRaises(TypeError, d.__setitem__, 13, 13) |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 1078 | |
| 1079 | def test_weak_keyed_cascading_deletes(self): |
| 1080 | # SF bug 742860. For some reason, before 2.3 __delitem__ iterated |
| 1081 | # over the keys via self.data.iterkeys(). If things vanished from |
| 1082 | # the dict during this (or got added), that caused a RuntimeError. |
| 1083 | |
| 1084 | d = weakref.WeakKeyDictionary() |
| 1085 | mutate = False |
| 1086 | |
| 1087 | class C(object): |
| 1088 | def __init__(self, i): |
| 1089 | self.value = i |
| 1090 | def __hash__(self): |
| 1091 | return hash(self.value) |
| 1092 | def __eq__(self, other): |
| 1093 | if mutate: |
| 1094 | # Side effect that mutates the dict, by removing the |
| 1095 | # last strong reference to a key. |
| 1096 | del objs[-1] |
| 1097 | return self.value == other.value |
| 1098 | |
| 1099 | objs = [C(i) for i in range(4)] |
| 1100 | for o in objs: |
| 1101 | d[o] = o.value |
| 1102 | del o # now the only strong references to keys are in objs |
| 1103 | # Find the order in which iterkeys sees the keys. |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 1104 | objs = list(d.keys()) |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 1105 | # Reverse it, so that the iteration implementation of __delitem__ |
| 1106 | # has to keep looping to find the first object we delete. |
| 1107 | objs.reverse() |
Tim Peters | 50d8b8b | 2003-05-25 17:44:31 +0000 | [diff] [blame] | 1108 | |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 1109 | # Turn on mutation in C.__eq__. The first time thru the loop, |
| 1110 | # under the iterkeys() business the first comparison will delete |
| 1111 | # the last item iterkeys() would see, and that causes a |
| 1112 | # RuntimeError: dictionary changed size during iteration |
| 1113 | # when the iterkeys() loop goes around to try comparing the next |
Tim Peters | 50d8b8b | 2003-05-25 17:44:31 +0000 | [diff] [blame] | 1114 | # key. After this was fixed, it just deletes the last object *our* |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 1115 | # "for o in obj" loop would have gotten to. |
| 1116 | mutate = True |
| 1117 | count = 0 |
| 1118 | for o in objs: |
| 1119 | count += 1 |
| 1120 | del d[o] |
| 1121 | self.assertEqual(len(d), 0) |
| 1122 | self.assertEqual(count, 2) |
| 1123 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 1124 | from test import mapping_tests |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 1125 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 1126 | class WeakValueDictionaryTestCase(mapping_tests.BasicTestMappingProtocol): |
Fred Drake | f425b1e | 2003-07-14 21:37:17 +0000 | [diff] [blame] | 1127 | """Check that WeakValueDictionary conforms to the mapping protocol""" |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 1128 | __ref = {"key1":Object(1), "key2":Object(2), "key3":Object(3)} |
Walter Dörwald | 118f931 | 2004-06-02 18:42:25 +0000 | [diff] [blame] | 1129 | type2test = weakref.WeakValueDictionary |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 1130 | def _reference(self): |
| 1131 | return self.__ref.copy() |
| 1132 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 1133 | class WeakKeyDictionaryTestCase(mapping_tests.BasicTestMappingProtocol): |
Fred Drake | f425b1e | 2003-07-14 21:37:17 +0000 | [diff] [blame] | 1134 | """Check that WeakKeyDictionary conforms to the mapping protocol""" |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 1135 | __ref = {Object("key1"):1, Object("key2"):2, Object("key3"):3} |
Walter Dörwald | 118f931 | 2004-06-02 18:42:25 +0000 | [diff] [blame] | 1136 | type2test = weakref.WeakKeyDictionary |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 1137 | def _reference(self): |
| 1138 | return self.__ref.copy() |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 1139 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1140 | libreftest = """ Doctest for examples in the library reference: weakref.rst |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1141 | |
| 1142 | >>> import weakref |
| 1143 | >>> class Dict(dict): |
| 1144 | ... pass |
| 1145 | ... |
| 1146 | >>> obj = Dict(red=1, green=2, blue=3) # this object is weak referencable |
| 1147 | >>> r = weakref.ref(obj) |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1148 | >>> print(r() is obj) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1149 | True |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1150 | |
| 1151 | >>> import weakref |
| 1152 | >>> class Object: |
| 1153 | ... pass |
| 1154 | ... |
| 1155 | >>> o = Object() |
| 1156 | >>> r = weakref.ref(o) |
| 1157 | >>> o2 = r() |
| 1158 | >>> o is o2 |
| 1159 | True |
| 1160 | >>> del o, o2 |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1161 | >>> print(r()) |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1162 | None |
| 1163 | |
| 1164 | >>> import weakref |
| 1165 | >>> class ExtendedRef(weakref.ref): |
| 1166 | ... def __init__(self, ob, callback=None, **annotations): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1167 | ... super().__init__(ob, callback) |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1168 | ... self.__counter = 0 |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 1169 | ... for k, v in annotations.items(): |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1170 | ... setattr(self, k, v) |
| 1171 | ... def __call__(self): |
| 1172 | ... '''Return a pair containing the referent and the number of |
| 1173 | ... times the reference has been called. |
| 1174 | ... ''' |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1175 | ... ob = super().__call__() |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1176 | ... if ob is not None: |
| 1177 | ... self.__counter += 1 |
| 1178 | ... ob = (ob, self.__counter) |
| 1179 | ... return ob |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1180 | ... |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1181 | >>> class A: # not in docs from here, just testing the ExtendedRef |
| 1182 | ... pass |
| 1183 | ... |
| 1184 | >>> a = A() |
| 1185 | >>> r = ExtendedRef(a, foo=1, bar="baz") |
| 1186 | >>> r.foo |
| 1187 | 1 |
| 1188 | >>> r.bar |
| 1189 | 'baz' |
| 1190 | >>> r()[1] |
| 1191 | 1 |
| 1192 | >>> r()[1] |
| 1193 | 2 |
| 1194 | >>> r()[0] is a |
| 1195 | True |
| 1196 | |
| 1197 | |
| 1198 | >>> import weakref |
| 1199 | >>> _id2obj_dict = weakref.WeakValueDictionary() |
| 1200 | >>> def remember(obj): |
| 1201 | ... oid = id(obj) |
| 1202 | ... _id2obj_dict[oid] = obj |
| 1203 | ... return oid |
| 1204 | ... |
| 1205 | >>> def id2obj(oid): |
| 1206 | ... return _id2obj_dict[oid] |
| 1207 | ... |
| 1208 | >>> a = A() # from here, just testing |
| 1209 | >>> a_id = remember(a) |
| 1210 | >>> id2obj(a_id) is a |
| 1211 | True |
| 1212 | >>> del a |
| 1213 | >>> try: |
| 1214 | ... id2obj(a_id) |
| 1215 | ... except KeyError: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1216 | ... print('OK') |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1217 | ... else: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1218 | ... print('WeakValueDictionary error') |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1219 | OK |
| 1220 | |
| 1221 | """ |
| 1222 | |
| 1223 | __test__ = {'libreftest' : libreftest} |
| 1224 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 1225 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1226 | support.run_unittest( |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1227 | ReferencesTestCase, |
| 1228 | MappingTestCase, |
| 1229 | WeakValueDictionaryTestCase, |
Fred Drake | f425b1e | 2003-07-14 21:37:17 +0000 | [diff] [blame] | 1230 | WeakKeyDictionaryTestCase, |
Amaury Forgeot d'Arc | c856c7a | 2008-06-16 19:50:09 +0000 | [diff] [blame] | 1231 | SubclassableWeakrefTestCase, |
Fred Drake | f425b1e | 2003-07-14 21:37:17 +0000 | [diff] [blame] | 1232 | ) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1233 | support.run_doctest(sys.modules[__name__]) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 1234 | |
| 1235 | |
| 1236 | if __name__ == "__main__": |
| 1237 | test_main() |