[Python-Dev] Re: [Python-checkins] CVS: python/dist/src/Lib/test/output test_gc,NONE,1.1

Neil Schemenauer nascheme@enme.ucalgary.ca
Fri, 30 Jun 2000 12:53:06 -0600


--AqsLC8rIMeq19msA
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

On Thu, Jun 29, 2000 at 11:00:29PM -0500, Skip Montanaro wrote:
> I can all but guarantee you this test will always fail.  Printing absolute
> machine addresses (via calls to id() in this case) in test cases should be
> verboten.

This test script should be better.  It doesn't generate any
output (the stuff the old script printed was pretty useless
anyhow).

  Neil

--AqsLC8rIMeq19msA
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="test_gc.py"

import gc
from test_support import *

def test_list():
    l = []
    l.append(l)
    gc.collect()
    del l
    assert gc.collect() == 1

def test_dict():
    d = {}
    d[1] = d
    gc.collect()
    del d
    assert gc.collect() == 1

def test_tuple():
    l = []
    t = (l,)
    l.append(t)
    gc.collect()
    del t
    del l
    assert gc.collect() == 2

def test_class():
    class A:
        pass
    A.a = A
    gc.collect()
    del A
    assert gc.collect() > 0

def test_instance():
    class A:
        pass
    a = A()
    a.a = a
    gc.collect()
    del a
    assert gc.collect() > 0

def test_method():
    class A:
        def __init__(self):
            self.init = self.__init__
    a = A()
    gc.collect()
    del a
    assert gc.collect() > 0

def test_finalizer():
    class A:
        def __del__(self): pass
    class B:
        pass
    a = A()
    a.a = a
    id_a = id(a)
    b = B()
    b.b = b
    gc.collect()
    del a
    del b
    assert gc.collect() > 0
    for obj in gc.garbage:
        if id(obj) == id_a:
            return
    raise TestFailed

def test_function():
    d = {}
    exec("def f(): pass\n") in d
    gc.collect()
    del d
    assert gc.collect() > 0


def test_all():
    threshold = gc.get_threshold()
    gc.set_threshold(0) # disable automatic collection
    gc.collect()
    test_list()
    test_dict()
    test_tuple()
    test_class()
    test_instance()
    test_method()
    test_finalizer()
    test_function()
    apply(gc.set_threshold, threshold)

test_all()

--AqsLC8rIMeq19msA--