[Python-checkins] python/dist/src/Lib/test test_gc.py,1.18,1.19

gvanrossum@users.sourceforge.net gvanrossum@users.sourceforge.net
Fri, 09 Aug 2002 10:38:19 -0700


Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv24847

Modified Files:
	test_gc.py 
Log Message:
Test finalizers and GC from inside __del__ for new classes.


Index: test_gc.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_gc.py,v
retrieving revision 1.18
retrieving revision 1.19
diff -C2 -d -r1.18 -r1.19
*** test_gc.py	23 Jul 2002 19:03:53 -0000	1.18
--- test_gc.py	9 Aug 2002 17:38:16 -0000	1.19
***************
*** 125,128 ****
--- 125,152 ----
      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
***************
*** 178,181 ****
--- 202,220 ----
      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
***************
*** 226,230 ****
--- 265,271 ----
      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)