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 |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 7 | import contextlib |
| 8 | import copy |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 9 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 10 | from test import support |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 11 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 12 | # Used in ReferencesTestCase.test_ref_created_during_del() . |
| 13 | ref_from_del = None |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 14 | |
| 15 | class C: |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 16 | def method(self): |
| 17 | pass |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 18 | |
| 19 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 20 | class Callable: |
| 21 | bar = None |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 22 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 23 | def __call__(self, x): |
| 24 | self.bar = x |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 25 | |
| 26 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 27 | def create_function(): |
| 28 | def f(): pass |
| 29 | return f |
| 30 | |
| 31 | def create_bound_method(): |
| 32 | return C().method |
| 33 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 34 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 35 | class TestBase(unittest.TestCase): |
| 36 | |
| 37 | def setUp(self): |
| 38 | self.cbcalled = 0 |
| 39 | |
| 40 | def callback(self, ref): |
| 41 | self.cbcalled += 1 |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 42 | |
| 43 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 44 | class ReferencesTestCase(TestBase): |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 45 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 46 | def test_basic_ref(self): |
| 47 | self.check_basic_ref(C) |
| 48 | self.check_basic_ref(create_function) |
| 49 | self.check_basic_ref(create_bound_method) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 50 | |
Fred Drake | 43735da | 2002-04-11 03:59:42 +0000 | [diff] [blame] | 51 | # Just make sure the tp_repr handler doesn't raise an exception. |
| 52 | # Live reference: |
| 53 | o = C() |
| 54 | wr = weakref.ref(o) |
Brett Cannon | 0b70cca | 2006-08-25 02:59:59 +0000 | [diff] [blame] | 55 | repr(wr) |
Fred Drake | 43735da | 2002-04-11 03:59:42 +0000 | [diff] [blame] | 56 | # Dead reference: |
| 57 | del o |
Brett Cannon | 0b70cca | 2006-08-25 02:59:59 +0000 | [diff] [blame] | 58 | repr(wr) |
Fred Drake | 43735da | 2002-04-11 03:59:42 +0000 | [diff] [blame] | 59 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 60 | def test_basic_callback(self): |
| 61 | self.check_basic_callback(C) |
| 62 | self.check_basic_callback(create_function) |
| 63 | self.check_basic_callback(create_bound_method) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 64 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 65 | def test_multiple_callbacks(self): |
| 66 | o = C() |
| 67 | ref1 = weakref.ref(o, self.callback) |
| 68 | ref2 = weakref.ref(o, self.callback) |
| 69 | del o |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 70 | self.assertTrue(ref1() 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(ref2() is None, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 73 | "expected reference to be invalidated") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 74 | self.assertTrue(self.cbcalled == 2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 75 | "callback not called the right number of times") |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 76 | |
Fred Drake | 705088e | 2001-04-13 17:18:15 +0000 | [diff] [blame] | 77 | def test_multiple_selfref_callbacks(self): |
Guido van Rossum | 9eee554 | 2002-08-22 20:21:30 +0000 | [diff] [blame] | 78 | # Make sure all references are invalidated before callbacks are called |
Fred Drake | 705088e | 2001-04-13 17:18:15 +0000 | [diff] [blame] | 79 | # |
| 80 | # What's important here is that we're using the first |
| 81 | # reference in the callback invoked on the second reference |
| 82 | # (the most recently created ref is cleaned up first). This |
| 83 | # tests that all references to the object are invalidated |
| 84 | # before any of the callbacks are invoked, so that we only |
| 85 | # have one invocation of _weakref.c:cleanup_helper() active |
| 86 | # for a particular object at a time. |
| 87 | # |
| 88 | def callback(object, self=self): |
| 89 | self.ref() |
| 90 | c = C() |
| 91 | self.ref = weakref.ref(c, callback) |
| 92 | ref1 = weakref.ref(c, callback) |
| 93 | del c |
| 94 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 95 | def test_proxy_ref(self): |
| 96 | o = C() |
| 97 | o.bar = 1 |
| 98 | ref1 = weakref.proxy(o, self.callback) |
| 99 | ref2 = weakref.proxy(o, self.callback) |
| 100 | del o |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 101 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 102 | def check(proxy): |
| 103 | proxy.bar |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 104 | |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 105 | self.assertRaises(ReferenceError, check, ref1) |
| 106 | self.assertRaises(ReferenceError, check, ref2) |
| 107 | self.assertRaises(ReferenceError, bool, weakref.proxy(C())) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 108 | self.assertEqual(self.cbcalled, 2) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 109 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 110 | def check_basic_ref(self, factory): |
| 111 | o = factory() |
| 112 | ref = weakref.ref(o) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 113 | self.assertTrue(ref() is not None, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 114 | "weak reference to live object should be live") |
| 115 | o2 = ref() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 116 | self.assertTrue(o is o2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 117 | "<ref>() should return original object if live") |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 118 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 119 | def check_basic_callback(self, factory): |
| 120 | self.cbcalled = 0 |
| 121 | o = factory() |
| 122 | ref = weakref.ref(o, self.callback) |
| 123 | del o |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 124 | self.assertTrue(self.cbcalled == 1, |
Fred Drake | 705088e | 2001-04-13 17:18:15 +0000 | [diff] [blame] | 125 | "callback did not properly set 'cbcalled'") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 126 | self.assertTrue(ref() is None, |
Fred Drake | 705088e | 2001-04-13 17:18:15 +0000 | [diff] [blame] | 127 | "ref2 should be dead after deleting object reference") |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 128 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 129 | def test_ref_reuse(self): |
| 130 | o = C() |
| 131 | ref1 = weakref.ref(o) |
| 132 | # create a proxy to make sure that there's an intervening creation |
| 133 | # between these two; it should make no difference |
| 134 | proxy = weakref.proxy(o) |
| 135 | ref2 = weakref.ref(o) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 136 | self.assertTrue(ref1 is ref2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 137 | "reference object w/out callback should be re-used") |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 138 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 139 | o = C() |
| 140 | proxy = weakref.proxy(o) |
| 141 | ref1 = weakref.ref(o) |
| 142 | ref2 = weakref.ref(o) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 143 | self.assertTrue(ref1 is ref2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 144 | "reference object w/out callback should be re-used") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 145 | self.assertTrue(weakref.getweakrefcount(o) == 2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 146 | "wrong weak ref count for object") |
| 147 | del proxy |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 148 | self.assertTrue(weakref.getweakrefcount(o) == 1, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 149 | "wrong weak ref count for object after deleting proxy") |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 150 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 151 | def test_proxy_reuse(self): |
| 152 | o = C() |
| 153 | proxy1 = weakref.proxy(o) |
| 154 | ref = weakref.ref(o) |
| 155 | proxy2 = weakref.proxy(o) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 156 | self.assertTrue(proxy1 is proxy2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 157 | "proxy object w/out callback should have been re-used") |
| 158 | |
| 159 | def test_basic_proxy(self): |
| 160 | o = C() |
| 161 | self.check_proxy(o, weakref.proxy(o)) |
| 162 | |
Raymond Hettinger | 53dbe39 | 2008-02-12 20:03:09 +0000 | [diff] [blame] | 163 | L = collections.UserList() |
Fred Drake | 5935ff0 | 2001-12-19 16:54:23 +0000 | [diff] [blame] | 164 | p = weakref.proxy(L) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 165 | self.assertFalse(p, "proxy for empty UserList should be false") |
Fred Drake | 5935ff0 | 2001-12-19 16:54:23 +0000 | [diff] [blame] | 166 | p.append(12) |
| 167 | self.assertEqual(len(L), 1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 168 | self.assertTrue(p, "proxy for non-empty UserList should be true") |
Fred Drake | 5935ff0 | 2001-12-19 16:54:23 +0000 | [diff] [blame] | 169 | p[:] = [2, 3] |
| 170 | self.assertEqual(len(L), 2) |
| 171 | self.assertEqual(len(p), 2) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 172 | self.assertIn(3, p, "proxy didn't support __contains__() properly") |
Fred Drake | 5935ff0 | 2001-12-19 16:54:23 +0000 | [diff] [blame] | 173 | p[1] = 5 |
| 174 | self.assertEqual(L[1], 5) |
| 175 | self.assertEqual(p[1], 5) |
Raymond Hettinger | 53dbe39 | 2008-02-12 20:03:09 +0000 | [diff] [blame] | 176 | L2 = collections.UserList(L) |
Fred Drake | 5935ff0 | 2001-12-19 16:54:23 +0000 | [diff] [blame] | 177 | p2 = weakref.proxy(L2) |
| 178 | self.assertEqual(p, p2) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 179 | ## self.assertEqual(repr(L2), repr(p2)) |
Raymond Hettinger | 53dbe39 | 2008-02-12 20:03:09 +0000 | [diff] [blame] | 180 | L3 = collections.UserList(range(10)) |
Fred Drake | 43735da | 2002-04-11 03:59:42 +0000 | [diff] [blame] | 181 | p3 = weakref.proxy(L3) |
| 182 | self.assertEqual(L3[:], p3[:]) |
| 183 | self.assertEqual(L3[5:], p3[5:]) |
| 184 | self.assertEqual(L3[:5], p3[:5]) |
| 185 | self.assertEqual(L3[2:5], p3[2:5]) |
Fred Drake | 5935ff0 | 2001-12-19 16:54:23 +0000 | [diff] [blame] | 186 | |
Benjamin Peterson | 3201977 | 2009-11-19 03:08:32 +0000 | [diff] [blame] | 187 | def test_proxy_unicode(self): |
| 188 | # See bug 5037 |
| 189 | class C(object): |
| 190 | def __str__(self): |
| 191 | return "string" |
| 192 | def __bytes__(self): |
| 193 | return b"bytes" |
| 194 | instance = C() |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 195 | self.assertIn("__bytes__", dir(weakref.proxy(instance))) |
Benjamin Peterson | 3201977 | 2009-11-19 03:08:32 +0000 | [diff] [blame] | 196 | self.assertEqual(bytes(weakref.proxy(instance)), b"bytes") |
| 197 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 198 | def test_proxy_index(self): |
| 199 | class C: |
| 200 | def __index__(self): |
| 201 | return 10 |
| 202 | o = C() |
| 203 | p = weakref.proxy(o) |
| 204 | self.assertEqual(operator.index(p), 10) |
| 205 | |
| 206 | def test_proxy_div(self): |
| 207 | class C: |
| 208 | def __floordiv__(self, other): |
| 209 | return 42 |
| 210 | def __ifloordiv__(self, other): |
| 211 | return 21 |
| 212 | o = C() |
| 213 | p = weakref.proxy(o) |
| 214 | self.assertEqual(p // 5, 42) |
| 215 | p //= 5 |
| 216 | self.assertEqual(p, 21) |
| 217 | |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 218 | # The PyWeakref_* C API is documented as allowing either NULL or |
| 219 | # None as the value for the callback, where either means "no |
| 220 | # callback". The "no callback" ref and proxy objects are supposed |
| 221 | # 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] | 222 | # they are active. In Python 2.3.3 and earlier, this guarantee |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 223 | # was not honored, and was broken in different ways for |
| 224 | # PyWeakref_NewRef() and PyWeakref_NewProxy(). (Two tests.) |
| 225 | |
| 226 | def test_shared_ref_without_callback(self): |
| 227 | self.check_shared_without_callback(weakref.ref) |
| 228 | |
| 229 | def test_shared_proxy_without_callback(self): |
| 230 | self.check_shared_without_callback(weakref.proxy) |
| 231 | |
| 232 | def check_shared_without_callback(self, makeref): |
| 233 | o = Object(1) |
| 234 | p1 = makeref(o, None) |
| 235 | p2 = makeref(o, None) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 236 | 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] | 237 | del p1, p2 |
| 238 | p1 = makeref(o) |
| 239 | p2 = makeref(o, None) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 240 | 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] | 241 | del p1, p2 |
| 242 | p1 = makeref(o) |
| 243 | p2 = makeref(o) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 244 | 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] | 245 | del p1, p2 |
| 246 | p1 = makeref(o, None) |
| 247 | p2 = makeref(o) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 248 | 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] | 249 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 250 | def test_callable_proxy(self): |
| 251 | o = Callable() |
| 252 | ref1 = weakref.proxy(o) |
| 253 | |
| 254 | self.check_proxy(o, ref1) |
| 255 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 256 | self.assertTrue(type(ref1) is weakref.CallableProxyType, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 257 | "proxy is not of callable type") |
| 258 | ref1('twinkies!') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 259 | self.assertTrue(o.bar == 'twinkies!', |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 260 | "call through proxy not passed through to original") |
Fred Drake | 3bb4d21 | 2001-10-18 19:28:29 +0000 | [diff] [blame] | 261 | ref1(x='Splat.') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 262 | self.assertTrue(o.bar == 'Splat.', |
Fred Drake | 3bb4d21 | 2001-10-18 19:28:29 +0000 | [diff] [blame] | 263 | "call through proxy not passed through to original") |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 264 | |
| 265 | # expect due to too few args |
| 266 | self.assertRaises(TypeError, ref1) |
| 267 | |
| 268 | # expect due to too many args |
| 269 | self.assertRaises(TypeError, ref1, 1, 2, 3) |
| 270 | |
| 271 | def check_proxy(self, o, proxy): |
| 272 | o.foo = 1 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 273 | self.assertTrue(proxy.foo == 1, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 274 | "proxy does not reflect attribute addition") |
| 275 | o.foo = 2 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 276 | self.assertTrue(proxy.foo == 2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 277 | "proxy does not reflect attribute modification") |
| 278 | del o.foo |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 279 | self.assertTrue(not hasattr(proxy, 'foo'), |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 280 | "proxy does not reflect attribute removal") |
| 281 | |
| 282 | proxy.foo = 1 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 283 | self.assertTrue(o.foo == 1, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 284 | "object does not reflect attribute addition via proxy") |
| 285 | proxy.foo = 2 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 286 | self.assertTrue( |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 287 | o.foo == 2, |
| 288 | "object does not reflect attribute modification via proxy") |
| 289 | del proxy.foo |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 290 | self.assertTrue(not hasattr(o, 'foo'), |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 291 | "object does not reflect attribute removal via proxy") |
| 292 | |
Raymond Hettinger | d693a81 | 2003-06-30 04:18:48 +0000 | [diff] [blame] | 293 | def test_proxy_deletion(self): |
| 294 | # Test clearing of SF bug #762891 |
| 295 | class Foo: |
| 296 | result = None |
| 297 | def __delitem__(self, accessor): |
| 298 | self.result = accessor |
| 299 | g = Foo() |
| 300 | f = weakref.proxy(g) |
| 301 | del f[0] |
| 302 | self.assertEqual(f.result, 0) |
| 303 | |
Raymond Hettinger | e6c470f | 2005-03-27 03:04:54 +0000 | [diff] [blame] | 304 | def test_proxy_bool(self): |
| 305 | # Test clearing of SF bug #1170766 |
| 306 | class List(list): pass |
| 307 | lyst = List() |
| 308 | self.assertEqual(bool(weakref.proxy(lyst)), bool(lyst)) |
| 309 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 310 | def test_getweakrefcount(self): |
| 311 | o = C() |
| 312 | ref1 = weakref.ref(o) |
| 313 | ref2 = weakref.ref(o, self.callback) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 314 | self.assertTrue(weakref.getweakrefcount(o) == 2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 315 | "got wrong number of weak reference objects") |
| 316 | |
| 317 | proxy1 = weakref.proxy(o) |
| 318 | proxy2 = weakref.proxy(o, self.callback) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 319 | self.assertTrue(weakref.getweakrefcount(o) == 4, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 320 | "got wrong number of weak reference objects") |
| 321 | |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 322 | del ref1, ref2, proxy1, proxy2 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 323 | self.assertTrue(weakref.getweakrefcount(o) == 0, |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 324 | "weak reference objects not unlinked from" |
| 325 | " referent when discarded.") |
| 326 | |
Walter Dörwald | b167b04 | 2003-12-11 12:34:05 +0000 | [diff] [blame] | 327 | # assumes ints do not support weakrefs |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 328 | self.assertTrue(weakref.getweakrefcount(1) == 0, |
Walter Dörwald | b167b04 | 2003-12-11 12:34:05 +0000 | [diff] [blame] | 329 | "got wrong number of weak reference objects for int") |
| 330 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 331 | def test_getweakrefs(self): |
| 332 | o = C() |
| 333 | ref1 = weakref.ref(o, self.callback) |
| 334 | ref2 = weakref.ref(o, self.callback) |
| 335 | del ref1 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 336 | self.assertTrue(weakref.getweakrefs(o) == [ref2], |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 337 | "list of refs does not match") |
| 338 | |
| 339 | o = C() |
| 340 | ref1 = weakref.ref(o, self.callback) |
| 341 | ref2 = weakref.ref(o, self.callback) |
| 342 | del ref2 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 343 | self.assertTrue(weakref.getweakrefs(o) == [ref1], |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 344 | "list of refs does not match") |
| 345 | |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 346 | del ref1 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 347 | self.assertTrue(weakref.getweakrefs(o) == [], |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 348 | "list of refs not cleared") |
| 349 | |
Walter Dörwald | b167b04 | 2003-12-11 12:34:05 +0000 | [diff] [blame] | 350 | # assumes ints do not support weakrefs |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 351 | self.assertTrue(weakref.getweakrefs(1) == [], |
Walter Dörwald | b167b04 | 2003-12-11 12:34:05 +0000 | [diff] [blame] | 352 | "list of refs does not match for int") |
| 353 | |
Fred Drake | 39c27f1 | 2001-10-18 18:06:05 +0000 | [diff] [blame] | 354 | def test_newstyle_number_ops(self): |
| 355 | class F(float): |
| 356 | pass |
| 357 | f = F(2.0) |
| 358 | p = weakref.proxy(f) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 359 | self.assertTrue(p + 1.0 == 3.0) |
| 360 | self.assertTrue(1.0 + p == 3.0) # this used to SEGV |
Fred Drake | 39c27f1 | 2001-10-18 18:06:05 +0000 | [diff] [blame] | 361 | |
Fred Drake | 2a64f46 | 2001-12-10 23:46:02 +0000 | [diff] [blame] | 362 | def test_callbacks_protected(self): |
Guido van Rossum | 9eee554 | 2002-08-22 20:21:30 +0000 | [diff] [blame] | 363 | # Callbacks protected from already-set exceptions? |
Fred Drake | 2a64f46 | 2001-12-10 23:46:02 +0000 | [diff] [blame] | 364 | # Regression test for SF bug #478534. |
| 365 | class BogusError(Exception): |
| 366 | pass |
| 367 | data = {} |
| 368 | def remove(k): |
| 369 | del data[k] |
| 370 | def encapsulate(): |
| 371 | f = lambda : () |
| 372 | data[weakref.ref(f, remove)] = None |
| 373 | raise BogusError |
| 374 | try: |
| 375 | encapsulate() |
| 376 | except BogusError: |
| 377 | pass |
| 378 | else: |
| 379 | self.fail("exception not properly restored") |
| 380 | try: |
| 381 | encapsulate() |
| 382 | except BogusError: |
| 383 | pass |
| 384 | else: |
| 385 | self.fail("exception not properly restored") |
| 386 | |
Tim Peters | add09b4 | 2003-11-12 20:43:28 +0000 | [diff] [blame] | 387 | def test_sf_bug_840829(self): |
| 388 | # "weakref callbacks and gc corrupt memory" |
| 389 | # subtype_dealloc erroneously exposed a new-style instance |
| 390 | # already in the process of getting deallocated to gc, |
| 391 | # causing double-deallocation if the instance had a weakref |
| 392 | # callback that triggered gc. |
| 393 | # If the bug exists, there probably won't be an obvious symptom |
| 394 | # in a release build. In a debug build, a segfault will occur |
| 395 | # when the second attempt to remove the instance from the "list |
| 396 | # of all objects" occurs. |
| 397 | |
| 398 | import gc |
| 399 | |
| 400 | class C(object): |
| 401 | pass |
| 402 | |
| 403 | c = C() |
| 404 | wr = weakref.ref(c, lambda ignore: gc.collect()) |
| 405 | del c |
| 406 | |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 407 | # There endeth the first part. It gets worse. |
| 408 | del wr |
| 409 | |
| 410 | c1 = C() |
| 411 | c1.i = C() |
| 412 | wr = weakref.ref(c1.i, lambda ignore: gc.collect()) |
| 413 | |
| 414 | c2 = C() |
| 415 | c2.c1 = c1 |
| 416 | del c1 # still alive because c2 points to it |
| 417 | |
| 418 | # Now when subtype_dealloc gets called on c2, it's not enough just |
| 419 | # that c2 is immune from gc while the weakref callbacks associated |
| 420 | # with c2 execute (there are none in this 2nd half of the test, btw). |
| 421 | # subtype_dealloc goes on to call the base classes' deallocs too, |
| 422 | # so any gc triggered by weakref callbacks associated with anything |
| 423 | # torn down by a base class dealloc can also trigger double |
| 424 | # deallocation of c2. |
| 425 | del c2 |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 426 | |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 427 | def test_callback_in_cycle_1(self): |
| 428 | import gc |
| 429 | |
| 430 | class J(object): |
| 431 | pass |
| 432 | |
| 433 | class II(object): |
| 434 | def acallback(self, ignore): |
| 435 | self.J |
| 436 | |
| 437 | I = II() |
| 438 | I.J = J |
| 439 | I.wr = weakref.ref(J, I.acallback) |
| 440 | |
| 441 | # Now J and II are each in a self-cycle (as all new-style class |
| 442 | # objects are, since their __mro__ points back to them). I holds |
| 443 | # both a weak reference (I.wr) and a strong reference (I.J) to class |
| 444 | # J. I is also in a cycle (I.wr points to a weakref that references |
| 445 | # I.acallback). When we del these three, they all become trash, but |
| 446 | # the cycles prevent any of them from getting cleaned up immediately. |
| 447 | # Instead they have to wait for cyclic gc to deduce that they're |
| 448 | # trash. |
| 449 | # |
| 450 | # gc used to call tp_clear on all of them, and the order in which |
| 451 | # it does that is pretty accidental. The exact order in which we |
| 452 | # built up these things manages to provoke gc into running tp_clear |
| 453 | # in just the right order (I last). Calling tp_clear on II leaves |
| 454 | # behind an insane class object (its __mro__ becomes NULL). Calling |
| 455 | # tp_clear on J breaks its self-cycle, but J doesn't get deleted |
| 456 | # just then because of the strong reference from I.J. Calling |
| 457 | # tp_clear on I starts to clear I's __dict__, and just happens to |
| 458 | # clear I.J first -- I.wr is still intact. That removes the last |
| 459 | # reference to J, which triggers the weakref callback. The callback |
| 460 | # tries to do "self.J", and instances of new-style classes look up |
| 461 | # attributes ("J") in the class dict first. The class (II) wants to |
| 462 | # search II.__mro__, but that's NULL. The result was a segfault in |
| 463 | # a release build, and an assert failure in a debug build. |
| 464 | del I, J, II |
| 465 | gc.collect() |
| 466 | |
| 467 | def test_callback_in_cycle_2(self): |
| 468 | import gc |
| 469 | |
| 470 | # This is just like test_callback_in_cycle_1, except that II is an |
| 471 | # old-style class. The symptom is different then: an instance of an |
| 472 | # old-style class looks in its own __dict__ first. 'J' happens to |
| 473 | # get cleared from I.__dict__ before 'wr', and 'J' was never in II's |
| 474 | # __dict__, so the attribute isn't found. The difference is that |
| 475 | # the old-style II doesn't have a NULL __mro__ (it doesn't have any |
| 476 | # __mro__), so no segfault occurs. Instead it got: |
| 477 | # test_callback_in_cycle_2 (__main__.ReferencesTestCase) ... |
| 478 | # Exception exceptions.AttributeError: |
| 479 | # "II instance has no attribute 'J'" in <bound method II.acallback |
| 480 | # of <?.II instance at 0x00B9B4B8>> ignored |
| 481 | |
| 482 | class J(object): |
| 483 | pass |
| 484 | |
| 485 | class II: |
| 486 | def acallback(self, ignore): |
| 487 | self.J |
| 488 | |
| 489 | I = II() |
| 490 | I.J = J |
| 491 | I.wr = weakref.ref(J, I.acallback) |
| 492 | |
| 493 | del I, J, II |
| 494 | gc.collect() |
| 495 | |
| 496 | def test_callback_in_cycle_3(self): |
| 497 | import gc |
| 498 | |
| 499 | # This one broke the first patch that fixed the last two. In this |
| 500 | # case, the objects reachable from the callback aren't also reachable |
| 501 | # from the object (c1) *triggering* the callback: you can get to |
| 502 | # c1 from c2, but not vice-versa. The result was that c2's __dict__ |
| 503 | # got tp_clear'ed by the time the c2.cb callback got invoked. |
| 504 | |
| 505 | class C: |
| 506 | def cb(self, ignore): |
| 507 | self.me |
| 508 | self.c1 |
| 509 | self.wr |
| 510 | |
| 511 | c1, c2 = C(), C() |
| 512 | |
| 513 | c2.me = c2 |
| 514 | c2.c1 = c1 |
| 515 | c2.wr = weakref.ref(c1, c2.cb) |
| 516 | |
| 517 | del c1, c2 |
| 518 | gc.collect() |
| 519 | |
| 520 | def test_callback_in_cycle_4(self): |
| 521 | import gc |
| 522 | |
| 523 | # Like test_callback_in_cycle_3, except c2 and c1 have different |
| 524 | # classes. c2's class (C) isn't reachable from c1 then, so protecting |
| 525 | # objects reachable from the dying object (c1) isn't enough to stop |
| 526 | # c2's class (C) from getting tp_clear'ed before c2.cb is invoked. |
| 527 | # The result was a segfault (C.__mro__ was NULL when the callback |
| 528 | # tried to look up self.me). |
| 529 | |
| 530 | class C(object): |
| 531 | def cb(self, ignore): |
| 532 | self.me |
| 533 | self.c1 |
| 534 | self.wr |
| 535 | |
| 536 | class D: |
| 537 | pass |
| 538 | |
| 539 | c1, c2 = D(), C() |
| 540 | |
| 541 | c2.me = c2 |
| 542 | c2.c1 = c1 |
| 543 | c2.wr = weakref.ref(c1, c2.cb) |
| 544 | |
| 545 | del c1, c2, C, D |
| 546 | gc.collect() |
| 547 | |
| 548 | def test_callback_in_cycle_resurrection(self): |
| 549 | import gc |
| 550 | |
| 551 | # Do something nasty in a weakref callback: resurrect objects |
| 552 | # from dead cycles. For this to be attempted, the weakref and |
| 553 | # its callback must also be part of the cyclic trash (else the |
| 554 | # objects reachable via the callback couldn't be in cyclic trash |
| 555 | # to begin with -- the callback would act like an external root). |
| 556 | # But gc clears trash weakrefs with callbacks early now, which |
| 557 | # disables the callbacks, so the callbacks shouldn't get called |
| 558 | # at all (and so nothing actually gets resurrected). |
| 559 | |
| 560 | alist = [] |
| 561 | class C(object): |
| 562 | def __init__(self, value): |
| 563 | self.attribute = value |
| 564 | |
| 565 | def acallback(self, ignore): |
| 566 | alist.append(self.c) |
| 567 | |
| 568 | c1, c2 = C(1), C(2) |
| 569 | c1.c = c2 |
| 570 | c2.c = c1 |
| 571 | c1.wr = weakref.ref(c2, c1.acallback) |
| 572 | c2.wr = weakref.ref(c1, c2.acallback) |
| 573 | |
| 574 | def C_went_away(ignore): |
| 575 | alist.append("C went away") |
| 576 | wr = weakref.ref(C, C_went_away) |
| 577 | |
| 578 | del c1, c2, C # make them all trash |
| 579 | self.assertEqual(alist, []) # del isn't enough to reclaim anything |
| 580 | |
| 581 | gc.collect() |
| 582 | # c1.wr and c2.wr were part of the cyclic trash, so should have |
| 583 | # been cleared without their callbacks executing. OTOH, the weakref |
| 584 | # to C is bound to a function local (wr), and wasn't trash, so that |
| 585 | # callback should have been invoked when C went away. |
| 586 | self.assertEqual(alist, ["C went away"]) |
| 587 | # The remaining weakref should be dead now (its callback ran). |
| 588 | self.assertEqual(wr(), None) |
| 589 | |
| 590 | del alist[:] |
| 591 | gc.collect() |
| 592 | self.assertEqual(alist, []) |
| 593 | |
| 594 | def test_callbacks_on_callback(self): |
| 595 | import gc |
| 596 | |
| 597 | # Set up weakref callbacks *on* weakref callbacks. |
| 598 | alist = [] |
| 599 | def safe_callback(ignore): |
| 600 | alist.append("safe_callback called") |
| 601 | |
| 602 | class C(object): |
| 603 | def cb(self, ignore): |
| 604 | alist.append("cb called") |
| 605 | |
| 606 | c, d = C(), C() |
| 607 | c.other = d |
| 608 | d.other = c |
| 609 | callback = c.cb |
| 610 | c.wr = weakref.ref(d, callback) # this won't trigger |
| 611 | d.wr = weakref.ref(callback, d.cb) # ditto |
| 612 | external_wr = weakref.ref(callback, safe_callback) # but this will |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 613 | self.assertTrue(external_wr() is callback) |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 614 | |
| 615 | # The weakrefs attached to c and d should get cleared, so that |
| 616 | # C.cb is never called. But external_wr isn't part of the cyclic |
| 617 | # trash, and no cyclic trash is reachable from it, so safe_callback |
| 618 | # should get invoked when the bound method object callback (c.cb) |
| 619 | # -- which is itself a callback, and also part of the cyclic trash -- |
| 620 | # gets reclaimed at the end of gc. |
| 621 | |
| 622 | del callback, c, d, C |
| 623 | self.assertEqual(alist, []) # del isn't enough to clean up cycles |
| 624 | gc.collect() |
| 625 | self.assertEqual(alist, ["safe_callback called"]) |
| 626 | self.assertEqual(external_wr(), None) |
| 627 | |
| 628 | del alist[:] |
| 629 | gc.collect() |
| 630 | self.assertEqual(alist, []) |
| 631 | |
Fred Drake | bc875f5 | 2004-02-04 23:14:14 +0000 | [diff] [blame] | 632 | def test_gc_during_ref_creation(self): |
| 633 | self.check_gc_during_creation(weakref.ref) |
| 634 | |
| 635 | def test_gc_during_proxy_creation(self): |
| 636 | self.check_gc_during_creation(weakref.proxy) |
| 637 | |
| 638 | def check_gc_during_creation(self, makeref): |
| 639 | thresholds = gc.get_threshold() |
| 640 | gc.set_threshold(1, 1, 1) |
| 641 | gc.collect() |
Fred Drake | 55cf434 | 2004-02-13 19:21:57 +0000 | [diff] [blame] | 642 | class A: |
| 643 | pass |
Fred Drake | bc875f5 | 2004-02-04 23:14:14 +0000 | [diff] [blame] | 644 | |
| 645 | def callback(*args): |
| 646 | pass |
| 647 | |
Fred Drake | 55cf434 | 2004-02-13 19:21:57 +0000 | [diff] [blame] | 648 | referenced = A() |
Fred Drake | bc875f5 | 2004-02-04 23:14:14 +0000 | [diff] [blame] | 649 | |
Fred Drake | 55cf434 | 2004-02-13 19:21:57 +0000 | [diff] [blame] | 650 | a = A() |
Fred Drake | bc875f5 | 2004-02-04 23:14:14 +0000 | [diff] [blame] | 651 | a.a = a |
| 652 | a.wr = makeref(referenced) |
| 653 | |
| 654 | try: |
| 655 | # now make sure the object and the ref get labeled as |
| 656 | # cyclic trash: |
Fred Drake | 55cf434 | 2004-02-13 19:21:57 +0000 | [diff] [blame] | 657 | a = A() |
| 658 | weakref.ref(referenced, callback) |
Fred Drake | bc875f5 | 2004-02-04 23:14:14 +0000 | [diff] [blame] | 659 | |
| 660 | finally: |
| 661 | gc.set_threshold(*thresholds) |
| 662 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 663 | def test_ref_created_during_del(self): |
| 664 | # Bug #1377858 |
| 665 | # A weakref created in an object's __del__() would crash the |
| 666 | # interpreter when the weakref was cleaned up since it would refer to |
| 667 | # non-existent memory. This test should not segfault the interpreter. |
| 668 | class Target(object): |
| 669 | def __del__(self): |
| 670 | global ref_from_del |
| 671 | ref_from_del = weakref.ref(self) |
| 672 | |
| 673 | w = Target() |
| 674 | |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 675 | def test_init(self): |
| 676 | # Issue 3634 |
| 677 | # <weakref to class>.__init__() doesn't check errors correctly |
| 678 | r = weakref.ref(Exception) |
| 679 | self.assertRaises(TypeError, r.__init__, 0, 0, 0, 0, 0) |
| 680 | # No exception should be raised here |
| 681 | gc.collect() |
| 682 | |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 683 | |
Amaury Forgeot d'Arc | c856c7a | 2008-06-16 19:50:09 +0000 | [diff] [blame] | 684 | class SubclassableWeakrefTestCase(TestBase): |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 685 | |
| 686 | def test_subclass_refs(self): |
| 687 | class MyRef(weakref.ref): |
| 688 | def __init__(self, ob, callback=None, value=42): |
| 689 | self.value = value |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 690 | super().__init__(ob, callback) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 691 | def __call__(self): |
| 692 | self.called = True |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 693 | return super().__call__() |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 694 | o = Object("foo") |
| 695 | mr = MyRef(o, value=24) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 696 | self.assertTrue(mr() is o) |
| 697 | self.assertTrue(mr.called) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 698 | self.assertEqual(mr.value, 24) |
| 699 | del o |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 700 | self.assertTrue(mr() is None) |
| 701 | self.assertTrue(mr.called) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 702 | |
| 703 | def test_subclass_refs_dont_replace_standard_refs(self): |
| 704 | class MyRef(weakref.ref): |
| 705 | pass |
| 706 | o = Object(42) |
| 707 | r1 = MyRef(o) |
| 708 | r2 = weakref.ref(o) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 709 | self.assertTrue(r1 is not r2) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 710 | self.assertEqual(weakref.getweakrefs(o), [r2, r1]) |
| 711 | self.assertEqual(weakref.getweakrefcount(o), 2) |
| 712 | r3 = MyRef(o) |
| 713 | self.assertEqual(weakref.getweakrefcount(o), 3) |
| 714 | refs = weakref.getweakrefs(o) |
| 715 | self.assertEqual(len(refs), 3) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 716 | self.assertTrue(r2 is refs[0]) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 717 | self.assertIn(r1, refs[1:]) |
| 718 | self.assertIn(r3, refs[1:]) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 719 | |
| 720 | def test_subclass_refs_dont_conflate_callbacks(self): |
| 721 | class MyRef(weakref.ref): |
| 722 | pass |
| 723 | o = Object(42) |
| 724 | r1 = MyRef(o, id) |
| 725 | r2 = MyRef(o, str) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 726 | self.assertTrue(r1 is not r2) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 727 | refs = weakref.getweakrefs(o) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 728 | self.assertIn(r1, refs) |
| 729 | self.assertIn(r2, refs) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 730 | |
| 731 | def test_subclass_refs_with_slots(self): |
| 732 | class MyRef(weakref.ref): |
| 733 | __slots__ = "slot1", "slot2" |
| 734 | def __new__(type, ob, callback, slot1, slot2): |
| 735 | return weakref.ref.__new__(type, ob, callback) |
| 736 | def __init__(self, ob, callback, slot1, slot2): |
| 737 | self.slot1 = slot1 |
| 738 | self.slot2 = slot2 |
| 739 | def meth(self): |
| 740 | return self.slot1 + self.slot2 |
| 741 | o = Object(42) |
| 742 | r = MyRef(o, None, "abc", "def") |
| 743 | self.assertEqual(r.slot1, "abc") |
| 744 | self.assertEqual(r.slot2, "def") |
| 745 | self.assertEqual(r.meth(), "abcdef") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 746 | self.assertFalse(hasattr(r, "__dict__")) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 747 | |
Amaury Forgeot d'Arc | c856c7a | 2008-06-16 19:50:09 +0000 | [diff] [blame] | 748 | def test_subclass_refs_with_cycle(self): |
| 749 | # Bug #3110 |
| 750 | # An instance of a weakref subclass can have attributes. |
| 751 | # If such a weakref holds the only strong reference to the object, |
| 752 | # deleting the weakref will delete the object. In this case, |
| 753 | # the callback must not be called, because the ref object is |
| 754 | # being deleted. |
| 755 | class MyRef(weakref.ref): |
| 756 | pass |
| 757 | |
| 758 | # Use a local callback, for "regrtest -R::" |
| 759 | # to detect refcounting problems |
| 760 | def callback(w): |
| 761 | self.cbcalled += 1 |
| 762 | |
| 763 | o = C() |
| 764 | r1 = MyRef(o, callback) |
| 765 | r1.o = o |
| 766 | del o |
| 767 | |
| 768 | del r1 # Used to crash here |
| 769 | |
| 770 | self.assertEqual(self.cbcalled, 0) |
| 771 | |
| 772 | # Same test, with two weakrefs to the same object |
| 773 | # (since code paths are different) |
| 774 | o = C() |
| 775 | r1 = MyRef(o, callback) |
| 776 | r2 = MyRef(o, callback) |
| 777 | r1.r = r2 |
| 778 | r2.o = o |
| 779 | del o |
| 780 | del r2 |
| 781 | |
| 782 | del r1 # Used to crash here |
| 783 | |
| 784 | self.assertEqual(self.cbcalled, 0) |
| 785 | |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 786 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 787 | class Object: |
| 788 | def __init__(self, arg): |
| 789 | self.arg = arg |
| 790 | def __repr__(self): |
| 791 | return "<Object %r>" % self.arg |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 792 | def __eq__(self, other): |
| 793 | if isinstance(other, Object): |
| 794 | return self.arg == other.arg |
| 795 | return NotImplemented |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 796 | def __lt__(self, other): |
| 797 | if isinstance(other, Object): |
| 798 | return self.arg < other.arg |
| 799 | return NotImplemented |
| 800 | def __hash__(self): |
| 801 | return hash(self.arg) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 802 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 803 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 804 | class MappingTestCase(TestBase): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 805 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 806 | COUNT = 10 |
| 807 | |
| 808 | def test_weak_values(self): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 809 | # |
| 810 | # This exercises d.copy(), d.items(), d[], del d[], len(d). |
| 811 | # |
| 812 | dict, objects = self.make_weak_valued_dict() |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 813 | for o in objects: |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 814 | self.assertEqual(weakref.getweakrefcount(o), 1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 815 | self.assertTrue(o is dict[o.arg], |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 816 | "wrong object returned by weak dict!") |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 817 | items1 = list(dict.items()) |
| 818 | items2 = list(dict.copy().items()) |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 819 | items1.sort() |
| 820 | items2.sort() |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 821 | self.assertEqual(items1, items2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 822 | "cloning of weak-valued dictionary did not work!") |
| 823 | del items1, items2 |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 824 | self.assertEqual(len(dict), self.COUNT) |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 825 | del objects[0] |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 826 | self.assertEqual(len(dict), self.COUNT - 1, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 827 | "deleting object did not cause dictionary update") |
| 828 | del objects, o |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 829 | self.assertEqual(len(dict), 0, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 830 | "deleting the values did not clear the dictionary") |
Fred Drake | 4fd06e0 | 2001-08-03 04:11:27 +0000 | [diff] [blame] | 831 | # regression on SF bug #447152: |
| 832 | dict = weakref.WeakValueDictionary() |
| 833 | self.assertRaises(KeyError, dict.__getitem__, 1) |
| 834 | dict[2] = C() |
| 835 | self.assertRaises(KeyError, dict.__getitem__, 2) |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 836 | |
| 837 | def test_weak_keys(self): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 838 | # |
| 839 | # This exercises d.copy(), d.items(), d[] = v, d[], del d[], |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 840 | # len(d), k in d. |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 841 | # |
| 842 | dict, objects = self.make_weak_keyed_dict() |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 843 | for o in objects: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 844 | self.assertTrue(weakref.getweakrefcount(o) == 1, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 845 | "wrong number of weak references to %r!" % o) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 846 | self.assertTrue(o.arg is dict[o], |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 847 | "wrong object returned by weak dict!") |
| 848 | items1 = dict.items() |
| 849 | items2 = dict.copy().items() |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 850 | self.assertEqual(set(items1), set(items2), |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 851 | "cloning of weak-keyed dictionary did not work!") |
| 852 | del items1, items2 |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 853 | self.assertEqual(len(dict), self.COUNT) |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 854 | del objects[0] |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 855 | self.assertTrue(len(dict) == (self.COUNT - 1), |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 856 | "deleting object did not cause dictionary update") |
| 857 | del objects, o |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 858 | self.assertTrue(len(dict) == 0, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 859 | "deleting the keys did not clear the dictionary") |
Fred Drake | 752eda4 | 2001-11-06 16:38:34 +0000 | [diff] [blame] | 860 | o = Object(42) |
| 861 | dict[o] = "What is the meaning of the universe?" |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 862 | self.assertIn(o, dict) |
| 863 | self.assertNotIn(34, dict) |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 864 | |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 865 | def test_weak_keyed_iters(self): |
| 866 | dict, objects = self.make_weak_keyed_dict() |
| 867 | self.check_iters(dict) |
| 868 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 869 | # Test keyrefs() |
| 870 | refs = dict.keyrefs() |
| 871 | self.assertEqual(len(refs), len(objects)) |
| 872 | objects2 = list(objects) |
| 873 | for wr in refs: |
| 874 | ob = wr() |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 875 | self.assertIn(ob, dict) |
| 876 | self.assertIn(ob, dict) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 877 | self.assertEqual(ob.arg, dict[ob]) |
| 878 | objects2.remove(ob) |
| 879 | self.assertEqual(len(objects2), 0) |
| 880 | |
| 881 | # Test iterkeyrefs() |
| 882 | objects2 = list(objects) |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 883 | self.assertEqual(len(list(dict.keyrefs())), len(objects)) |
| 884 | for wr in dict.keyrefs(): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 885 | ob = wr() |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 886 | self.assertIn(ob, dict) |
| 887 | self.assertIn(ob, dict) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 888 | self.assertEqual(ob.arg, dict[ob]) |
| 889 | objects2.remove(ob) |
| 890 | self.assertEqual(len(objects2), 0) |
| 891 | |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 892 | def test_weak_valued_iters(self): |
| 893 | dict, objects = self.make_weak_valued_dict() |
| 894 | self.check_iters(dict) |
| 895 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 896 | # Test valuerefs() |
| 897 | refs = dict.valuerefs() |
| 898 | self.assertEqual(len(refs), len(objects)) |
| 899 | objects2 = list(objects) |
| 900 | for wr in refs: |
| 901 | ob = wr() |
| 902 | self.assertEqual(ob, dict[ob.arg]) |
| 903 | self.assertEqual(ob.arg, dict[ob.arg].arg) |
| 904 | objects2.remove(ob) |
| 905 | self.assertEqual(len(objects2), 0) |
| 906 | |
| 907 | # Test itervaluerefs() |
| 908 | objects2 = list(objects) |
| 909 | self.assertEqual(len(list(dict.itervaluerefs())), len(objects)) |
| 910 | for wr in dict.itervaluerefs(): |
| 911 | ob = wr() |
| 912 | self.assertEqual(ob, dict[ob.arg]) |
| 913 | self.assertEqual(ob.arg, dict[ob.arg].arg) |
| 914 | objects2.remove(ob) |
| 915 | self.assertEqual(len(objects2), 0) |
| 916 | |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 917 | def check_iters(self, dict): |
| 918 | # item iterator: |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 919 | items = list(dict.items()) |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 920 | for item in dict.items(): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 921 | items.remove(item) |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 922 | self.assertFalse(items, "items() did not touch all items") |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 923 | |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 924 | # key iterator, via __iter__(): |
Guido van Rossum | 07f2436 | 2007-02-11 22:59:48 +0000 | [diff] [blame] | 925 | keys = list(dict.keys()) |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 926 | for k in dict: |
| 927 | keys.remove(k) |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 928 | self.assertFalse(keys, "__iter__() did not touch all keys") |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 929 | |
| 930 | # key iterator, via iterkeys(): |
Guido van Rossum | 07f2436 | 2007-02-11 22:59:48 +0000 | [diff] [blame] | 931 | keys = list(dict.keys()) |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 932 | for k in dict.keys(): |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 933 | keys.remove(k) |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 934 | self.assertFalse(keys, "iterkeys() did not touch all keys") |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 935 | |
| 936 | # value iterator: |
Guido van Rossum | 07f2436 | 2007-02-11 22:59:48 +0000 | [diff] [blame] | 937 | values = list(dict.values()) |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 938 | for v in dict.values(): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 939 | values.remove(v) |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 940 | self.assertFalse(values, |
Fred Drake | f425b1e | 2003-07-14 21:37:17 +0000 | [diff] [blame] | 941 | "itervalues() did not touch all values") |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 942 | |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 943 | def check_weak_destroy_while_iterating(self, dict, objects, iter_name): |
| 944 | n = len(dict) |
| 945 | it = iter(getattr(dict, iter_name)()) |
| 946 | next(it) # Trigger internal iteration |
| 947 | # Destroy an object |
| 948 | del objects[-1] |
| 949 | gc.collect() # just in case |
| 950 | # We have removed either the first consumed object, or another one |
| 951 | self.assertIn(len(list(it)), [len(objects), len(objects) - 1]) |
| 952 | del it |
| 953 | # The removal has been committed |
| 954 | self.assertEqual(len(dict), n - 1) |
| 955 | |
| 956 | def check_weak_destroy_and_mutate_while_iterating(self, dict, testcontext): |
| 957 | # Check that we can explicitly mutate the weak dict without |
| 958 | # interfering with delayed removal. |
| 959 | # `testcontext` should create an iterator, destroy one of the |
| 960 | # weakref'ed objects and then return a new key/value pair corresponding |
| 961 | # to the destroyed object. |
| 962 | with testcontext() as (k, v): |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 963 | self.assertNotIn(k, dict) |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 964 | with testcontext() as (k, v): |
| 965 | self.assertRaises(KeyError, dict.__delitem__, k) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 966 | self.assertNotIn(k, dict) |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 967 | with testcontext() as (k, v): |
| 968 | self.assertRaises(KeyError, dict.pop, k) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 969 | self.assertNotIn(k, dict) |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 970 | with testcontext() as (k, v): |
| 971 | dict[k] = v |
| 972 | self.assertEqual(dict[k], v) |
| 973 | ddict = copy.copy(dict) |
| 974 | with testcontext() as (k, v): |
| 975 | dict.update(ddict) |
| 976 | self.assertEqual(dict, ddict) |
| 977 | with testcontext() as (k, v): |
| 978 | dict.clear() |
| 979 | self.assertEqual(len(dict), 0) |
| 980 | |
| 981 | def test_weak_keys_destroy_while_iterating(self): |
| 982 | # Issue #7105: iterators shouldn't crash when a key is implicitly removed |
| 983 | dict, objects = self.make_weak_keyed_dict() |
| 984 | self.check_weak_destroy_while_iterating(dict, objects, 'keys') |
| 985 | self.check_weak_destroy_while_iterating(dict, objects, 'items') |
| 986 | self.check_weak_destroy_while_iterating(dict, objects, 'values') |
| 987 | self.check_weak_destroy_while_iterating(dict, objects, 'keyrefs') |
| 988 | dict, objects = self.make_weak_keyed_dict() |
| 989 | @contextlib.contextmanager |
| 990 | def testcontext(): |
| 991 | try: |
| 992 | it = iter(dict.items()) |
| 993 | next(it) |
| 994 | # Schedule a key/value for removal and recreate it |
| 995 | v = objects.pop().arg |
| 996 | gc.collect() # just in case |
| 997 | yield Object(v), v |
| 998 | finally: |
| 999 | it = None # should commit all removals |
| 1000 | self.check_weak_destroy_and_mutate_while_iterating(dict, testcontext) |
| 1001 | |
| 1002 | def test_weak_values_destroy_while_iterating(self): |
| 1003 | # Issue #7105: iterators shouldn't crash when a key is implicitly removed |
| 1004 | dict, objects = self.make_weak_valued_dict() |
| 1005 | self.check_weak_destroy_while_iterating(dict, objects, 'keys') |
| 1006 | self.check_weak_destroy_while_iterating(dict, objects, 'items') |
| 1007 | self.check_weak_destroy_while_iterating(dict, objects, 'values') |
| 1008 | self.check_weak_destroy_while_iterating(dict, objects, 'itervaluerefs') |
| 1009 | self.check_weak_destroy_while_iterating(dict, objects, 'valuerefs') |
| 1010 | dict, objects = self.make_weak_valued_dict() |
| 1011 | @contextlib.contextmanager |
| 1012 | def testcontext(): |
| 1013 | try: |
| 1014 | it = iter(dict.items()) |
| 1015 | next(it) |
| 1016 | # Schedule a key/value for removal and recreate it |
| 1017 | k = objects.pop().arg |
| 1018 | gc.collect() # just in case |
| 1019 | yield k, Object(k) |
| 1020 | finally: |
| 1021 | it = None # should commit all removals |
| 1022 | self.check_weak_destroy_and_mutate_while_iterating(dict, testcontext) |
| 1023 | |
Guido van Rossum | 009afb7 | 2002-06-10 20:00:52 +0000 | [diff] [blame] | 1024 | def test_make_weak_keyed_dict_from_dict(self): |
| 1025 | o = Object(3) |
| 1026 | dict = weakref.WeakKeyDictionary({o:364}) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1027 | self.assertEqual(dict[o], 364) |
Guido van Rossum | 009afb7 | 2002-06-10 20:00:52 +0000 | [diff] [blame] | 1028 | |
| 1029 | def test_make_weak_keyed_dict_from_weak_keyed_dict(self): |
| 1030 | o = Object(3) |
| 1031 | dict = weakref.WeakKeyDictionary({o:364}) |
| 1032 | dict2 = weakref.WeakKeyDictionary(dict) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1033 | self.assertEqual(dict[o], 364) |
Guido van Rossum | 009afb7 | 2002-06-10 20:00:52 +0000 | [diff] [blame] | 1034 | |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1035 | def make_weak_keyed_dict(self): |
| 1036 | dict = weakref.WeakKeyDictionary() |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1037 | objects = list(map(Object, range(self.COUNT))) |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1038 | for o in objects: |
| 1039 | dict[o] = o.arg |
| 1040 | return dict, objects |
| 1041 | |
Antoine Pitrou | c06de47 | 2009-05-30 21:04:26 +0000 | [diff] [blame] | 1042 | def test_make_weak_valued_dict_from_dict(self): |
| 1043 | o = Object(3) |
| 1044 | dict = weakref.WeakValueDictionary({364:o}) |
| 1045 | self.assertEqual(dict[364], o) |
| 1046 | |
| 1047 | def test_make_weak_valued_dict_from_weak_valued_dict(self): |
| 1048 | o = Object(3) |
| 1049 | dict = weakref.WeakValueDictionary({364:o}) |
| 1050 | dict2 = weakref.WeakValueDictionary(dict) |
| 1051 | self.assertEqual(dict[364], o) |
| 1052 | |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1053 | def make_weak_valued_dict(self): |
| 1054 | dict = weakref.WeakValueDictionary() |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1055 | objects = list(map(Object, range(self.COUNT))) |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1056 | for o in objects: |
| 1057 | dict[o.arg] = o |
| 1058 | return dict, objects |
| 1059 | |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1060 | def check_popitem(self, klass, key1, value1, key2, value2): |
| 1061 | weakdict = klass() |
| 1062 | weakdict[key1] = value1 |
| 1063 | weakdict[key2] = value2 |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1064 | self.assertEqual(len(weakdict), 2) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1065 | k, v = weakdict.popitem() |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1066 | self.assertEqual(len(weakdict), 1) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1067 | if k is key1: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1068 | self.assertTrue(v is value1) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1069 | else: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1070 | self.assertTrue(v is value2) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1071 | k, v = weakdict.popitem() |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1072 | self.assertEqual(len(weakdict), 0) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1073 | if k is key1: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1074 | self.assertTrue(v is value1) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1075 | else: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1076 | self.assertTrue(v is value2) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1077 | |
| 1078 | def test_weak_valued_dict_popitem(self): |
| 1079 | self.check_popitem(weakref.WeakValueDictionary, |
| 1080 | "key1", C(), "key2", C()) |
| 1081 | |
| 1082 | def test_weak_keyed_dict_popitem(self): |
| 1083 | self.check_popitem(weakref.WeakKeyDictionary, |
| 1084 | C(), "value 1", C(), "value 2") |
| 1085 | |
| 1086 | def check_setdefault(self, klass, key, value1, value2): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1087 | self.assertTrue(value1 is not value2, |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1088 | "invalid test" |
| 1089 | " -- value parameters must be distinct objects") |
| 1090 | weakdict = klass() |
| 1091 | o = weakdict.setdefault(key, value1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1092 | self.assertTrue(o is value1) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1093 | self.assertIn(key, weakdict) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1094 | self.assertTrue(weakdict.get(key) is value1) |
| 1095 | self.assertTrue(weakdict[key] is value1) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1096 | |
| 1097 | o = weakdict.setdefault(key, value2) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1098 | self.assertTrue(o is value1) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1099 | self.assertIn(key, weakdict) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1100 | self.assertTrue(weakdict.get(key) is value1) |
| 1101 | self.assertTrue(weakdict[key] is value1) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1102 | |
| 1103 | def test_weak_valued_dict_setdefault(self): |
| 1104 | self.check_setdefault(weakref.WeakValueDictionary, |
| 1105 | "key", C(), C()) |
| 1106 | |
| 1107 | def test_weak_keyed_dict_setdefault(self): |
| 1108 | self.check_setdefault(weakref.WeakKeyDictionary, |
| 1109 | C(), "value 1", "value 2") |
| 1110 | |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 1111 | def check_update(self, klass, dict): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1112 | # |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 1113 | # This exercises d.update(), len(d), d.keys(), k in d, |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1114 | # d.get(), d[]. |
| 1115 | # |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 1116 | weakdict = klass() |
| 1117 | weakdict.update(dict) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1118 | self.assertEqual(len(weakdict), len(dict)) |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 1119 | for k in weakdict.keys(): |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 1120 | self.assertIn(k, dict, "mysterious new key appeared in weak dict") |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 1121 | v = dict.get(k) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1122 | self.assertTrue(v is weakdict[k]) |
| 1123 | self.assertTrue(v is weakdict.get(k)) |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 1124 | for k in dict.keys(): |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 1125 | self.assertIn(k, weakdict, "original key disappeared in weak dict") |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 1126 | v = dict[k] |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1127 | self.assertTrue(v is weakdict[k]) |
| 1128 | self.assertTrue(v is weakdict.get(k)) |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 1129 | |
| 1130 | def test_weak_valued_dict_update(self): |
| 1131 | self.check_update(weakref.WeakValueDictionary, |
| 1132 | {1: C(), 'a': C(), C(): C()}) |
| 1133 | |
| 1134 | def test_weak_keyed_dict_update(self): |
| 1135 | self.check_update(weakref.WeakKeyDictionary, |
| 1136 | {C(): 1, C(): 2, C(): 3}) |
| 1137 | |
Fred Drake | ccc7562 | 2001-09-06 14:52:39 +0000 | [diff] [blame] | 1138 | def test_weak_keyed_delitem(self): |
| 1139 | d = weakref.WeakKeyDictionary() |
| 1140 | o1 = Object('1') |
| 1141 | o2 = Object('2') |
| 1142 | d[o1] = 'something' |
| 1143 | d[o2] = 'something' |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1144 | self.assertEqual(len(d), 2) |
Fred Drake | ccc7562 | 2001-09-06 14:52:39 +0000 | [diff] [blame] | 1145 | del d[o1] |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1146 | self.assertEqual(len(d), 1) |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 1147 | self.assertEqual(list(d.keys()), [o2]) |
Fred Drake | ccc7562 | 2001-09-06 14:52:39 +0000 | [diff] [blame] | 1148 | |
| 1149 | def test_weak_valued_delitem(self): |
| 1150 | d = weakref.WeakValueDictionary() |
| 1151 | o1 = Object('1') |
| 1152 | o2 = Object('2') |
| 1153 | d['something'] = o1 |
| 1154 | d['something else'] = o2 |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1155 | self.assertEqual(len(d), 2) |
Fred Drake | ccc7562 | 2001-09-06 14:52:39 +0000 | [diff] [blame] | 1156 | del d['something'] |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1157 | self.assertEqual(len(d), 1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1158 | self.assertTrue(list(d.items()) == [('something else', o2)]) |
Fred Drake | ccc7562 | 2001-09-06 14:52:39 +0000 | [diff] [blame] | 1159 | |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 1160 | def test_weak_keyed_bad_delitem(self): |
| 1161 | d = weakref.WeakKeyDictionary() |
| 1162 | o = Object('1') |
| 1163 | # An attempt to delete an object that isn't there should raise |
Tim Peters | 50d8b8b | 2003-05-25 17:44:31 +0000 | [diff] [blame] | 1164 | # KeyError. It didn't before 2.3. |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 1165 | self.assertRaises(KeyError, d.__delitem__, o) |
Tim Peters | 50d8b8b | 2003-05-25 17:44:31 +0000 | [diff] [blame] | 1166 | self.assertRaises(KeyError, d.__getitem__, o) |
| 1167 | |
| 1168 | # If a key isn't of a weakly referencable type, __getitem__ and |
| 1169 | # __setitem__ raise TypeError. __delitem__ should too. |
| 1170 | self.assertRaises(TypeError, d.__delitem__, 13) |
| 1171 | self.assertRaises(TypeError, d.__getitem__, 13) |
| 1172 | self.assertRaises(TypeError, d.__setitem__, 13, 13) |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 1173 | |
| 1174 | def test_weak_keyed_cascading_deletes(self): |
| 1175 | # SF bug 742860. For some reason, before 2.3 __delitem__ iterated |
| 1176 | # over the keys via self.data.iterkeys(). If things vanished from |
| 1177 | # the dict during this (or got added), that caused a RuntimeError. |
| 1178 | |
| 1179 | d = weakref.WeakKeyDictionary() |
| 1180 | mutate = False |
| 1181 | |
| 1182 | class C(object): |
| 1183 | def __init__(self, i): |
| 1184 | self.value = i |
| 1185 | def __hash__(self): |
| 1186 | return hash(self.value) |
| 1187 | def __eq__(self, other): |
| 1188 | if mutate: |
| 1189 | # Side effect that mutates the dict, by removing the |
| 1190 | # last strong reference to a key. |
| 1191 | del objs[-1] |
| 1192 | return self.value == other.value |
| 1193 | |
| 1194 | objs = [C(i) for i in range(4)] |
| 1195 | for o in objs: |
| 1196 | d[o] = o.value |
| 1197 | del o # now the only strong references to keys are in objs |
| 1198 | # Find the order in which iterkeys sees the keys. |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 1199 | objs = list(d.keys()) |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 1200 | # Reverse it, so that the iteration implementation of __delitem__ |
| 1201 | # has to keep looping to find the first object we delete. |
| 1202 | objs.reverse() |
Tim Peters | 50d8b8b | 2003-05-25 17:44:31 +0000 | [diff] [blame] | 1203 | |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 1204 | # Turn on mutation in C.__eq__. The first time thru the loop, |
| 1205 | # under the iterkeys() business the first comparison will delete |
| 1206 | # the last item iterkeys() would see, and that causes a |
| 1207 | # RuntimeError: dictionary changed size during iteration |
| 1208 | # when the iterkeys() loop goes around to try comparing the next |
Tim Peters | 50d8b8b | 2003-05-25 17:44:31 +0000 | [diff] [blame] | 1209 | # key. After this was fixed, it just deletes the last object *our* |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 1210 | # "for o in obj" loop would have gotten to. |
| 1211 | mutate = True |
| 1212 | count = 0 |
| 1213 | for o in objs: |
| 1214 | count += 1 |
| 1215 | del d[o] |
| 1216 | self.assertEqual(len(d), 0) |
| 1217 | self.assertEqual(count, 2) |
| 1218 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 1219 | from test import mapping_tests |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 1220 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 1221 | class WeakValueDictionaryTestCase(mapping_tests.BasicTestMappingProtocol): |
Fred Drake | f425b1e | 2003-07-14 21:37:17 +0000 | [diff] [blame] | 1222 | """Check that WeakValueDictionary conforms to the mapping protocol""" |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 1223 | __ref = {"key1":Object(1), "key2":Object(2), "key3":Object(3)} |
Walter Dörwald | 118f931 | 2004-06-02 18:42:25 +0000 | [diff] [blame] | 1224 | type2test = weakref.WeakValueDictionary |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 1225 | def _reference(self): |
| 1226 | return self.__ref.copy() |
| 1227 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 1228 | class WeakKeyDictionaryTestCase(mapping_tests.BasicTestMappingProtocol): |
Fred Drake | f425b1e | 2003-07-14 21:37:17 +0000 | [diff] [blame] | 1229 | """Check that WeakKeyDictionary conforms to the mapping protocol""" |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 1230 | __ref = {Object("key1"):1, Object("key2"):2, Object("key3"):3} |
Walter Dörwald | 118f931 | 2004-06-02 18:42:25 +0000 | [diff] [blame] | 1231 | type2test = weakref.WeakKeyDictionary |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 1232 | def _reference(self): |
| 1233 | return self.__ref.copy() |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 1234 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1235 | libreftest = """ Doctest for examples in the library reference: weakref.rst |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1236 | |
| 1237 | >>> import weakref |
| 1238 | >>> class Dict(dict): |
| 1239 | ... pass |
| 1240 | ... |
| 1241 | >>> obj = Dict(red=1, green=2, blue=3) # this object is weak referencable |
| 1242 | >>> r = weakref.ref(obj) |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1243 | >>> print(r() is obj) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1244 | True |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1245 | |
| 1246 | >>> import weakref |
| 1247 | >>> class Object: |
| 1248 | ... pass |
| 1249 | ... |
| 1250 | >>> o = Object() |
| 1251 | >>> r = weakref.ref(o) |
| 1252 | >>> o2 = r() |
| 1253 | >>> o is o2 |
| 1254 | True |
| 1255 | >>> del o, o2 |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1256 | >>> print(r()) |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1257 | None |
| 1258 | |
| 1259 | >>> import weakref |
| 1260 | >>> class ExtendedRef(weakref.ref): |
| 1261 | ... def __init__(self, ob, callback=None, **annotations): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1262 | ... super().__init__(ob, callback) |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1263 | ... self.__counter = 0 |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 1264 | ... for k, v in annotations.items(): |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1265 | ... setattr(self, k, v) |
| 1266 | ... def __call__(self): |
| 1267 | ... '''Return a pair containing the referent and the number of |
| 1268 | ... times the reference has been called. |
| 1269 | ... ''' |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1270 | ... ob = super().__call__() |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1271 | ... if ob is not None: |
| 1272 | ... self.__counter += 1 |
| 1273 | ... ob = (ob, self.__counter) |
| 1274 | ... return ob |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1275 | ... |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1276 | >>> class A: # not in docs from here, just testing the ExtendedRef |
| 1277 | ... pass |
| 1278 | ... |
| 1279 | >>> a = A() |
| 1280 | >>> r = ExtendedRef(a, foo=1, bar="baz") |
| 1281 | >>> r.foo |
| 1282 | 1 |
| 1283 | >>> r.bar |
| 1284 | 'baz' |
| 1285 | >>> r()[1] |
| 1286 | 1 |
| 1287 | >>> r()[1] |
| 1288 | 2 |
| 1289 | >>> r()[0] is a |
| 1290 | True |
| 1291 | |
| 1292 | |
| 1293 | >>> import weakref |
| 1294 | >>> _id2obj_dict = weakref.WeakValueDictionary() |
| 1295 | >>> def remember(obj): |
| 1296 | ... oid = id(obj) |
| 1297 | ... _id2obj_dict[oid] = obj |
| 1298 | ... return oid |
| 1299 | ... |
| 1300 | >>> def id2obj(oid): |
| 1301 | ... return _id2obj_dict[oid] |
| 1302 | ... |
| 1303 | >>> a = A() # from here, just testing |
| 1304 | >>> a_id = remember(a) |
| 1305 | >>> id2obj(a_id) is a |
| 1306 | True |
| 1307 | >>> del a |
| 1308 | >>> try: |
| 1309 | ... id2obj(a_id) |
| 1310 | ... except KeyError: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1311 | ... print('OK') |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1312 | ... else: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1313 | ... print('WeakValueDictionary error') |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1314 | OK |
| 1315 | |
| 1316 | """ |
| 1317 | |
| 1318 | __test__ = {'libreftest' : libreftest} |
| 1319 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 1320 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1321 | support.run_unittest( |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1322 | ReferencesTestCase, |
| 1323 | MappingTestCase, |
| 1324 | WeakValueDictionaryTestCase, |
Fred Drake | f425b1e | 2003-07-14 21:37:17 +0000 | [diff] [blame] | 1325 | WeakKeyDictionaryTestCase, |
Amaury Forgeot d'Arc | c856c7a | 2008-06-16 19:50:09 +0000 | [diff] [blame] | 1326 | SubclassableWeakrefTestCase, |
Fred Drake | f425b1e | 2003-07-14 21:37:17 +0000 | [diff] [blame] | 1327 | ) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1328 | support.run_doctest(sys.modules[__name__]) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 1329 | |
| 1330 | |
| 1331 | if __name__ == "__main__": |
| 1332 | test_main() |