diff options
author | Fred Drake <fdrake@acm.org> | 2004-02-04 23:14:14 (GMT) |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 2004-02-04 23:14:14 (GMT) |
commit | bc875f5a36b744c1a7263c35befaf31208a416b1 (patch) | |
tree | 9b362880eeb66497bd813f43f2806eef2d2bb7f0 /Lib/test/test_weakref.py | |
parent | 21ae4f983e3625cc16807c68952c6cd9fe44bb8b (diff) | |
download | cpython-bc875f5a36b744c1a7263c35befaf31208a416b1.zip cpython-bc875f5a36b744c1a7263c35befaf31208a416b1.tar.gz cpython-bc875f5a36b744c1a7263c35befaf31208a416b1.tar.bz2 |
Allocating a new weakref object can cause existing weakref objects for
the same object to be collected by the cyclic GC support if they are
only referenced by a cycle. If the weakref being collected was one of
the weakrefs without callbacks, some local variables for the
constructor became invalid and have to be re-computed.
The test caused a segfault under a debug build without the fix applied.
Diffstat (limited to 'Lib/test/test_weakref.py')
-rw-r--r-- | Lib/test/test_weakref.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py index f732d7b..00cd7ae 100644 --- a/Lib/test/test_weakref.py +++ b/Lib/test/test_weakref.py @@ -1,3 +1,4 @@ +import gc import sys import unittest import UserList @@ -591,6 +592,37 @@ class ReferencesTestCase(TestBase): gc.collect() self.assertEqual(alist, []) + def test_gc_during_ref_creation(self): + self.check_gc_during_creation(weakref.ref) + + def test_gc_during_proxy_creation(self): + self.check_gc_during_creation(weakref.proxy) + + def check_gc_during_creation(self, makeref): + thresholds = gc.get_threshold() + gc.set_threshold(1, 1, 1) + gc.collect() + class A: + pass + + def callback(*args): + pass + + referenced = A() + + a = A() + a.a = a + a.wr = makeref(referenced) + + try: + # now make sure the object and the ref get labeled as + # cyclic trash: + a = A() + a.wrc = weakref.ref(referenced, callback) + + finally: + gc.set_threshold(*thresholds) + class Object: def __init__(self, arg): self.arg = arg |