Commit d84c0584 authored by Neil Schemenauer's avatar Neil Schemenauer

- Add test for new SAVEALL debugging flag

- Use exceptions rather than asserts for failing tests.
- Reorganize tests and produce some output if verbose option is set.
parent e0a001d7
from test_support import verbose, TestFailed
import gc import gc
def run_test(name, thunk):
if verbose:
print "testing %s..." % name,
try:
thunk()
except TestFailed:
if verbose:
print "failed (expected %s but got %s)" % (result,
test_result)
raise TestFailed, name
else:
if verbose:
print "ok"
def test_list(): def test_list():
l = [] l = []
l.append(l) l.append(l)
gc.collect() gc.collect()
del l del l
assert gc.collect() == 1 if gc.collect() != 1:
raise TestFailed
def test_dict(): def test_dict():
d = {} d = {}
d[1] = d d[1] = d
gc.collect() gc.collect()
del d del d
assert gc.collect() == 1 if gc.collect() != 1:
raise TestFailed
def test_tuple(): def test_tuple():
# since tuples are immutable we close the loop with a list # since tuples are immutable we close the loop with a list
...@@ -22,7 +39,8 @@ def test_tuple(): ...@@ -22,7 +39,8 @@ def test_tuple():
gc.collect() gc.collect()
del t del t
del l del l
assert gc.collect() == 2 if gc.collect() != 2:
raise TestFailed
def test_class(): def test_class():
class A: class A:
...@@ -30,7 +48,8 @@ def test_class(): ...@@ -30,7 +48,8 @@ def test_class():
A.a = A A.a = A
gc.collect() gc.collect()
del A del A
assert gc.collect() > 0 if gc.collect() == 0:
raise TestFailed
def test_instance(): def test_instance():
class A: class A:
...@@ -39,7 +58,8 @@ def test_instance(): ...@@ -39,7 +58,8 @@ def test_instance():
a.a = a a.a = a
gc.collect() gc.collect()
del a del a
assert gc.collect() > 0 if gc.collect() == 0:
raise TestFailed
def test_method(): def test_method():
# Tricky: self.__init__ is a bound method, it references the instance. # Tricky: self.__init__ is a bound method, it references the instance.
...@@ -49,7 +69,8 @@ def test_method(): ...@@ -49,7 +69,8 @@ def test_method():
a = A() a = A()
gc.collect() gc.collect()
del a del a
assert gc.collect() > 0 if gc.collect() == 0:
raise TestFailed
def test_finalizer(): def test_finalizer():
# A() is uncollectable if it is part of a cycle, make sure it shows up # A() is uncollectable if it is part of a cycle, make sure it shows up
...@@ -64,11 +85,17 @@ def test_finalizer(): ...@@ -64,11 +85,17 @@ def test_finalizer():
b = B() b = B()
b.b = b b.b = b
gc.collect() gc.collect()
gc.garbage[:] = []
del a del a
del b del b
assert gc.collect() > 0 if gc.collect() == 0:
assert id(gc.garbage[0]) == id_a raise TestFailed
for obj in gc.garbage:
if id(obj) == id_a:
del obj.a
break
else:
raise TestFailed
gc.garbage.remove(obj)
def test_function(): def test_function():
# Tricky: f -> d -> f, code should call d.clear() after the exec to # Tricky: f -> d -> f, code should call d.clear() after the exec to
...@@ -77,7 +104,29 @@ def test_function(): ...@@ -77,7 +104,29 @@ def test_function():
exec("def f(): pass\n") in d exec("def f(): pass\n") in d
gc.collect() gc.collect()
del d del d
assert gc.collect() == 2 if gc.collect() != 2:
raise TestFailed
def test_saveall():
# Verify that cyclic garbage like lists show up in gc.garbage if the
# SAVEALL option is enabled.
debug = gc.get_debug()
gc.set_debug(debug | gc.DEBUG_SAVEALL)
l = []
l.append(l)
id_l = id(l)
del l
gc.collect()
try:
for obj in gc.garbage:
if id(obj) == id_l:
del obj[:]
break
else:
raise TestFailed
gc.garbage.remove(obj)
finally:
gc.set_debug(debug)
def test_del(): def test_del():
# __del__ methods can trigger collection, make this to happen # __del__ methods can trigger collection, make this to happen
...@@ -96,26 +145,38 @@ def test_del(): ...@@ -96,26 +145,38 @@ def test_del():
def test_all(): def test_all():
run_test("lists", test_list)
run_test("dicts", test_dict)
run_test("tuples", test_tuple)
run_test("classes", test_class)
run_test("instances", test_instance)
run_test("methods", test_method)
run_test("functions", test_function)
run_test("finalizers", test_finalizer)
run_test("__del__", test_del)
run_test("saveall", test_saveall)
def test():
if verbose:
print "disabling automatic collection"
enabled = gc.isenabled() enabled = gc.isenabled()
gc.disable() gc.disable()
assert not gc.isenabled() assert not gc.isenabled()
debug = gc.get_debug()
test_list() gc.set_debug(debug & ~gc.DEBUG_LEAK) # this test is supposed to leak
test_dict()
test_tuple() try:
test_class() test_all()
test_instance() finally:
test_method() gc.set_debug(debug)
test_finalizer() # test gc.enable() even if GC is disabled by default
test_function() if verbose:
test_del() print "restoring automatic collection"
# make sure to always test gc.enable()
# test gc.enable() even if GC is disabled by default gc.enable()
gc.enable() assert gc.isenabled()
assert gc.isenabled() if not enabled:
if not enabled: gc.disable()
gc.disable()
test()
test_all()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment