[Python-checkins] python/dist/src/Lib/test test_weakref.py, 1.34, 1.35

fdrake at users.sourceforge.net fdrake at users.sourceforge.net
Wed Feb 4 18:14:16 EST 2004


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22056/Lib/test

Modified Files:
	test_weakref.py 
Log Message:
Allocating a new weakref object can cause existing weakref objects for
the same object to be collected by the cyclic GC support if they are
only referenced by a cycle.  If the weakref being collected was one of
the weakrefs without callbacks, some local variables for the
constructor became invalid and have to be re-computed.

The test caused a segfault under a debug build without the fix applied.


Index: test_weakref.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_weakref.py,v
retrieving revision 1.34
retrieving revision 1.35
diff -C2 -d -r1.34 -r1.35
*** test_weakref.py	3 Feb 2004 19:56:46 -0000	1.34
--- test_weakref.py	4 Feb 2004 23:14:14 -0000	1.35
***************
*** 1,2 ****
--- 1,3 ----
+ import gc
  import sys
  import unittest
***************
*** 592,595 ****
--- 593,627 ----
          self.assertEqual(alist, [])
  
+     def test_gc_during_ref_creation(self):
+         self.check_gc_during_creation(weakref.ref)
+ 
+     def test_gc_during_proxy_creation(self):
+         self.check_gc_during_creation(weakref.proxy)
+ 
+     def check_gc_during_creation(self, makeref):
+         thresholds = gc.get_threshold()
+         gc.set_threshold(1, 1, 1)
+         gc.collect()
+         class A:
+             pass
+ 
+         def callback(*args):
+             pass
+ 
+         referenced = A()
+ 
+         a = A()
+         a.a = a
+         a.wr = makeref(referenced)
+ 
+         try:
+             # now make sure the object and the ref get labeled as
+             # cyclic trash:
+             a = A()
+             a.wrc = weakref.ref(referenced, callback)
+ 
+         finally:
+             gc.set_threshold(*thresholds)
+ 
  class Object:
      def __init__(self, arg):




More information about the Python-checkins mailing list