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 |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 9 | import threading |
Antoine Pitrou | c1ee488 | 2016-12-19 10:56:40 +0100 | [diff] [blame] | 10 | import time |
Fish | 96d37db | 2019-02-07 14:51:59 -0500 | [diff] [blame] | 11 | import random |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 12 | |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 13 | from test import support |
| 14 | from test.support import script_helper |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 15 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 16 | # Used in ReferencesTestCase.test_ref_created_during_del() . |
| 17 | ref_from_del = None |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 18 | |
Richard Oudkerk | 7a3dae0 | 2013-05-05 23:05:00 +0100 | [diff] [blame] | 19 | # Used by FinalizeTestCase as a global that may be replaced by None |
| 20 | # when the interpreter shuts down. |
| 21 | _global_var = 'foobar' |
| 22 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 23 | class C: |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 24 | def method(self): |
| 25 | pass |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 26 | |
| 27 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 28 | class Callable: |
| 29 | bar = None |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 30 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 31 | def __call__(self, x): |
| 32 | self.bar = x |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 33 | |
| 34 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 35 | def create_function(): |
| 36 | def f(): pass |
| 37 | return f |
| 38 | |
| 39 | def create_bound_method(): |
| 40 | return C().method |
| 41 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 42 | |
Antoine Pitrou | e11fecb | 2012-11-11 19:36:51 +0100 | [diff] [blame] | 43 | class Object: |
| 44 | def __init__(self, arg): |
| 45 | self.arg = arg |
| 46 | def __repr__(self): |
| 47 | return "<Object %r>" % self.arg |
| 48 | def __eq__(self, other): |
| 49 | if isinstance(other, Object): |
| 50 | return self.arg == other.arg |
| 51 | return NotImplemented |
| 52 | def __lt__(self, other): |
| 53 | if isinstance(other, Object): |
| 54 | return self.arg < other.arg |
| 55 | return NotImplemented |
| 56 | def __hash__(self): |
| 57 | return hash(self.arg) |
Antoine Pitrou | c3afba1 | 2012-11-17 18:57:38 +0100 | [diff] [blame] | 58 | def some_method(self): |
| 59 | return 4 |
| 60 | def other_method(self): |
| 61 | return 5 |
| 62 | |
Antoine Pitrou | e11fecb | 2012-11-11 19:36:51 +0100 | [diff] [blame] | 63 | |
| 64 | class RefCycle: |
| 65 | def __init__(self): |
| 66 | self.cycle = self |
| 67 | |
| 68 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 69 | class TestBase(unittest.TestCase): |
| 70 | |
| 71 | def setUp(self): |
| 72 | self.cbcalled = 0 |
| 73 | |
| 74 | def callback(self, ref): |
| 75 | self.cbcalled += 1 |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 76 | |
| 77 | |
Antoine Pitrou | c1ee488 | 2016-12-19 10:56:40 +0100 | [diff] [blame] | 78 | @contextlib.contextmanager |
| 79 | def collect_in_thread(period=0.0001): |
| 80 | """ |
| 81 | Ensure GC collections happen in a different thread, at a high frequency. |
| 82 | """ |
Antoine Pitrou | c1ee488 | 2016-12-19 10:56:40 +0100 | [diff] [blame] | 83 | please_stop = False |
| 84 | |
| 85 | def collect(): |
| 86 | while not please_stop: |
| 87 | time.sleep(period) |
| 88 | gc.collect() |
| 89 | |
| 90 | with support.disable_gc(): |
| 91 | t = threading.Thread(target=collect) |
| 92 | t.start() |
| 93 | try: |
| 94 | yield |
| 95 | finally: |
| 96 | please_stop = True |
| 97 | t.join() |
| 98 | |
| 99 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 100 | class ReferencesTestCase(TestBase): |
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 test_basic_ref(self): |
| 103 | self.check_basic_ref(C) |
| 104 | self.check_basic_ref(create_function) |
| 105 | self.check_basic_ref(create_bound_method) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 106 | |
Fred Drake | 43735da | 2002-04-11 03:59:42 +0000 | [diff] [blame] | 107 | # Just make sure the tp_repr handler doesn't raise an exception. |
| 108 | # Live reference: |
| 109 | o = C() |
| 110 | wr = weakref.ref(o) |
Brett Cannon | 0b70cca | 2006-08-25 02:59:59 +0000 | [diff] [blame] | 111 | repr(wr) |
Fred Drake | 43735da | 2002-04-11 03:59:42 +0000 | [diff] [blame] | 112 | # Dead reference: |
| 113 | del o |
Brett Cannon | 0b70cca | 2006-08-25 02:59:59 +0000 | [diff] [blame] | 114 | repr(wr) |
Fred Drake | 43735da | 2002-04-11 03:59:42 +0000 | [diff] [blame] | 115 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 116 | def test_basic_callback(self): |
| 117 | self.check_basic_callback(C) |
| 118 | self.check_basic_callback(create_function) |
| 119 | self.check_basic_callback(create_bound_method) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 120 | |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 121 | @support.cpython_only |
| 122 | def test_cfunction(self): |
| 123 | import _testcapi |
| 124 | create_cfunction = _testcapi.create_cfunction |
| 125 | f = create_cfunction() |
| 126 | wr = weakref.ref(f) |
| 127 | self.assertIs(wr(), f) |
| 128 | del f |
| 129 | self.assertIsNone(wr()) |
| 130 | self.check_basic_ref(create_cfunction) |
| 131 | self.check_basic_callback(create_cfunction) |
| 132 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 133 | def test_multiple_callbacks(self): |
| 134 | o = C() |
| 135 | ref1 = weakref.ref(o, self.callback) |
| 136 | ref2 = weakref.ref(o, self.callback) |
| 137 | del o |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 138 | self.assertIsNone(ref1(), "expected reference to be invalidated") |
| 139 | self.assertIsNone(ref2(), "expected reference to be invalidated") |
| 140 | self.assertEqual(self.cbcalled, 2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 141 | "callback not called the right number of times") |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 142 | |
Fred Drake | 705088e | 2001-04-13 17:18:15 +0000 | [diff] [blame] | 143 | def test_multiple_selfref_callbacks(self): |
Guido van Rossum | 9eee554 | 2002-08-22 20:21:30 +0000 | [diff] [blame] | 144 | # Make sure all references are invalidated before callbacks are called |
Fred Drake | 705088e | 2001-04-13 17:18:15 +0000 | [diff] [blame] | 145 | # |
| 146 | # What's important here is that we're using the first |
| 147 | # reference in the callback invoked on the second reference |
| 148 | # (the most recently created ref is cleaned up first). This |
| 149 | # tests that all references to the object are invalidated |
| 150 | # before any of the callbacks are invoked, so that we only |
| 151 | # have one invocation of _weakref.c:cleanup_helper() active |
| 152 | # for a particular object at a time. |
| 153 | # |
| 154 | def callback(object, self=self): |
| 155 | self.ref() |
| 156 | c = C() |
| 157 | self.ref = weakref.ref(c, callback) |
| 158 | ref1 = weakref.ref(c, callback) |
| 159 | del c |
| 160 | |
Serhiy Storchaka | 21eb487 | 2016-05-07 15:41:09 +0300 | [diff] [blame] | 161 | def test_constructor_kwargs(self): |
| 162 | c = C() |
| 163 | self.assertRaises(TypeError, weakref.ref, c, callback=None) |
| 164 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 165 | def test_proxy_ref(self): |
| 166 | o = C() |
| 167 | o.bar = 1 |
| 168 | ref1 = weakref.proxy(o, self.callback) |
| 169 | ref2 = weakref.proxy(o, self.callback) |
| 170 | del o |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 171 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 172 | def check(proxy): |
| 173 | proxy.bar |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 174 | |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 175 | self.assertRaises(ReferenceError, check, ref1) |
| 176 | self.assertRaises(ReferenceError, check, ref2) |
| 177 | self.assertRaises(ReferenceError, bool, weakref.proxy(C())) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 178 | self.assertEqual(self.cbcalled, 2) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 179 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 180 | def check_basic_ref(self, factory): |
| 181 | o = factory() |
| 182 | ref = weakref.ref(o) |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 183 | self.assertIsNotNone(ref(), |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 184 | "weak reference to live object should be live") |
| 185 | o2 = ref() |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 186 | self.assertIs(o, o2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 187 | "<ref>() should return original object if live") |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 188 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 189 | def check_basic_callback(self, factory): |
| 190 | self.cbcalled = 0 |
| 191 | o = factory() |
| 192 | ref = weakref.ref(o, self.callback) |
| 193 | del o |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 194 | self.assertEqual(self.cbcalled, 1, |
Fred Drake | 705088e | 2001-04-13 17:18:15 +0000 | [diff] [blame] | 195 | "callback did not properly set 'cbcalled'") |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 196 | self.assertIsNone(ref(), |
Fred Drake | 705088e | 2001-04-13 17:18:15 +0000 | [diff] [blame] | 197 | "ref2 should be dead after deleting object reference") |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 198 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 199 | def test_ref_reuse(self): |
| 200 | o = C() |
| 201 | ref1 = weakref.ref(o) |
| 202 | # create a proxy to make sure that there's an intervening creation |
| 203 | # between these two; it should make no difference |
| 204 | proxy = weakref.proxy(o) |
| 205 | ref2 = weakref.ref(o) |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 206 | self.assertIs(ref1, ref2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 207 | "reference object w/out callback should be re-used") |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 208 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 209 | o = C() |
| 210 | proxy = weakref.proxy(o) |
| 211 | ref1 = weakref.ref(o) |
| 212 | ref2 = weakref.ref(o) |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 213 | self.assertIs(ref1, ref2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 214 | "reference object w/out callback should be re-used") |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 215 | self.assertEqual(weakref.getweakrefcount(o), 2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 216 | "wrong weak ref count for object") |
| 217 | del proxy |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 218 | self.assertEqual(weakref.getweakrefcount(o), 1, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 219 | "wrong weak ref count for object after deleting proxy") |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 220 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 221 | def test_proxy_reuse(self): |
| 222 | o = C() |
| 223 | proxy1 = weakref.proxy(o) |
| 224 | ref = weakref.ref(o) |
| 225 | proxy2 = weakref.proxy(o) |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 226 | self.assertIs(proxy1, proxy2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 227 | "proxy object w/out callback should have been re-used") |
| 228 | |
| 229 | def test_basic_proxy(self): |
| 230 | o = C() |
| 231 | self.check_proxy(o, weakref.proxy(o)) |
| 232 | |
Raymond Hettinger | 53dbe39 | 2008-02-12 20:03:09 +0000 | [diff] [blame] | 233 | L = collections.UserList() |
Fred Drake | 5935ff0 | 2001-12-19 16:54:23 +0000 | [diff] [blame] | 234 | p = weakref.proxy(L) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 235 | self.assertFalse(p, "proxy for empty UserList should be false") |
Fred Drake | 5935ff0 | 2001-12-19 16:54:23 +0000 | [diff] [blame] | 236 | p.append(12) |
| 237 | self.assertEqual(len(L), 1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 238 | self.assertTrue(p, "proxy for non-empty UserList should be true") |
Fred Drake | 5935ff0 | 2001-12-19 16:54:23 +0000 | [diff] [blame] | 239 | p[:] = [2, 3] |
| 240 | self.assertEqual(len(L), 2) |
| 241 | self.assertEqual(len(p), 2) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 242 | self.assertIn(3, p, "proxy didn't support __contains__() properly") |
Fred Drake | 5935ff0 | 2001-12-19 16:54:23 +0000 | [diff] [blame] | 243 | p[1] = 5 |
| 244 | self.assertEqual(L[1], 5) |
| 245 | self.assertEqual(p[1], 5) |
Raymond Hettinger | 53dbe39 | 2008-02-12 20:03:09 +0000 | [diff] [blame] | 246 | L2 = collections.UserList(L) |
Fred Drake | 5935ff0 | 2001-12-19 16:54:23 +0000 | [diff] [blame] | 247 | p2 = weakref.proxy(L2) |
| 248 | self.assertEqual(p, p2) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 249 | ## self.assertEqual(repr(L2), repr(p2)) |
Raymond Hettinger | 53dbe39 | 2008-02-12 20:03:09 +0000 | [diff] [blame] | 250 | L3 = collections.UserList(range(10)) |
Fred Drake | 43735da | 2002-04-11 03:59:42 +0000 | [diff] [blame] | 251 | p3 = weakref.proxy(L3) |
| 252 | self.assertEqual(L3[:], p3[:]) |
| 253 | self.assertEqual(L3[5:], p3[5:]) |
| 254 | self.assertEqual(L3[:5], p3[:5]) |
| 255 | self.assertEqual(L3[2:5], p3[2:5]) |
Fred Drake | 5935ff0 | 2001-12-19 16:54:23 +0000 | [diff] [blame] | 256 | |
Benjamin Peterson | 3201977 | 2009-11-19 03:08:32 +0000 | [diff] [blame] | 257 | def test_proxy_unicode(self): |
| 258 | # See bug 5037 |
| 259 | class C(object): |
| 260 | def __str__(self): |
| 261 | return "string" |
| 262 | def __bytes__(self): |
| 263 | return b"bytes" |
| 264 | instance = C() |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 265 | self.assertIn("__bytes__", dir(weakref.proxy(instance))) |
Benjamin Peterson | 3201977 | 2009-11-19 03:08:32 +0000 | [diff] [blame] | 266 | self.assertEqual(bytes(weakref.proxy(instance)), b"bytes") |
| 267 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 268 | def test_proxy_index(self): |
| 269 | class C: |
| 270 | def __index__(self): |
| 271 | return 10 |
| 272 | o = C() |
| 273 | p = weakref.proxy(o) |
| 274 | self.assertEqual(operator.index(p), 10) |
| 275 | |
| 276 | def test_proxy_div(self): |
| 277 | class C: |
| 278 | def __floordiv__(self, other): |
| 279 | return 42 |
| 280 | def __ifloordiv__(self, other): |
| 281 | return 21 |
| 282 | o = C() |
| 283 | p = weakref.proxy(o) |
| 284 | self.assertEqual(p // 5, 42) |
| 285 | p //= 5 |
| 286 | self.assertEqual(p, 21) |
| 287 | |
Mark Dickinson | 7abb6c0 | 2019-04-26 15:56:15 +0900 | [diff] [blame] | 288 | def test_proxy_matmul(self): |
| 289 | class C: |
| 290 | def __matmul__(self, other): |
| 291 | return 1729 |
| 292 | def __rmatmul__(self, other): |
| 293 | return -163 |
| 294 | def __imatmul__(self, other): |
| 295 | return 561 |
| 296 | o = C() |
| 297 | p = weakref.proxy(o) |
| 298 | self.assertEqual(p @ 5, 1729) |
| 299 | self.assertEqual(5 @ p, -163) |
| 300 | p @= 5 |
| 301 | self.assertEqual(p, 561) |
| 302 | |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 303 | # The PyWeakref_* C API is documented as allowing either NULL or |
| 304 | # None as the value for the callback, where either means "no |
| 305 | # callback". The "no callback" ref and proxy objects are supposed |
| 306 | # 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] | 307 | # they are active. In Python 2.3.3 and earlier, this guarantee |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 308 | # was not honored, and was broken in different ways for |
| 309 | # PyWeakref_NewRef() and PyWeakref_NewProxy(). (Two tests.) |
| 310 | |
| 311 | def test_shared_ref_without_callback(self): |
| 312 | self.check_shared_without_callback(weakref.ref) |
| 313 | |
| 314 | def test_shared_proxy_without_callback(self): |
| 315 | self.check_shared_without_callback(weakref.proxy) |
| 316 | |
| 317 | def check_shared_without_callback(self, makeref): |
| 318 | o = Object(1) |
| 319 | p1 = makeref(o, None) |
| 320 | p2 = makeref(o, None) |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 321 | self.assertIs(p1, p2, "both callbacks were None in the C API") |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 322 | del p1, p2 |
| 323 | p1 = makeref(o) |
| 324 | p2 = makeref(o, None) |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 325 | self.assertIs(p1, p2, "callbacks were NULL, None in the C API") |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 326 | del p1, p2 |
| 327 | p1 = makeref(o) |
| 328 | p2 = makeref(o) |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 329 | self.assertIs(p1, p2, "both callbacks were NULL in the C API") |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 330 | del p1, p2 |
| 331 | p1 = makeref(o, None) |
| 332 | p2 = makeref(o) |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 333 | self.assertIs(p1, p2, "callbacks were None, NULL in the C API") |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 334 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 335 | def test_callable_proxy(self): |
| 336 | o = Callable() |
| 337 | ref1 = weakref.proxy(o) |
| 338 | |
| 339 | self.check_proxy(o, ref1) |
| 340 | |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 341 | self.assertIs(type(ref1), weakref.CallableProxyType, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 342 | "proxy is not of callable type") |
| 343 | ref1('twinkies!') |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 344 | self.assertEqual(o.bar, 'twinkies!', |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 345 | "call through proxy not passed through to original") |
Fred Drake | 3bb4d21 | 2001-10-18 19:28:29 +0000 | [diff] [blame] | 346 | ref1(x='Splat.') |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 347 | self.assertEqual(o.bar, 'Splat.', |
Fred Drake | 3bb4d21 | 2001-10-18 19:28:29 +0000 | [diff] [blame] | 348 | "call through proxy not passed through to original") |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 349 | |
| 350 | # expect due to too few args |
| 351 | self.assertRaises(TypeError, ref1) |
| 352 | |
| 353 | # expect due to too many args |
| 354 | self.assertRaises(TypeError, ref1, 1, 2, 3) |
| 355 | |
| 356 | def check_proxy(self, o, proxy): |
| 357 | o.foo = 1 |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 358 | self.assertEqual(proxy.foo, 1, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 359 | "proxy does not reflect attribute addition") |
| 360 | o.foo = 2 |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 361 | self.assertEqual(proxy.foo, 2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 362 | "proxy does not reflect attribute modification") |
| 363 | del o.foo |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 364 | self.assertFalse(hasattr(proxy, 'foo'), |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 365 | "proxy does not reflect attribute removal") |
| 366 | |
| 367 | proxy.foo = 1 |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 368 | self.assertEqual(o.foo, 1, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 369 | "object does not reflect attribute addition via proxy") |
| 370 | proxy.foo = 2 |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 371 | self.assertEqual(o.foo, 2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 372 | "object does not reflect attribute modification via proxy") |
| 373 | del proxy.foo |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 374 | self.assertFalse(hasattr(o, 'foo'), |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 375 | "object does not reflect attribute removal via proxy") |
| 376 | |
Raymond Hettinger | d693a81 | 2003-06-30 04:18:48 +0000 | [diff] [blame] | 377 | def test_proxy_deletion(self): |
| 378 | # Test clearing of SF bug #762891 |
| 379 | class Foo: |
| 380 | result = None |
| 381 | def __delitem__(self, accessor): |
| 382 | self.result = accessor |
| 383 | g = Foo() |
| 384 | f = weakref.proxy(g) |
| 385 | del f[0] |
| 386 | self.assertEqual(f.result, 0) |
| 387 | |
Raymond Hettinger | e6c470f | 2005-03-27 03:04:54 +0000 | [diff] [blame] | 388 | def test_proxy_bool(self): |
| 389 | # Test clearing of SF bug #1170766 |
| 390 | class List(list): pass |
| 391 | lyst = List() |
| 392 | self.assertEqual(bool(weakref.proxy(lyst)), bool(lyst)) |
| 393 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 394 | def test_getweakrefcount(self): |
| 395 | o = C() |
| 396 | ref1 = weakref.ref(o) |
| 397 | ref2 = weakref.ref(o, self.callback) |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 398 | self.assertEqual(weakref.getweakrefcount(o), 2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 399 | "got wrong number of weak reference objects") |
| 400 | |
| 401 | proxy1 = weakref.proxy(o) |
| 402 | proxy2 = weakref.proxy(o, self.callback) |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 403 | self.assertEqual(weakref.getweakrefcount(o), 4, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 404 | "got wrong number of weak reference objects") |
| 405 | |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 406 | del ref1, ref2, proxy1, proxy2 |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 407 | self.assertEqual(weakref.getweakrefcount(o), 0, |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 408 | "weak reference objects not unlinked from" |
| 409 | " referent when discarded.") |
| 410 | |
Walter Dörwald | b167b04 | 2003-12-11 12:34:05 +0000 | [diff] [blame] | 411 | # assumes ints do not support weakrefs |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 412 | self.assertEqual(weakref.getweakrefcount(1), 0, |
Walter Dörwald | b167b04 | 2003-12-11 12:34:05 +0000 | [diff] [blame] | 413 | "got wrong number of weak reference objects for int") |
| 414 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 415 | def test_getweakrefs(self): |
| 416 | o = C() |
| 417 | ref1 = weakref.ref(o, self.callback) |
| 418 | ref2 = weakref.ref(o, self.callback) |
| 419 | del ref1 |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 420 | self.assertEqual(weakref.getweakrefs(o), [ref2], |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 421 | "list of refs does not match") |
| 422 | |
| 423 | o = C() |
| 424 | ref1 = weakref.ref(o, self.callback) |
| 425 | ref2 = weakref.ref(o, self.callback) |
| 426 | del ref2 |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 427 | self.assertEqual(weakref.getweakrefs(o), [ref1], |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 428 | "list of refs does not match") |
| 429 | |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 430 | del ref1 |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 431 | self.assertEqual(weakref.getweakrefs(o), [], |
Fred Drake | ea2adc9 | 2004-02-03 19:56:46 +0000 | [diff] [blame] | 432 | "list of refs not cleared") |
| 433 | |
Walter Dörwald | b167b04 | 2003-12-11 12:34:05 +0000 | [diff] [blame] | 434 | # assumes ints do not support weakrefs |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 435 | self.assertEqual(weakref.getweakrefs(1), [], |
Walter Dörwald | b167b04 | 2003-12-11 12:34:05 +0000 | [diff] [blame] | 436 | "list of refs does not match for int") |
| 437 | |
Fred Drake | 39c27f1 | 2001-10-18 18:06:05 +0000 | [diff] [blame] | 438 | def test_newstyle_number_ops(self): |
| 439 | class F(float): |
| 440 | pass |
| 441 | f = F(2.0) |
| 442 | p = weakref.proxy(f) |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 443 | self.assertEqual(p + 1.0, 3.0) |
| 444 | self.assertEqual(1.0 + p, 3.0) # this used to SEGV |
Fred Drake | 39c27f1 | 2001-10-18 18:06:05 +0000 | [diff] [blame] | 445 | |
Fred Drake | 2a64f46 | 2001-12-10 23:46:02 +0000 | [diff] [blame] | 446 | def test_callbacks_protected(self): |
Guido van Rossum | 9eee554 | 2002-08-22 20:21:30 +0000 | [diff] [blame] | 447 | # Callbacks protected from already-set exceptions? |
Fred Drake | 2a64f46 | 2001-12-10 23:46:02 +0000 | [diff] [blame] | 448 | # Regression test for SF bug #478534. |
| 449 | class BogusError(Exception): |
| 450 | pass |
| 451 | data = {} |
| 452 | def remove(k): |
| 453 | del data[k] |
| 454 | def encapsulate(): |
| 455 | f = lambda : () |
| 456 | data[weakref.ref(f, remove)] = None |
| 457 | raise BogusError |
| 458 | try: |
| 459 | encapsulate() |
| 460 | except BogusError: |
| 461 | pass |
| 462 | else: |
| 463 | self.fail("exception not properly restored") |
| 464 | try: |
| 465 | encapsulate() |
| 466 | except BogusError: |
| 467 | pass |
| 468 | else: |
| 469 | self.fail("exception not properly restored") |
| 470 | |
Tim Peters | add09b4 | 2003-11-12 20:43:28 +0000 | [diff] [blame] | 471 | def test_sf_bug_840829(self): |
| 472 | # "weakref callbacks and gc corrupt memory" |
| 473 | # subtype_dealloc erroneously exposed a new-style instance |
| 474 | # already in the process of getting deallocated to gc, |
| 475 | # causing double-deallocation if the instance had a weakref |
| 476 | # callback that triggered gc. |
| 477 | # If the bug exists, there probably won't be an obvious symptom |
| 478 | # in a release build. In a debug build, a segfault will occur |
| 479 | # when the second attempt to remove the instance from the "list |
| 480 | # of all objects" occurs. |
| 481 | |
| 482 | import gc |
| 483 | |
| 484 | class C(object): |
| 485 | pass |
| 486 | |
| 487 | c = C() |
| 488 | wr = weakref.ref(c, lambda ignore: gc.collect()) |
| 489 | del c |
| 490 | |
Tim Peters | f7f9e99 | 2003-11-13 21:59:32 +0000 | [diff] [blame] | 491 | # There endeth the first part. It gets worse. |
| 492 | del wr |
| 493 | |
| 494 | c1 = C() |
| 495 | c1.i = C() |
| 496 | wr = weakref.ref(c1.i, lambda ignore: gc.collect()) |
| 497 | |
| 498 | c2 = C() |
| 499 | c2.c1 = c1 |
| 500 | del c1 # still alive because c2 points to it |
| 501 | |
| 502 | # Now when subtype_dealloc gets called on c2, it's not enough just |
| 503 | # that c2 is immune from gc while the weakref callbacks associated |
| 504 | # with c2 execute (there are none in this 2nd half of the test, btw). |
| 505 | # subtype_dealloc goes on to call the base classes' deallocs too, |
| 506 | # so any gc triggered by weakref callbacks associated with anything |
| 507 | # torn down by a base class dealloc can also trigger double |
| 508 | # deallocation of c2. |
| 509 | del c2 |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 510 | |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 511 | def test_callback_in_cycle_1(self): |
| 512 | import gc |
| 513 | |
| 514 | class J(object): |
| 515 | pass |
| 516 | |
| 517 | class II(object): |
| 518 | def acallback(self, ignore): |
| 519 | self.J |
| 520 | |
| 521 | I = II() |
| 522 | I.J = J |
| 523 | I.wr = weakref.ref(J, I.acallback) |
| 524 | |
| 525 | # Now J and II are each in a self-cycle (as all new-style class |
| 526 | # objects are, since their __mro__ points back to them). I holds |
| 527 | # both a weak reference (I.wr) and a strong reference (I.J) to class |
| 528 | # J. I is also in a cycle (I.wr points to a weakref that references |
| 529 | # I.acallback). When we del these three, they all become trash, but |
| 530 | # the cycles prevent any of them from getting cleaned up immediately. |
| 531 | # Instead they have to wait for cyclic gc to deduce that they're |
| 532 | # trash. |
| 533 | # |
| 534 | # gc used to call tp_clear on all of them, and the order in which |
| 535 | # it does that is pretty accidental. The exact order in which we |
| 536 | # built up these things manages to provoke gc into running tp_clear |
| 537 | # in just the right order (I last). Calling tp_clear on II leaves |
| 538 | # behind an insane class object (its __mro__ becomes NULL). Calling |
| 539 | # tp_clear on J breaks its self-cycle, but J doesn't get deleted |
| 540 | # just then because of the strong reference from I.J. Calling |
| 541 | # tp_clear on I starts to clear I's __dict__, and just happens to |
| 542 | # clear I.J first -- I.wr is still intact. That removes the last |
| 543 | # reference to J, which triggers the weakref callback. The callback |
| 544 | # tries to do "self.J", and instances of new-style classes look up |
| 545 | # attributes ("J") in the class dict first. The class (II) wants to |
| 546 | # search II.__mro__, but that's NULL. The result was a segfault in |
| 547 | # a release build, and an assert failure in a debug build. |
| 548 | del I, J, II |
| 549 | gc.collect() |
| 550 | |
| 551 | def test_callback_in_cycle_2(self): |
| 552 | import gc |
| 553 | |
| 554 | # This is just like test_callback_in_cycle_1, except that II is an |
| 555 | # old-style class. The symptom is different then: an instance of an |
| 556 | # old-style class looks in its own __dict__ first. 'J' happens to |
| 557 | # get cleared from I.__dict__ before 'wr', and 'J' was never in II's |
| 558 | # __dict__, so the attribute isn't found. The difference is that |
| 559 | # the old-style II doesn't have a NULL __mro__ (it doesn't have any |
| 560 | # __mro__), so no segfault occurs. Instead it got: |
| 561 | # test_callback_in_cycle_2 (__main__.ReferencesTestCase) ... |
| 562 | # Exception exceptions.AttributeError: |
| 563 | # "II instance has no attribute 'J'" in <bound method II.acallback |
| 564 | # of <?.II instance at 0x00B9B4B8>> ignored |
| 565 | |
| 566 | class J(object): |
| 567 | pass |
| 568 | |
| 569 | class II: |
| 570 | def acallback(self, ignore): |
| 571 | self.J |
| 572 | |
| 573 | I = II() |
| 574 | I.J = J |
| 575 | I.wr = weakref.ref(J, I.acallback) |
| 576 | |
| 577 | del I, J, II |
| 578 | gc.collect() |
| 579 | |
| 580 | def test_callback_in_cycle_3(self): |
| 581 | import gc |
| 582 | |
| 583 | # This one broke the first patch that fixed the last two. In this |
| 584 | # case, the objects reachable from the callback aren't also reachable |
| 585 | # from the object (c1) *triggering* the callback: you can get to |
| 586 | # c1 from c2, but not vice-versa. The result was that c2's __dict__ |
| 587 | # got tp_clear'ed by the time the c2.cb callback got invoked. |
| 588 | |
| 589 | class C: |
| 590 | def cb(self, ignore): |
| 591 | self.me |
| 592 | self.c1 |
| 593 | self.wr |
| 594 | |
| 595 | c1, c2 = C(), C() |
| 596 | |
| 597 | c2.me = c2 |
| 598 | c2.c1 = c1 |
| 599 | c2.wr = weakref.ref(c1, c2.cb) |
| 600 | |
| 601 | del c1, c2 |
| 602 | gc.collect() |
| 603 | |
| 604 | def test_callback_in_cycle_4(self): |
| 605 | import gc |
| 606 | |
| 607 | # Like test_callback_in_cycle_3, except c2 and c1 have different |
| 608 | # classes. c2's class (C) isn't reachable from c1 then, so protecting |
| 609 | # objects reachable from the dying object (c1) isn't enough to stop |
| 610 | # c2's class (C) from getting tp_clear'ed before c2.cb is invoked. |
| 611 | # The result was a segfault (C.__mro__ was NULL when the callback |
| 612 | # tried to look up self.me). |
| 613 | |
| 614 | class C(object): |
| 615 | def cb(self, ignore): |
| 616 | self.me |
| 617 | self.c1 |
| 618 | self.wr |
| 619 | |
| 620 | class D: |
| 621 | pass |
| 622 | |
| 623 | c1, c2 = D(), C() |
| 624 | |
| 625 | c2.me = c2 |
| 626 | c2.c1 = c1 |
| 627 | c2.wr = weakref.ref(c1, c2.cb) |
| 628 | |
| 629 | del c1, c2, C, D |
| 630 | gc.collect() |
| 631 | |
Serhiy Storchaka | a793037 | 2016-07-03 22:27:26 +0300 | [diff] [blame] | 632 | @support.requires_type_collecting |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 633 | def test_callback_in_cycle_resurrection(self): |
| 634 | import gc |
| 635 | |
| 636 | # Do something nasty in a weakref callback: resurrect objects |
| 637 | # from dead cycles. For this to be attempted, the weakref and |
| 638 | # its callback must also be part of the cyclic trash (else the |
| 639 | # objects reachable via the callback couldn't be in cyclic trash |
| 640 | # to begin with -- the callback would act like an external root). |
| 641 | # But gc clears trash weakrefs with callbacks early now, which |
| 642 | # disables the callbacks, so the callbacks shouldn't get called |
| 643 | # at all (and so nothing actually gets resurrected). |
| 644 | |
| 645 | alist = [] |
| 646 | class C(object): |
| 647 | def __init__(self, value): |
| 648 | self.attribute = value |
| 649 | |
| 650 | def acallback(self, ignore): |
| 651 | alist.append(self.c) |
| 652 | |
| 653 | c1, c2 = C(1), C(2) |
| 654 | c1.c = c2 |
| 655 | c2.c = c1 |
| 656 | c1.wr = weakref.ref(c2, c1.acallback) |
| 657 | c2.wr = weakref.ref(c1, c2.acallback) |
| 658 | |
| 659 | def C_went_away(ignore): |
| 660 | alist.append("C went away") |
| 661 | wr = weakref.ref(C, C_went_away) |
| 662 | |
| 663 | del c1, c2, C # make them all trash |
| 664 | self.assertEqual(alist, []) # del isn't enough to reclaim anything |
| 665 | |
| 666 | gc.collect() |
| 667 | # c1.wr and c2.wr were part of the cyclic trash, so should have |
| 668 | # been cleared without their callbacks executing. OTOH, the weakref |
| 669 | # to C is bound to a function local (wr), and wasn't trash, so that |
| 670 | # callback should have been invoked when C went away. |
| 671 | self.assertEqual(alist, ["C went away"]) |
| 672 | # The remaining weakref should be dead now (its callback ran). |
| 673 | self.assertEqual(wr(), None) |
| 674 | |
| 675 | del alist[:] |
| 676 | gc.collect() |
| 677 | self.assertEqual(alist, []) |
| 678 | |
| 679 | def test_callbacks_on_callback(self): |
| 680 | import gc |
| 681 | |
| 682 | # Set up weakref callbacks *on* weakref callbacks. |
| 683 | alist = [] |
| 684 | def safe_callback(ignore): |
| 685 | alist.append("safe_callback called") |
| 686 | |
| 687 | class C(object): |
| 688 | def cb(self, ignore): |
| 689 | alist.append("cb called") |
| 690 | |
| 691 | c, d = C(), C() |
| 692 | c.other = d |
| 693 | d.other = c |
| 694 | callback = c.cb |
| 695 | c.wr = weakref.ref(d, callback) # this won't trigger |
| 696 | d.wr = weakref.ref(callback, d.cb) # ditto |
| 697 | external_wr = weakref.ref(callback, safe_callback) # but this will |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 698 | self.assertIs(external_wr(), callback) |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 699 | |
| 700 | # The weakrefs attached to c and d should get cleared, so that |
| 701 | # C.cb is never called. But external_wr isn't part of the cyclic |
| 702 | # trash, and no cyclic trash is reachable from it, so safe_callback |
| 703 | # should get invoked when the bound method object callback (c.cb) |
| 704 | # -- which is itself a callback, and also part of the cyclic trash -- |
| 705 | # gets reclaimed at the end of gc. |
| 706 | |
| 707 | del callback, c, d, C |
| 708 | self.assertEqual(alist, []) # del isn't enough to clean up cycles |
| 709 | gc.collect() |
| 710 | self.assertEqual(alist, ["safe_callback called"]) |
| 711 | self.assertEqual(external_wr(), None) |
| 712 | |
| 713 | del alist[:] |
| 714 | gc.collect() |
| 715 | self.assertEqual(alist, []) |
| 716 | |
Fred Drake | bc875f5 | 2004-02-04 23:14:14 +0000 | [diff] [blame] | 717 | def test_gc_during_ref_creation(self): |
| 718 | self.check_gc_during_creation(weakref.ref) |
| 719 | |
| 720 | def test_gc_during_proxy_creation(self): |
| 721 | self.check_gc_during_creation(weakref.proxy) |
| 722 | |
| 723 | def check_gc_during_creation(self, makeref): |
| 724 | thresholds = gc.get_threshold() |
| 725 | gc.set_threshold(1, 1, 1) |
| 726 | gc.collect() |
Fred Drake | 55cf434 | 2004-02-13 19:21:57 +0000 | [diff] [blame] | 727 | class A: |
| 728 | pass |
Fred Drake | bc875f5 | 2004-02-04 23:14:14 +0000 | [diff] [blame] | 729 | |
| 730 | def callback(*args): |
| 731 | pass |
| 732 | |
Fred Drake | 55cf434 | 2004-02-13 19:21:57 +0000 | [diff] [blame] | 733 | referenced = A() |
Fred Drake | bc875f5 | 2004-02-04 23:14:14 +0000 | [diff] [blame] | 734 | |
Fred Drake | 55cf434 | 2004-02-13 19:21:57 +0000 | [diff] [blame] | 735 | a = A() |
Fred Drake | bc875f5 | 2004-02-04 23:14:14 +0000 | [diff] [blame] | 736 | a.a = a |
| 737 | a.wr = makeref(referenced) |
| 738 | |
| 739 | try: |
| 740 | # now make sure the object and the ref get labeled as |
| 741 | # cyclic trash: |
Fred Drake | 55cf434 | 2004-02-13 19:21:57 +0000 | [diff] [blame] | 742 | a = A() |
| 743 | weakref.ref(referenced, callback) |
Fred Drake | bc875f5 | 2004-02-04 23:14:14 +0000 | [diff] [blame] | 744 | |
| 745 | finally: |
| 746 | gc.set_threshold(*thresholds) |
| 747 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 748 | def test_ref_created_during_del(self): |
| 749 | # Bug #1377858 |
| 750 | # A weakref created in an object's __del__() would crash the |
| 751 | # interpreter when the weakref was cleaned up since it would refer to |
| 752 | # non-existent memory. This test should not segfault the interpreter. |
| 753 | class Target(object): |
| 754 | def __del__(self): |
| 755 | global ref_from_del |
| 756 | ref_from_del = weakref.ref(self) |
| 757 | |
| 758 | w = Target() |
| 759 | |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 760 | def test_init(self): |
| 761 | # Issue 3634 |
| 762 | # <weakref to class>.__init__() doesn't check errors correctly |
| 763 | r = weakref.ref(Exception) |
| 764 | self.assertRaises(TypeError, r.__init__, 0, 0, 0, 0, 0) |
| 765 | # No exception should be raised here |
| 766 | gc.collect() |
| 767 | |
Antoine Pitrou | 3af01a1 | 2010-03-31 21:40:47 +0000 | [diff] [blame] | 768 | def test_classes(self): |
| 769 | # Check that classes are weakrefable. |
| 770 | class A(object): |
| 771 | pass |
| 772 | l = [] |
| 773 | weakref.ref(int) |
| 774 | a = weakref.ref(A, l.append) |
| 775 | A = None |
| 776 | gc.collect() |
| 777 | self.assertEqual(a(), None) |
| 778 | self.assertEqual(l, [a]) |
| 779 | |
Antoine Pitrou | e11fecb | 2012-11-11 19:36:51 +0100 | [diff] [blame] | 780 | def test_equality(self): |
| 781 | # Alive weakrefs defer equality testing to their underlying object. |
| 782 | x = Object(1) |
| 783 | y = Object(1) |
| 784 | z = Object(2) |
| 785 | a = weakref.ref(x) |
| 786 | b = weakref.ref(y) |
| 787 | c = weakref.ref(z) |
| 788 | d = weakref.ref(x) |
| 789 | # Note how we directly test the operators here, to stress both |
| 790 | # __eq__ and __ne__. |
| 791 | self.assertTrue(a == b) |
| 792 | self.assertFalse(a != b) |
| 793 | self.assertFalse(a == c) |
| 794 | self.assertTrue(a != c) |
| 795 | self.assertTrue(a == d) |
| 796 | self.assertFalse(a != d) |
| 797 | del x, y, z |
| 798 | gc.collect() |
| 799 | for r in a, b, c: |
| 800 | # Sanity check |
| 801 | self.assertIs(r(), None) |
| 802 | # Dead weakrefs compare by identity: whether `a` and `d` are the |
| 803 | # same weakref object is an implementation detail, since they pointed |
| 804 | # to the same original object and didn't have a callback. |
| 805 | # (see issue #16453). |
| 806 | self.assertFalse(a == b) |
| 807 | self.assertTrue(a != b) |
| 808 | self.assertFalse(a == c) |
| 809 | self.assertTrue(a != c) |
| 810 | self.assertEqual(a == d, a is d) |
| 811 | self.assertEqual(a != d, a is not d) |
| 812 | |
| 813 | def test_ordering(self): |
| 814 | # weakrefs cannot be ordered, even if the underlying objects can. |
| 815 | ops = [operator.lt, operator.gt, operator.le, operator.ge] |
| 816 | x = Object(1) |
| 817 | y = Object(1) |
| 818 | a = weakref.ref(x) |
| 819 | b = weakref.ref(y) |
| 820 | for op in ops: |
| 821 | self.assertRaises(TypeError, op, a, b) |
| 822 | # Same when dead. |
| 823 | del x, y |
| 824 | gc.collect() |
| 825 | for op in ops: |
| 826 | self.assertRaises(TypeError, op, a, b) |
| 827 | |
| 828 | def test_hashing(self): |
| 829 | # Alive weakrefs hash the same as the underlying object |
| 830 | x = Object(42) |
| 831 | y = Object(42) |
| 832 | a = weakref.ref(x) |
| 833 | b = weakref.ref(y) |
| 834 | self.assertEqual(hash(a), hash(42)) |
| 835 | del x, y |
| 836 | gc.collect() |
| 837 | # Dead weakrefs: |
| 838 | # - retain their hash is they were hashed when alive; |
| 839 | # - otherwise, cannot be hashed. |
| 840 | self.assertEqual(hash(a), hash(42)) |
| 841 | self.assertRaises(TypeError, hash, b) |
| 842 | |
Antoine Pitrou | 62a0d6e | 2012-12-08 21:15:26 +0100 | [diff] [blame] | 843 | def test_trashcan_16602(self): |
| 844 | # Issue #16602: when a weakref's target was part of a long |
| 845 | # deallocation chain, the trashcan mechanism could delay clearing |
| 846 | # of the weakref and make the target object visible from outside |
| 847 | # code even though its refcount had dropped to 0. A crash ensued. |
| 848 | class C: |
| 849 | def __init__(self, parent): |
| 850 | if not parent: |
| 851 | return |
| 852 | wself = weakref.ref(self) |
| 853 | def cb(wparent): |
| 854 | o = wself() |
| 855 | self.wparent = weakref.ref(parent, cb) |
| 856 | |
| 857 | d = weakref.WeakKeyDictionary() |
| 858 | root = c = C(None) |
| 859 | for n in range(100): |
| 860 | d[c] = c = C(c) |
| 861 | del root |
| 862 | gc.collect() |
| 863 | |
Mark Dickinson | 556e94b | 2013-04-13 15:45:44 +0100 | [diff] [blame] | 864 | def test_callback_attribute(self): |
| 865 | x = Object(1) |
| 866 | callback = lambda ref: None |
| 867 | ref1 = weakref.ref(x, callback) |
| 868 | self.assertIs(ref1.__callback__, callback) |
| 869 | |
| 870 | ref2 = weakref.ref(x) |
| 871 | self.assertIsNone(ref2.__callback__) |
| 872 | |
| 873 | def test_callback_attribute_after_deletion(self): |
| 874 | x = Object(1) |
| 875 | ref = weakref.ref(x, self.callback) |
| 876 | self.assertIsNotNone(ref.__callback__) |
| 877 | del x |
| 878 | support.gc_collect() |
| 879 | self.assertIsNone(ref.__callback__) |
| 880 | |
| 881 | def test_set_callback_attribute(self): |
| 882 | x = Object(1) |
| 883 | callback = lambda ref: None |
| 884 | ref1 = weakref.ref(x, callback) |
| 885 | with self.assertRaises(AttributeError): |
| 886 | ref1.__callback__ = lambda ref: None |
| 887 | |
Benjamin Peterson | 8f657c3 | 2016-10-04 00:00:02 -0700 | [diff] [blame] | 888 | def test_callback_gcs(self): |
| 889 | class ObjectWithDel(Object): |
| 890 | def __del__(self): pass |
| 891 | x = ObjectWithDel(1) |
| 892 | ref1 = weakref.ref(x, lambda ref: support.gc_collect()) |
| 893 | del x |
| 894 | support.gc_collect() |
| 895 | |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 896 | |
Amaury Forgeot d'Arc | c856c7a | 2008-06-16 19:50:09 +0000 | [diff] [blame] | 897 | class SubclassableWeakrefTestCase(TestBase): |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 898 | |
| 899 | def test_subclass_refs(self): |
| 900 | class MyRef(weakref.ref): |
| 901 | def __init__(self, ob, callback=None, value=42): |
| 902 | self.value = value |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 903 | super().__init__(ob, callback) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 904 | def __call__(self): |
| 905 | self.called = True |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 906 | return super().__call__() |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 907 | o = Object("foo") |
| 908 | mr = MyRef(o, value=24) |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 909 | self.assertIs(mr(), o) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 910 | self.assertTrue(mr.called) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 911 | self.assertEqual(mr.value, 24) |
| 912 | del o |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 913 | self.assertIsNone(mr()) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 914 | self.assertTrue(mr.called) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 915 | |
| 916 | def test_subclass_refs_dont_replace_standard_refs(self): |
| 917 | class MyRef(weakref.ref): |
| 918 | pass |
| 919 | o = Object(42) |
| 920 | r1 = MyRef(o) |
| 921 | r2 = weakref.ref(o) |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 922 | self.assertIsNot(r1, r2) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 923 | self.assertEqual(weakref.getweakrefs(o), [r2, r1]) |
| 924 | self.assertEqual(weakref.getweakrefcount(o), 2) |
| 925 | r3 = MyRef(o) |
| 926 | self.assertEqual(weakref.getweakrefcount(o), 3) |
| 927 | refs = weakref.getweakrefs(o) |
| 928 | self.assertEqual(len(refs), 3) |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 929 | self.assertIs(r2, refs[0]) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 930 | self.assertIn(r1, refs[1:]) |
| 931 | self.assertIn(r3, refs[1:]) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 932 | |
| 933 | def test_subclass_refs_dont_conflate_callbacks(self): |
| 934 | class MyRef(weakref.ref): |
| 935 | pass |
| 936 | o = Object(42) |
| 937 | r1 = MyRef(o, id) |
| 938 | r2 = MyRef(o, str) |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 939 | self.assertIsNot(r1, r2) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 940 | refs = weakref.getweakrefs(o) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 941 | self.assertIn(r1, refs) |
| 942 | self.assertIn(r2, refs) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 943 | |
| 944 | def test_subclass_refs_with_slots(self): |
| 945 | class MyRef(weakref.ref): |
| 946 | __slots__ = "slot1", "slot2" |
| 947 | def __new__(type, ob, callback, slot1, slot2): |
| 948 | return weakref.ref.__new__(type, ob, callback) |
| 949 | def __init__(self, ob, callback, slot1, slot2): |
| 950 | self.slot1 = slot1 |
| 951 | self.slot2 = slot2 |
| 952 | def meth(self): |
| 953 | return self.slot1 + self.slot2 |
| 954 | o = Object(42) |
| 955 | r = MyRef(o, None, "abc", "def") |
| 956 | self.assertEqual(r.slot1, "abc") |
| 957 | self.assertEqual(r.slot2, "def") |
| 958 | self.assertEqual(r.meth(), "abcdef") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 959 | self.assertFalse(hasattr(r, "__dict__")) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 960 | |
Amaury Forgeot d'Arc | c856c7a | 2008-06-16 19:50:09 +0000 | [diff] [blame] | 961 | def test_subclass_refs_with_cycle(self): |
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D) | cd14d5d | 2016-09-07 00:22:22 +0000 | [diff] [blame] | 962 | """Confirm https://bugs.python.org/issue3100 is fixed.""" |
Amaury Forgeot d'Arc | c856c7a | 2008-06-16 19:50:09 +0000 | [diff] [blame] | 963 | # An instance of a weakref subclass can have attributes. |
| 964 | # If such a weakref holds the only strong reference to the object, |
| 965 | # deleting the weakref will delete the object. In this case, |
| 966 | # the callback must not be called, because the ref object is |
| 967 | # being deleted. |
| 968 | class MyRef(weakref.ref): |
| 969 | pass |
| 970 | |
| 971 | # Use a local callback, for "regrtest -R::" |
| 972 | # to detect refcounting problems |
| 973 | def callback(w): |
| 974 | self.cbcalled += 1 |
| 975 | |
| 976 | o = C() |
| 977 | r1 = MyRef(o, callback) |
| 978 | r1.o = o |
| 979 | del o |
| 980 | |
| 981 | del r1 # Used to crash here |
| 982 | |
| 983 | self.assertEqual(self.cbcalled, 0) |
| 984 | |
| 985 | # Same test, with two weakrefs to the same object |
| 986 | # (since code paths are different) |
| 987 | o = C() |
| 988 | r1 = MyRef(o, callback) |
| 989 | r2 = MyRef(o, callback) |
| 990 | r1.r = r2 |
| 991 | r2.o = o |
| 992 | del o |
| 993 | del r2 |
| 994 | |
| 995 | del r1 # Used to crash here |
| 996 | |
| 997 | self.assertEqual(self.cbcalled, 0) |
| 998 | |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 999 | |
Antoine Pitrou | c3afba1 | 2012-11-17 18:57:38 +0100 | [diff] [blame] | 1000 | class WeakMethodTestCase(unittest.TestCase): |
| 1001 | |
| 1002 | def _subclass(self): |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 1003 | """Return an Object subclass overriding `some_method`.""" |
Antoine Pitrou | c3afba1 | 2012-11-17 18:57:38 +0100 | [diff] [blame] | 1004 | class C(Object): |
| 1005 | def some_method(self): |
| 1006 | return 6 |
| 1007 | return C |
| 1008 | |
| 1009 | def test_alive(self): |
| 1010 | o = Object(1) |
| 1011 | r = weakref.WeakMethod(o.some_method) |
| 1012 | self.assertIsInstance(r, weakref.ReferenceType) |
| 1013 | self.assertIsInstance(r(), type(o.some_method)) |
| 1014 | self.assertIs(r().__self__, o) |
| 1015 | self.assertIs(r().__func__, o.some_method.__func__) |
| 1016 | self.assertEqual(r()(), 4) |
| 1017 | |
| 1018 | def test_object_dead(self): |
| 1019 | o = Object(1) |
| 1020 | r = weakref.WeakMethod(o.some_method) |
| 1021 | del o |
| 1022 | gc.collect() |
| 1023 | self.assertIs(r(), None) |
| 1024 | |
| 1025 | def test_method_dead(self): |
| 1026 | C = self._subclass() |
| 1027 | o = C(1) |
| 1028 | r = weakref.WeakMethod(o.some_method) |
| 1029 | del C.some_method |
| 1030 | gc.collect() |
| 1031 | self.assertIs(r(), None) |
| 1032 | |
| 1033 | def test_callback_when_object_dead(self): |
| 1034 | # Test callback behaviour when object dies first. |
| 1035 | C = self._subclass() |
| 1036 | calls = [] |
| 1037 | def cb(arg): |
| 1038 | calls.append(arg) |
| 1039 | o = C(1) |
| 1040 | r = weakref.WeakMethod(o.some_method, cb) |
| 1041 | del o |
| 1042 | gc.collect() |
| 1043 | self.assertEqual(calls, [r]) |
| 1044 | # Callback is only called once. |
| 1045 | C.some_method = Object.some_method |
| 1046 | gc.collect() |
| 1047 | self.assertEqual(calls, [r]) |
| 1048 | |
| 1049 | def test_callback_when_method_dead(self): |
| 1050 | # Test callback behaviour when method dies first. |
| 1051 | C = self._subclass() |
| 1052 | calls = [] |
| 1053 | def cb(arg): |
| 1054 | calls.append(arg) |
| 1055 | o = C(1) |
| 1056 | r = weakref.WeakMethod(o.some_method, cb) |
| 1057 | del C.some_method |
| 1058 | gc.collect() |
| 1059 | self.assertEqual(calls, [r]) |
| 1060 | # Callback is only called once. |
| 1061 | del o |
| 1062 | gc.collect() |
| 1063 | self.assertEqual(calls, [r]) |
| 1064 | |
| 1065 | @support.cpython_only |
| 1066 | def test_no_cycles(self): |
| 1067 | # A WeakMethod doesn't create any reference cycle to itself. |
| 1068 | o = Object(1) |
| 1069 | def cb(_): |
| 1070 | pass |
| 1071 | r = weakref.WeakMethod(o.some_method, cb) |
| 1072 | wr = weakref.ref(r) |
| 1073 | del r |
| 1074 | self.assertIs(wr(), None) |
| 1075 | |
| 1076 | def test_equality(self): |
| 1077 | def _eq(a, b): |
| 1078 | self.assertTrue(a == b) |
| 1079 | self.assertFalse(a != b) |
| 1080 | def _ne(a, b): |
| 1081 | self.assertTrue(a != b) |
| 1082 | self.assertFalse(a == b) |
| 1083 | x = Object(1) |
| 1084 | y = Object(1) |
| 1085 | a = weakref.WeakMethod(x.some_method) |
| 1086 | b = weakref.WeakMethod(y.some_method) |
| 1087 | c = weakref.WeakMethod(x.other_method) |
| 1088 | d = weakref.WeakMethod(y.other_method) |
| 1089 | # Objects equal, same method |
| 1090 | _eq(a, b) |
| 1091 | _eq(c, d) |
| 1092 | # Objects equal, different method |
| 1093 | _ne(a, c) |
| 1094 | _ne(a, d) |
| 1095 | _ne(b, c) |
| 1096 | _ne(b, d) |
| 1097 | # Objects unequal, same or different method |
| 1098 | z = Object(2) |
| 1099 | e = weakref.WeakMethod(z.some_method) |
| 1100 | f = weakref.WeakMethod(z.other_method) |
| 1101 | _ne(a, e) |
| 1102 | _ne(a, f) |
| 1103 | _ne(b, e) |
| 1104 | _ne(b, f) |
| 1105 | del x, y, z |
| 1106 | gc.collect() |
| 1107 | # Dead WeakMethods compare by identity |
| 1108 | refs = a, b, c, d, e, f |
| 1109 | for q in refs: |
| 1110 | for r in refs: |
| 1111 | self.assertEqual(q == r, q is r) |
| 1112 | self.assertEqual(q != r, q is not r) |
| 1113 | |
| 1114 | def test_hashing(self): |
| 1115 | # Alive WeakMethods are hashable if the underlying object is |
| 1116 | # hashable. |
| 1117 | x = Object(1) |
| 1118 | y = Object(1) |
| 1119 | a = weakref.WeakMethod(x.some_method) |
| 1120 | b = weakref.WeakMethod(y.some_method) |
| 1121 | c = weakref.WeakMethod(y.other_method) |
| 1122 | # Since WeakMethod objects are equal, the hashes should be equal. |
| 1123 | self.assertEqual(hash(a), hash(b)) |
| 1124 | ha = hash(a) |
| 1125 | # Dead WeakMethods retain their old hash value |
| 1126 | del x, y |
| 1127 | gc.collect() |
| 1128 | self.assertEqual(hash(a), ha) |
| 1129 | self.assertEqual(hash(b), ha) |
| 1130 | # If it wasn't hashed when alive, a dead WeakMethod cannot be hashed. |
| 1131 | self.assertRaises(TypeError, hash, c) |
| 1132 | |
| 1133 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 1134 | class MappingTestCase(TestBase): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 1135 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 1136 | COUNT = 10 |
| 1137 | |
Antoine Pitrou | bbe2f60 | 2012-03-01 16:26:35 +0100 | [diff] [blame] | 1138 | def check_len_cycles(self, dict_type, cons): |
| 1139 | N = 20 |
| 1140 | items = [RefCycle() for i in range(N)] |
| 1141 | dct = dict_type(cons(o) for o in items) |
| 1142 | # Keep an iterator alive |
| 1143 | it = dct.items() |
| 1144 | try: |
| 1145 | next(it) |
| 1146 | except StopIteration: |
| 1147 | pass |
| 1148 | del items |
| 1149 | gc.collect() |
| 1150 | n1 = len(dct) |
| 1151 | del it |
| 1152 | gc.collect() |
| 1153 | n2 = len(dct) |
| 1154 | # one item may be kept alive inside the iterator |
| 1155 | self.assertIn(n1, (0, 1)) |
| 1156 | self.assertEqual(n2, 0) |
| 1157 | |
| 1158 | def test_weak_keyed_len_cycles(self): |
| 1159 | self.check_len_cycles(weakref.WeakKeyDictionary, lambda k: (k, 1)) |
| 1160 | |
| 1161 | def test_weak_valued_len_cycles(self): |
| 1162 | self.check_len_cycles(weakref.WeakValueDictionary, lambda k: (1, k)) |
| 1163 | |
| 1164 | def check_len_race(self, dict_type, cons): |
| 1165 | # Extended sanity checks for len() in the face of cyclic collection |
| 1166 | self.addCleanup(gc.set_threshold, *gc.get_threshold()) |
| 1167 | for th in range(1, 100): |
| 1168 | N = 20 |
| 1169 | gc.collect(0) |
| 1170 | gc.set_threshold(th, th, th) |
| 1171 | items = [RefCycle() for i in range(N)] |
| 1172 | dct = dict_type(cons(o) for o in items) |
| 1173 | del items |
| 1174 | # All items will be collected at next garbage collection pass |
| 1175 | it = dct.items() |
| 1176 | try: |
| 1177 | next(it) |
| 1178 | except StopIteration: |
| 1179 | pass |
| 1180 | n1 = len(dct) |
| 1181 | del it |
| 1182 | n2 = len(dct) |
| 1183 | self.assertGreaterEqual(n1, 0) |
| 1184 | self.assertLessEqual(n1, N) |
| 1185 | self.assertGreaterEqual(n2, 0) |
| 1186 | self.assertLessEqual(n2, n1) |
| 1187 | |
| 1188 | def test_weak_keyed_len_race(self): |
| 1189 | self.check_len_race(weakref.WeakKeyDictionary, lambda k: (k, 1)) |
| 1190 | |
| 1191 | def test_weak_valued_len_race(self): |
| 1192 | self.check_len_race(weakref.WeakValueDictionary, lambda k: (1, k)) |
| 1193 | |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 1194 | def test_weak_values(self): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1195 | # |
| 1196 | # This exercises d.copy(), d.items(), d[], del d[], len(d). |
| 1197 | # |
| 1198 | dict, objects = self.make_weak_valued_dict() |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 1199 | for o in objects: |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 1200 | self.assertEqual(weakref.getweakrefcount(o), 1) |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 1201 | self.assertIs(o, dict[o.arg], |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 1202 | "wrong object returned by weak dict!") |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 1203 | items1 = list(dict.items()) |
| 1204 | items2 = list(dict.copy().items()) |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 1205 | items1.sort() |
| 1206 | items2.sort() |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1207 | self.assertEqual(items1, items2, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 1208 | "cloning of weak-valued dictionary did not work!") |
| 1209 | del items1, items2 |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1210 | self.assertEqual(len(dict), self.COUNT) |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 1211 | del objects[0] |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1212 | self.assertEqual(len(dict), self.COUNT - 1, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 1213 | "deleting object did not cause dictionary update") |
| 1214 | del objects, o |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1215 | self.assertEqual(len(dict), 0, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 1216 | "deleting the values did not clear the dictionary") |
Fred Drake | 4fd06e0 | 2001-08-03 04:11:27 +0000 | [diff] [blame] | 1217 | # regression on SF bug #447152: |
| 1218 | dict = weakref.WeakValueDictionary() |
| 1219 | self.assertRaises(KeyError, dict.__getitem__, 1) |
| 1220 | dict[2] = C() |
| 1221 | self.assertRaises(KeyError, dict.__getitem__, 2) |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 1222 | |
| 1223 | def test_weak_keys(self): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1224 | # |
| 1225 | # This exercises d.copy(), d.items(), d[] = v, d[], del d[], |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 1226 | # len(d), k in d. |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1227 | # |
| 1228 | dict, objects = self.make_weak_keyed_dict() |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 1229 | for o in objects: |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 1230 | self.assertEqual(weakref.getweakrefcount(o), 1, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 1231 | "wrong number of weak references to %r!" % o) |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 1232 | self.assertIs(o.arg, dict[o], |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 1233 | "wrong object returned by weak dict!") |
| 1234 | items1 = dict.items() |
| 1235 | items2 = dict.copy().items() |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1236 | self.assertEqual(set(items1), set(items2), |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 1237 | "cloning of weak-keyed dictionary did not work!") |
| 1238 | del items1, items2 |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1239 | self.assertEqual(len(dict), self.COUNT) |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 1240 | del objects[0] |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 1241 | self.assertEqual(len(dict), (self.COUNT - 1), |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 1242 | "deleting object did not cause dictionary update") |
| 1243 | del objects, o |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 1244 | self.assertEqual(len(dict), 0, |
Fred Drake | b0fefc5 | 2001-03-23 04:22:45 +0000 | [diff] [blame] | 1245 | "deleting the keys did not clear the dictionary") |
Fred Drake | 752eda4 | 2001-11-06 16:38:34 +0000 | [diff] [blame] | 1246 | o = Object(42) |
| 1247 | dict[o] = "What is the meaning of the universe?" |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1248 | self.assertIn(o, dict) |
| 1249 | self.assertNotIn(34, dict) |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 1250 | |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1251 | def test_weak_keyed_iters(self): |
| 1252 | dict, objects = self.make_weak_keyed_dict() |
| 1253 | self.check_iters(dict) |
| 1254 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1255 | # Test keyrefs() |
| 1256 | refs = dict.keyrefs() |
| 1257 | self.assertEqual(len(refs), len(objects)) |
| 1258 | objects2 = list(objects) |
| 1259 | for wr in refs: |
| 1260 | ob = wr() |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1261 | self.assertIn(ob, dict) |
| 1262 | self.assertIn(ob, dict) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1263 | self.assertEqual(ob.arg, dict[ob]) |
| 1264 | objects2.remove(ob) |
| 1265 | self.assertEqual(len(objects2), 0) |
| 1266 | |
| 1267 | # Test iterkeyrefs() |
| 1268 | objects2 = list(objects) |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 1269 | self.assertEqual(len(list(dict.keyrefs())), len(objects)) |
| 1270 | for wr in dict.keyrefs(): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1271 | ob = wr() |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1272 | self.assertIn(ob, dict) |
| 1273 | self.assertIn(ob, dict) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1274 | self.assertEqual(ob.arg, dict[ob]) |
| 1275 | objects2.remove(ob) |
| 1276 | self.assertEqual(len(objects2), 0) |
| 1277 | |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1278 | def test_weak_valued_iters(self): |
| 1279 | dict, objects = self.make_weak_valued_dict() |
| 1280 | self.check_iters(dict) |
| 1281 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1282 | # Test valuerefs() |
| 1283 | refs = dict.valuerefs() |
| 1284 | self.assertEqual(len(refs), len(objects)) |
| 1285 | objects2 = list(objects) |
| 1286 | for wr in refs: |
| 1287 | ob = wr() |
| 1288 | self.assertEqual(ob, dict[ob.arg]) |
| 1289 | self.assertEqual(ob.arg, dict[ob.arg].arg) |
| 1290 | objects2.remove(ob) |
| 1291 | self.assertEqual(len(objects2), 0) |
| 1292 | |
| 1293 | # Test itervaluerefs() |
| 1294 | objects2 = list(objects) |
| 1295 | self.assertEqual(len(list(dict.itervaluerefs())), len(objects)) |
| 1296 | for wr in dict.itervaluerefs(): |
| 1297 | ob = wr() |
| 1298 | self.assertEqual(ob, dict[ob.arg]) |
| 1299 | self.assertEqual(ob.arg, dict[ob.arg].arg) |
| 1300 | objects2.remove(ob) |
| 1301 | self.assertEqual(len(objects2), 0) |
| 1302 | |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1303 | def check_iters(self, dict): |
| 1304 | # item iterator: |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 1305 | items = list(dict.items()) |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 1306 | for item in dict.items(): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1307 | items.remove(item) |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 1308 | self.assertFalse(items, "items() did not touch all items") |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1309 | |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1310 | # key iterator, via __iter__(): |
Guido van Rossum | 07f2436 | 2007-02-11 22:59:48 +0000 | [diff] [blame] | 1311 | keys = list(dict.keys()) |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1312 | for k in dict: |
| 1313 | keys.remove(k) |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 1314 | self.assertFalse(keys, "__iter__() did not touch all keys") |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1315 | |
| 1316 | # key iterator, via iterkeys(): |
Guido van Rossum | 07f2436 | 2007-02-11 22:59:48 +0000 | [diff] [blame] | 1317 | keys = list(dict.keys()) |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 1318 | for k in dict.keys(): |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1319 | keys.remove(k) |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 1320 | self.assertFalse(keys, "iterkeys() did not touch all keys") |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1321 | |
| 1322 | # value iterator: |
Guido van Rossum | 07f2436 | 2007-02-11 22:59:48 +0000 | [diff] [blame] | 1323 | values = list(dict.values()) |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 1324 | for v in dict.values(): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1325 | values.remove(v) |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 1326 | self.assertFalse(values, |
Fred Drake | f425b1e | 2003-07-14 21:37:17 +0000 | [diff] [blame] | 1327 | "itervalues() did not touch all values") |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1328 | |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 1329 | def check_weak_destroy_while_iterating(self, dict, objects, iter_name): |
| 1330 | n = len(dict) |
| 1331 | it = iter(getattr(dict, iter_name)()) |
| 1332 | next(it) # Trigger internal iteration |
| 1333 | # Destroy an object |
| 1334 | del objects[-1] |
| 1335 | gc.collect() # just in case |
| 1336 | # We have removed either the first consumed object, or another one |
| 1337 | self.assertIn(len(list(it)), [len(objects), len(objects) - 1]) |
| 1338 | del it |
| 1339 | # The removal has been committed |
| 1340 | self.assertEqual(len(dict), n - 1) |
| 1341 | |
| 1342 | def check_weak_destroy_and_mutate_while_iterating(self, dict, testcontext): |
| 1343 | # Check that we can explicitly mutate the weak dict without |
| 1344 | # interfering with delayed removal. |
| 1345 | # `testcontext` should create an iterator, destroy one of the |
| 1346 | # weakref'ed objects and then return a new key/value pair corresponding |
| 1347 | # to the destroyed object. |
| 1348 | with testcontext() as (k, v): |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 1349 | self.assertNotIn(k, dict) |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 1350 | with testcontext() as (k, v): |
| 1351 | self.assertRaises(KeyError, dict.__delitem__, k) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 1352 | self.assertNotIn(k, dict) |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 1353 | with testcontext() as (k, v): |
| 1354 | self.assertRaises(KeyError, dict.pop, k) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 1355 | self.assertNotIn(k, dict) |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 1356 | with testcontext() as (k, v): |
| 1357 | dict[k] = v |
| 1358 | self.assertEqual(dict[k], v) |
| 1359 | ddict = copy.copy(dict) |
| 1360 | with testcontext() as (k, v): |
| 1361 | dict.update(ddict) |
| 1362 | self.assertEqual(dict, ddict) |
| 1363 | with testcontext() as (k, v): |
| 1364 | dict.clear() |
| 1365 | self.assertEqual(len(dict), 0) |
| 1366 | |
Antoine Pitrou | 1bf974d | 2014-10-05 20:02:28 +0200 | [diff] [blame] | 1367 | def check_weak_del_and_len_while_iterating(self, dict, testcontext): |
| 1368 | # Check that len() works when both iterating and removing keys |
| 1369 | # explicitly through various means (.pop(), .clear()...), while |
| 1370 | # implicit mutation is deferred because an iterator is alive. |
| 1371 | # (each call to testcontext() should schedule one item for removal |
| 1372 | # for this test to work properly) |
| 1373 | o = Object(123456) |
| 1374 | with testcontext(): |
| 1375 | n = len(dict) |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1376 | # Since underlaying dict is ordered, first item is popped |
| 1377 | dict.pop(next(dict.keys())) |
Antoine Pitrou | 1bf974d | 2014-10-05 20:02:28 +0200 | [diff] [blame] | 1378 | self.assertEqual(len(dict), n - 1) |
| 1379 | dict[o] = o |
| 1380 | self.assertEqual(len(dict), n) |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1381 | # last item in objects is removed from dict in context shutdown |
Antoine Pitrou | 1bf974d | 2014-10-05 20:02:28 +0200 | [diff] [blame] | 1382 | with testcontext(): |
| 1383 | self.assertEqual(len(dict), n - 1) |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1384 | # Then, (o, o) is popped |
| 1385 | dict.popitem() |
Antoine Pitrou | 1bf974d | 2014-10-05 20:02:28 +0200 | [diff] [blame] | 1386 | self.assertEqual(len(dict), n - 2) |
| 1387 | with testcontext(): |
| 1388 | self.assertEqual(len(dict), n - 3) |
| 1389 | del dict[next(dict.keys())] |
| 1390 | self.assertEqual(len(dict), n - 4) |
| 1391 | with testcontext(): |
| 1392 | self.assertEqual(len(dict), n - 5) |
| 1393 | dict.popitem() |
| 1394 | self.assertEqual(len(dict), n - 6) |
| 1395 | with testcontext(): |
| 1396 | dict.clear() |
| 1397 | self.assertEqual(len(dict), 0) |
| 1398 | self.assertEqual(len(dict), 0) |
| 1399 | |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 1400 | def test_weak_keys_destroy_while_iterating(self): |
| 1401 | # Issue #7105: iterators shouldn't crash when a key is implicitly removed |
| 1402 | dict, objects = self.make_weak_keyed_dict() |
| 1403 | self.check_weak_destroy_while_iterating(dict, objects, 'keys') |
| 1404 | self.check_weak_destroy_while_iterating(dict, objects, 'items') |
| 1405 | self.check_weak_destroy_while_iterating(dict, objects, 'values') |
| 1406 | self.check_weak_destroy_while_iterating(dict, objects, 'keyrefs') |
| 1407 | dict, objects = self.make_weak_keyed_dict() |
| 1408 | @contextlib.contextmanager |
| 1409 | def testcontext(): |
| 1410 | try: |
| 1411 | it = iter(dict.items()) |
| 1412 | next(it) |
| 1413 | # Schedule a key/value for removal and recreate it |
| 1414 | v = objects.pop().arg |
| 1415 | gc.collect() # just in case |
| 1416 | yield Object(v), v |
| 1417 | finally: |
| 1418 | it = None # should commit all removals |
Benjamin Peterson | 18bb702 | 2014-08-24 18:02:15 -0500 | [diff] [blame] | 1419 | gc.collect() |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 1420 | self.check_weak_destroy_and_mutate_while_iterating(dict, testcontext) |
Antoine Pitrou | 1bf974d | 2014-10-05 20:02:28 +0200 | [diff] [blame] | 1421 | # Issue #21173: len() fragile when keys are both implicitly and |
| 1422 | # explicitly removed. |
| 1423 | dict, objects = self.make_weak_keyed_dict() |
| 1424 | self.check_weak_del_and_len_while_iterating(dict, testcontext) |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 1425 | |
| 1426 | def test_weak_values_destroy_while_iterating(self): |
| 1427 | # Issue #7105: iterators shouldn't crash when a key is implicitly removed |
| 1428 | dict, objects = self.make_weak_valued_dict() |
| 1429 | self.check_weak_destroy_while_iterating(dict, objects, 'keys') |
| 1430 | self.check_weak_destroy_while_iterating(dict, objects, 'items') |
| 1431 | self.check_weak_destroy_while_iterating(dict, objects, 'values') |
| 1432 | self.check_weak_destroy_while_iterating(dict, objects, 'itervaluerefs') |
| 1433 | self.check_weak_destroy_while_iterating(dict, objects, 'valuerefs') |
| 1434 | dict, objects = self.make_weak_valued_dict() |
| 1435 | @contextlib.contextmanager |
| 1436 | def testcontext(): |
| 1437 | try: |
| 1438 | it = iter(dict.items()) |
| 1439 | next(it) |
| 1440 | # Schedule a key/value for removal and recreate it |
| 1441 | k = objects.pop().arg |
| 1442 | gc.collect() # just in case |
| 1443 | yield k, Object(k) |
| 1444 | finally: |
| 1445 | it = None # should commit all removals |
Benjamin Peterson | 18bb702 | 2014-08-24 18:02:15 -0500 | [diff] [blame] | 1446 | gc.collect() |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 1447 | self.check_weak_destroy_and_mutate_while_iterating(dict, testcontext) |
Antoine Pitrou | 1bf974d | 2014-10-05 20:02:28 +0200 | [diff] [blame] | 1448 | dict, objects = self.make_weak_valued_dict() |
| 1449 | self.check_weak_del_and_len_while_iterating(dict, testcontext) |
Antoine Pitrou | c1baa60 | 2010-01-08 17:54:23 +0000 | [diff] [blame] | 1450 | |
Guido van Rossum | 009afb7 | 2002-06-10 20:00:52 +0000 | [diff] [blame] | 1451 | def test_make_weak_keyed_dict_from_dict(self): |
| 1452 | o = Object(3) |
| 1453 | dict = weakref.WeakKeyDictionary({o:364}) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1454 | self.assertEqual(dict[o], 364) |
Guido van Rossum | 009afb7 | 2002-06-10 20:00:52 +0000 | [diff] [blame] | 1455 | |
| 1456 | def test_make_weak_keyed_dict_from_weak_keyed_dict(self): |
| 1457 | o = Object(3) |
| 1458 | dict = weakref.WeakKeyDictionary({o:364}) |
| 1459 | dict2 = weakref.WeakKeyDictionary(dict) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1460 | self.assertEqual(dict[o], 364) |
Guido van Rossum | 009afb7 | 2002-06-10 20:00:52 +0000 | [diff] [blame] | 1461 | |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1462 | def make_weak_keyed_dict(self): |
| 1463 | dict = weakref.WeakKeyDictionary() |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1464 | objects = list(map(Object, range(self.COUNT))) |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1465 | for o in objects: |
| 1466 | dict[o] = o.arg |
| 1467 | return dict, objects |
| 1468 | |
Antoine Pitrou | c06de47 | 2009-05-30 21:04:26 +0000 | [diff] [blame] | 1469 | def test_make_weak_valued_dict_from_dict(self): |
| 1470 | o = Object(3) |
| 1471 | dict = weakref.WeakValueDictionary({364:o}) |
| 1472 | self.assertEqual(dict[364], o) |
| 1473 | |
| 1474 | def test_make_weak_valued_dict_from_weak_valued_dict(self): |
| 1475 | o = Object(3) |
| 1476 | dict = weakref.WeakValueDictionary({364:o}) |
| 1477 | dict2 = weakref.WeakValueDictionary(dict) |
| 1478 | self.assertEqual(dict[364], o) |
| 1479 | |
Serhiy Storchaka | b5102e3 | 2015-09-29 23:52:09 +0300 | [diff] [blame] | 1480 | def test_make_weak_valued_dict_misc(self): |
| 1481 | # errors |
| 1482 | self.assertRaises(TypeError, weakref.WeakValueDictionary.__init__) |
| 1483 | self.assertRaises(TypeError, weakref.WeakValueDictionary, {}, {}) |
| 1484 | self.assertRaises(TypeError, weakref.WeakValueDictionary, (), ()) |
| 1485 | # special keyword arguments |
| 1486 | o = Object(3) |
| 1487 | for kw in 'self', 'dict', 'other', 'iterable': |
| 1488 | d = weakref.WeakValueDictionary(**{kw: o}) |
| 1489 | self.assertEqual(list(d.keys()), [kw]) |
| 1490 | self.assertEqual(d[kw], o) |
| 1491 | |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1492 | def make_weak_valued_dict(self): |
| 1493 | dict = weakref.WeakValueDictionary() |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1494 | objects = list(map(Object, range(self.COUNT))) |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1495 | for o in objects: |
| 1496 | dict[o.arg] = o |
| 1497 | return dict, objects |
| 1498 | |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1499 | def check_popitem(self, klass, key1, value1, key2, value2): |
| 1500 | weakdict = klass() |
| 1501 | weakdict[key1] = value1 |
| 1502 | weakdict[key2] = value2 |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1503 | self.assertEqual(len(weakdict), 2) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1504 | k, v = weakdict.popitem() |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1505 | self.assertEqual(len(weakdict), 1) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1506 | if k is key1: |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 1507 | self.assertIs(v, value1) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1508 | else: |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 1509 | self.assertIs(v, value2) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1510 | k, v = weakdict.popitem() |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1511 | self.assertEqual(len(weakdict), 0) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1512 | if k is key1: |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 1513 | self.assertIs(v, value1) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1514 | else: |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 1515 | self.assertIs(v, value2) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1516 | |
| 1517 | def test_weak_valued_dict_popitem(self): |
| 1518 | self.check_popitem(weakref.WeakValueDictionary, |
| 1519 | "key1", C(), "key2", C()) |
| 1520 | |
| 1521 | def test_weak_keyed_dict_popitem(self): |
| 1522 | self.check_popitem(weakref.WeakKeyDictionary, |
| 1523 | C(), "value 1", C(), "value 2") |
| 1524 | |
| 1525 | def check_setdefault(self, klass, key, value1, value2): |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 1526 | self.assertIsNot(value1, value2, |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1527 | "invalid test" |
| 1528 | " -- value parameters must be distinct objects") |
| 1529 | weakdict = klass() |
| 1530 | o = weakdict.setdefault(key, value1) |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 1531 | self.assertIs(o, value1) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1532 | self.assertIn(key, weakdict) |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 1533 | self.assertIs(weakdict.get(key), value1) |
| 1534 | self.assertIs(weakdict[key], value1) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1535 | |
| 1536 | o = weakdict.setdefault(key, value2) |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 1537 | self.assertIs(o, value1) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1538 | self.assertIn(key, weakdict) |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 1539 | self.assertIs(weakdict.get(key), value1) |
| 1540 | self.assertIs(weakdict[key], value1) |
Fred Drake | aaa48ff | 2001-05-10 17:16:38 +0000 | [diff] [blame] | 1541 | |
| 1542 | def test_weak_valued_dict_setdefault(self): |
| 1543 | self.check_setdefault(weakref.WeakValueDictionary, |
| 1544 | "key", C(), C()) |
| 1545 | |
| 1546 | def test_weak_keyed_dict_setdefault(self): |
| 1547 | self.check_setdefault(weakref.WeakKeyDictionary, |
| 1548 | C(), "value 1", "value 2") |
| 1549 | |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 1550 | def check_update(self, klass, dict): |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1551 | # |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 1552 | # This exercises d.update(), len(d), d.keys(), k in d, |
Fred Drake | 0e540c3 | 2001-05-02 05:44:22 +0000 | [diff] [blame] | 1553 | # d.get(), d[]. |
| 1554 | # |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 1555 | weakdict = klass() |
| 1556 | weakdict.update(dict) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1557 | self.assertEqual(len(weakdict), len(dict)) |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 1558 | for k in weakdict.keys(): |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 1559 | self.assertIn(k, dict, "mysterious new key appeared in weak dict") |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 1560 | v = dict.get(k) |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 1561 | self.assertIs(v, weakdict[k]) |
| 1562 | self.assertIs(v, weakdict.get(k)) |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 1563 | for k in dict.keys(): |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 1564 | self.assertIn(k, weakdict, "original key disappeared in weak dict") |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 1565 | v = dict[k] |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 1566 | self.assertIs(v, weakdict[k]) |
| 1567 | self.assertIs(v, weakdict.get(k)) |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 1568 | |
| 1569 | def test_weak_valued_dict_update(self): |
| 1570 | self.check_update(weakref.WeakValueDictionary, |
| 1571 | {1: C(), 'a': C(), C(): C()}) |
Serhiy Storchaka | b5102e3 | 2015-09-29 23:52:09 +0300 | [diff] [blame] | 1572 | # errors |
| 1573 | self.assertRaises(TypeError, weakref.WeakValueDictionary.update) |
| 1574 | d = weakref.WeakValueDictionary() |
| 1575 | self.assertRaises(TypeError, d.update, {}, {}) |
| 1576 | self.assertRaises(TypeError, d.update, (), ()) |
| 1577 | self.assertEqual(list(d.keys()), []) |
| 1578 | # special keyword arguments |
| 1579 | o = Object(3) |
| 1580 | for kw in 'self', 'dict', 'other', 'iterable': |
| 1581 | d = weakref.WeakValueDictionary() |
| 1582 | d.update(**{kw: o}) |
| 1583 | self.assertEqual(list(d.keys()), [kw]) |
| 1584 | self.assertEqual(d[kw], o) |
Fred Drake | a0a4ab1 | 2001-04-16 17:37:27 +0000 | [diff] [blame] | 1585 | |
| 1586 | def test_weak_keyed_dict_update(self): |
| 1587 | self.check_update(weakref.WeakKeyDictionary, |
| 1588 | {C(): 1, C(): 2, C(): 3}) |
| 1589 | |
Fred Drake | ccc7562 | 2001-09-06 14:52:39 +0000 | [diff] [blame] | 1590 | def test_weak_keyed_delitem(self): |
| 1591 | d = weakref.WeakKeyDictionary() |
| 1592 | o1 = Object('1') |
| 1593 | o2 = Object('2') |
| 1594 | d[o1] = 'something' |
| 1595 | d[o2] = 'something' |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1596 | self.assertEqual(len(d), 2) |
Fred Drake | ccc7562 | 2001-09-06 14:52:39 +0000 | [diff] [blame] | 1597 | del d[o1] |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1598 | self.assertEqual(len(d), 1) |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 1599 | self.assertEqual(list(d.keys()), [o2]) |
Fred Drake | ccc7562 | 2001-09-06 14:52:39 +0000 | [diff] [blame] | 1600 | |
| 1601 | def test_weak_valued_delitem(self): |
| 1602 | d = weakref.WeakValueDictionary() |
| 1603 | o1 = Object('1') |
| 1604 | o2 = Object('2') |
| 1605 | d['something'] = o1 |
| 1606 | d['something else'] = o2 |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1607 | self.assertEqual(len(d), 2) |
Fred Drake | ccc7562 | 2001-09-06 14:52:39 +0000 | [diff] [blame] | 1608 | del d['something'] |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1609 | self.assertEqual(len(d), 1) |
Serhiy Storchaka | 2e29c9e | 2013-11-17 13:20:09 +0200 | [diff] [blame] | 1610 | self.assertEqual(list(d.items()), [('something else', o2)]) |
Fred Drake | ccc7562 | 2001-09-06 14:52:39 +0000 | [diff] [blame] | 1611 | |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 1612 | def test_weak_keyed_bad_delitem(self): |
| 1613 | d = weakref.WeakKeyDictionary() |
| 1614 | o = Object('1') |
| 1615 | # An attempt to delete an object that isn't there should raise |
Tim Peters | 50d8b8b | 2003-05-25 17:44:31 +0000 | [diff] [blame] | 1616 | # KeyError. It didn't before 2.3. |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 1617 | self.assertRaises(KeyError, d.__delitem__, o) |
Tim Peters | 50d8b8b | 2003-05-25 17:44:31 +0000 | [diff] [blame] | 1618 | self.assertRaises(KeyError, d.__getitem__, o) |
| 1619 | |
| 1620 | # If a key isn't of a weakly referencable type, __getitem__ and |
| 1621 | # __setitem__ raise TypeError. __delitem__ should too. |
| 1622 | self.assertRaises(TypeError, d.__delitem__, 13) |
| 1623 | self.assertRaises(TypeError, d.__getitem__, 13) |
| 1624 | self.assertRaises(TypeError, d.__setitem__, 13, 13) |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 1625 | |
| 1626 | def test_weak_keyed_cascading_deletes(self): |
| 1627 | # SF bug 742860. For some reason, before 2.3 __delitem__ iterated |
| 1628 | # over the keys via self.data.iterkeys(). If things vanished from |
| 1629 | # the dict during this (or got added), that caused a RuntimeError. |
| 1630 | |
| 1631 | d = weakref.WeakKeyDictionary() |
| 1632 | mutate = False |
| 1633 | |
| 1634 | class C(object): |
| 1635 | def __init__(self, i): |
| 1636 | self.value = i |
| 1637 | def __hash__(self): |
| 1638 | return hash(self.value) |
| 1639 | def __eq__(self, other): |
| 1640 | if mutate: |
| 1641 | # Side effect that mutates the dict, by removing the |
| 1642 | # last strong reference to a key. |
| 1643 | del objs[-1] |
| 1644 | return self.value == other.value |
| 1645 | |
| 1646 | objs = [C(i) for i in range(4)] |
| 1647 | for o in objs: |
| 1648 | d[o] = o.value |
| 1649 | del o # now the only strong references to keys are in objs |
| 1650 | # Find the order in which iterkeys sees the keys. |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 1651 | objs = list(d.keys()) |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 1652 | # Reverse it, so that the iteration implementation of __delitem__ |
| 1653 | # has to keep looping to find the first object we delete. |
| 1654 | objs.reverse() |
Tim Peters | 50d8b8b | 2003-05-25 17:44:31 +0000 | [diff] [blame] | 1655 | |
Leo Arias | c3d9508 | 2018-02-03 18:36:10 -0600 | [diff] [blame] | 1656 | # Turn on mutation in C.__eq__. The first time through the loop, |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 1657 | # under the iterkeys() business the first comparison will delete |
| 1658 | # the last item iterkeys() would see, and that causes a |
| 1659 | # RuntimeError: dictionary changed size during iteration |
| 1660 | # when the iterkeys() loop goes around to try comparing the next |
Tim Peters | 50d8b8b | 2003-05-25 17:44:31 +0000 | [diff] [blame] | 1661 | # key. After this was fixed, it just deletes the last object *our* |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 1662 | # "for o in obj" loop would have gotten to. |
| 1663 | mutate = True |
| 1664 | count = 0 |
| 1665 | for o in objs: |
| 1666 | count += 1 |
| 1667 | del d[o] |
| 1668 | self.assertEqual(len(d), 0) |
| 1669 | self.assertEqual(count, 2) |
| 1670 | |
Serhiy Storchaka | 0c937b3 | 2014-07-22 12:14:52 +0300 | [diff] [blame] | 1671 | def test_make_weak_valued_dict_repr(self): |
| 1672 | dict = weakref.WeakValueDictionary() |
| 1673 | self.assertRegex(repr(dict), '<WeakValueDictionary at 0x.*>') |
| 1674 | |
| 1675 | def test_make_weak_keyed_dict_repr(self): |
| 1676 | dict = weakref.WeakKeyDictionary() |
| 1677 | self.assertRegex(repr(dict), '<WeakKeyDictionary at 0x.*>') |
| 1678 | |
Antoine Pitrou | c1ee488 | 2016-12-19 10:56:40 +0100 | [diff] [blame] | 1679 | def test_threaded_weak_valued_setdefault(self): |
| 1680 | d = weakref.WeakValueDictionary() |
| 1681 | with collect_in_thread(): |
| 1682 | for i in range(100000): |
| 1683 | x = d.setdefault(10, RefCycle()) |
| 1684 | self.assertIsNot(x, None) # we never put None in there! |
| 1685 | del x |
| 1686 | |
| 1687 | def test_threaded_weak_valued_pop(self): |
| 1688 | d = weakref.WeakValueDictionary() |
| 1689 | with collect_in_thread(): |
| 1690 | for i in range(100000): |
| 1691 | d[10] = RefCycle() |
| 1692 | x = d.pop(10, 10) |
| 1693 | self.assertIsNot(x, None) # we never put None in there! |
| 1694 | |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 1695 | def test_threaded_weak_valued_consistency(self): |
| 1696 | # Issue #28427: old keys should not remove new values from |
| 1697 | # WeakValueDictionary when collecting from another thread. |
| 1698 | d = weakref.WeakValueDictionary() |
| 1699 | with collect_in_thread(): |
| 1700 | for i in range(200000): |
| 1701 | o = RefCycle() |
| 1702 | d[10] = o |
| 1703 | # o is still alive, so the dict can't be empty |
| 1704 | self.assertEqual(len(d), 1) |
| 1705 | o = None # lose ref |
| 1706 | |
Fish | 96d37db | 2019-02-07 14:51:59 -0500 | [diff] [blame] | 1707 | def check_threaded_weak_dict_copy(self, type_, deepcopy): |
| 1708 | # `type_` should be either WeakKeyDictionary or WeakValueDictionary. |
| 1709 | # `deepcopy` should be either True or False. |
| 1710 | exc = [] |
| 1711 | |
| 1712 | class DummyKey: |
| 1713 | def __init__(self, ctr): |
| 1714 | self.ctr = ctr |
| 1715 | |
| 1716 | class DummyValue: |
| 1717 | def __init__(self, ctr): |
| 1718 | self.ctr = ctr |
| 1719 | |
| 1720 | def dict_copy(d, exc): |
| 1721 | try: |
| 1722 | if deepcopy is True: |
| 1723 | _ = copy.deepcopy(d) |
| 1724 | else: |
| 1725 | _ = d.copy() |
| 1726 | except Exception as ex: |
| 1727 | exc.append(ex) |
| 1728 | |
| 1729 | def pop_and_collect(lst): |
| 1730 | gc_ctr = 0 |
| 1731 | while lst: |
| 1732 | i = random.randint(0, len(lst) - 1) |
| 1733 | gc_ctr += 1 |
| 1734 | lst.pop(i) |
| 1735 | if gc_ctr % 10000 == 0: |
| 1736 | gc.collect() # just in case |
| 1737 | |
| 1738 | self.assertIn(type_, (weakref.WeakKeyDictionary, weakref.WeakValueDictionary)) |
| 1739 | |
| 1740 | d = type_() |
| 1741 | keys = [] |
| 1742 | values = [] |
| 1743 | # Initialize d with many entries |
| 1744 | for i in range(70000): |
| 1745 | k, v = DummyKey(i), DummyValue(i) |
| 1746 | keys.append(k) |
| 1747 | values.append(v) |
| 1748 | d[k] = v |
| 1749 | del k |
| 1750 | del v |
| 1751 | |
| 1752 | t_copy = threading.Thread(target=dict_copy, args=(d, exc,)) |
| 1753 | if type_ is weakref.WeakKeyDictionary: |
| 1754 | t_collect = threading.Thread(target=pop_and_collect, args=(keys,)) |
| 1755 | else: # weakref.WeakValueDictionary |
| 1756 | t_collect = threading.Thread(target=pop_and_collect, args=(values,)) |
| 1757 | |
| 1758 | t_copy.start() |
| 1759 | t_collect.start() |
| 1760 | |
| 1761 | t_copy.join() |
| 1762 | t_collect.join() |
| 1763 | |
| 1764 | # Test exceptions |
| 1765 | if exc: |
| 1766 | raise exc[0] |
| 1767 | |
| 1768 | def test_threaded_weak_key_dict_copy(self): |
| 1769 | # Issue #35615: Weakref keys or values getting GC'ed during dict |
| 1770 | # copying should not result in a crash. |
| 1771 | self.check_threaded_weak_dict_copy(weakref.WeakKeyDictionary, False) |
| 1772 | |
| 1773 | def test_threaded_weak_key_dict_deepcopy(self): |
| 1774 | # Issue #35615: Weakref keys or values getting GC'ed during dict |
| 1775 | # copying should not result in a crash. |
| 1776 | self.check_threaded_weak_dict_copy(weakref.WeakKeyDictionary, True) |
| 1777 | |
| 1778 | def test_threaded_weak_value_dict_copy(self): |
| 1779 | # Issue #35615: Weakref keys or values getting GC'ed during dict |
| 1780 | # copying should not result in a crash. |
| 1781 | self.check_threaded_weak_dict_copy(weakref.WeakValueDictionary, False) |
| 1782 | |
| 1783 | def test_threaded_weak_value_dict_deepcopy(self): |
| 1784 | # Issue #35615: Weakref keys or values getting GC'ed during dict |
| 1785 | # copying should not result in a crash. |
| 1786 | self.check_threaded_weak_dict_copy(weakref.WeakValueDictionary, True) |
| 1787 | |
Antoine Pitrou | c1ee488 | 2016-12-19 10:56:40 +0100 | [diff] [blame] | 1788 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 1789 | from test import mapping_tests |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 1790 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 1791 | class WeakValueDictionaryTestCase(mapping_tests.BasicTestMappingProtocol): |
Fred Drake | f425b1e | 2003-07-14 21:37:17 +0000 | [diff] [blame] | 1792 | """Check that WeakValueDictionary conforms to the mapping protocol""" |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 1793 | __ref = {"key1":Object(1), "key2":Object(2), "key3":Object(3)} |
Walter Dörwald | 118f931 | 2004-06-02 18:42:25 +0000 | [diff] [blame] | 1794 | type2test = weakref.WeakValueDictionary |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 1795 | def _reference(self): |
| 1796 | return self.__ref.copy() |
| 1797 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 1798 | class WeakKeyDictionaryTestCase(mapping_tests.BasicTestMappingProtocol): |
Fred Drake | f425b1e | 2003-07-14 21:37:17 +0000 | [diff] [blame] | 1799 | """Check that WeakKeyDictionary conforms to the mapping protocol""" |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 1800 | __ref = {Object("key1"):1, Object("key2"):2, Object("key3"):3} |
Walter Dörwald | 118f931 | 2004-06-02 18:42:25 +0000 | [diff] [blame] | 1801 | type2test = weakref.WeakKeyDictionary |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 1802 | def _reference(self): |
| 1803 | return self.__ref.copy() |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 1804 | |
Richard Oudkerk | 7a3dae0 | 2013-05-05 23:05:00 +0100 | [diff] [blame] | 1805 | |
| 1806 | class FinalizeTestCase(unittest.TestCase): |
| 1807 | |
| 1808 | class A: |
| 1809 | pass |
| 1810 | |
| 1811 | def _collect_if_necessary(self): |
| 1812 | # we create no ref-cycles so in CPython no gc should be needed |
| 1813 | if sys.implementation.name != 'cpython': |
| 1814 | support.gc_collect() |
| 1815 | |
| 1816 | def test_finalize(self): |
| 1817 | def add(x,y,z): |
| 1818 | res.append(x + y + z) |
| 1819 | return x + y + z |
| 1820 | |
| 1821 | a = self.A() |
| 1822 | |
| 1823 | res = [] |
| 1824 | f = weakref.finalize(a, add, 67, 43, z=89) |
| 1825 | self.assertEqual(f.alive, True) |
| 1826 | self.assertEqual(f.peek(), (a, add, (67,43), {'z':89})) |
| 1827 | self.assertEqual(f(), 199) |
| 1828 | self.assertEqual(f(), None) |
| 1829 | self.assertEqual(f(), None) |
| 1830 | self.assertEqual(f.peek(), None) |
| 1831 | self.assertEqual(f.detach(), None) |
| 1832 | self.assertEqual(f.alive, False) |
| 1833 | self.assertEqual(res, [199]) |
| 1834 | |
| 1835 | res = [] |
| 1836 | f = weakref.finalize(a, add, 67, 43, 89) |
| 1837 | self.assertEqual(f.peek(), (a, add, (67,43,89), {})) |
| 1838 | self.assertEqual(f.detach(), (a, add, (67,43,89), {})) |
| 1839 | self.assertEqual(f(), None) |
| 1840 | self.assertEqual(f(), None) |
| 1841 | self.assertEqual(f.peek(), None) |
| 1842 | self.assertEqual(f.detach(), None) |
| 1843 | self.assertEqual(f.alive, False) |
| 1844 | self.assertEqual(res, []) |
| 1845 | |
| 1846 | res = [] |
| 1847 | f = weakref.finalize(a, add, x=67, y=43, z=89) |
| 1848 | del a |
| 1849 | self._collect_if_necessary() |
| 1850 | self.assertEqual(f(), None) |
| 1851 | self.assertEqual(f(), None) |
| 1852 | self.assertEqual(f.peek(), None) |
| 1853 | self.assertEqual(f.detach(), None) |
| 1854 | self.assertEqual(f.alive, False) |
| 1855 | self.assertEqual(res, [199]) |
| 1856 | |
Serhiy Storchaka | 42a139e | 2019-04-01 09:16:35 +0300 | [diff] [blame] | 1857 | def test_arg_errors(self): |
| 1858 | def fin(*args, **kwargs): |
| 1859 | res.append((args, kwargs)) |
| 1860 | |
| 1861 | a = self.A() |
| 1862 | |
| 1863 | res = [] |
| 1864 | f = weakref.finalize(a, fin, 1, 2, func=3, obj=4) |
| 1865 | self.assertEqual(f.peek(), (a, fin, (1, 2), {'func': 3, 'obj': 4})) |
| 1866 | f() |
| 1867 | self.assertEqual(res, [((1, 2), {'func': 3, 'obj': 4})]) |
| 1868 | |
| 1869 | res = [] |
| 1870 | with self.assertWarns(DeprecationWarning): |
| 1871 | f = weakref.finalize(a, func=fin, arg=1) |
| 1872 | self.assertEqual(f.peek(), (a, fin, (), {'arg': 1})) |
| 1873 | f() |
| 1874 | self.assertEqual(res, [((), {'arg': 1})]) |
| 1875 | |
| 1876 | res = [] |
| 1877 | with self.assertWarns(DeprecationWarning): |
| 1878 | f = weakref.finalize(obj=a, func=fin, arg=1) |
| 1879 | self.assertEqual(f.peek(), (a, fin, (), {'arg': 1})) |
| 1880 | f() |
| 1881 | self.assertEqual(res, [((), {'arg': 1})]) |
| 1882 | |
| 1883 | self.assertRaises(TypeError, weakref.finalize, a) |
| 1884 | self.assertRaises(TypeError, weakref.finalize) |
| 1885 | |
Richard Oudkerk | 7a3dae0 | 2013-05-05 23:05:00 +0100 | [diff] [blame] | 1886 | def test_order(self): |
| 1887 | a = self.A() |
| 1888 | res = [] |
| 1889 | |
| 1890 | f1 = weakref.finalize(a, res.append, 'f1') |
| 1891 | f2 = weakref.finalize(a, res.append, 'f2') |
| 1892 | f3 = weakref.finalize(a, res.append, 'f3') |
| 1893 | f4 = weakref.finalize(a, res.append, 'f4') |
| 1894 | f5 = weakref.finalize(a, res.append, 'f5') |
| 1895 | |
| 1896 | # make sure finalizers can keep themselves alive |
| 1897 | del f1, f4 |
| 1898 | |
| 1899 | self.assertTrue(f2.alive) |
| 1900 | self.assertTrue(f3.alive) |
| 1901 | self.assertTrue(f5.alive) |
| 1902 | |
| 1903 | self.assertTrue(f5.detach()) |
| 1904 | self.assertFalse(f5.alive) |
| 1905 | |
| 1906 | f5() # nothing because previously unregistered |
| 1907 | res.append('A') |
| 1908 | f3() # => res.append('f3') |
| 1909 | self.assertFalse(f3.alive) |
| 1910 | res.append('B') |
| 1911 | f3() # nothing because previously called |
| 1912 | res.append('C') |
| 1913 | del a |
| 1914 | self._collect_if_necessary() |
| 1915 | # => res.append('f4') |
| 1916 | # => res.append('f2') |
| 1917 | # => res.append('f1') |
| 1918 | self.assertFalse(f2.alive) |
| 1919 | res.append('D') |
| 1920 | f2() # nothing because previously called by gc |
| 1921 | |
| 1922 | expected = ['A', 'f3', 'B', 'C', 'f4', 'f2', 'f1', 'D'] |
| 1923 | self.assertEqual(res, expected) |
| 1924 | |
| 1925 | def test_all_freed(self): |
| 1926 | # we want a weakrefable subclass of weakref.finalize |
| 1927 | class MyFinalizer(weakref.finalize): |
| 1928 | pass |
| 1929 | |
| 1930 | a = self.A() |
| 1931 | res = [] |
| 1932 | def callback(): |
| 1933 | res.append(123) |
| 1934 | f = MyFinalizer(a, callback) |
| 1935 | |
| 1936 | wr_callback = weakref.ref(callback) |
| 1937 | wr_f = weakref.ref(f) |
| 1938 | del callback, f |
| 1939 | |
| 1940 | self.assertIsNotNone(wr_callback()) |
| 1941 | self.assertIsNotNone(wr_f()) |
| 1942 | |
| 1943 | del a |
| 1944 | self._collect_if_necessary() |
| 1945 | |
| 1946 | self.assertIsNone(wr_callback()) |
| 1947 | self.assertIsNone(wr_f()) |
| 1948 | self.assertEqual(res, [123]) |
| 1949 | |
| 1950 | @classmethod |
| 1951 | def run_in_child(cls): |
| 1952 | def error(): |
| 1953 | # Create an atexit finalizer from inside a finalizer called |
| 1954 | # at exit. This should be the next to be run. |
| 1955 | g1 = weakref.finalize(cls, print, 'g1') |
| 1956 | print('f3 error') |
| 1957 | 1/0 |
| 1958 | |
| 1959 | # cls should stay alive till atexit callbacks run |
| 1960 | f1 = weakref.finalize(cls, print, 'f1', _global_var) |
| 1961 | f2 = weakref.finalize(cls, print, 'f2', _global_var) |
| 1962 | f3 = weakref.finalize(cls, error) |
| 1963 | f4 = weakref.finalize(cls, print, 'f4', _global_var) |
| 1964 | |
| 1965 | assert f1.atexit == True |
| 1966 | f2.atexit = False |
| 1967 | assert f3.atexit == True |
| 1968 | assert f4.atexit == True |
| 1969 | |
| 1970 | def test_atexit(self): |
| 1971 | prog = ('from test.test_weakref import FinalizeTestCase;'+ |
| 1972 | 'FinalizeTestCase.run_in_child()') |
| 1973 | rc, out, err = script_helper.assert_python_ok('-c', prog) |
| 1974 | out = out.decode('ascii').splitlines() |
| 1975 | self.assertEqual(out, ['f4 foobar', 'f3 error', 'g1', 'f1 foobar']) |
| 1976 | self.assertTrue(b'ZeroDivisionError' in err) |
| 1977 | |
| 1978 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1979 | libreftest = """ Doctest for examples in the library reference: weakref.rst |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1980 | |
| 1981 | >>> import weakref |
| 1982 | >>> class Dict(dict): |
| 1983 | ... pass |
| 1984 | ... |
| 1985 | >>> obj = Dict(red=1, green=2, blue=3) # this object is weak referencable |
| 1986 | >>> r = weakref.ref(obj) |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1987 | >>> print(r() is obj) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1988 | True |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 1989 | |
| 1990 | >>> import weakref |
| 1991 | >>> class Object: |
| 1992 | ... pass |
| 1993 | ... |
| 1994 | >>> o = Object() |
| 1995 | >>> r = weakref.ref(o) |
| 1996 | >>> o2 = r() |
| 1997 | >>> o is o2 |
| 1998 | True |
| 1999 | >>> del o, o2 |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 2000 | >>> print(r()) |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 2001 | None |
| 2002 | |
| 2003 | >>> import weakref |
| 2004 | >>> class ExtendedRef(weakref.ref): |
| 2005 | ... def __init__(self, ob, callback=None, **annotations): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 2006 | ... super().__init__(ob, callback) |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 2007 | ... self.__counter = 0 |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 2008 | ... for k, v in annotations.items(): |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 2009 | ... setattr(self, k, v) |
| 2010 | ... def __call__(self): |
| 2011 | ... '''Return a pair containing the referent and the number of |
| 2012 | ... times the reference has been called. |
| 2013 | ... ''' |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 2014 | ... ob = super().__call__() |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 2015 | ... if ob is not None: |
| 2016 | ... self.__counter += 1 |
| 2017 | ... ob = (ob, self.__counter) |
| 2018 | ... return ob |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2019 | ... |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 2020 | >>> class A: # not in docs from here, just testing the ExtendedRef |
| 2021 | ... pass |
| 2022 | ... |
| 2023 | >>> a = A() |
| 2024 | >>> r = ExtendedRef(a, foo=1, bar="baz") |
| 2025 | >>> r.foo |
| 2026 | 1 |
| 2027 | >>> r.bar |
| 2028 | 'baz' |
| 2029 | >>> r()[1] |
| 2030 | 1 |
| 2031 | >>> r()[1] |
| 2032 | 2 |
| 2033 | >>> r()[0] is a |
| 2034 | True |
| 2035 | |
| 2036 | |
| 2037 | >>> import weakref |
| 2038 | >>> _id2obj_dict = weakref.WeakValueDictionary() |
| 2039 | >>> def remember(obj): |
| 2040 | ... oid = id(obj) |
| 2041 | ... _id2obj_dict[oid] = obj |
| 2042 | ... return oid |
| 2043 | ... |
| 2044 | >>> def id2obj(oid): |
| 2045 | ... return _id2obj_dict[oid] |
| 2046 | ... |
| 2047 | >>> a = A() # from here, just testing |
| 2048 | >>> a_id = remember(a) |
| 2049 | >>> id2obj(a_id) is a |
| 2050 | True |
| 2051 | >>> del a |
| 2052 | >>> try: |
| 2053 | ... id2obj(a_id) |
| 2054 | ... except KeyError: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 2055 | ... print('OK') |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 2056 | ... else: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 2057 | ... print('WeakValueDictionary error') |
Georg Brandl | 9a65d58 | 2005-07-02 19:07:30 +0000 | [diff] [blame] | 2058 | OK |
| 2059 | |
| 2060 | """ |
| 2061 | |
| 2062 | __test__ = {'libreftest' : libreftest} |
| 2063 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 2064 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2065 | support.run_unittest( |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 2066 | ReferencesTestCase, |
Antoine Pitrou | c3afba1 | 2012-11-17 18:57:38 +0100 | [diff] [blame] | 2067 | WeakMethodTestCase, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 2068 | MappingTestCase, |
| 2069 | WeakValueDictionaryTestCase, |
Fred Drake | f425b1e | 2003-07-14 21:37:17 +0000 | [diff] [blame] | 2070 | WeakKeyDictionaryTestCase, |
Amaury Forgeot d'Arc | c856c7a | 2008-06-16 19:50:09 +0000 | [diff] [blame] | 2071 | SubclassableWeakrefTestCase, |
Richard Oudkerk | 7a3dae0 | 2013-05-05 23:05:00 +0100 | [diff] [blame] | 2072 | FinalizeTestCase, |
Fred Drake | f425b1e | 2003-07-14 21:37:17 +0000 | [diff] [blame] | 2073 | ) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2074 | support.run_doctest(sys.modules[__name__]) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 2075 | |
| 2076 | |
| 2077 | if __name__ == "__main__": |
| 2078 | test_main() |