def test_WeakObjectInstanceMethodLifetime(self): """ Checks that bound functions returned from a weak object do not prevent the referenced object from being deleted, as compared to weakref.proxy. """ obj = TestClass() # Get a local reference to the bound method through a proxy; it will # increase obj's reference count. reffoo = weakref.proxy(obj).foo # Get a local reference to the bound method through a weak object; it # should not increase obj's reference count. objfoo = weakobj.objectref(obj).foo # Remove the original reference, and call the functions; this should # succeed because of reffoo's im_self. del obj reffoo() objfoo() # Remove the local reference-via-proxy, which should now allow the # original object to be deleted. del reffoo try: objfoo() except weakref.ReferenceError: pass else: self.fail('Weak object should be invalidated')
def getEventChannel(self, name): """ Get the sandbox event channel 'name'. If it does not exist, throw a NameError. """ if name not in self._eventChannels: raise NameError("No event channel '%s'" % name) else: # Return a weak proxy so that destroy() can fully delete the # object in normal usage and ensure that it stays destroyed. return weakobj.objectref(self._eventChannels[name])
def test_WeakObject(self): """ Tests basic extended weak object support. """ obj = TestClass() ref = weakref.ref(obj) objref = weakobj.objectref(obj) objref2 = weakobj.objectref(obj) self.assertEquals(objref, objref2) self.assertEquals(objref.foo(), obj.foo()) # Delete what should be the only reference to the original object. del obj self.assertEqual(ref(), None) try: objref.foo() except weakref.ReferenceError: pass else: self.fail('Weak object should be invalidated')
def test_WeakObjectCallback(self): """ Test that the weak object callback occurs as expected. """ obj = TestClass() results = set() def callback(target): results.add(id(target)) ref = weakobj.objectref(obj, callback) expected = set([id(ref)]) del obj self.assertEqual(results, expected)
def getEventChannels(self): return [weakobj.objectref(c) for c in self._eventChannels.itervalues()]