def testRefCountingOfReferredObjectAfterDeletingReferrer(self): '''Deleting the object referring to other object should decrease the reference count of the referee.''' d = Derived() o = ObjectType() refcount = getrefcount(o) d.objectTypeField = o self.assertEqual(getrefcount(o), refcount + 1) del d self.assertEqual(getrefcount(o), refcount)
def testRefCountingAccessingObjectTypeField(self): '''Accessing a object type field should respect the reference counting rules.''' d = Derived() # attributing object to instance's field should increase its reference count o1 = ObjectType() refcount1 = getrefcount(o1) d.objectTypeField = o1 self.assertEqual(d.objectTypeField, o1) self.assertEqual(getrefcount(d.objectTypeField), refcount1 + 1) # attributing a new object to instance's field should decrease the previous object's reference count o2 = ObjectType() refcount2 = getrefcount(o2) d.objectTypeField = o2 self.assertEqual(d.objectTypeField, o2) self.assertEqual(getrefcount(o1), refcount1) self.assertEqual(getrefcount(d.objectTypeField), refcount2 + 1)
def testAccessingObjectTypeField(self): '''Reads and writes a object type (in this case an 'ObjectType') field.''' d = Derived() # attribution old_value = d.objectTypeField new_value = ObjectType() d.objectTypeField = new_value self.assertEqual(d.objectTypeField, new_value) self.assertNotEqual(d.objectTypeField, old_value) # attribution with a convertible type value = None d.objectTypeField = value self.assertEqual(d.objectTypeField, value) # attribution with invalid type self.assertRaises(TypeError, lambda : setattr(d, 'objectTypeField', 123))