def test_Source_notify(self): """Source notify""" def T(got, expected=True): self.assert_(expected == got, 'got: %s expected: %s' % (got, expected)) a = Source() # Records the value of the attr when called class cb: def __init__(self, source, attr): self.source = source self.attr = attr self.missing = object() def __call__(self, *args, **kwargs): T(args, ()) T(kwargs, {}) try: self.v = getattr(self.source, self.attr) except AttributeError: self.v = self.missing # Setting a callback before the attr is created is perfectly valid. c = cb(a, 'spam') a.notify('spam', c) a.spam = 479606400 T(c.v, 479606400) # Callbacks are a one shot affair a.spam = object() T(c.v, 479606400) # Callbacks are called on deletion as well a.notify('spam', c) del a.spam T(c.v is c.missing) # Make sure callbacks can set further callbacks class cb: def __init__(self, source, attr): self.source = source self.attr = attr self.v = [] def __call__(self): self.source.notify(self.attr, self) self.v.append(getattr(self.source, self.attr)) c = cb(a, 'ham') crefs = sys.getrefcount(c) arefs = sys.getrefcount(a) a.notify('ham', c) for i in range(1000): a.ham = i T(sys.getrefcount(c), crefs) T(sys.getrefcount(a), arefs) T(c.v, list(range(1000)))
def test_Source_notify(self): """Source notify""" def T(got,expected = True): self.assert_(expected == got,'got: %s expected: %s' % (got,expected)) a = Source() # Records the value of the attr when called class cb: def __init__(self,source,attr): self.source = source self.attr = attr self.missing = object() def __call__(self,*args,**kwargs): T(args,()) T(kwargs,{}) try: self.v = getattr(self.source,self.attr) except AttributeError: self.v = self.missing # Setting a callback before the attr is created is perfectly valid. c = cb(a,'spam') a.notify('spam',c) a.spam = 479606400 T(c.v,479606400) # Callbacks are a one shot affair a.spam = object() T(c.v,479606400) # Callbacks are called on deletion as well a.notify('spam',c) del a.spam T(c.v is c.missing) # Make sure callbacks can set further callbacks class cb: def __init__(self,source,attr): self.source = source self.attr = attr self.v = [] def __call__(self): self.source.notify(self.attr,self) self.v.append(getattr(self.source,self.attr)) c = cb(a,'ham') crefs = sys.getrefcount(c) arefs = sys.getrefcount(a) a.notify('ham',c) for i in range(1000): a.ham = i T(sys.getrefcount(c),crefs) T(sys.getrefcount(a),arefs) T(c.v,list(range(1000)))