Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 1 | import sys |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 2 | import unittest |
Fred Drake | 5935ff0 | 2001-12-19 16:54:23 +0000 | [diff] [blame] | 3 | import UserList |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 4 | import weakref |
| 5 | |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 6 | from test import test_support |
Raymond Hettinger | 91bbd9a | 2003-05-02 09:06:28 +0000 | [diff] [blame] | 7 | from sets import Set |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 8 | |
| 9 | |
| 10 | class C: |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 11 | def method(self): |
| 12 | pass |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 13 | |
| 14 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 15 | class Callable: |
| 16 | bar = None |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 17 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 18 | def __call__(self, x): |
| 19 | self.bar = x |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 20 | |
| 21 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 22 | def create_function(): |
| 23 | def f(): pass |
| 24 | return f |
| 25 | |
| 26 | def create_bound_method(): |
| 27 | return C().method |
| 28 | |
| 29 | def create_unbound_method(): |
| 30 | return C.method |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 31 | |
| 32 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 33 | class TestBase(unittest.TestCase): |
| 34 | |
| 35 | def setUp(self): |
| 36 | self.cbcalled = 0 |
| 37 | |
| 38 | def callback(self, ref): |
| 39 | self.cbcalled += 1 |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 40 | |
| 41 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 42 | class ReferencesTestCase(TestBase): |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 43 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 44 | def test_basic_ref(self): |
| 45 | self.check_basic_ref(C) |
| 46 | self.check_basic_ref(create_function) |
| 47 | self.check_basic_ref(create_bound_method) |
| 48 | self.check_basic_ref(create_unbound_method) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 49 | |
Fred Drake | 43735da | 2002-04-11 03:59:42 +0000 | [diff] [blame] | 50 | # Just make sure the tp_repr handler doesn't raise an exception. |
| 51 | # Live reference: |
| 52 | o = C() |
| 53 | wr = weakref.ref(o) |
| 54 | `wr` |
| 55 | # Dead reference: |
| 56 | del o |
| 57 | `wr` |
| 58 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 59 | def test_basic_callback(self): |
| 60 | self.check_basic_callback(C) |
| 61 | self.check_basic_callback(create_function) |
| 62 | self.check_basic_callback(create_bound_method) |
| 63 | self.check_basic_callback(create_unbound_method) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 64 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 65 | def test_multiple_callbacks(self): |
| 66 | o = C() |
| 67 | ref1 = weakref.ref(o, self.callback) |
| 68 | ref2 = weakref.ref(o, self.callback) |
| 69 | del o |
| 70 | self.assert_(ref1() is None, |
| 71 | "expected reference to be invalidated") |
| 72 | self.assert_(ref2() is None, |
| 73 | "expected reference to be invalidated") |
| 74 | self.assert_(self.cbcalled == 2, |
| 75 | "callback not called the right number of times") |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 76 | |
Fred Drake | 705088e | 2001-04-13 17:18:15 +0000 | [diff] [blame] | 77 | def test_multiple_selfref_callbacks(self): |
Guido van Rossum | 9eee554 | 2002-08-22 20:21:30 +0000 | [diff] [blame] | 78 | # Make sure all references are invalidated before callbacks are called |
Fred Drake | 705088e | 2001-04-13 17:18:15 +0000 | [diff] [blame] | 79 | # |
| 80 | # What's important here is that we're using the first |
| 81 | # reference in the callback invoked on the second reference |
| 82 | # (the most recently created ref is cleaned up first). This |
| 83 | # tests that all references to the object are invalidated |
| 84 | # before any of the callbacks are invoked, so that we only |
| 85 | # have one invocation of _weakref.c:cleanup_helper() active |
| 86 | # for a particular object at a time. |
| 87 | # |
| 88 | def callback(object, self=self): |
| 89 | self.ref() |
| 90 | c = C() |
| 91 | self.ref = weakref.ref(c, callback) |
| 92 | ref1 = weakref.ref(c, callback) |
| 93 | del c |
| 94 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 95 | def test_proxy_ref(self): |
| 96 | o = C() |
| 97 | o.bar = 1 |
| 98 | ref1 = weakref.proxy(o, self.callback) |
| 99 | ref2 = weakref.proxy(o, self.callback) |
| 100 | del o |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 101 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 102 | def check(proxy): |
| 103 | proxy.bar |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 104 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 105 | self.assertRaises(weakref.ReferenceError, check, ref1) |
| 106 | self.assertRaises(weakref.ReferenceError, check, ref2) |
| 107 | self.assert_(self.cbcalled == 2) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 108 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 109 | def check_basic_ref(self, factory): |
| 110 | o = factory() |
| 111 | ref = weakref.ref(o) |
| 112 | self.assert_(ref() is not None, |
| 113 | "weak reference to live object should be live") |
| 114 | o2 = ref() |
| 115 | self.assert_(o is o2, |
| 116 | "<ref>() should return original object if live") |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 117 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 118 | def check_basic_callback(self, factory): |
| 119 | self.cbcalled = 0 |
| 120 | o = factory() |
| 121 | ref = weakref.ref(o, self.callback) |
| 122 | del o |
Fred Drake | 705088e | 2001-04-13 17:18:15 +0000 | [diff] [blame] | 123 | self.assert_(self.cbcalled == 1, |
| 124 | "callback did not properly set 'cbcalled'") |
| 125 | self.assert_(ref() is None, |
| 126 | "ref2 should be dead after deleting object reference") |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 127 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 128 | def test_ref_reuse(self): |
| 129 | o = C() |
| 130 | ref1 = weakref.ref(o) |
| 131 | # create a proxy to make sure that there's an intervening creation |
| 132 | # between these two; it should make no difference |
| 133 | proxy = weakref.proxy(o) |
| 134 | ref2 = weakref.ref(o) |
| 135 | self.assert_(ref1 is ref2, |
| 136 | "reference object w/out callback should be re-used") |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 137 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 138 | o = C() |
| 139 | proxy = weakref.proxy(o) |
| 140 | ref1 = weakref.ref(o) |
| 141 | ref2 = weakref.ref(o) |
| 142 | self.assert_(ref1 is ref2, |
| 143 | "reference object w/out callback should be re-used") |
| 144 | self.assert_(weakref.getweakrefcount(o) == 2, |
| 145 | "wrong weak ref count for object") |
| 146 | del proxy |
| 147 | self.assert_(weakref.getweakrefcount(o) == 1, |
| 148 | "wrong weak ref count for object after deleting proxy") |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 149 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 150 | def test_proxy_reuse(self): |
| 151 | o = C() |
| 152 | proxy1 = weakref.proxy(o) |
| 153 | ref = weakref.ref(o) |
| 154 | proxy2 = weakref.proxy(o) |
| 155 | self.assert_(proxy1 is proxy2, |
| 156 | "proxy object w/out callback should have been re-used") |
| 157 | |
| 158 | def test_basic_proxy(self): |
| 159 | o = C() |
| 160 | self.check_proxy(o, weakref.proxy(o)) |
| 161 | |
Fred Drake | 5935ff0 | 2001-12-19 16:54:23 +0000 | [diff] [blame] | 162 | L = UserList.UserList() |
| 163 | p = weakref.proxy(L) |
| 164 | self.failIf(p, "proxy for empty UserList should be false") |
| 165 | p.append(12) |
| 166 | self.assertEqual(len(L), 1) |
| 167 | self.failUnless(p, "proxy for non-empty UserList should be true") |
| 168 | p[:] = [2, 3] |
| 169 | self.assertEqual(len(L), 2) |
| 170 | self.assertEqual(len(p), 2) |
| 171 | self.failUnless(3 in p, "proxy didn't support __contains__() properly") |
| 172 | p[1] = 5 |
| 173 | self.assertEqual(L[1], 5) |
| 174 | self.assertEqual(p[1], 5) |
| 175 | L2 = UserList.UserList(L) |
| 176 | p2 = weakref.proxy(L2) |
| 177 | self.assertEqual(p, p2) |
Fred Drake | 43735da | 2002-04-11 03:59:42 +0000 | [diff] [blame] | 178 | ## self.assertEqual(`L2`, `p2`) |
| 179 | L3 = UserList.UserList(range(10)) |
| 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 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 186 | def test_callable_proxy(self): |
| 187 | o = Callable() |
| 188 | ref1 = weakref.proxy(o) |
| 189 | |
| 190 | self.check_proxy(o, ref1) |
| 191 | |
| 192 | self.assert_(type(ref1) is weakref.CallableProxyType, |
| 193 | "proxy is not of callable type") |
| 194 | ref1('twinkies!') |
| 195 | self.assert_(o.bar == 'twinkies!', |
| 196 | "call through proxy not passed through to original") |
Fred Drake | 3bb4d21 | 2001-10-18 19:28:29 +0000 | [diff] [blame] | 197 | ref1(x='Splat.') |
| 198 | self.assert_(o.bar == 'Splat.', |
| 199 | "call through proxy not passed through to original") |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 200 | |
| 201 | # expect due to too few args |
| 202 | self.assertRaises(TypeError, ref1) |
| 203 | |
| 204 | # expect due to too many args |
| 205 | self.assertRaises(TypeError, ref1, 1, 2, 3) |
| 206 | |
| 207 | def check_proxy(self, o, proxy): |
| 208 | o.foo = 1 |
| 209 | self.assert_(proxy.foo == 1, |
| 210 | "proxy does not reflect attribute addition") |
| 211 | o.foo = 2 |
| 212 | self.assert_(proxy.foo == 2, |
| 213 | "proxy does not reflect attribute modification") |
| 214 | del o.foo |
| 215 | self.assert_(not hasattr(proxy, 'foo'), |
| 216 | "proxy does not reflect attribute removal") |
| 217 | |
| 218 | proxy.foo = 1 |
| 219 | self.assert_(o.foo == 1, |
| 220 | "object does not reflect attribute addition via proxy") |
| 221 | proxy.foo = 2 |
| 222 | self.assert_( |
| 223 | o.foo == 2, |
| 224 | "object does not reflect attribute modification via proxy") |
| 225 | del proxy.foo |
| 226 | self.assert_(not hasattr(o, 'foo'), |
| 227 | "object does not reflect attribute removal via proxy") |
| 228 | |
| 229 | def test_getweakrefcount(self): |
| 230 | o = C() |
| 231 | ref1 = weakref.ref(o) |
| 232 | ref2 = weakref.ref(o, self.callback) |
| 233 | self.assert_(weakref.getweakrefcount(o) == 2, |
| 234 | "got wrong number of weak reference objects") |
| 235 | |
| 236 | proxy1 = weakref.proxy(o) |
| 237 | proxy2 = weakref.proxy(o, self.callback) |
| 238 | self.assert_(weakref.getweakrefcount(o) == 4, |
| 239 | "got wrong number of weak reference objects") |
| 240 | |
| 241 | def test_getweakrefs(self): |
| 242 | o = C() |
| 243 | ref1 = weakref.ref(o, self.callback) |
| 244 | ref2 = weakref.ref(o, self.callback) |
| 245 | del ref1 |
| 246 | self.assert_(weakref.getweakrefs(o) == [ref2], |
| 247 | "list of refs does not match") |
| 248 | |
| 249 | o = C() |
| 250 | ref1 = weakref.ref(o, self.callback) |
| 251 | ref2 = weakref.ref(o, self.callback) |
| 252 | del ref2 |
| 253 | self.assert_(weakref.getweakrefs(o) == [ref1], |
| 254 | "list of refs does not match") |
| 255 | |
Fred Drake | 39c27f1 | 2001-10-18 18:06:05 +0000 | [diff] [blame] | 256 | def test_newstyle_number_ops(self): |
| 257 | class F(float): |
| 258 | pass |
| 259 | f = F(2.0) |
| 260 | p = weakref.proxy(f) |
| 261 | self.assert_(p + 1.0 == 3.0) |
| 262 | self.assert_(1.0 + p == 3.0) # this used to SEGV |
| 263 | |
Fred Drake | 2a64f46 | 2001-12-10 23:46:02 +0000 | [diff] [blame] | 264 | def test_callbacks_protected(self): |
Guido van Rossum | 9eee554 | 2002-08-22 20:21:30 +0000 | [diff] [blame] | 265 | # Callbacks protected from already-set exceptions? |
Fred Drake | 2a64f46 | 2001-12-10 23:46:02 +0000 | [diff] [blame] | 266 | # Regression test for SF bug #478534. |
| 267 | class BogusError(Exception): |
| 268 | pass |
| 269 | data = {} |
| 270 | def remove(k): |
| 271 | del data[k] |
| 272 | def encapsulate(): |
| 273 | f = lambda : () |
| 274 | data[weakref.ref(f, remove)] = None |
| 275 | raise BogusError |
| 276 | try: |
| 277 | encapsulate() |
| 278 | except BogusError: |
| 279 | pass |
| 280 | else: |
| 281 | self.fail("exception not properly restored") |
| 282 | try: |
| 283 | encapsulate() |
| 284 | except BogusError: |
| 285 | pass |
| 286 | else: |
| 287 | self.fail("exception not properly restored") |
| 288 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 289 | |
| 290 | class Object: |
| 291 | def __init__(self, arg): |
| 292 | self.arg = arg |
| 293 | def __repr__(self): |
| 294 | return "<Object %r>" % self.arg |
| 295 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 296 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 297 | class MappingTestCase(TestBase): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 298 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 299 | COUNT = 10 |
| 300 | |
| 301 | def test_weak_values(self): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 302 | # |
| 303 | # This exercises d.copy(), d.items(), d[], del d[], len(d). |
| 304 | # |
| 305 | dict, objects = self.make_weak_valued_dict() |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 306 | for o in objects: |
| 307 | self.assert_(weakref.getweakrefcount(o) == 1, |
| 308 | "wrong number of weak references to %r!" % o) |
| 309 | self.assert_(o is dict[o.arg], |
| 310 | "wrong object returned by weak dict!") |
| 311 | items1 = dict.items() |
| 312 | items2 = dict.copy().items() |
| 313 | items1.sort() |
| 314 | items2.sort() |
| 315 | self.assert_(items1 == items2, |
| 316 | "cloning of weak-valued dictionary did not work!") |
| 317 | del items1, items2 |
| 318 | self.assert_(len(dict) == self.COUNT) |
| 319 | del objects[0] |
| 320 | self.assert_(len(dict) == (self.COUNT - 1), |
| 321 | "deleting object did not cause dictionary update") |
| 322 | del objects, o |
| 323 | self.assert_(len(dict) == 0, |
| 324 | "deleting the values did not clear the dictionary") |
Fred Drake | 4fd06e0 | 2001-08-03 04:11:27 +0000 | [diff] [blame] | 325 | # regression on SF bug #447152: |
| 326 | dict = weakref.WeakValueDictionary() |
| 327 | self.assertRaises(KeyError, dict.__getitem__, 1) |
| 328 | dict[2] = C() |
| 329 | self.assertRaises(KeyError, dict.__getitem__, 2) |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 330 | |
| 331 | def test_weak_keys(self): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 332 | # |
| 333 | # This exercises d.copy(), d.items(), d[] = v, d[], del d[], |
Fred Drake | 752eda4 | 2001-11-06 16:38:34 +0000 | [diff] [blame] | 334 | # len(d), d.has_key(). |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 335 | # |
| 336 | dict, objects = self.make_weak_keyed_dict() |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 337 | for o in objects: |
| 338 | self.assert_(weakref.getweakrefcount(o) == 1, |
| 339 | "wrong number of weak references to %r!" % o) |
| 340 | self.assert_(o.arg is dict[o], |
| 341 | "wrong object returned by weak dict!") |
| 342 | items1 = dict.items() |
| 343 | items2 = dict.copy().items() |
Raymond Hettinger | 91bbd9a | 2003-05-02 09:06:28 +0000 | [diff] [blame] | 344 | self.assert_(Set(items1) == Set(items2), |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 345 | "cloning of weak-keyed dictionary did not work!") |
| 346 | del items1, items2 |
| 347 | self.assert_(len(dict) == self.COUNT) |
| 348 | del objects[0] |
| 349 | self.assert_(len(dict) == (self.COUNT - 1), |
| 350 | "deleting object did not cause dictionary update") |
| 351 | del objects, o |
| 352 | self.assert_(len(dict) == 0, |
| 353 | "deleting the keys did not clear the dictionary") |
Fred Drake | 752eda4 | 2001-11-06 16:38:34 +0000 | [diff] [blame] | 354 | o = Object(42) |
| 355 | dict[o] = "What is the meaning of the universe?" |
| 356 | self.assert_(dict.has_key(o)) |
| 357 | self.assert_(not dict.has_key(34)) |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 358 | |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 359 | def test_weak_keyed_iters(self): |
| 360 | dict, objects = self.make_weak_keyed_dict() |
| 361 | self.check_iters(dict) |
| 362 | |
| 363 | def test_weak_valued_iters(self): |
| 364 | dict, objects = self.make_weak_valued_dict() |
| 365 | self.check_iters(dict) |
| 366 | |
| 367 | def check_iters(self, dict): |
| 368 | # item iterator: |
| 369 | items = dict.items() |
| 370 | for item in dict.iteritems(): |
| 371 | items.remove(item) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 372 | self.assert_(len(items) == 0, "iteritems() did not touch all items") |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 373 | |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 374 | # key iterator, via __iter__(): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 375 | keys = dict.keys() |
| 376 | for k in dict: |
| 377 | keys.remove(k) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 378 | self.assert_(len(keys) == 0, "__iter__() did not touch all keys") |
| 379 | |
| 380 | # key iterator, via iterkeys(): |
| 381 | keys = dict.keys() |
| 382 | for k in dict.iterkeys(): |
| 383 | keys.remove(k) |
| 384 | self.assert_(len(keys) == 0, "iterkeys() did not touch all keys") |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 385 | |
| 386 | # value iterator: |
| 387 | values = dict.values() |
| 388 | for v in dict.itervalues(): |
| 389 | values.remove(v) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 390 | self.assert_(len(values) == 0, "itervalues() did not touch all values") |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 391 | |
Guido van Rossum | 009afb7 | 2002-06-10 20:00:52 +0000 | [diff] [blame] | 392 | def test_make_weak_keyed_dict_from_dict(self): |
| 393 | o = Object(3) |
| 394 | dict = weakref.WeakKeyDictionary({o:364}) |
| 395 | self.assert_(dict[o] == 364) |
| 396 | |
| 397 | def test_make_weak_keyed_dict_from_weak_keyed_dict(self): |
| 398 | o = Object(3) |
| 399 | dict = weakref.WeakKeyDictionary({o:364}) |
| 400 | dict2 = weakref.WeakKeyDictionary(dict) |
| 401 | self.assert_(dict[o] == 364) |
| 402 | |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 403 | def make_weak_keyed_dict(self): |
| 404 | dict = weakref.WeakKeyDictionary() |
| 405 | objects = map(Object, range(self.COUNT)) |
| 406 | for o in objects: |
| 407 | dict[o] = o.arg |
| 408 | return dict, objects |
| 409 | |
| 410 | def make_weak_valued_dict(self): |
| 411 | dict = weakref.WeakValueDictionary() |
| 412 | objects = map(Object, range(self.COUNT)) |
| 413 | for o in objects: |
| 414 | dict[o.arg] = o |
| 415 | return dict, objects |
| 416 | |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 417 | def check_popitem(self, klass, key1, value1, key2, value2): |
| 418 | weakdict = klass() |
| 419 | weakdict[key1] = value1 |
| 420 | weakdict[key2] = value2 |
| 421 | self.assert_(len(weakdict) == 2) |
| 422 | k, v = weakdict.popitem() |
| 423 | self.assert_(len(weakdict) == 1) |
| 424 | if k is key1: |
| 425 | self.assert_(v is value1) |
| 426 | else: |
| 427 | self.assert_(v is value2) |
| 428 | k, v = weakdict.popitem() |
| 429 | self.assert_(len(weakdict) == 0) |
| 430 | if k is key1: |
| 431 | self.assert_(v is value1) |
| 432 | else: |
| 433 | self.assert_(v is value2) |
| 434 | |
| 435 | def test_weak_valued_dict_popitem(self): |
| 436 | self.check_popitem(weakref.WeakValueDictionary, |
| 437 | "key1", C(), "key2", C()) |
| 438 | |
| 439 | def test_weak_keyed_dict_popitem(self): |
| 440 | self.check_popitem(weakref.WeakKeyDictionary, |
| 441 | C(), "value 1", C(), "value 2") |
| 442 | |
| 443 | def check_setdefault(self, klass, key, value1, value2): |
| 444 | self.assert_(value1 is not value2, |
| 445 | "invalid test" |
| 446 | " -- value parameters must be distinct objects") |
| 447 | weakdict = klass() |
| 448 | o = weakdict.setdefault(key, value1) |
| 449 | self.assert_(o is value1) |
| 450 | self.assert_(weakdict.has_key(key)) |
| 451 | self.assert_(weakdict.get(key) is value1) |
| 452 | self.assert_(weakdict[key] is value1) |
| 453 | |
| 454 | o = weakdict.setdefault(key, value2) |
| 455 | self.assert_(o is value1) |
| 456 | self.assert_(weakdict.has_key(key)) |
| 457 | self.assert_(weakdict.get(key) is value1) |
| 458 | self.assert_(weakdict[key] is value1) |
| 459 | |
| 460 | def test_weak_valued_dict_setdefault(self): |
| 461 | self.check_setdefault(weakref.WeakValueDictionary, |
| 462 | "key", C(), C()) |
| 463 | |
| 464 | def test_weak_keyed_dict_setdefault(self): |
| 465 | self.check_setdefault(weakref.WeakKeyDictionary, |
| 466 | C(), "value 1", "value 2") |
| 467 | |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 468 | def check_update(self, klass, dict): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 469 | # |
| 470 | # This exercises d.update(), len(d), d.keys(), d.has_key(), |
| 471 | # d.get(), d[]. |
| 472 | # |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 473 | weakdict = klass() |
| 474 | weakdict.update(dict) |
| 475 | self.assert_(len(weakdict) == len(dict)) |
| 476 | for k in weakdict.keys(): |
| 477 | self.assert_(dict.has_key(k), |
| 478 | "mysterious new key appeared in weak dict") |
| 479 | v = dict.get(k) |
| 480 | self.assert_(v is weakdict[k]) |
| 481 | self.assert_(v is weakdict.get(k)) |
| 482 | for k in dict.keys(): |
| 483 | self.assert_(weakdict.has_key(k), |
| 484 | "original key disappeared in weak dict") |
| 485 | v = dict[k] |
| 486 | self.assert_(v is weakdict[k]) |
| 487 | self.assert_(v is weakdict.get(k)) |
| 488 | |
| 489 | def test_weak_valued_dict_update(self): |
| 490 | self.check_update(weakref.WeakValueDictionary, |
| 491 | {1: C(), 'a': C(), C(): C()}) |
| 492 | |
| 493 | def test_weak_keyed_dict_update(self): |
| 494 | self.check_update(weakref.WeakKeyDictionary, |
| 495 | {C(): 1, C(): 2, C(): 3}) |
| 496 | |
Fred Drake | ccc7562 | 2001-09-06 14:52:39 +0000 | [diff] [blame] | 497 | def test_weak_keyed_delitem(self): |
| 498 | d = weakref.WeakKeyDictionary() |
| 499 | o1 = Object('1') |
| 500 | o2 = Object('2') |
| 501 | d[o1] = 'something' |
| 502 | d[o2] = 'something' |
| 503 | self.assert_(len(d) == 2) |
| 504 | del d[o1] |
| 505 | self.assert_(len(d) == 1) |
| 506 | self.assert_(d.keys() == [o2]) |
| 507 | |
| 508 | def test_weak_valued_delitem(self): |
| 509 | d = weakref.WeakValueDictionary() |
| 510 | o1 = Object('1') |
| 511 | o2 = Object('2') |
| 512 | d['something'] = o1 |
| 513 | d['something else'] = o2 |
| 514 | self.assert_(len(d) == 2) |
| 515 | del d['something'] |
| 516 | self.assert_(len(d) == 1) |
| 517 | self.assert_(d.items() == [('something else', o2)]) |
| 518 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 519 | from test_userdict import TestMappingProtocol |
| 520 | |
| 521 | class WeakValueDictionaryTestCase(TestMappingProtocol): |
| 522 | """Check that WeakValueDictionary class conforms to the mapping protocol""" |
| 523 | __ref = {"key1":Object(1), "key2":Object(2), "key3":Object(3)} |
| 524 | _tested_class = weakref.WeakValueDictionary |
| 525 | def _reference(self): |
| 526 | return self.__ref.copy() |
| 527 | |
| 528 | class WeakKeyDictionaryTestCase(TestMappingProtocol): |
| 529 | """Check that WeakKeyDictionary class conforms to the mapping protocol""" |
| 530 | __ref = {Object("key1"):1, Object("key2"):2, Object("key3"):3} |
| 531 | _tested_class = weakref.WeakKeyDictionary |
| 532 | def _reference(self): |
| 533 | return self.__ref.copy() |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 534 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 535 | def test_main(): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 536 | test_support.run_unittest( |
| 537 | ReferencesTestCase, |
| 538 | MappingTestCase, |
| 539 | WeakValueDictionaryTestCase, |
| 540 | WeakKeyDictionaryTestCase |
| 541 | ) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 542 | |
| 543 | |
| 544 | if __name__ == "__main__": |
| 545 | test_main() |