blob: 5a3e59c3e9ef1599cb4ee3fcb5606152abbc742d [file] [log] [blame]
Fred Drakebc875f52004-02-04 23:14:14 +00001import gc
Fred Drake41deb1e2001-02-01 05:27:45 +00002import sys
Fred Drakeb0fefc52001-03-23 04:22:45 +00003import unittest
Raymond Hettinger53dbe392008-02-12 20:03:09 +00004import collections
Fred Drake41deb1e2001-02-01 05:27:45 +00005import weakref
Georg Brandlb533e262008-05-25 18:19:30 +00006import operator
Antoine Pitrouc1baa602010-01-08 17:54:23 +00007import contextlib
8import copy
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02009import threading
Antoine Pitrouc1ee4882016-12-19 10:56:40 +010010import time
Fish96d37db2019-02-07 14:51:59 -050011import random
Fred Drake41deb1e2001-02-01 05:27:45 +000012
Berker Peksagce643912015-05-06 06:33:17 +030013from test import support
Serhiy Storchaka662db122019-08-08 08:42:54 +030014from test.support import script_helper, ALWAYS_EQ
Serhiy Storchaka462c1f02021-09-08 18:08:57 +030015from test.support import gc_collect
Fred Drake41deb1e2001-02-01 05:27:45 +000016
Thomas Woutersb2137042007-02-01 18:02:27 +000017# Used in ReferencesTestCase.test_ref_created_during_del() .
18ref_from_del = None
Fred Drake41deb1e2001-02-01 05:27:45 +000019
Richard Oudkerk7a3dae02013-05-05 23:05:00 +010020# Used by FinalizeTestCase as a global that may be replaced by None
21# when the interpreter shuts down.
22_global_var = 'foobar'
23
Fred Drake41deb1e2001-02-01 05:27:45 +000024class C:
Fred Drakeb0fefc52001-03-23 04:22:45 +000025 def method(self):
26 pass
Fred Drake41deb1e2001-02-01 05:27:45 +000027
28
Fred Drakeb0fefc52001-03-23 04:22:45 +000029class Callable:
30 bar = None
Fred Drake41deb1e2001-02-01 05:27:45 +000031
Fred Drakeb0fefc52001-03-23 04:22:45 +000032 def __call__(self, x):
33 self.bar = x
Fred Drake41deb1e2001-02-01 05:27:45 +000034
35
Fred Drakeb0fefc52001-03-23 04:22:45 +000036def create_function():
37 def f(): pass
38 return f
39
40def create_bound_method():
41 return C().method
42
Fred Drake41deb1e2001-02-01 05:27:45 +000043
Antoine Pitroue11fecb2012-11-11 19:36:51 +010044class Object:
45 def __init__(self, arg):
46 self.arg = arg
47 def __repr__(self):
48 return "<Object %r>" % self.arg
49 def __eq__(self, other):
50 if isinstance(other, Object):
51 return self.arg == other.arg
52 return NotImplemented
53 def __lt__(self, other):
54 if isinstance(other, Object):
55 return self.arg < other.arg
56 return NotImplemented
57 def __hash__(self):
58 return hash(self.arg)
Antoine Pitrouc3afba12012-11-17 18:57:38 +010059 def some_method(self):
60 return 4
61 def other_method(self):
62 return 5
63
Antoine Pitroue11fecb2012-11-11 19:36:51 +010064
65class RefCycle:
66 def __init__(self):
67 self.cycle = self
68
69
Fred Drakeb0fefc52001-03-23 04:22:45 +000070class TestBase(unittest.TestCase):
71
72 def setUp(self):
73 self.cbcalled = 0
74
75 def callback(self, ref):
76 self.cbcalled += 1
Fred Drake41deb1e2001-02-01 05:27:45 +000077
78
Antoine Pitrouc1ee4882016-12-19 10:56:40 +010079@contextlib.contextmanager
80def collect_in_thread(period=0.0001):
81 """
82 Ensure GC collections happen in a different thread, at a high frequency.
83 """
Antoine Pitrouc1ee4882016-12-19 10:56:40 +010084 please_stop = False
85
86 def collect():
87 while not please_stop:
88 time.sleep(period)
89 gc.collect()
90
91 with support.disable_gc():
92 t = threading.Thread(target=collect)
93 t.start()
94 try:
95 yield
96 finally:
97 please_stop = True
98 t.join()
99
100
Fred Drakeb0fefc52001-03-23 04:22:45 +0000101class ReferencesTestCase(TestBase):
Fred Drake41deb1e2001-02-01 05:27:45 +0000102
Fred Drakeb0fefc52001-03-23 04:22:45 +0000103 def test_basic_ref(self):
104 self.check_basic_ref(C)
105 self.check_basic_ref(create_function)
106 self.check_basic_ref(create_bound_method)
Fred Drake41deb1e2001-02-01 05:27:45 +0000107
Fred Drake43735da2002-04-11 03:59:42 +0000108 # Just make sure the tp_repr handler doesn't raise an exception.
109 # Live reference:
110 o = C()
111 wr = weakref.ref(o)
Brett Cannon0b70cca2006-08-25 02:59:59 +0000112 repr(wr)
Fred Drake43735da2002-04-11 03:59:42 +0000113 # Dead reference:
114 del o
Brett Cannon0b70cca2006-08-25 02:59:59 +0000115 repr(wr)
Fred Drake43735da2002-04-11 03:59:42 +0000116
Fred Drakeb0fefc52001-03-23 04:22:45 +0000117 def test_basic_callback(self):
118 self.check_basic_callback(C)
119 self.check_basic_callback(create_function)
120 self.check_basic_callback(create_bound_method)
Fred Drake41deb1e2001-02-01 05:27:45 +0000121
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400122 @support.cpython_only
123 def test_cfunction(self):
124 import _testcapi
125 create_cfunction = _testcapi.create_cfunction
126 f = create_cfunction()
127 wr = weakref.ref(f)
128 self.assertIs(wr(), f)
129 del f
130 self.assertIsNone(wr())
131 self.check_basic_ref(create_cfunction)
132 self.check_basic_callback(create_cfunction)
133
Fred Drakeb0fefc52001-03-23 04:22:45 +0000134 def test_multiple_callbacks(self):
135 o = C()
136 ref1 = weakref.ref(o, self.callback)
137 ref2 = weakref.ref(o, self.callback)
138 del o
Serhiy Storchaka462c1f02021-09-08 18:08:57 +0300139 gc_collect() # For PyPy or other GCs.
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200140 self.assertIsNone(ref1(), "expected reference to be invalidated")
141 self.assertIsNone(ref2(), "expected reference to be invalidated")
142 self.assertEqual(self.cbcalled, 2,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000143 "callback not called the right number of times")
Fred Drake41deb1e2001-02-01 05:27:45 +0000144
Fred Drake705088e2001-04-13 17:18:15 +0000145 def test_multiple_selfref_callbacks(self):
Guido van Rossum9eee5542002-08-22 20:21:30 +0000146 # Make sure all references are invalidated before callbacks are called
Fred Drake705088e2001-04-13 17:18:15 +0000147 #
148 # What's important here is that we're using the first
149 # reference in the callback invoked on the second reference
150 # (the most recently created ref is cleaned up first). This
151 # tests that all references to the object are invalidated
152 # before any of the callbacks are invoked, so that we only
153 # have one invocation of _weakref.c:cleanup_helper() active
154 # for a particular object at a time.
155 #
156 def callback(object, self=self):
157 self.ref()
158 c = C()
159 self.ref = weakref.ref(c, callback)
160 ref1 = weakref.ref(c, callback)
161 del c
162
Serhiy Storchaka21eb4872016-05-07 15:41:09 +0300163 def test_constructor_kwargs(self):
164 c = C()
165 self.assertRaises(TypeError, weakref.ref, c, callback=None)
166
Fred Drakeb0fefc52001-03-23 04:22:45 +0000167 def test_proxy_ref(self):
168 o = C()
169 o.bar = 1
170 ref1 = weakref.proxy(o, self.callback)
171 ref2 = weakref.proxy(o, self.callback)
172 del o
Serhiy Storchaka462c1f02021-09-08 18:08:57 +0300173 gc_collect() # For PyPy or other GCs.
Fred Drake41deb1e2001-02-01 05:27:45 +0000174
Fred Drakeb0fefc52001-03-23 04:22:45 +0000175 def check(proxy):
176 proxy.bar
Fred Drake41deb1e2001-02-01 05:27:45 +0000177
Neal Norwitz2633c692007-02-26 22:22:47 +0000178 self.assertRaises(ReferenceError, check, ref1)
179 self.assertRaises(ReferenceError, check, ref2)
Serhiy Storchaka462c1f02021-09-08 18:08:57 +0300180 ref3 = weakref.proxy(C())
181 gc_collect() # For PyPy or other GCs.
182 self.assertRaises(ReferenceError, bool, ref3)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000183 self.assertEqual(self.cbcalled, 2)
Fred Drake41deb1e2001-02-01 05:27:45 +0000184
Fred Drakeb0fefc52001-03-23 04:22:45 +0000185 def check_basic_ref(self, factory):
186 o = factory()
187 ref = weakref.ref(o)
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200188 self.assertIsNotNone(ref(),
Fred Drakeb0fefc52001-03-23 04:22:45 +0000189 "weak reference to live object should be live")
190 o2 = ref()
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200191 self.assertIs(o, o2,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000192 "<ref>() should return original object if live")
Fred Drake41deb1e2001-02-01 05:27:45 +0000193
Fred Drakeb0fefc52001-03-23 04:22:45 +0000194 def check_basic_callback(self, factory):
195 self.cbcalled = 0
196 o = factory()
197 ref = weakref.ref(o, self.callback)
198 del o
Serhiy Storchaka462c1f02021-09-08 18:08:57 +0300199 gc_collect() # For PyPy or other GCs.
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200200 self.assertEqual(self.cbcalled, 1,
Fred Drake705088e2001-04-13 17:18:15 +0000201 "callback did not properly set 'cbcalled'")
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200202 self.assertIsNone(ref(),
Fred Drake705088e2001-04-13 17:18:15 +0000203 "ref2 should be dead after deleting object reference")
Fred Drake41deb1e2001-02-01 05:27:45 +0000204
Fred Drakeb0fefc52001-03-23 04:22:45 +0000205 def test_ref_reuse(self):
206 o = C()
207 ref1 = weakref.ref(o)
208 # create a proxy to make sure that there's an intervening creation
209 # between these two; it should make no difference
210 proxy = weakref.proxy(o)
211 ref2 = weakref.ref(o)
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200212 self.assertIs(ref1, ref2,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000213 "reference object w/out callback should be re-used")
Fred Drake41deb1e2001-02-01 05:27:45 +0000214
Fred Drakeb0fefc52001-03-23 04:22:45 +0000215 o = C()
216 proxy = weakref.proxy(o)
217 ref1 = weakref.ref(o)
218 ref2 = weakref.ref(o)
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200219 self.assertIs(ref1, ref2,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000220 "reference object w/out callback should be re-used")
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200221 self.assertEqual(weakref.getweakrefcount(o), 2,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000222 "wrong weak ref count for object")
223 del proxy
Serhiy Storchaka462c1f02021-09-08 18:08:57 +0300224 gc_collect() # For PyPy or other GCs.
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200225 self.assertEqual(weakref.getweakrefcount(o), 1,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000226 "wrong weak ref count for object after deleting proxy")
Fred Drake41deb1e2001-02-01 05:27:45 +0000227
Fred Drakeb0fefc52001-03-23 04:22:45 +0000228 def test_proxy_reuse(self):
229 o = C()
230 proxy1 = weakref.proxy(o)
231 ref = weakref.ref(o)
232 proxy2 = weakref.proxy(o)
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200233 self.assertIs(proxy1, proxy2,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000234 "proxy object w/out callback should have been re-used")
235
236 def test_basic_proxy(self):
237 o = C()
238 self.check_proxy(o, weakref.proxy(o))
239
Raymond Hettinger53dbe392008-02-12 20:03:09 +0000240 L = collections.UserList()
Fred Drake5935ff02001-12-19 16:54:23 +0000241 p = weakref.proxy(L)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000242 self.assertFalse(p, "proxy for empty UserList should be false")
Fred Drake5935ff02001-12-19 16:54:23 +0000243 p.append(12)
244 self.assertEqual(len(L), 1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000245 self.assertTrue(p, "proxy for non-empty UserList should be true")
Fred Drake5935ff02001-12-19 16:54:23 +0000246 p[:] = [2, 3]
247 self.assertEqual(len(L), 2)
248 self.assertEqual(len(p), 2)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000249 self.assertIn(3, p, "proxy didn't support __contains__() properly")
Fred Drake5935ff02001-12-19 16:54:23 +0000250 p[1] = 5
251 self.assertEqual(L[1], 5)
252 self.assertEqual(p[1], 5)
Raymond Hettinger53dbe392008-02-12 20:03:09 +0000253 L2 = collections.UserList(L)
Fred Drake5935ff02001-12-19 16:54:23 +0000254 p2 = weakref.proxy(L2)
255 self.assertEqual(p, p2)
Walter Dörwald70a6b492004-02-12 17:35:32 +0000256 ## self.assertEqual(repr(L2), repr(p2))
Raymond Hettinger53dbe392008-02-12 20:03:09 +0000257 L3 = collections.UserList(range(10))
Fred Drake43735da2002-04-11 03:59:42 +0000258 p3 = weakref.proxy(L3)
259 self.assertEqual(L3[:], p3[:])
260 self.assertEqual(L3[5:], p3[5:])
261 self.assertEqual(L3[:5], p3[:5])
262 self.assertEqual(L3[2:5], p3[2:5])
Fred Drake5935ff02001-12-19 16:54:23 +0000263
Benjamin Peterson32019772009-11-19 03:08:32 +0000264 def test_proxy_unicode(self):
265 # See bug 5037
266 class C(object):
267 def __str__(self):
268 return "string"
269 def __bytes__(self):
270 return b"bytes"
271 instance = C()
Benjamin Peterson577473f2010-01-19 00:09:57 +0000272 self.assertIn("__bytes__", dir(weakref.proxy(instance)))
Benjamin Peterson32019772009-11-19 03:08:32 +0000273 self.assertEqual(bytes(weakref.proxy(instance)), b"bytes")
274
Georg Brandlb533e262008-05-25 18:19:30 +0000275 def test_proxy_index(self):
276 class C:
277 def __index__(self):
278 return 10
279 o = C()
280 p = weakref.proxy(o)
281 self.assertEqual(operator.index(p), 10)
282
283 def test_proxy_div(self):
284 class C:
285 def __floordiv__(self, other):
286 return 42
287 def __ifloordiv__(self, other):
288 return 21
289 o = C()
290 p = weakref.proxy(o)
291 self.assertEqual(p // 5, 42)
292 p //= 5
293 self.assertEqual(p, 21)
294
Mark Dickinson7abb6c02019-04-26 15:56:15 +0900295 def test_proxy_matmul(self):
296 class C:
297 def __matmul__(self, other):
298 return 1729
299 def __rmatmul__(self, other):
300 return -163
301 def __imatmul__(self, other):
302 return 561
303 o = C()
304 p = weakref.proxy(o)
305 self.assertEqual(p @ 5, 1729)
306 self.assertEqual(5 @ p, -163)
307 p @= 5
308 self.assertEqual(p, 561)
309
Fred Drakeea2adc92004-02-03 19:56:46 +0000310 # The PyWeakref_* C API is documented as allowing either NULL or
311 # None as the value for the callback, where either means "no
312 # callback". The "no callback" ref and proxy objects are supposed
313 # to be shared so long as they exist by all callers so long as
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000314 # they are active. In Python 2.3.3 and earlier, this guarantee
Fred Drakeea2adc92004-02-03 19:56:46 +0000315 # was not honored, and was broken in different ways for
316 # PyWeakref_NewRef() and PyWeakref_NewProxy(). (Two tests.)
317
318 def test_shared_ref_without_callback(self):
319 self.check_shared_without_callback(weakref.ref)
320
321 def test_shared_proxy_without_callback(self):
322 self.check_shared_without_callback(weakref.proxy)
323
324 def check_shared_without_callback(self, makeref):
325 o = Object(1)
326 p1 = makeref(o, None)
327 p2 = makeref(o, None)
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200328 self.assertIs(p1, p2, "both callbacks were None in the C API")
Fred Drakeea2adc92004-02-03 19:56:46 +0000329 del p1, p2
330 p1 = makeref(o)
331 p2 = makeref(o, None)
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200332 self.assertIs(p1, p2, "callbacks were NULL, None in the C API")
Fred Drakeea2adc92004-02-03 19:56:46 +0000333 del p1, p2
334 p1 = makeref(o)
335 p2 = makeref(o)
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200336 self.assertIs(p1, p2, "both callbacks were NULL in the C API")
Fred Drakeea2adc92004-02-03 19:56:46 +0000337 del p1, p2
338 p1 = makeref(o, None)
339 p2 = makeref(o)
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200340 self.assertIs(p1, p2, "callbacks were None, NULL in the C API")
Fred Drakeea2adc92004-02-03 19:56:46 +0000341
Fred Drakeb0fefc52001-03-23 04:22:45 +0000342 def test_callable_proxy(self):
343 o = Callable()
344 ref1 = weakref.proxy(o)
345
346 self.check_proxy(o, ref1)
347
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200348 self.assertIs(type(ref1), weakref.CallableProxyType,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000349 "proxy is not of callable type")
350 ref1('twinkies!')
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200351 self.assertEqual(o.bar, 'twinkies!',
Fred Drakeb0fefc52001-03-23 04:22:45 +0000352 "call through proxy not passed through to original")
Fred Drake3bb4d212001-10-18 19:28:29 +0000353 ref1(x='Splat.')
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200354 self.assertEqual(o.bar, 'Splat.',
Fred Drake3bb4d212001-10-18 19:28:29 +0000355 "call through proxy not passed through to original")
Fred Drakeb0fefc52001-03-23 04:22:45 +0000356
357 # expect due to too few args
358 self.assertRaises(TypeError, ref1)
359
360 # expect due to too many args
361 self.assertRaises(TypeError, ref1, 1, 2, 3)
362
363 def check_proxy(self, o, proxy):
364 o.foo = 1
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200365 self.assertEqual(proxy.foo, 1,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000366 "proxy does not reflect attribute addition")
367 o.foo = 2
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200368 self.assertEqual(proxy.foo, 2,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000369 "proxy does not reflect attribute modification")
370 del o.foo
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200371 self.assertFalse(hasattr(proxy, 'foo'),
Fred Drakeb0fefc52001-03-23 04:22:45 +0000372 "proxy does not reflect attribute removal")
373
374 proxy.foo = 1
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200375 self.assertEqual(o.foo, 1,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000376 "object does not reflect attribute addition via proxy")
377 proxy.foo = 2
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200378 self.assertEqual(o.foo, 2,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000379 "object does not reflect attribute modification via proxy")
380 del proxy.foo
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200381 self.assertFalse(hasattr(o, 'foo'),
Fred Drakeb0fefc52001-03-23 04:22:45 +0000382 "object does not reflect attribute removal via proxy")
383
Raymond Hettingerd693a812003-06-30 04:18:48 +0000384 def test_proxy_deletion(self):
385 # Test clearing of SF bug #762891
386 class Foo:
387 result = None
388 def __delitem__(self, accessor):
389 self.result = accessor
390 g = Foo()
391 f = weakref.proxy(g)
392 del f[0]
393 self.assertEqual(f.result, 0)
394
Raymond Hettingere6c470f2005-03-27 03:04:54 +0000395 def test_proxy_bool(self):
396 # Test clearing of SF bug #1170766
397 class List(list): pass
398 lyst = List()
399 self.assertEqual(bool(weakref.proxy(lyst)), bool(lyst))
400
Pablo Galindo10cd00a2019-10-08 16:30:50 +0100401 def test_proxy_iter(self):
402 # Test fails with a debug build of the interpreter
403 # (see bpo-38395).
404
405 obj = None
406
407 class MyObj:
408 def __iter__(self):
409 nonlocal obj
410 del obj
411 return NotImplemented
412
413 obj = MyObj()
414 p = weakref.proxy(obj)
415 with self.assertRaises(TypeError):
416 # "blech" in p calls MyObj.__iter__ through the proxy,
417 # without keeping a reference to the real object, so it
418 # can be killed in the middle of the call
419 "blech" in p
420
Miss Islington (bot)659030c2021-07-24 02:45:13 -0700421 def test_proxy_next(self):
422 arr = [4, 5, 6]
423 def iterator_func():
424 yield from arr
425 it = iterator_func()
426
427 class IteratesWeakly:
428 def __iter__(self):
429 return weakref.proxy(it)
430
431 weak_it = IteratesWeakly()
432
433 # Calls proxy.__next__
434 self.assertEqual(list(weak_it), [4, 5, 6])
435
436 def test_proxy_bad_next(self):
437 # bpo-44720: PyIter_Next() shouldn't be called if the reference
438 # isn't an iterator.
439
440 not_an_iterator = lambda: 0
441
442 class A:
443 def __iter__(self):
444 return weakref.proxy(not_an_iterator)
445 a = A()
446
447 msg = "Weakref proxy referenced a non-iterator"
448 with self.assertRaisesRegex(TypeError, msg):
449 list(a)
450
Pablo Galindo96074de2020-05-05 22:58:19 +0100451 def test_proxy_reversed(self):
452 class MyObj:
453 def __len__(self):
454 return 3
455 def __reversed__(self):
456 return iter('cba')
457
458 obj = MyObj()
459 self.assertEqual("".join(reversed(weakref.proxy(obj))), "cba")
460
461 def test_proxy_hash(self):
Pablo Galindo96074de2020-05-05 22:58:19 +0100462 class MyObj:
463 def __hash__(self):
Miss Islington (bot)2df13e12021-06-29 16:19:06 -0700464 return 42
Pablo Galindo96074de2020-05-05 22:58:19 +0100465
466 obj = MyObj()
Miss Islington (bot)2df13e12021-06-29 16:19:06 -0700467 with self.assertRaises(TypeError):
468 hash(weakref.proxy(obj))
469
470 class MyObj:
471 __hash__ = None
472
473 obj = MyObj()
474 with self.assertRaises(TypeError):
475 hash(weakref.proxy(obj))
Pablo Galindo96074de2020-05-05 22:58:19 +0100476
Fred Drakeb0fefc52001-03-23 04:22:45 +0000477 def test_getweakrefcount(self):
478 o = C()
479 ref1 = weakref.ref(o)
480 ref2 = weakref.ref(o, self.callback)
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200481 self.assertEqual(weakref.getweakrefcount(o), 2,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000482 "got wrong number of weak reference objects")
483
484 proxy1 = weakref.proxy(o)
485 proxy2 = weakref.proxy(o, self.callback)
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200486 self.assertEqual(weakref.getweakrefcount(o), 4,
Fred Drakeb0fefc52001-03-23 04:22:45 +0000487 "got wrong number of weak reference objects")
488
Fred Drakeea2adc92004-02-03 19:56:46 +0000489 del ref1, ref2, proxy1, proxy2
Serhiy Storchaka462c1f02021-09-08 18:08:57 +0300490 gc_collect() # For PyPy or other GCs.
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200491 self.assertEqual(weakref.getweakrefcount(o), 0,
Fred Drakeea2adc92004-02-03 19:56:46 +0000492 "weak reference objects not unlinked from"
493 " referent when discarded.")
494
Walter Dörwaldb167b042003-12-11 12:34:05 +0000495 # assumes ints do not support weakrefs
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200496 self.assertEqual(weakref.getweakrefcount(1), 0,
Walter Dörwaldb167b042003-12-11 12:34:05 +0000497 "got wrong number of weak reference objects for int")
498
Fred Drakeb0fefc52001-03-23 04:22:45 +0000499 def test_getweakrefs(self):
500 o = C()
501 ref1 = weakref.ref(o, self.callback)
502 ref2 = weakref.ref(o, self.callback)
503 del ref1
Serhiy Storchaka462c1f02021-09-08 18:08:57 +0300504 gc_collect() # For PyPy or other GCs.
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200505 self.assertEqual(weakref.getweakrefs(o), [ref2],
Fred Drakeb0fefc52001-03-23 04:22:45 +0000506 "list of refs does not match")
507
508 o = C()
509 ref1 = weakref.ref(o, self.callback)
510 ref2 = weakref.ref(o, self.callback)
511 del ref2
Serhiy Storchaka462c1f02021-09-08 18:08:57 +0300512 gc_collect() # For PyPy or other GCs.
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200513 self.assertEqual(weakref.getweakrefs(o), [ref1],
Fred Drakeb0fefc52001-03-23 04:22:45 +0000514 "list of refs does not match")
515
Fred Drakeea2adc92004-02-03 19:56:46 +0000516 del ref1
Serhiy Storchaka462c1f02021-09-08 18:08:57 +0300517 gc_collect() # For PyPy or other GCs.
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200518 self.assertEqual(weakref.getweakrefs(o), [],
Fred Drakeea2adc92004-02-03 19:56:46 +0000519 "list of refs not cleared")
520
Walter Dörwaldb167b042003-12-11 12:34:05 +0000521 # assumes ints do not support weakrefs
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200522 self.assertEqual(weakref.getweakrefs(1), [],
Walter Dörwaldb167b042003-12-11 12:34:05 +0000523 "list of refs does not match for int")
524
Fred Drake39c27f12001-10-18 18:06:05 +0000525 def test_newstyle_number_ops(self):
526 class F(float):
527 pass
528 f = F(2.0)
529 p = weakref.proxy(f)
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200530 self.assertEqual(p + 1.0, 3.0)
531 self.assertEqual(1.0 + p, 3.0) # this used to SEGV
Fred Drake39c27f12001-10-18 18:06:05 +0000532
Fred Drake2a64f462001-12-10 23:46:02 +0000533 def test_callbacks_protected(self):
Guido van Rossum9eee5542002-08-22 20:21:30 +0000534 # Callbacks protected from already-set exceptions?
Fred Drake2a64f462001-12-10 23:46:02 +0000535 # Regression test for SF bug #478534.
536 class BogusError(Exception):
537 pass
538 data = {}
539 def remove(k):
540 del data[k]
541 def encapsulate():
542 f = lambda : ()
543 data[weakref.ref(f, remove)] = None
544 raise BogusError
545 try:
546 encapsulate()
547 except BogusError:
548 pass
549 else:
550 self.fail("exception not properly restored")
551 try:
552 encapsulate()
553 except BogusError:
554 pass
555 else:
556 self.fail("exception not properly restored")
557
Tim Petersadd09b42003-11-12 20:43:28 +0000558 def test_sf_bug_840829(self):
559 # "weakref callbacks and gc corrupt memory"
560 # subtype_dealloc erroneously exposed a new-style instance
561 # already in the process of getting deallocated to gc,
562 # causing double-deallocation if the instance had a weakref
563 # callback that triggered gc.
564 # If the bug exists, there probably won't be an obvious symptom
565 # in a release build. In a debug build, a segfault will occur
566 # when the second attempt to remove the instance from the "list
567 # of all objects" occurs.
568
569 import gc
570
571 class C(object):
572 pass
573
574 c = C()
575 wr = weakref.ref(c, lambda ignore: gc.collect())
576 del c
577
Tim Petersf7f9e992003-11-13 21:59:32 +0000578 # There endeth the first part. It gets worse.
579 del wr
580
581 c1 = C()
582 c1.i = C()
583 wr = weakref.ref(c1.i, lambda ignore: gc.collect())
584
585 c2 = C()
586 c2.c1 = c1
587 del c1 # still alive because c2 points to it
588
589 # Now when subtype_dealloc gets called on c2, it's not enough just
590 # that c2 is immune from gc while the weakref callbacks associated
591 # with c2 execute (there are none in this 2nd half of the test, btw).
592 # subtype_dealloc goes on to call the base classes' deallocs too,
593 # so any gc triggered by weakref callbacks associated with anything
594 # torn down by a base class dealloc can also trigger double
595 # deallocation of c2.
596 del c2
Fred Drake41deb1e2001-02-01 05:27:45 +0000597
Tim Peters403a2032003-11-20 21:21:46 +0000598 def test_callback_in_cycle_1(self):
599 import gc
600
601 class J(object):
602 pass
603
604 class II(object):
605 def acallback(self, ignore):
606 self.J
607
608 I = II()
609 I.J = J
610 I.wr = weakref.ref(J, I.acallback)
611
612 # Now J and II are each in a self-cycle (as all new-style class
613 # objects are, since their __mro__ points back to them). I holds
614 # both a weak reference (I.wr) and a strong reference (I.J) to class
615 # J. I is also in a cycle (I.wr points to a weakref that references
616 # I.acallback). When we del these three, they all become trash, but
617 # the cycles prevent any of them from getting cleaned up immediately.
618 # Instead they have to wait for cyclic gc to deduce that they're
619 # trash.
620 #
621 # gc used to call tp_clear on all of them, and the order in which
622 # it does that is pretty accidental. The exact order in which we
623 # built up these things manages to provoke gc into running tp_clear
624 # in just the right order (I last). Calling tp_clear on II leaves
625 # behind an insane class object (its __mro__ becomes NULL). Calling
626 # tp_clear on J breaks its self-cycle, but J doesn't get deleted
627 # just then because of the strong reference from I.J. Calling
628 # tp_clear on I starts to clear I's __dict__, and just happens to
629 # clear I.J first -- I.wr is still intact. That removes the last
630 # reference to J, which triggers the weakref callback. The callback
631 # tries to do "self.J", and instances of new-style classes look up
632 # attributes ("J") in the class dict first. The class (II) wants to
633 # search II.__mro__, but that's NULL. The result was a segfault in
634 # a release build, and an assert failure in a debug build.
635 del I, J, II
636 gc.collect()
637
638 def test_callback_in_cycle_2(self):
639 import gc
640
641 # This is just like test_callback_in_cycle_1, except that II is an
642 # old-style class. The symptom is different then: an instance of an
643 # old-style class looks in its own __dict__ first. 'J' happens to
644 # get cleared from I.__dict__ before 'wr', and 'J' was never in II's
645 # __dict__, so the attribute isn't found. The difference is that
646 # the old-style II doesn't have a NULL __mro__ (it doesn't have any
647 # __mro__), so no segfault occurs. Instead it got:
648 # test_callback_in_cycle_2 (__main__.ReferencesTestCase) ...
649 # Exception exceptions.AttributeError:
650 # "II instance has no attribute 'J'" in <bound method II.acallback
651 # of <?.II instance at 0x00B9B4B8>> ignored
652
653 class J(object):
654 pass
655
656 class II:
657 def acallback(self, ignore):
658 self.J
659
660 I = II()
661 I.J = J
662 I.wr = weakref.ref(J, I.acallback)
663
664 del I, J, II
665 gc.collect()
666
667 def test_callback_in_cycle_3(self):
668 import gc
669
670 # This one broke the first patch that fixed the last two. In this
671 # case, the objects reachable from the callback aren't also reachable
672 # from the object (c1) *triggering* the callback: you can get to
673 # c1 from c2, but not vice-versa. The result was that c2's __dict__
674 # got tp_clear'ed by the time the c2.cb callback got invoked.
675
676 class C:
677 def cb(self, ignore):
678 self.me
679 self.c1
680 self.wr
681
682 c1, c2 = C(), C()
683
684 c2.me = c2
685 c2.c1 = c1
686 c2.wr = weakref.ref(c1, c2.cb)
687
688 del c1, c2
689 gc.collect()
690
691 def test_callback_in_cycle_4(self):
692 import gc
693
694 # Like test_callback_in_cycle_3, except c2 and c1 have different
695 # classes. c2's class (C) isn't reachable from c1 then, so protecting
696 # objects reachable from the dying object (c1) isn't enough to stop
697 # c2's class (C) from getting tp_clear'ed before c2.cb is invoked.
698 # The result was a segfault (C.__mro__ was NULL when the callback
699 # tried to look up self.me).
700
701 class C(object):
702 def cb(self, ignore):
703 self.me
704 self.c1
705 self.wr
706
707 class D:
708 pass
709
710 c1, c2 = D(), C()
711
712 c2.me = c2
713 c2.c1 = c1
714 c2.wr = weakref.ref(c1, c2.cb)
715
716 del c1, c2, C, D
717 gc.collect()
718
719 def test_callback_in_cycle_resurrection(self):
720 import gc
721
722 # Do something nasty in a weakref callback: resurrect objects
723 # from dead cycles. For this to be attempted, the weakref and
724 # its callback must also be part of the cyclic trash (else the
725 # objects reachable via the callback couldn't be in cyclic trash
726 # to begin with -- the callback would act like an external root).
727 # But gc clears trash weakrefs with callbacks early now, which
728 # disables the callbacks, so the callbacks shouldn't get called
729 # at all (and so nothing actually gets resurrected).
730
731 alist = []
732 class C(object):
733 def __init__(self, value):
734 self.attribute = value
735
736 def acallback(self, ignore):
737 alist.append(self.c)
738
739 c1, c2 = C(1), C(2)
740 c1.c = c2
741 c2.c = c1
742 c1.wr = weakref.ref(c2, c1.acallback)
743 c2.wr = weakref.ref(c1, c2.acallback)
744
745 def C_went_away(ignore):
746 alist.append("C went away")
747 wr = weakref.ref(C, C_went_away)
748
749 del c1, c2, C # make them all trash
750 self.assertEqual(alist, []) # del isn't enough to reclaim anything
751
752 gc.collect()
753 # c1.wr and c2.wr were part of the cyclic trash, so should have
754 # been cleared without their callbacks executing. OTOH, the weakref
755 # to C is bound to a function local (wr), and wasn't trash, so that
756 # callback should have been invoked when C went away.
757 self.assertEqual(alist, ["C went away"])
758 # The remaining weakref should be dead now (its callback ran).
759 self.assertEqual(wr(), None)
760
761 del alist[:]
762 gc.collect()
763 self.assertEqual(alist, [])
764
765 def test_callbacks_on_callback(self):
766 import gc
767
768 # Set up weakref callbacks *on* weakref callbacks.
769 alist = []
770 def safe_callback(ignore):
771 alist.append("safe_callback called")
772
773 class C(object):
774 def cb(self, ignore):
775 alist.append("cb called")
776
777 c, d = C(), C()
778 c.other = d
779 d.other = c
780 callback = c.cb
781 c.wr = weakref.ref(d, callback) # this won't trigger
782 d.wr = weakref.ref(callback, d.cb) # ditto
783 external_wr = weakref.ref(callback, safe_callback) # but this will
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200784 self.assertIs(external_wr(), callback)
Tim Peters403a2032003-11-20 21:21:46 +0000785
786 # The weakrefs attached to c and d should get cleared, so that
787 # C.cb is never called. But external_wr isn't part of the cyclic
788 # trash, and no cyclic trash is reachable from it, so safe_callback
789 # should get invoked when the bound method object callback (c.cb)
790 # -- which is itself a callback, and also part of the cyclic trash --
791 # gets reclaimed at the end of gc.
792
793 del callback, c, d, C
794 self.assertEqual(alist, []) # del isn't enough to clean up cycles
795 gc.collect()
796 self.assertEqual(alist, ["safe_callback called"])
797 self.assertEqual(external_wr(), None)
798
799 del alist[:]
800 gc.collect()
801 self.assertEqual(alist, [])
802
Fred Drakebc875f52004-02-04 23:14:14 +0000803 def test_gc_during_ref_creation(self):
804 self.check_gc_during_creation(weakref.ref)
805
806 def test_gc_during_proxy_creation(self):
807 self.check_gc_during_creation(weakref.proxy)
808
809 def check_gc_during_creation(self, makeref):
810 thresholds = gc.get_threshold()
811 gc.set_threshold(1, 1, 1)
812 gc.collect()
Fred Drake55cf4342004-02-13 19:21:57 +0000813 class A:
814 pass
Fred Drakebc875f52004-02-04 23:14:14 +0000815
816 def callback(*args):
817 pass
818
Fred Drake55cf4342004-02-13 19:21:57 +0000819 referenced = A()
Fred Drakebc875f52004-02-04 23:14:14 +0000820
Fred Drake55cf4342004-02-13 19:21:57 +0000821 a = A()
Fred Drakebc875f52004-02-04 23:14:14 +0000822 a.a = a
823 a.wr = makeref(referenced)
824
825 try:
826 # now make sure the object and the ref get labeled as
827 # cyclic trash:
Fred Drake55cf4342004-02-13 19:21:57 +0000828 a = A()
829 weakref.ref(referenced, callback)
Fred Drakebc875f52004-02-04 23:14:14 +0000830
831 finally:
832 gc.set_threshold(*thresholds)
833
Thomas Woutersb2137042007-02-01 18:02:27 +0000834 def test_ref_created_during_del(self):
835 # Bug #1377858
836 # A weakref created in an object's __del__() would crash the
837 # interpreter when the weakref was cleaned up since it would refer to
838 # non-existent memory. This test should not segfault the interpreter.
839 class Target(object):
840 def __del__(self):
841 global ref_from_del
842 ref_from_del = weakref.ref(self)
843
844 w = Target()
845
Benjamin Peterson9aa42992008-09-10 21:57:34 +0000846 def test_init(self):
847 # Issue 3634
848 # <weakref to class>.__init__() doesn't check errors correctly
849 r = weakref.ref(Exception)
850 self.assertRaises(TypeError, r.__init__, 0, 0, 0, 0, 0)
851 # No exception should be raised here
852 gc.collect()
853
Antoine Pitrou3af01a12010-03-31 21:40:47 +0000854 def test_classes(self):
855 # Check that classes are weakrefable.
856 class A(object):
857 pass
858 l = []
859 weakref.ref(int)
860 a = weakref.ref(A, l.append)
861 A = None
862 gc.collect()
863 self.assertEqual(a(), None)
864 self.assertEqual(l, [a])
865
Antoine Pitroue11fecb2012-11-11 19:36:51 +0100866 def test_equality(self):
867 # Alive weakrefs defer equality testing to their underlying object.
868 x = Object(1)
869 y = Object(1)
870 z = Object(2)
871 a = weakref.ref(x)
872 b = weakref.ref(y)
873 c = weakref.ref(z)
874 d = weakref.ref(x)
875 # Note how we directly test the operators here, to stress both
876 # __eq__ and __ne__.
877 self.assertTrue(a == b)
878 self.assertFalse(a != b)
879 self.assertFalse(a == c)
880 self.assertTrue(a != c)
881 self.assertTrue(a == d)
882 self.assertFalse(a != d)
Serhiy Storchaka662db122019-08-08 08:42:54 +0300883 self.assertFalse(a == x)
884 self.assertTrue(a != x)
885 self.assertTrue(a == ALWAYS_EQ)
886 self.assertFalse(a != ALWAYS_EQ)
Antoine Pitroue11fecb2012-11-11 19:36:51 +0100887 del x, y, z
888 gc.collect()
889 for r in a, b, c:
890 # Sanity check
891 self.assertIs(r(), None)
892 # Dead weakrefs compare by identity: whether `a` and `d` are the
893 # same weakref object is an implementation detail, since they pointed
894 # to the same original object and didn't have a callback.
895 # (see issue #16453).
896 self.assertFalse(a == b)
897 self.assertTrue(a != b)
898 self.assertFalse(a == c)
899 self.assertTrue(a != c)
900 self.assertEqual(a == d, a is d)
901 self.assertEqual(a != d, a is not d)
902
903 def test_ordering(self):
904 # weakrefs cannot be ordered, even if the underlying objects can.
905 ops = [operator.lt, operator.gt, operator.le, operator.ge]
906 x = Object(1)
907 y = Object(1)
908 a = weakref.ref(x)
909 b = weakref.ref(y)
910 for op in ops:
911 self.assertRaises(TypeError, op, a, b)
912 # Same when dead.
913 del x, y
914 gc.collect()
915 for op in ops:
916 self.assertRaises(TypeError, op, a, b)
917
918 def test_hashing(self):
919 # Alive weakrefs hash the same as the underlying object
920 x = Object(42)
921 y = Object(42)
922 a = weakref.ref(x)
923 b = weakref.ref(y)
924 self.assertEqual(hash(a), hash(42))
925 del x, y
926 gc.collect()
927 # Dead weakrefs:
928 # - retain their hash is they were hashed when alive;
929 # - otherwise, cannot be hashed.
930 self.assertEqual(hash(a), hash(42))
931 self.assertRaises(TypeError, hash, b)
932
Antoine Pitrou62a0d6e2012-12-08 21:15:26 +0100933 def test_trashcan_16602(self):
934 # Issue #16602: when a weakref's target was part of a long
935 # deallocation chain, the trashcan mechanism could delay clearing
936 # of the weakref and make the target object visible from outside
937 # code even though its refcount had dropped to 0. A crash ensued.
938 class C:
939 def __init__(self, parent):
940 if not parent:
941 return
942 wself = weakref.ref(self)
943 def cb(wparent):
944 o = wself()
945 self.wparent = weakref.ref(parent, cb)
946
947 d = weakref.WeakKeyDictionary()
948 root = c = C(None)
949 for n in range(100):
950 d[c] = c = C(c)
951 del root
952 gc.collect()
953
Mark Dickinson556e94b2013-04-13 15:45:44 +0100954 def test_callback_attribute(self):
955 x = Object(1)
956 callback = lambda ref: None
957 ref1 = weakref.ref(x, callback)
958 self.assertIs(ref1.__callback__, callback)
959
960 ref2 = weakref.ref(x)
961 self.assertIsNone(ref2.__callback__)
962
963 def test_callback_attribute_after_deletion(self):
964 x = Object(1)
965 ref = weakref.ref(x, self.callback)
966 self.assertIsNotNone(ref.__callback__)
967 del x
968 support.gc_collect()
969 self.assertIsNone(ref.__callback__)
970
971 def test_set_callback_attribute(self):
972 x = Object(1)
973 callback = lambda ref: None
974 ref1 = weakref.ref(x, callback)
975 with self.assertRaises(AttributeError):
976 ref1.__callback__ = lambda ref: None
977
Benjamin Peterson8f657c32016-10-04 00:00:02 -0700978 def test_callback_gcs(self):
979 class ObjectWithDel(Object):
980 def __del__(self): pass
981 x = ObjectWithDel(1)
982 ref1 = weakref.ref(x, lambda ref: support.gc_collect())
983 del x
984 support.gc_collect()
985
Fred Drake0a4dd392004-07-02 18:57:45 +0000986
Amaury Forgeot d'Arcc856c7a2008-06-16 19:50:09 +0000987class SubclassableWeakrefTestCase(TestBase):
Fred Drake0a4dd392004-07-02 18:57:45 +0000988
989 def test_subclass_refs(self):
990 class MyRef(weakref.ref):
991 def __init__(self, ob, callback=None, value=42):
992 self.value = value
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000993 super().__init__(ob, callback)
Fred Drake0a4dd392004-07-02 18:57:45 +0000994 def __call__(self):
995 self.called = True
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000996 return super().__call__()
Fred Drake0a4dd392004-07-02 18:57:45 +0000997 o = Object("foo")
998 mr = MyRef(o, value=24)
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +0200999 self.assertIs(mr(), o)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001000 self.assertTrue(mr.called)
Fred Drake0a4dd392004-07-02 18:57:45 +00001001 self.assertEqual(mr.value, 24)
1002 del o
Serhiy Storchaka462c1f02021-09-08 18:08:57 +03001003 gc_collect() # For PyPy or other GCs.
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +02001004 self.assertIsNone(mr())
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001005 self.assertTrue(mr.called)
Fred Drake0a4dd392004-07-02 18:57:45 +00001006
1007 def test_subclass_refs_dont_replace_standard_refs(self):
1008 class MyRef(weakref.ref):
1009 pass
1010 o = Object(42)
1011 r1 = MyRef(o)
1012 r2 = weakref.ref(o)
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +02001013 self.assertIsNot(r1, r2)
Fred Drake0a4dd392004-07-02 18:57:45 +00001014 self.assertEqual(weakref.getweakrefs(o), [r2, r1])
1015 self.assertEqual(weakref.getweakrefcount(o), 2)
1016 r3 = MyRef(o)
1017 self.assertEqual(weakref.getweakrefcount(o), 3)
1018 refs = weakref.getweakrefs(o)
1019 self.assertEqual(len(refs), 3)
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +02001020 self.assertIs(r2, refs[0])
Benjamin Peterson577473f2010-01-19 00:09:57 +00001021 self.assertIn(r1, refs[1:])
1022 self.assertIn(r3, refs[1:])
Fred Drake0a4dd392004-07-02 18:57:45 +00001023
1024 def test_subclass_refs_dont_conflate_callbacks(self):
1025 class MyRef(weakref.ref):
1026 pass
1027 o = Object(42)
1028 r1 = MyRef(o, id)
1029 r2 = MyRef(o, str)
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +02001030 self.assertIsNot(r1, r2)
Fred Drake0a4dd392004-07-02 18:57:45 +00001031 refs = weakref.getweakrefs(o)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001032 self.assertIn(r1, refs)
1033 self.assertIn(r2, refs)
Fred Drake0a4dd392004-07-02 18:57:45 +00001034
1035 def test_subclass_refs_with_slots(self):
1036 class MyRef(weakref.ref):
1037 __slots__ = "slot1", "slot2"
1038 def __new__(type, ob, callback, slot1, slot2):
1039 return weakref.ref.__new__(type, ob, callback)
1040 def __init__(self, ob, callback, slot1, slot2):
1041 self.slot1 = slot1
1042 self.slot2 = slot2
1043 def meth(self):
1044 return self.slot1 + self.slot2
1045 o = Object(42)
1046 r = MyRef(o, None, "abc", "def")
1047 self.assertEqual(r.slot1, "abc")
1048 self.assertEqual(r.slot2, "def")
1049 self.assertEqual(r.meth(), "abcdef")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001050 self.assertFalse(hasattr(r, "__dict__"))
Fred Drake0a4dd392004-07-02 18:57:45 +00001051
Amaury Forgeot d'Arcc856c7a2008-06-16 19:50:09 +00001052 def test_subclass_refs_with_cycle(self):
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)cd14d5d2016-09-07 00:22:22 +00001053 """Confirm https://bugs.python.org/issue3100 is fixed."""
Amaury Forgeot d'Arcc856c7a2008-06-16 19:50:09 +00001054 # An instance of a weakref subclass can have attributes.
1055 # If such a weakref holds the only strong reference to the object,
1056 # deleting the weakref will delete the object. In this case,
1057 # the callback must not be called, because the ref object is
1058 # being deleted.
1059 class MyRef(weakref.ref):
1060 pass
1061
1062 # Use a local callback, for "regrtest -R::"
1063 # to detect refcounting problems
1064 def callback(w):
1065 self.cbcalled += 1
1066
1067 o = C()
1068 r1 = MyRef(o, callback)
1069 r1.o = o
1070 del o
1071
1072 del r1 # Used to crash here
1073
1074 self.assertEqual(self.cbcalled, 0)
1075
1076 # Same test, with two weakrefs to the same object
1077 # (since code paths are different)
1078 o = C()
1079 r1 = MyRef(o, callback)
1080 r2 = MyRef(o, callback)
1081 r1.r = r2
1082 r2.o = o
1083 del o
1084 del r2
1085
1086 del r1 # Used to crash here
1087
1088 self.assertEqual(self.cbcalled, 0)
1089
Fred Drake0a4dd392004-07-02 18:57:45 +00001090
Antoine Pitrouc3afba12012-11-17 18:57:38 +01001091class WeakMethodTestCase(unittest.TestCase):
1092
1093 def _subclass(self):
Martin Panter7462b6492015-11-02 03:37:02 +00001094 """Return an Object subclass overriding `some_method`."""
Antoine Pitrouc3afba12012-11-17 18:57:38 +01001095 class C(Object):
1096 def some_method(self):
1097 return 6
1098 return C
1099
1100 def test_alive(self):
1101 o = Object(1)
1102 r = weakref.WeakMethod(o.some_method)
1103 self.assertIsInstance(r, weakref.ReferenceType)
1104 self.assertIsInstance(r(), type(o.some_method))
1105 self.assertIs(r().__self__, o)
1106 self.assertIs(r().__func__, o.some_method.__func__)
1107 self.assertEqual(r()(), 4)
1108
1109 def test_object_dead(self):
1110 o = Object(1)
1111 r = weakref.WeakMethod(o.some_method)
1112 del o
1113 gc.collect()
1114 self.assertIs(r(), None)
1115
1116 def test_method_dead(self):
1117 C = self._subclass()
1118 o = C(1)
1119 r = weakref.WeakMethod(o.some_method)
1120 del C.some_method
1121 gc.collect()
1122 self.assertIs(r(), None)
1123
1124 def test_callback_when_object_dead(self):
1125 # Test callback behaviour when object dies first.
1126 C = self._subclass()
1127 calls = []
1128 def cb(arg):
1129 calls.append(arg)
1130 o = C(1)
1131 r = weakref.WeakMethod(o.some_method, cb)
1132 del o
1133 gc.collect()
1134 self.assertEqual(calls, [r])
1135 # Callback is only called once.
1136 C.some_method = Object.some_method
1137 gc.collect()
1138 self.assertEqual(calls, [r])
1139
1140 def test_callback_when_method_dead(self):
1141 # Test callback behaviour when method dies first.
1142 C = self._subclass()
1143 calls = []
1144 def cb(arg):
1145 calls.append(arg)
1146 o = C(1)
1147 r = weakref.WeakMethod(o.some_method, cb)
1148 del C.some_method
1149 gc.collect()
1150 self.assertEqual(calls, [r])
1151 # Callback is only called once.
1152 del o
1153 gc.collect()
1154 self.assertEqual(calls, [r])
1155
1156 @support.cpython_only
1157 def test_no_cycles(self):
1158 # A WeakMethod doesn't create any reference cycle to itself.
1159 o = Object(1)
1160 def cb(_):
1161 pass
1162 r = weakref.WeakMethod(o.some_method, cb)
1163 wr = weakref.ref(r)
1164 del r
1165 self.assertIs(wr(), None)
1166
1167 def test_equality(self):
1168 def _eq(a, b):
1169 self.assertTrue(a == b)
1170 self.assertFalse(a != b)
1171 def _ne(a, b):
1172 self.assertTrue(a != b)
1173 self.assertFalse(a == b)
1174 x = Object(1)
1175 y = Object(1)
1176 a = weakref.WeakMethod(x.some_method)
1177 b = weakref.WeakMethod(y.some_method)
1178 c = weakref.WeakMethod(x.other_method)
1179 d = weakref.WeakMethod(y.other_method)
1180 # Objects equal, same method
1181 _eq(a, b)
1182 _eq(c, d)
1183 # Objects equal, different method
1184 _ne(a, c)
1185 _ne(a, d)
1186 _ne(b, c)
1187 _ne(b, d)
1188 # Objects unequal, same or different method
1189 z = Object(2)
1190 e = weakref.WeakMethod(z.some_method)
1191 f = weakref.WeakMethod(z.other_method)
1192 _ne(a, e)
1193 _ne(a, f)
1194 _ne(b, e)
1195 _ne(b, f)
Serhiy Storchaka662db122019-08-08 08:42:54 +03001196 # Compare with different types
1197 _ne(a, x.some_method)
1198 _eq(a, ALWAYS_EQ)
Antoine Pitrouc3afba12012-11-17 18:57:38 +01001199 del x, y, z
1200 gc.collect()
1201 # Dead WeakMethods compare by identity
1202 refs = a, b, c, d, e, f
1203 for q in refs:
1204 for r in refs:
1205 self.assertEqual(q == r, q is r)
1206 self.assertEqual(q != r, q is not r)
1207
1208 def test_hashing(self):
1209 # Alive WeakMethods are hashable if the underlying object is
1210 # hashable.
1211 x = Object(1)
1212 y = Object(1)
1213 a = weakref.WeakMethod(x.some_method)
1214 b = weakref.WeakMethod(y.some_method)
1215 c = weakref.WeakMethod(y.other_method)
1216 # Since WeakMethod objects are equal, the hashes should be equal.
1217 self.assertEqual(hash(a), hash(b))
1218 ha = hash(a)
1219 # Dead WeakMethods retain their old hash value
1220 del x, y
1221 gc.collect()
1222 self.assertEqual(hash(a), ha)
1223 self.assertEqual(hash(b), ha)
1224 # If it wasn't hashed when alive, a dead WeakMethod cannot be hashed.
1225 self.assertRaises(TypeError, hash, c)
1226
1227
Fred Drakeb0fefc52001-03-23 04:22:45 +00001228class MappingTestCase(TestBase):
Martin v. Löwis5e163332001-02-27 18:36:56 +00001229
Fred Drakeb0fefc52001-03-23 04:22:45 +00001230 COUNT = 10
1231
Antoine Pitroubbe2f602012-03-01 16:26:35 +01001232 def check_len_cycles(self, dict_type, cons):
1233 N = 20
1234 items = [RefCycle() for i in range(N)]
1235 dct = dict_type(cons(o) for o in items)
1236 # Keep an iterator alive
1237 it = dct.items()
1238 try:
1239 next(it)
1240 except StopIteration:
1241 pass
1242 del items
1243 gc.collect()
1244 n1 = len(dct)
1245 del it
1246 gc.collect()
1247 n2 = len(dct)
1248 # one item may be kept alive inside the iterator
1249 self.assertIn(n1, (0, 1))
1250 self.assertEqual(n2, 0)
1251
1252 def test_weak_keyed_len_cycles(self):
1253 self.check_len_cycles(weakref.WeakKeyDictionary, lambda k: (k, 1))
1254
1255 def test_weak_valued_len_cycles(self):
1256 self.check_len_cycles(weakref.WeakValueDictionary, lambda k: (1, k))
1257
1258 def check_len_race(self, dict_type, cons):
1259 # Extended sanity checks for len() in the face of cyclic collection
1260 self.addCleanup(gc.set_threshold, *gc.get_threshold())
1261 for th in range(1, 100):
1262 N = 20
1263 gc.collect(0)
1264 gc.set_threshold(th, th, th)
1265 items = [RefCycle() for i in range(N)]
1266 dct = dict_type(cons(o) for o in items)
1267 del items
1268 # All items will be collected at next garbage collection pass
1269 it = dct.items()
1270 try:
1271 next(it)
1272 except StopIteration:
1273 pass
1274 n1 = len(dct)
1275 del it
1276 n2 = len(dct)
1277 self.assertGreaterEqual(n1, 0)
1278 self.assertLessEqual(n1, N)
1279 self.assertGreaterEqual(n2, 0)
1280 self.assertLessEqual(n2, n1)
1281
1282 def test_weak_keyed_len_race(self):
1283 self.check_len_race(weakref.WeakKeyDictionary, lambda k: (k, 1))
1284
1285 def test_weak_valued_len_race(self):
1286 self.check_len_race(weakref.WeakValueDictionary, lambda k: (1, k))
1287
Fred Drakeb0fefc52001-03-23 04:22:45 +00001288 def test_weak_values(self):
Fred Drake0e540c32001-05-02 05:44:22 +00001289 #
1290 # This exercises d.copy(), d.items(), d[], del d[], len(d).
1291 #
1292 dict, objects = self.make_weak_valued_dict()
Fred Drakeb0fefc52001-03-23 04:22:45 +00001293 for o in objects:
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001294 self.assertEqual(weakref.getweakrefcount(o), 1)
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +02001295 self.assertIs(o, dict[o.arg],
Fred Drakeb0fefc52001-03-23 04:22:45 +00001296 "wrong object returned by weak dict!")
Barry Warsawecaab832008-09-04 01:42:51 +00001297 items1 = list(dict.items())
1298 items2 = list(dict.copy().items())
Fred Drakeb0fefc52001-03-23 04:22:45 +00001299 items1.sort()
1300 items2.sort()
Guido van Rossumc1f779c2007-07-03 08:25:58 +00001301 self.assertEqual(items1, items2,
Fred Drakeb0fefc52001-03-23 04:22:45 +00001302 "cloning of weak-valued dictionary did not work!")
1303 del items1, items2
Guido van Rossumc1f779c2007-07-03 08:25:58 +00001304 self.assertEqual(len(dict), self.COUNT)
Fred Drakeb0fefc52001-03-23 04:22:45 +00001305 del objects[0]
Serhiy Storchaka462c1f02021-09-08 18:08:57 +03001306 gc_collect() # For PyPy or other GCs.
Guido van Rossumc1f779c2007-07-03 08:25:58 +00001307 self.assertEqual(len(dict), self.COUNT - 1,
Fred Drakeb0fefc52001-03-23 04:22:45 +00001308 "deleting object did not cause dictionary update")
1309 del objects, o
Serhiy Storchaka462c1f02021-09-08 18:08:57 +03001310 gc_collect() # For PyPy or other GCs.
Guido van Rossumc1f779c2007-07-03 08:25:58 +00001311 self.assertEqual(len(dict), 0,
Fred Drakeb0fefc52001-03-23 04:22:45 +00001312 "deleting the values did not clear the dictionary")
Fred Drake4fd06e02001-08-03 04:11:27 +00001313 # regression on SF bug #447152:
1314 dict = weakref.WeakValueDictionary()
1315 self.assertRaises(KeyError, dict.__getitem__, 1)
1316 dict[2] = C()
Serhiy Storchaka462c1f02021-09-08 18:08:57 +03001317 gc_collect() # For PyPy or other GCs.
Fred Drake4fd06e02001-08-03 04:11:27 +00001318 self.assertRaises(KeyError, dict.__getitem__, 2)
Fred Drakeb0fefc52001-03-23 04:22:45 +00001319
1320 def test_weak_keys(self):
Fred Drake0e540c32001-05-02 05:44:22 +00001321 #
1322 # This exercises d.copy(), d.items(), d[] = v, d[], del d[],
Guido van Rossume2b70bc2006-08-18 22:13:04 +00001323 # len(d), k in d.
Fred Drake0e540c32001-05-02 05:44:22 +00001324 #
1325 dict, objects = self.make_weak_keyed_dict()
Fred Drakeb0fefc52001-03-23 04:22:45 +00001326 for o in objects:
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +02001327 self.assertEqual(weakref.getweakrefcount(o), 1,
Fred Drakeb0fefc52001-03-23 04:22:45 +00001328 "wrong number of weak references to %r!" % o)
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +02001329 self.assertIs(o.arg, dict[o],
Fred Drakeb0fefc52001-03-23 04:22:45 +00001330 "wrong object returned by weak dict!")
1331 items1 = dict.items()
1332 items2 = dict.copy().items()
Guido van Rossume61fd5b2007-07-11 12:20:59 +00001333 self.assertEqual(set(items1), set(items2),
Fred Drakeb0fefc52001-03-23 04:22:45 +00001334 "cloning of weak-keyed dictionary did not work!")
1335 del items1, items2
Guido van Rossume61fd5b2007-07-11 12:20:59 +00001336 self.assertEqual(len(dict), self.COUNT)
Fred Drakeb0fefc52001-03-23 04:22:45 +00001337 del objects[0]
Serhiy Storchaka462c1f02021-09-08 18:08:57 +03001338 gc_collect() # For PyPy or other GCs.
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +02001339 self.assertEqual(len(dict), (self.COUNT - 1),
Fred Drakeb0fefc52001-03-23 04:22:45 +00001340 "deleting object did not cause dictionary update")
1341 del objects, o
Serhiy Storchaka462c1f02021-09-08 18:08:57 +03001342 gc_collect() # For PyPy or other GCs.
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +02001343 self.assertEqual(len(dict), 0,
Fred Drakeb0fefc52001-03-23 04:22:45 +00001344 "deleting the keys did not clear the dictionary")
Fred Drake752eda42001-11-06 16:38:34 +00001345 o = Object(42)
1346 dict[o] = "What is the meaning of the universe?"
Benjamin Peterson577473f2010-01-19 00:09:57 +00001347 self.assertIn(o, dict)
1348 self.assertNotIn(34, dict)
Martin v. Löwis5e163332001-02-27 18:36:56 +00001349
Fred Drake0e540c32001-05-02 05:44:22 +00001350 def test_weak_keyed_iters(self):
1351 dict, objects = self.make_weak_keyed_dict()
1352 self.check_iters(dict)
1353
Thomas Wouters477c8d52006-05-27 19:21:47 +00001354 # Test keyrefs()
1355 refs = dict.keyrefs()
1356 self.assertEqual(len(refs), len(objects))
1357 objects2 = list(objects)
1358 for wr in refs:
1359 ob = wr()
Benjamin Peterson577473f2010-01-19 00:09:57 +00001360 self.assertIn(ob, dict)
1361 self.assertIn(ob, dict)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001362 self.assertEqual(ob.arg, dict[ob])
1363 objects2.remove(ob)
1364 self.assertEqual(len(objects2), 0)
1365
1366 # Test iterkeyrefs()
1367 objects2 = list(objects)
Barry Warsawecaab832008-09-04 01:42:51 +00001368 self.assertEqual(len(list(dict.keyrefs())), len(objects))
1369 for wr in dict.keyrefs():
Thomas Wouters477c8d52006-05-27 19:21:47 +00001370 ob = wr()
Benjamin Peterson577473f2010-01-19 00:09:57 +00001371 self.assertIn(ob, dict)
1372 self.assertIn(ob, dict)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001373 self.assertEqual(ob.arg, dict[ob])
1374 objects2.remove(ob)
1375 self.assertEqual(len(objects2), 0)
1376
Fred Drake0e540c32001-05-02 05:44:22 +00001377 def test_weak_valued_iters(self):
1378 dict, objects = self.make_weak_valued_dict()
1379 self.check_iters(dict)
1380
Thomas Wouters477c8d52006-05-27 19:21:47 +00001381 # Test valuerefs()
1382 refs = dict.valuerefs()
1383 self.assertEqual(len(refs), len(objects))
1384 objects2 = list(objects)
1385 for wr in refs:
1386 ob = wr()
1387 self.assertEqual(ob, dict[ob.arg])
1388 self.assertEqual(ob.arg, dict[ob.arg].arg)
1389 objects2.remove(ob)
1390 self.assertEqual(len(objects2), 0)
1391
1392 # Test itervaluerefs()
1393 objects2 = list(objects)
1394 self.assertEqual(len(list(dict.itervaluerefs())), len(objects))
1395 for wr in dict.itervaluerefs():
1396 ob = wr()
1397 self.assertEqual(ob, dict[ob.arg])
1398 self.assertEqual(ob.arg, dict[ob.arg].arg)
1399 objects2.remove(ob)
1400 self.assertEqual(len(objects2), 0)
1401
Fred Drake0e540c32001-05-02 05:44:22 +00001402 def check_iters(self, dict):
1403 # item iterator:
Barry Warsawecaab832008-09-04 01:42:51 +00001404 items = list(dict.items())
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001405 for item in dict.items():
Fred Drake0e540c32001-05-02 05:44:22 +00001406 items.remove(item)
Barry Warsawecaab832008-09-04 01:42:51 +00001407 self.assertFalse(items, "items() did not touch all items")
Fred Drake0e540c32001-05-02 05:44:22 +00001408
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001409 # key iterator, via __iter__():
Guido van Rossum07f24362007-02-11 22:59:48 +00001410 keys = list(dict.keys())
Fred Drake0e540c32001-05-02 05:44:22 +00001411 for k in dict:
1412 keys.remove(k)
Barry Warsawecaab832008-09-04 01:42:51 +00001413 self.assertFalse(keys, "__iter__() did not touch all keys")
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001414
1415 # key iterator, via iterkeys():
Guido van Rossum07f24362007-02-11 22:59:48 +00001416 keys = list(dict.keys())
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001417 for k in dict.keys():
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001418 keys.remove(k)
Barry Warsawecaab832008-09-04 01:42:51 +00001419 self.assertFalse(keys, "iterkeys() did not touch all keys")
Fred Drake0e540c32001-05-02 05:44:22 +00001420
1421 # value iterator:
Guido van Rossum07f24362007-02-11 22:59:48 +00001422 values = list(dict.values())
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001423 for v in dict.values():
Fred Drake0e540c32001-05-02 05:44:22 +00001424 values.remove(v)
Barry Warsawecaab832008-09-04 01:42:51 +00001425 self.assertFalse(values,
Fred Drakef425b1e2003-07-14 21:37:17 +00001426 "itervalues() did not touch all values")
Fred Drake0e540c32001-05-02 05:44:22 +00001427
Antoine Pitrouc1baa602010-01-08 17:54:23 +00001428 def check_weak_destroy_while_iterating(self, dict, objects, iter_name):
1429 n = len(dict)
1430 it = iter(getattr(dict, iter_name)())
1431 next(it) # Trigger internal iteration
1432 # Destroy an object
1433 del objects[-1]
1434 gc.collect() # just in case
1435 # We have removed either the first consumed object, or another one
1436 self.assertIn(len(list(it)), [len(objects), len(objects) - 1])
1437 del it
1438 # The removal has been committed
1439 self.assertEqual(len(dict), n - 1)
1440
1441 def check_weak_destroy_and_mutate_while_iterating(self, dict, testcontext):
1442 # Check that we can explicitly mutate the weak dict without
1443 # interfering with delayed removal.
1444 # `testcontext` should create an iterator, destroy one of the
1445 # weakref'ed objects and then return a new key/value pair corresponding
1446 # to the destroyed object.
1447 with testcontext() as (k, v):
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001448 self.assertNotIn(k, dict)
Antoine Pitrouc1baa602010-01-08 17:54:23 +00001449 with testcontext() as (k, v):
1450 self.assertRaises(KeyError, dict.__delitem__, k)
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001451 self.assertNotIn(k, dict)
Antoine Pitrouc1baa602010-01-08 17:54:23 +00001452 with testcontext() as (k, v):
1453 self.assertRaises(KeyError, dict.pop, k)
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001454 self.assertNotIn(k, dict)
Antoine Pitrouc1baa602010-01-08 17:54:23 +00001455 with testcontext() as (k, v):
1456 dict[k] = v
1457 self.assertEqual(dict[k], v)
1458 ddict = copy.copy(dict)
1459 with testcontext() as (k, v):
1460 dict.update(ddict)
1461 self.assertEqual(dict, ddict)
1462 with testcontext() as (k, v):
1463 dict.clear()
1464 self.assertEqual(len(dict), 0)
1465
Antoine Pitrou1bf974d2014-10-05 20:02:28 +02001466 def check_weak_del_and_len_while_iterating(self, dict, testcontext):
1467 # Check that len() works when both iterating and removing keys
1468 # explicitly through various means (.pop(), .clear()...), while
1469 # implicit mutation is deferred because an iterator is alive.
1470 # (each call to testcontext() should schedule one item for removal
1471 # for this test to work properly)
1472 o = Object(123456)
1473 with testcontext():
1474 n = len(dict)
Victor Stinner742da042016-09-07 17:40:12 -07001475 # Since underlaying dict is ordered, first item is popped
1476 dict.pop(next(dict.keys()))
Antoine Pitrou1bf974d2014-10-05 20:02:28 +02001477 self.assertEqual(len(dict), n - 1)
1478 dict[o] = o
1479 self.assertEqual(len(dict), n)
Victor Stinner742da042016-09-07 17:40:12 -07001480 # last item in objects is removed from dict in context shutdown
Antoine Pitrou1bf974d2014-10-05 20:02:28 +02001481 with testcontext():
1482 self.assertEqual(len(dict), n - 1)
Victor Stinner742da042016-09-07 17:40:12 -07001483 # Then, (o, o) is popped
1484 dict.popitem()
Antoine Pitrou1bf974d2014-10-05 20:02:28 +02001485 self.assertEqual(len(dict), n - 2)
1486 with testcontext():
1487 self.assertEqual(len(dict), n - 3)
1488 del dict[next(dict.keys())]
1489 self.assertEqual(len(dict), n - 4)
1490 with testcontext():
1491 self.assertEqual(len(dict), n - 5)
1492 dict.popitem()
1493 self.assertEqual(len(dict), n - 6)
1494 with testcontext():
1495 dict.clear()
1496 self.assertEqual(len(dict), 0)
1497 self.assertEqual(len(dict), 0)
1498
Antoine Pitrouc1baa602010-01-08 17:54:23 +00001499 def test_weak_keys_destroy_while_iterating(self):
1500 # Issue #7105: iterators shouldn't crash when a key is implicitly removed
1501 dict, objects = self.make_weak_keyed_dict()
1502 self.check_weak_destroy_while_iterating(dict, objects, 'keys')
1503 self.check_weak_destroy_while_iterating(dict, objects, 'items')
1504 self.check_weak_destroy_while_iterating(dict, objects, 'values')
1505 self.check_weak_destroy_while_iterating(dict, objects, 'keyrefs')
1506 dict, objects = self.make_weak_keyed_dict()
1507 @contextlib.contextmanager
1508 def testcontext():
1509 try:
1510 it = iter(dict.items())
1511 next(it)
1512 # Schedule a key/value for removal and recreate it
1513 v = objects.pop().arg
1514 gc.collect() # just in case
1515 yield Object(v), v
1516 finally:
1517 it = None # should commit all removals
Benjamin Peterson18bb7022014-08-24 18:02:15 -05001518 gc.collect()
Antoine Pitrouc1baa602010-01-08 17:54:23 +00001519 self.check_weak_destroy_and_mutate_while_iterating(dict, testcontext)
Antoine Pitrou1bf974d2014-10-05 20:02:28 +02001520 # Issue #21173: len() fragile when keys are both implicitly and
1521 # explicitly removed.
1522 dict, objects = self.make_weak_keyed_dict()
1523 self.check_weak_del_and_len_while_iterating(dict, testcontext)
Antoine Pitrouc1baa602010-01-08 17:54:23 +00001524
1525 def test_weak_values_destroy_while_iterating(self):
1526 # Issue #7105: iterators shouldn't crash when a key is implicitly removed
1527 dict, objects = self.make_weak_valued_dict()
1528 self.check_weak_destroy_while_iterating(dict, objects, 'keys')
1529 self.check_weak_destroy_while_iterating(dict, objects, 'items')
1530 self.check_weak_destroy_while_iterating(dict, objects, 'values')
1531 self.check_weak_destroy_while_iterating(dict, objects, 'itervaluerefs')
1532 self.check_weak_destroy_while_iterating(dict, objects, 'valuerefs')
1533 dict, objects = self.make_weak_valued_dict()
1534 @contextlib.contextmanager
1535 def testcontext():
1536 try:
1537 it = iter(dict.items())
1538 next(it)
1539 # Schedule a key/value for removal and recreate it
1540 k = objects.pop().arg
1541 gc.collect() # just in case
1542 yield k, Object(k)
1543 finally:
1544 it = None # should commit all removals
Benjamin Peterson18bb7022014-08-24 18:02:15 -05001545 gc.collect()
Antoine Pitrouc1baa602010-01-08 17:54:23 +00001546 self.check_weak_destroy_and_mutate_while_iterating(dict, testcontext)
Antoine Pitrou1bf974d2014-10-05 20:02:28 +02001547 dict, objects = self.make_weak_valued_dict()
1548 self.check_weak_del_and_len_while_iterating(dict, testcontext)
Antoine Pitrouc1baa602010-01-08 17:54:23 +00001549
Guido van Rossum009afb72002-06-10 20:00:52 +00001550 def test_make_weak_keyed_dict_from_dict(self):
1551 o = Object(3)
1552 dict = weakref.WeakKeyDictionary({o:364})
Guido van Rossume61fd5b2007-07-11 12:20:59 +00001553 self.assertEqual(dict[o], 364)
Guido van Rossum009afb72002-06-10 20:00:52 +00001554
1555 def test_make_weak_keyed_dict_from_weak_keyed_dict(self):
1556 o = Object(3)
1557 dict = weakref.WeakKeyDictionary({o:364})
1558 dict2 = weakref.WeakKeyDictionary(dict)
Guido van Rossume61fd5b2007-07-11 12:20:59 +00001559 self.assertEqual(dict[o], 364)
Guido van Rossum009afb72002-06-10 20:00:52 +00001560
Fred Drake0e540c32001-05-02 05:44:22 +00001561 def make_weak_keyed_dict(self):
1562 dict = weakref.WeakKeyDictionary()
Guido van Rossumc1f779c2007-07-03 08:25:58 +00001563 objects = list(map(Object, range(self.COUNT)))
Fred Drake0e540c32001-05-02 05:44:22 +00001564 for o in objects:
1565 dict[o] = o.arg
1566 return dict, objects
1567
Antoine Pitrouc06de472009-05-30 21:04:26 +00001568 def test_make_weak_valued_dict_from_dict(self):
1569 o = Object(3)
1570 dict = weakref.WeakValueDictionary({364:o})
1571 self.assertEqual(dict[364], o)
1572
1573 def test_make_weak_valued_dict_from_weak_valued_dict(self):
1574 o = Object(3)
1575 dict = weakref.WeakValueDictionary({364:o})
1576 dict2 = weakref.WeakValueDictionary(dict)
1577 self.assertEqual(dict[364], o)
1578
Serhiy Storchakab5102e32015-09-29 23:52:09 +03001579 def test_make_weak_valued_dict_misc(self):
1580 # errors
1581 self.assertRaises(TypeError, weakref.WeakValueDictionary.__init__)
1582 self.assertRaises(TypeError, weakref.WeakValueDictionary, {}, {})
1583 self.assertRaises(TypeError, weakref.WeakValueDictionary, (), ())
1584 # special keyword arguments
1585 o = Object(3)
1586 for kw in 'self', 'dict', 'other', 'iterable':
1587 d = weakref.WeakValueDictionary(**{kw: o})
1588 self.assertEqual(list(d.keys()), [kw])
1589 self.assertEqual(d[kw], o)
1590
Fred Drake0e540c32001-05-02 05:44:22 +00001591 def make_weak_valued_dict(self):
1592 dict = weakref.WeakValueDictionary()
Guido van Rossumc1f779c2007-07-03 08:25:58 +00001593 objects = list(map(Object, range(self.COUNT)))
Fred Drake0e540c32001-05-02 05:44:22 +00001594 for o in objects:
1595 dict[o.arg] = o
1596 return dict, objects
1597
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001598 def check_popitem(self, klass, key1, value1, key2, value2):
1599 weakdict = klass()
1600 weakdict[key1] = value1
1601 weakdict[key2] = value2
Guido van Rossume61fd5b2007-07-11 12:20:59 +00001602 self.assertEqual(len(weakdict), 2)
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001603 k, v = weakdict.popitem()
Guido van Rossume61fd5b2007-07-11 12:20:59 +00001604 self.assertEqual(len(weakdict), 1)
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001605 if k is key1:
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +02001606 self.assertIs(v, value1)
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001607 else:
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +02001608 self.assertIs(v, value2)
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001609 k, v = weakdict.popitem()
Guido van Rossume61fd5b2007-07-11 12:20:59 +00001610 self.assertEqual(len(weakdict), 0)
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001611 if k is key1:
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +02001612 self.assertIs(v, value1)
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001613 else:
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +02001614 self.assertIs(v, value2)
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001615
1616 def test_weak_valued_dict_popitem(self):
1617 self.check_popitem(weakref.WeakValueDictionary,
1618 "key1", C(), "key2", C())
1619
1620 def test_weak_keyed_dict_popitem(self):
1621 self.check_popitem(weakref.WeakKeyDictionary,
1622 C(), "value 1", C(), "value 2")
1623
1624 def check_setdefault(self, klass, key, value1, value2):
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +02001625 self.assertIsNot(value1, value2,
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001626 "invalid test"
1627 " -- value parameters must be distinct objects")
1628 weakdict = klass()
1629 o = weakdict.setdefault(key, value1)
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +02001630 self.assertIs(o, value1)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001631 self.assertIn(key, weakdict)
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +02001632 self.assertIs(weakdict.get(key), value1)
1633 self.assertIs(weakdict[key], value1)
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001634
1635 o = weakdict.setdefault(key, value2)
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +02001636 self.assertIs(o, value1)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001637 self.assertIn(key, weakdict)
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +02001638 self.assertIs(weakdict.get(key), value1)
1639 self.assertIs(weakdict[key], value1)
Fred Drakeaaa48ff2001-05-10 17:16:38 +00001640
1641 def test_weak_valued_dict_setdefault(self):
1642 self.check_setdefault(weakref.WeakValueDictionary,
1643 "key", C(), C())
1644
1645 def test_weak_keyed_dict_setdefault(self):
1646 self.check_setdefault(weakref.WeakKeyDictionary,
1647 C(), "value 1", "value 2")
1648
Fred Drakea0a4ab12001-04-16 17:37:27 +00001649 def check_update(self, klass, dict):
Fred Drake0e540c32001-05-02 05:44:22 +00001650 #
Guido van Rossume2b70bc2006-08-18 22:13:04 +00001651 # This exercises d.update(), len(d), d.keys(), k in d,
Fred Drake0e540c32001-05-02 05:44:22 +00001652 # d.get(), d[].
1653 #
Fred Drakea0a4ab12001-04-16 17:37:27 +00001654 weakdict = klass()
1655 weakdict.update(dict)
Guido van Rossume61fd5b2007-07-11 12:20:59 +00001656 self.assertEqual(len(weakdict), len(dict))
Fred Drakea0a4ab12001-04-16 17:37:27 +00001657 for k in weakdict.keys():
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001658 self.assertIn(k, dict, "mysterious new key appeared in weak dict")
Fred Drakea0a4ab12001-04-16 17:37:27 +00001659 v = dict.get(k)
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +02001660 self.assertIs(v, weakdict[k])
1661 self.assertIs(v, weakdict.get(k))
Fred Drakea0a4ab12001-04-16 17:37:27 +00001662 for k in dict.keys():
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001663 self.assertIn(k, weakdict, "original key disappeared in weak dict")
Fred Drakea0a4ab12001-04-16 17:37:27 +00001664 v = dict[k]
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +02001665 self.assertIs(v, weakdict[k])
1666 self.assertIs(v, weakdict.get(k))
Fred Drakea0a4ab12001-04-16 17:37:27 +00001667
1668 def test_weak_valued_dict_update(self):
1669 self.check_update(weakref.WeakValueDictionary,
1670 {1: C(), 'a': C(), C(): C()})
Serhiy Storchakab5102e32015-09-29 23:52:09 +03001671 # errors
1672 self.assertRaises(TypeError, weakref.WeakValueDictionary.update)
1673 d = weakref.WeakValueDictionary()
1674 self.assertRaises(TypeError, d.update, {}, {})
1675 self.assertRaises(TypeError, d.update, (), ())
1676 self.assertEqual(list(d.keys()), [])
1677 # special keyword arguments
1678 o = Object(3)
1679 for kw in 'self', 'dict', 'other', 'iterable':
1680 d = weakref.WeakValueDictionary()
1681 d.update(**{kw: o})
1682 self.assertEqual(list(d.keys()), [kw])
1683 self.assertEqual(d[kw], o)
Fred Drakea0a4ab12001-04-16 17:37:27 +00001684
Curtis Bucher8f1ed212020-03-24 18:51:29 -07001685 def test_weak_valued_union_operators(self):
1686 a = C()
1687 b = C()
1688 c = C()
1689 wvd1 = weakref.WeakValueDictionary({1: a})
1690 wvd2 = weakref.WeakValueDictionary({1: b, 2: a})
1691 wvd3 = wvd1.copy()
1692 d1 = {1: c, 3: b}
1693 pairs = [(5, c), (6, b)]
1694
1695 tmp1 = wvd1 | wvd2 # Between two WeakValueDictionaries
1696 self.assertEqual(dict(tmp1), dict(wvd1) | dict(wvd2))
1697 self.assertIs(type(tmp1), weakref.WeakValueDictionary)
1698 wvd1 |= wvd2
1699 self.assertEqual(wvd1, tmp1)
1700
1701 tmp2 = wvd2 | d1 # Between WeakValueDictionary and mapping
1702 self.assertEqual(dict(tmp2), dict(wvd2) | d1)
1703 self.assertIs(type(tmp2), weakref.WeakValueDictionary)
1704 wvd2 |= d1
1705 self.assertEqual(wvd2, tmp2)
1706
1707 tmp3 = wvd3.copy() # Between WeakValueDictionary and iterable key, value
1708 tmp3 |= pairs
1709 self.assertEqual(dict(tmp3), dict(wvd3) | dict(pairs))
1710 self.assertIs(type(tmp3), weakref.WeakValueDictionary)
1711
1712 tmp4 = d1 | wvd3 # Testing .__ror__
1713 self.assertEqual(dict(tmp4), d1 | dict(wvd3))
1714 self.assertIs(type(tmp4), weakref.WeakValueDictionary)
1715
1716 del a
1717 self.assertNotIn(2, tmp1)
1718 self.assertNotIn(2, tmp2)
1719 self.assertNotIn(1, tmp3)
1720 self.assertNotIn(1, tmp4)
1721
Fred Drakea0a4ab12001-04-16 17:37:27 +00001722 def test_weak_keyed_dict_update(self):
1723 self.check_update(weakref.WeakKeyDictionary,
1724 {C(): 1, C(): 2, C(): 3})
1725
Fred Drakeccc75622001-09-06 14:52:39 +00001726 def test_weak_keyed_delitem(self):
1727 d = weakref.WeakKeyDictionary()
1728 o1 = Object('1')
1729 o2 = Object('2')
1730 d[o1] = 'something'
1731 d[o2] = 'something'
Guido van Rossume61fd5b2007-07-11 12:20:59 +00001732 self.assertEqual(len(d), 2)
Fred Drakeccc75622001-09-06 14:52:39 +00001733 del d[o1]
Guido van Rossume61fd5b2007-07-11 12:20:59 +00001734 self.assertEqual(len(d), 1)
Barry Warsawecaab832008-09-04 01:42:51 +00001735 self.assertEqual(list(d.keys()), [o2])
Fred Drakeccc75622001-09-06 14:52:39 +00001736
Curtis Bucher25e580a2020-03-23 13:49:46 -07001737 def test_weak_keyed_union_operators(self):
1738 o1 = C()
1739 o2 = C()
1740 o3 = C()
1741 wkd1 = weakref.WeakKeyDictionary({o1: 1, o2: 2})
1742 wkd2 = weakref.WeakKeyDictionary({o3: 3, o1: 4})
1743 wkd3 = wkd1.copy()
1744 d1 = {o2: '5', o3: '6'}
1745 pairs = [(o2, 7), (o3, 8)]
1746
1747 tmp1 = wkd1 | wkd2 # Between two WeakKeyDictionaries
1748 self.assertEqual(dict(tmp1), dict(wkd1) | dict(wkd2))
1749 self.assertIs(type(tmp1), weakref.WeakKeyDictionary)
1750 wkd1 |= wkd2
1751 self.assertEqual(wkd1, tmp1)
1752
1753 tmp2 = wkd2 | d1 # Between WeakKeyDictionary and mapping
1754 self.assertEqual(dict(tmp2), dict(wkd2) | d1)
1755 self.assertIs(type(tmp2), weakref.WeakKeyDictionary)
1756 wkd2 |= d1
1757 self.assertEqual(wkd2, tmp2)
1758
1759 tmp3 = wkd3.copy() # Between WeakKeyDictionary and iterable key, value
1760 tmp3 |= pairs
1761 self.assertEqual(dict(tmp3), dict(wkd3) | dict(pairs))
1762 self.assertIs(type(tmp3), weakref.WeakKeyDictionary)
1763
1764 tmp4 = d1 | wkd3 # Testing .__ror__
1765 self.assertEqual(dict(tmp4), d1 | dict(wkd3))
1766 self.assertIs(type(tmp4), weakref.WeakKeyDictionary)
1767
1768 del o1
1769 self.assertNotIn(4, tmp1.values())
1770 self.assertNotIn(4, tmp2.values())
1771 self.assertNotIn(1, tmp3.values())
1772 self.assertNotIn(1, tmp4.values())
1773
Fred Drakeccc75622001-09-06 14:52:39 +00001774 def test_weak_valued_delitem(self):
1775 d = weakref.WeakValueDictionary()
1776 o1 = Object('1')
1777 o2 = Object('2')
1778 d['something'] = o1
1779 d['something else'] = o2
Guido van Rossume61fd5b2007-07-11 12:20:59 +00001780 self.assertEqual(len(d), 2)
Fred Drakeccc75622001-09-06 14:52:39 +00001781 del d['something']
Guido van Rossume61fd5b2007-07-11 12:20:59 +00001782 self.assertEqual(len(d), 1)
Serhiy Storchaka2e29c9e2013-11-17 13:20:09 +02001783 self.assertEqual(list(d.items()), [('something else', o2)])
Fred Drakeccc75622001-09-06 14:52:39 +00001784
Tim Peters886128f2003-05-25 01:45:11 +00001785 def test_weak_keyed_bad_delitem(self):
1786 d = weakref.WeakKeyDictionary()
1787 o = Object('1')
1788 # An attempt to delete an object that isn't there should raise
Tim Peters50d8b8b2003-05-25 17:44:31 +00001789 # KeyError. It didn't before 2.3.
Tim Peters886128f2003-05-25 01:45:11 +00001790 self.assertRaises(KeyError, d.__delitem__, o)
Tim Peters50d8b8b2003-05-25 17:44:31 +00001791 self.assertRaises(KeyError, d.__getitem__, o)
1792
1793 # If a key isn't of a weakly referencable type, __getitem__ and
1794 # __setitem__ raise TypeError. __delitem__ should too.
1795 self.assertRaises(TypeError, d.__delitem__, 13)
1796 self.assertRaises(TypeError, d.__getitem__, 13)
1797 self.assertRaises(TypeError, d.__setitem__, 13, 13)
Tim Peters886128f2003-05-25 01:45:11 +00001798
1799 def test_weak_keyed_cascading_deletes(self):
1800 # SF bug 742860. For some reason, before 2.3 __delitem__ iterated
1801 # over the keys via self.data.iterkeys(). If things vanished from
1802 # the dict during this (or got added), that caused a RuntimeError.
1803
1804 d = weakref.WeakKeyDictionary()
1805 mutate = False
1806
1807 class C(object):
1808 def __init__(self, i):
1809 self.value = i
1810 def __hash__(self):
1811 return hash(self.value)
1812 def __eq__(self, other):
1813 if mutate:
1814 # Side effect that mutates the dict, by removing the
1815 # last strong reference to a key.
1816 del objs[-1]
1817 return self.value == other.value
1818
1819 objs = [C(i) for i in range(4)]
1820 for o in objs:
1821 d[o] = o.value
1822 del o # now the only strong references to keys are in objs
1823 # Find the order in which iterkeys sees the keys.
Barry Warsawecaab832008-09-04 01:42:51 +00001824 objs = list(d.keys())
Tim Peters886128f2003-05-25 01:45:11 +00001825 # Reverse it, so that the iteration implementation of __delitem__
1826 # has to keep looping to find the first object we delete.
1827 objs.reverse()
Tim Peters50d8b8b2003-05-25 17:44:31 +00001828
Leo Ariasc3d95082018-02-03 18:36:10 -06001829 # Turn on mutation in C.__eq__. The first time through the loop,
Tim Peters886128f2003-05-25 01:45:11 +00001830 # under the iterkeys() business the first comparison will delete
1831 # the last item iterkeys() would see, and that causes a
1832 # RuntimeError: dictionary changed size during iteration
1833 # when the iterkeys() loop goes around to try comparing the next
Tim Peters50d8b8b2003-05-25 17:44:31 +00001834 # key. After this was fixed, it just deletes the last object *our*
Tim Peters886128f2003-05-25 01:45:11 +00001835 # "for o in obj" loop would have gotten to.
1836 mutate = True
1837 count = 0
1838 for o in objs:
1839 count += 1
1840 del d[o]
Serhiy Storchaka462c1f02021-09-08 18:08:57 +03001841 gc_collect() # For PyPy or other GCs.
Tim Peters886128f2003-05-25 01:45:11 +00001842 self.assertEqual(len(d), 0)
1843 self.assertEqual(count, 2)
1844
Serhiy Storchaka0c937b32014-07-22 12:14:52 +03001845 def test_make_weak_valued_dict_repr(self):
1846 dict = weakref.WeakValueDictionary()
1847 self.assertRegex(repr(dict), '<WeakValueDictionary at 0x.*>')
1848
1849 def test_make_weak_keyed_dict_repr(self):
1850 dict = weakref.WeakKeyDictionary()
1851 self.assertRegex(repr(dict), '<WeakKeyDictionary at 0x.*>')
1852
Antoine Pitrouc1ee4882016-12-19 10:56:40 +01001853 def test_threaded_weak_valued_setdefault(self):
1854 d = weakref.WeakValueDictionary()
1855 with collect_in_thread():
1856 for i in range(100000):
1857 x = d.setdefault(10, RefCycle())
1858 self.assertIsNot(x, None) # we never put None in there!
1859 del x
1860
1861 def test_threaded_weak_valued_pop(self):
1862 d = weakref.WeakValueDictionary()
1863 with collect_in_thread():
1864 for i in range(100000):
1865 d[10] = RefCycle()
1866 x = d.pop(10, 10)
1867 self.assertIsNot(x, None) # we never put None in there!
1868
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001869 def test_threaded_weak_valued_consistency(self):
1870 # Issue #28427: old keys should not remove new values from
1871 # WeakValueDictionary when collecting from another thread.
1872 d = weakref.WeakValueDictionary()
1873 with collect_in_thread():
1874 for i in range(200000):
1875 o = RefCycle()
1876 d[10] = o
1877 # o is still alive, so the dict can't be empty
1878 self.assertEqual(len(d), 1)
1879 o = None # lose ref
1880
Fish96d37db2019-02-07 14:51:59 -05001881 def check_threaded_weak_dict_copy(self, type_, deepcopy):
1882 # `type_` should be either WeakKeyDictionary or WeakValueDictionary.
1883 # `deepcopy` should be either True or False.
1884 exc = []
1885
1886 class DummyKey:
1887 def __init__(self, ctr):
1888 self.ctr = ctr
1889
1890 class DummyValue:
1891 def __init__(self, ctr):
1892 self.ctr = ctr
1893
1894 def dict_copy(d, exc):
1895 try:
1896 if deepcopy is True:
1897 _ = copy.deepcopy(d)
1898 else:
1899 _ = d.copy()
1900 except Exception as ex:
1901 exc.append(ex)
1902
1903 def pop_and_collect(lst):
1904 gc_ctr = 0
1905 while lst:
1906 i = random.randint(0, len(lst) - 1)
1907 gc_ctr += 1
1908 lst.pop(i)
1909 if gc_ctr % 10000 == 0:
1910 gc.collect() # just in case
1911
1912 self.assertIn(type_, (weakref.WeakKeyDictionary, weakref.WeakValueDictionary))
1913
1914 d = type_()
1915 keys = []
1916 values = []
1917 # Initialize d with many entries
1918 for i in range(70000):
1919 k, v = DummyKey(i), DummyValue(i)
1920 keys.append(k)
1921 values.append(v)
1922 d[k] = v
1923 del k
1924 del v
1925
1926 t_copy = threading.Thread(target=dict_copy, args=(d, exc,))
1927 if type_ is weakref.WeakKeyDictionary:
1928 t_collect = threading.Thread(target=pop_and_collect, args=(keys,))
1929 else: # weakref.WeakValueDictionary
1930 t_collect = threading.Thread(target=pop_and_collect, args=(values,))
1931
1932 t_copy.start()
1933 t_collect.start()
1934
1935 t_copy.join()
1936 t_collect.join()
1937
1938 # Test exceptions
1939 if exc:
1940 raise exc[0]
1941
1942 def test_threaded_weak_key_dict_copy(self):
1943 # Issue #35615: Weakref keys or values getting GC'ed during dict
1944 # copying should not result in a crash.
1945 self.check_threaded_weak_dict_copy(weakref.WeakKeyDictionary, False)
1946
1947 def test_threaded_weak_key_dict_deepcopy(self):
1948 # Issue #35615: Weakref keys or values getting GC'ed during dict
1949 # copying should not result in a crash.
1950 self.check_threaded_weak_dict_copy(weakref.WeakKeyDictionary, True)
1951
1952 def test_threaded_weak_value_dict_copy(self):
1953 # Issue #35615: Weakref keys or values getting GC'ed during dict
1954 # copying should not result in a crash.
1955 self.check_threaded_weak_dict_copy(weakref.WeakValueDictionary, False)
1956
1957 def test_threaded_weak_value_dict_deepcopy(self):
1958 # Issue #35615: Weakref keys or values getting GC'ed during dict
1959 # copying should not result in a crash.
1960 self.check_threaded_weak_dict_copy(weakref.WeakValueDictionary, True)
1961
Victor Stinnera2af05a2019-09-09 16:55:58 +02001962 @support.cpython_only
1963 def test_remove_closure(self):
1964 d = weakref.WeakValueDictionary()
1965 self.assertIsNone(d._remove.__closure__)
1966
Antoine Pitrouc1ee4882016-12-19 10:56:40 +01001967
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +00001968from test import mapping_tests
Raymond Hettinger2c2d3222003-03-09 07:05:43 +00001969
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +00001970class WeakValueDictionaryTestCase(mapping_tests.BasicTestMappingProtocol):
Fred Drakef425b1e2003-07-14 21:37:17 +00001971 """Check that WeakValueDictionary conforms to the mapping protocol"""
Raymond Hettinger2c2d3222003-03-09 07:05:43 +00001972 __ref = {"key1":Object(1), "key2":Object(2), "key3":Object(3)}
Walter Dörwald118f9312004-06-02 18:42:25 +00001973 type2test = weakref.WeakValueDictionary
Raymond Hettinger2c2d3222003-03-09 07:05:43 +00001974 def _reference(self):
1975 return self.__ref.copy()
1976
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +00001977class WeakKeyDictionaryTestCase(mapping_tests.BasicTestMappingProtocol):
Fred Drakef425b1e2003-07-14 21:37:17 +00001978 """Check that WeakKeyDictionary conforms to the mapping protocol"""
Raymond Hettinger2c2d3222003-03-09 07:05:43 +00001979 __ref = {Object("key1"):1, Object("key2"):2, Object("key3"):3}
Walter Dörwald118f9312004-06-02 18:42:25 +00001980 type2test = weakref.WeakKeyDictionary
Raymond Hettinger2c2d3222003-03-09 07:05:43 +00001981 def _reference(self):
1982 return self.__ref.copy()
Martin v. Löwis5e163332001-02-27 18:36:56 +00001983
Richard Oudkerk7a3dae02013-05-05 23:05:00 +01001984
1985class FinalizeTestCase(unittest.TestCase):
1986
1987 class A:
1988 pass
1989
1990 def _collect_if_necessary(self):
1991 # we create no ref-cycles so in CPython no gc should be needed
1992 if sys.implementation.name != 'cpython':
1993 support.gc_collect()
1994
1995 def test_finalize(self):
1996 def add(x,y,z):
1997 res.append(x + y + z)
1998 return x + y + z
1999
2000 a = self.A()
2001
2002 res = []
2003 f = weakref.finalize(a, add, 67, 43, z=89)
2004 self.assertEqual(f.alive, True)
2005 self.assertEqual(f.peek(), (a, add, (67,43), {'z':89}))
2006 self.assertEqual(f(), 199)
2007 self.assertEqual(f(), None)
2008 self.assertEqual(f(), None)
2009 self.assertEqual(f.peek(), None)
2010 self.assertEqual(f.detach(), None)
2011 self.assertEqual(f.alive, False)
2012 self.assertEqual(res, [199])
2013
2014 res = []
2015 f = weakref.finalize(a, add, 67, 43, 89)
2016 self.assertEqual(f.peek(), (a, add, (67,43,89), {}))
2017 self.assertEqual(f.detach(), (a, add, (67,43,89), {}))
2018 self.assertEqual(f(), None)
2019 self.assertEqual(f(), None)
2020 self.assertEqual(f.peek(), None)
2021 self.assertEqual(f.detach(), None)
2022 self.assertEqual(f.alive, False)
2023 self.assertEqual(res, [])
2024
2025 res = []
2026 f = weakref.finalize(a, add, x=67, y=43, z=89)
2027 del a
2028 self._collect_if_necessary()
2029 self.assertEqual(f(), None)
2030 self.assertEqual(f(), None)
2031 self.assertEqual(f.peek(), None)
2032 self.assertEqual(f.detach(), None)
2033 self.assertEqual(f.alive, False)
2034 self.assertEqual(res, [199])
2035
Serhiy Storchaka42a139e2019-04-01 09:16:35 +03002036 def test_arg_errors(self):
2037 def fin(*args, **kwargs):
2038 res.append((args, kwargs))
2039
2040 a = self.A()
2041
2042 res = []
2043 f = weakref.finalize(a, fin, 1, 2, func=3, obj=4)
2044 self.assertEqual(f.peek(), (a, fin, (1, 2), {'func': 3, 'obj': 4}))
2045 f()
2046 self.assertEqual(res, [((1, 2), {'func': 3, 'obj': 4})])
2047
Serhiy Storchaka142566c2019-06-05 18:22:31 +03002048 with self.assertRaises(TypeError):
2049 weakref.finalize(a, func=fin, arg=1)
2050 with self.assertRaises(TypeError):
2051 weakref.finalize(obj=a, func=fin, arg=1)
Serhiy Storchaka42a139e2019-04-01 09:16:35 +03002052 self.assertRaises(TypeError, weakref.finalize, a)
2053 self.assertRaises(TypeError, weakref.finalize)
2054
Richard Oudkerk7a3dae02013-05-05 23:05:00 +01002055 def test_order(self):
2056 a = self.A()
2057 res = []
2058
2059 f1 = weakref.finalize(a, res.append, 'f1')
2060 f2 = weakref.finalize(a, res.append, 'f2')
2061 f3 = weakref.finalize(a, res.append, 'f3')
2062 f4 = weakref.finalize(a, res.append, 'f4')
2063 f5 = weakref.finalize(a, res.append, 'f5')
2064
2065 # make sure finalizers can keep themselves alive
2066 del f1, f4
2067
2068 self.assertTrue(f2.alive)
2069 self.assertTrue(f3.alive)
2070 self.assertTrue(f5.alive)
2071
2072 self.assertTrue(f5.detach())
2073 self.assertFalse(f5.alive)
2074
2075 f5() # nothing because previously unregistered
2076 res.append('A')
2077 f3() # => res.append('f3')
2078 self.assertFalse(f3.alive)
2079 res.append('B')
2080 f3() # nothing because previously called
2081 res.append('C')
2082 del a
2083 self._collect_if_necessary()
2084 # => res.append('f4')
2085 # => res.append('f2')
2086 # => res.append('f1')
2087 self.assertFalse(f2.alive)
2088 res.append('D')
2089 f2() # nothing because previously called by gc
2090
2091 expected = ['A', 'f3', 'B', 'C', 'f4', 'f2', 'f1', 'D']
2092 self.assertEqual(res, expected)
2093
2094 def test_all_freed(self):
2095 # we want a weakrefable subclass of weakref.finalize
2096 class MyFinalizer(weakref.finalize):
2097 pass
2098
2099 a = self.A()
2100 res = []
2101 def callback():
2102 res.append(123)
2103 f = MyFinalizer(a, callback)
2104
2105 wr_callback = weakref.ref(callback)
2106 wr_f = weakref.ref(f)
2107 del callback, f
2108
2109 self.assertIsNotNone(wr_callback())
2110 self.assertIsNotNone(wr_f())
2111
2112 del a
2113 self._collect_if_necessary()
2114
2115 self.assertIsNone(wr_callback())
2116 self.assertIsNone(wr_f())
2117 self.assertEqual(res, [123])
2118
2119 @classmethod
2120 def run_in_child(cls):
2121 def error():
2122 # Create an atexit finalizer from inside a finalizer called
2123 # at exit. This should be the next to be run.
2124 g1 = weakref.finalize(cls, print, 'g1')
2125 print('f3 error')
2126 1/0
2127
2128 # cls should stay alive till atexit callbacks run
2129 f1 = weakref.finalize(cls, print, 'f1', _global_var)
2130 f2 = weakref.finalize(cls, print, 'f2', _global_var)
2131 f3 = weakref.finalize(cls, error)
2132 f4 = weakref.finalize(cls, print, 'f4', _global_var)
2133
2134 assert f1.atexit == True
2135 f2.atexit = False
2136 assert f3.atexit == True
2137 assert f4.atexit == True
2138
2139 def test_atexit(self):
2140 prog = ('from test.test_weakref import FinalizeTestCase;'+
2141 'FinalizeTestCase.run_in_child()')
2142 rc, out, err = script_helper.assert_python_ok('-c', prog)
2143 out = out.decode('ascii').splitlines()
2144 self.assertEqual(out, ['f4 foobar', 'f3 error', 'g1', 'f1 foobar'])
2145 self.assertTrue(b'ZeroDivisionError' in err)
2146
2147
Georg Brandlb533e262008-05-25 18:19:30 +00002148libreftest = """ Doctest for examples in the library reference: weakref.rst
Georg Brandl9a65d582005-07-02 19:07:30 +00002149
Serhiy Storchaka462c1f02021-09-08 18:08:57 +03002150>>> from test.support import gc_collect
Georg Brandl9a65d582005-07-02 19:07:30 +00002151>>> import weakref
2152>>> class Dict(dict):
2153... pass
2154...
2155>>> obj = Dict(red=1, green=2, blue=3) # this object is weak referencable
2156>>> r = weakref.ref(obj)
Guido van Rossum7131f842007-02-09 20:13:25 +00002157>>> print(r() is obj)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002158True
Georg Brandl9a65d582005-07-02 19:07:30 +00002159
2160>>> import weakref
2161>>> class Object:
2162... pass
2163...
2164>>> o = Object()
2165>>> r = weakref.ref(o)
2166>>> o2 = r()
2167>>> o is o2
2168True
2169>>> del o, o2
Serhiy Storchaka462c1f02021-09-08 18:08:57 +03002170>>> gc_collect() # For PyPy or other GCs.
Guido van Rossum7131f842007-02-09 20:13:25 +00002171>>> print(r())
Georg Brandl9a65d582005-07-02 19:07:30 +00002172None
2173
2174>>> import weakref
2175>>> class ExtendedRef(weakref.ref):
2176... def __init__(self, ob, callback=None, **annotations):
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002177... super().__init__(ob, callback)
Georg Brandl9a65d582005-07-02 19:07:30 +00002178... self.__counter = 0
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002179... for k, v in annotations.items():
Georg Brandl9a65d582005-07-02 19:07:30 +00002180... setattr(self, k, v)
2181... def __call__(self):
2182... '''Return a pair containing the referent and the number of
2183... times the reference has been called.
2184... '''
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002185... ob = super().__call__()
Georg Brandl9a65d582005-07-02 19:07:30 +00002186... if ob is not None:
2187... self.__counter += 1
2188... ob = (ob, self.__counter)
2189... return ob
Guido van Rossumd8faa362007-04-27 19:54:29 +00002190...
Georg Brandl9a65d582005-07-02 19:07:30 +00002191>>> class A: # not in docs from here, just testing the ExtendedRef
2192... pass
2193...
2194>>> a = A()
2195>>> r = ExtendedRef(a, foo=1, bar="baz")
2196>>> r.foo
21971
2198>>> r.bar
2199'baz'
2200>>> r()[1]
22011
2202>>> r()[1]
22032
2204>>> r()[0] is a
2205True
2206
2207
2208>>> import weakref
2209>>> _id2obj_dict = weakref.WeakValueDictionary()
2210>>> def remember(obj):
2211... oid = id(obj)
2212... _id2obj_dict[oid] = obj
2213... return oid
2214...
2215>>> def id2obj(oid):
2216... return _id2obj_dict[oid]
2217...
2218>>> a = A() # from here, just testing
2219>>> a_id = remember(a)
2220>>> id2obj(a_id) is a
2221True
2222>>> del a
Serhiy Storchaka462c1f02021-09-08 18:08:57 +03002223>>> gc_collect() # For PyPy or other GCs.
Georg Brandl9a65d582005-07-02 19:07:30 +00002224>>> try:
2225... id2obj(a_id)
2226... except KeyError:
Guido van Rossum7131f842007-02-09 20:13:25 +00002227... print('OK')
Georg Brandl9a65d582005-07-02 19:07:30 +00002228... else:
Guido van Rossum7131f842007-02-09 20:13:25 +00002229... print('WeakValueDictionary error')
Georg Brandl9a65d582005-07-02 19:07:30 +00002230OK
2231
2232"""
2233
2234__test__ = {'libreftest' : libreftest}
2235
Fred Drake2e2be372001-09-20 21:33:42 +00002236def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002237 support.run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +00002238 ReferencesTestCase,
Antoine Pitrouc3afba12012-11-17 18:57:38 +01002239 WeakMethodTestCase,
Walter Dörwald21d3a322003-05-01 17:45:56 +00002240 MappingTestCase,
2241 WeakValueDictionaryTestCase,
Fred Drakef425b1e2003-07-14 21:37:17 +00002242 WeakKeyDictionaryTestCase,
Amaury Forgeot d'Arcc856c7a2008-06-16 19:50:09 +00002243 SubclassableWeakrefTestCase,
Richard Oudkerk7a3dae02013-05-05 23:05:00 +01002244 FinalizeTestCase,
Fred Drakef425b1e2003-07-14 21:37:17 +00002245 )
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002246 support.run_doctest(sys.modules[__name__])
Fred Drake2e2be372001-09-20 21:33:42 +00002247
2248
2249if __name__ == "__main__":
2250 test_main()