summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2002-08-09 17:38:16 (GMT)
committerGuido van Rossum <guido@python.org>2002-08-09 17:38:16 (GMT)
commit4aa21aa5b331183af372c1d0a8e59be997f78c33 (patch)
tree3568f13e6eb663c08efbdfb0aa82d89bc8e63171 /Lib/test
parentbffb2efee00fb65cc23372095003d1c3ddfa0e21 (diff)
downloadcpython-4aa21aa5b331183af372c1d0a8e59be997f78c33.zip
cpython-4aa21aa5b331183af372c1d0a8e59be997f78c33.tar.gz
cpython-4aa21aa5b331183af372c1d0a8e59be997f78c33.tar.bz2
Test finalizers and GC from inside __del__ for new classes.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_gc.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py
index 40edd30..8c79ac6 100644
--- a/Lib/test/test_gc.py
+++ b/Lib/test/test_gc.py
@@ -124,6 +124,30 @@ def test_finalizer():
raise TestFailed, "didn't find obj in garbage (finalizer)"
gc.garbage.remove(obj)
+def test_finalizer_newclass():
+ # A() is uncollectable if it is part of a cycle, make sure it shows up
+ # in gc.garbage.
+ class A(object):
+ def __del__(self): pass
+ class B(object):
+ pass
+ a = A()
+ a.a = a
+ id_a = id(a)
+ b = B()
+ b.b = b
+ gc.collect()
+ del a
+ del b
+ expect_nonzero(gc.collect(), "finalizer")
+ for obj in gc.garbage:
+ if id(obj) == id_a:
+ del obj.a
+ break
+ else:
+ raise TestFailed, "didn't find obj in garbage (finalizer)"
+ gc.garbage.remove(obj)
+
def test_function():
# Tricky: f -> d -> f, code should call d.clear() after the exec to
# break the cycle.
@@ -177,6 +201,21 @@ def test_del():
gc.disable()
apply(gc.set_threshold, thresholds)
+def test_del_newclass():
+ # __del__ methods can trigger collection, make this to happen
+ thresholds = gc.get_threshold()
+ gc.enable()
+ gc.set_threshold(1)
+
+ class A(object):
+ def __del__(self):
+ dir(self)
+ a = A()
+ del a
+
+ gc.disable()
+ apply(gc.set_threshold, thresholds)
+
class Ouch:
n = 0
def __del__(self):
@@ -225,7 +264,9 @@ def test_all():
run_test("functions", test_function)
run_test("frames", test_frame)
run_test("finalizers", test_finalizer)
+ run_test("finalizers (new class)", test_finalizer_newclass)
run_test("__del__", test_del)
+ run_test("__del__ (new class)", test_del_newclass)
run_test("saveall", test_saveall)
run_test("trashcan", test_trashcan)