mirror of
https://github.com/python/cpython.git
synced 2025-10-18 04:38:07 +00:00
- 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.
This commit is contained in:
parent
544de1effb
commit
faae266e89
1 changed files with 88 additions and 27 deletions
|
@ -1,18 +1,35 @@
|
||||||
|
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():
|
||||||
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():
|
||||||
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():
|
||||||
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():
|
||||||
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():
|
||||||
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():
|
||||||
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():
|
||||||
|
|
||||||
|
|
||||||
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()
|
||||||
|
gc.set_debug(debug & ~gc.DEBUG_LEAK) # this test is supposed to leak
|
||||||
|
|
||||||
test_list()
|
try:
|
||||||
test_dict()
|
test_all()
|
||||||
test_tuple()
|
finally:
|
||||||
test_class()
|
gc.set_debug(debug)
|
||||||
test_instance()
|
# test gc.enable() even if GC is disabled by default
|
||||||
test_method()
|
if verbose:
|
||||||
test_finalizer()
|
print "restoring automatic collection"
|
||||||
test_function()
|
# make sure to always test gc.enable()
|
||||||
test_del()
|
gc.enable()
|
||||||
|
assert gc.isenabled()
|
||||||
# test gc.enable() even if GC is disabled by default
|
if not enabled:
|
||||||
gc.enable()
|
gc.disable()
|
||||||
assert gc.isenabled()
|
|
||||||
if not enabled:
|
|
||||||
gc.disable()
|
|
||||||
|
|
||||||
|
|
||||||
test_all()
|
test()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue