Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_weakref.SubclassableWeakrefTestCase Class Reference
Inheritance diagram for test.test_weakref.SubclassableWeakrefTestCase:
Inheritance graph
[legend]
Collaboration diagram for test.test_weakref.SubclassableWeakrefTestCase:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_subclass_refs
def test_subclass_refs_dont_replace_standard_refs
def test_subclass_refs_dont_conflate_callbacks
def test_subclass_refs_with_slots
def test_subclass_refs_with_cycle
def setUp
def callback

Public Attributes

 value
 called
 slot1
 slot2
 cbcalled

Detailed Description

Definition at line 696 of file test_weakref.py.


Member Function Documentation

def test.test_weakref.TestBase.callback (   self,
  ref 
) [inherited]

Definition at line 40 of file test_weakref.py.

00040 
00041     def callback(self, ref):
00042         self.cbcalled += 1
00043 

Here is the caller graph for this function:

def test.test_weakref.TestBase.setUp (   self) [inherited]

Definition at line 37 of file test_weakref.py.

00037 
00038     def setUp(self):
00039         self.cbcalled = 0

Here is the caller graph for this function:

Definition at line 698 of file test_weakref.py.

00698 
00699     def test_subclass_refs(self):
00700         class MyRef(weakref.ref):
00701             def __init__(self, ob, callback=None, value=42):
00702                 self.value = value
00703                 super().__init__(ob, callback)
00704             def __call__(self):
00705                 self.called = True
00706                 return super().__call__()
00707         o = Object("foo")
00708         mr = MyRef(o, value=24)
00709         self.assertTrue(mr() is o)
00710         self.assertTrue(mr.called)
00711         self.assertEqual(mr.value, 24)
00712         del o
00713         self.assertTrue(mr() is None)
00714         self.assertTrue(mr.called)

Here is the call graph for this function:

Definition at line 732 of file test_weakref.py.

00732 
00733     def test_subclass_refs_dont_conflate_callbacks(self):
00734         class MyRef(weakref.ref):
00735             pass
00736         o = Object(42)
00737         r1 = MyRef(o, id)
00738         r2 = MyRef(o, str)
00739         self.assertTrue(r1 is not r2)
00740         refs = weakref.getweakrefs(o)
00741         self.assertIn(r1, refs)
00742         self.assertIn(r2, refs)

Definition at line 715 of file test_weakref.py.

00715 
00716     def test_subclass_refs_dont_replace_standard_refs(self):
00717         class MyRef(weakref.ref):
00718             pass
00719         o = Object(42)
00720         r1 = MyRef(o)
00721         r2 = weakref.ref(o)
00722         self.assertTrue(r1 is not r2)
00723         self.assertEqual(weakref.getweakrefs(o), [r2, r1])
00724         self.assertEqual(weakref.getweakrefcount(o), 2)
00725         r3 = MyRef(o)
00726         self.assertEqual(weakref.getweakrefcount(o), 3)
00727         refs = weakref.getweakrefs(o)
00728         self.assertEqual(len(refs), 3)
00729         self.assertTrue(r2 is refs[0])
00730         self.assertIn(r1, refs[1:])
00731         self.assertIn(r3, refs[1:])

Definition at line 760 of file test_weakref.py.

00760 
00761     def test_subclass_refs_with_cycle(self):
00762         # Bug #3110
00763         # An instance of a weakref subclass can have attributes.
00764         # If such a weakref holds the only strong reference to the object,
00765         # deleting the weakref will delete the object. In this case,
00766         # the callback must not be called, because the ref object is
00767         # being deleted.
00768         class MyRef(weakref.ref):
00769             pass
00770 
00771         # Use a local callback, for "regrtest -R::"
00772         # to detect refcounting problems
00773         def callback(w):
00774             self.cbcalled += 1
00775 
00776         o = C()
00777         r1 = MyRef(o, callback)
00778         r1.o = o
00779         del o
00780 
00781         del r1 # Used to crash here
00782 
00783         self.assertEqual(self.cbcalled, 0)
00784 
00785         # Same test, with two weakrefs to the same object
00786         # (since code paths are different)
00787         o = C()
00788         r1 = MyRef(o, callback)
00789         r2 = MyRef(o, callback)
00790         r1.r = r2
00791         r2.o = o
00792         del o
00793         del r2
00794 
00795         del r1 # Used to crash here
00796 
00797         self.assertEqual(self.cbcalled, 0)
00798 

Definition at line 743 of file test_weakref.py.

00743 
00744     def test_subclass_refs_with_slots(self):
00745         class MyRef(weakref.ref):
00746             __slots__ = "slot1", "slot2"
00747             def __new__(type, ob, callback, slot1, slot2):
00748                 return weakref.ref.__new__(type, ob, callback)
00749             def __init__(self, ob, callback, slot1, slot2):
00750                 self.slot1 = slot1
00751                 self.slot2 = slot2
00752             def meth(self):
00753                 return self.slot1 + self.slot2
00754         o = Object(42)
00755         r = MyRef(o, None, "abc", "def")
00756         self.assertEqual(r.slot1, "abc")
00757         self.assertEqual(r.slot2, "def")
00758         self.assertEqual(r.meth(), "abcdef")
00759         self.assertFalse(hasattr(r, "__dict__"))

Here is the call graph for this function:


Member Data Documentation

Definition at line 704 of file test_weakref.py.

Reimplemented in test.test_weakref.ReferencesTestCase.

Definition at line 38 of file test_weakref.py.

Definition at line 749 of file test_weakref.py.

Definition at line 750 of file test_weakref.py.

Definition at line 701 of file test_weakref.py.


The documentation for this class was generated from the following file: