示例#1
0
 def test_simple(self):
     import _weakref, gc
     class A(object):
         pass
     a = A()
     assert _weakref.getweakrefcount(a) == 0
     ref = _weakref.ref(a)
     assert ref() is a
     assert a.__weakref__ is ref
     assert _weakref.getweakrefcount(a) == 1
     del a
     gc.collect()
     assert ref() is None
示例#2
0
 def test_simple(self):
     import _weakref, gc
     class A(object):
         pass
     a = A()
     assert _weakref.getweakrefcount(a) == 0
     ref = _weakref.ref(a)
     assert ref() is a
     assert a.__weakref__ is ref
     assert _weakref.getweakrefcount(a) == 1
     del a
     gc.collect()
     assert ref() is None
示例#3
0
 def test_correct_weakrefcount_after_death(self):
     import _weakref, gc
     class A(object):
         pass
     a = A()
     ref1 = _weakref.ref(a)
     ref2 = _weakref.ref(a)
     assert _weakref.getweakrefcount(a) == 1
     del ref1
     gc.collect()
     assert _weakref.getweakrefcount(a) == 1
     del ref2
     gc.collect()
     assert _weakref.getweakrefcount(a) == 0
示例#4
0
 def test_correct_weakrefcount_after_death(self):
     import _weakref, gc
     class A(object):
         pass
     a = A()
     ref1 = _weakref.ref(a)
     ref2 = _weakref.ref(a)
     assert _weakref.getweakrefcount(a) == 1
     del ref1
     gc.collect()
     assert _weakref.getweakrefcount(a) == 1
     del ref2
     gc.collect()
     assert _weakref.getweakrefcount(a) == 0
示例#5
0
 def test_dont_callback_if_weakref_dead(self):
     import _weakref, gc
     class A(object):
         pass
     a1 = A()
     a1.x = 40
     a2 = A()
     def callback(ref):
         a1.x = 42
     assert _weakref.getweakrefcount(a2) == 0
     ref = _weakref.ref(a2, callback)
     assert _weakref.getweakrefcount(a2) == 1
     ref = None
     gc.collect()
     assert _weakref.getweakrefcount(a2) == 0
     a2 = None
     gc.collect()
     assert a1.x == 40
示例#6
0
 def test_dont_callback_if_weakref_dead(self):
     import _weakref, gc
     class A(object):
         pass
     a1 = A()
     a1.x = 40
     a2 = A()
     def callback(ref):
         a1.x = 42
     assert _weakref.getweakrefcount(a2) == 0
     ref = _weakref.ref(a2, callback)
     assert _weakref.getweakrefcount(a2) == 1
     ref = None
     gc.collect()
     assert _weakref.getweakrefcount(a2) == 0
     a2 = None
     gc.collect()
     assert a1.x == 40
示例#7
0
 def test_callback(self):
     import _weakref, gc
     class A(object):
         pass
     a1 = A()
     a2 = A()
     def callback(ref):
         a2.ref = ref()
     ref1 = _weakref.ref(a1, callback)
     ref2 = _weakref.ref(a1)
     assert _weakref.getweakrefcount(a1) == 2
     del a1
     gc.collect()
     assert ref1() is None
     assert a2.ref is None
示例#8
0
 def test_callback(self):
     import _weakref, gc
     class A(object):
         pass
     a1 = A()
     a2 = A()
     def callback(ref):
         a2.ref = ref()
     ref1 = _weakref.ref(a1, callback)
     ref2 = _weakref.ref(a1)
     assert _weakref.getweakrefcount(a1) == 2
     del a1
     gc.collect()
     assert ref1() is None
     assert a2.ref is None
示例#9
0
 def test_weakref_subclass_with_del(self):
     import _weakref, gc
     class Ref(_weakref.ref):
         def __del__(self):
             b.a = 42
     class A(object):
         pass
     a = A()
     b = A()
     b.a = 1
     w = Ref(a)
     del w
     gc.collect()
     assert b.a == 42
     if _weakref.getweakrefcount(a) > 0:
         # the following can crash if the presence of the applevel __del__
         # leads to the fact that the __del__ of _weakref.ref is not called.
         assert _weakref.getweakrefs(a)[0]() is a
示例#10
0
 def test_weakref_subclass_with_del(self):
     import _weakref, gc
     class Ref(_weakref.ref):
         def __del__(self):
             b.a = 42
     class A(object):
         pass
     a = A()
     b = A()
     b.a = 1
     w = Ref(a)
     del w
     gc.collect()
     assert b.a == 42
     if _weakref.getweakrefcount(a) > 0:
         # the following can crash if the presence of the applevel __del__
         # leads to the fact that the __del__ of _weakref.ref is not called.
         assert _weakref.getweakrefs(a)[0]() is a