def test_Source__shadowless__(self): """Source.__shadowless__""" def T(got,expected = True): self.assert_(expected == got,'got: %s expected: %s' % (got,expected)) a = Source() a.__dict_shadow__['spam'] = 'ham' self.assertRaises(AttributeError,lambda:getattr(a.__shadowless__,'spam')) a.spam = 'can' T(a.__shadowless__.spam,'can') T(a.__shadowless__.__dict_shadow__.items(),[]) # Should be completely read-only def f(): a.__shadowless__.spam = 10 self.assertRaises(TypeError,f) def f(): del a.__shadowless__.spam self.assertRaises(TypeError,f) def f(): a.__shadowless__.__dict_shadow__['foo'] = 'bar' self.assertRaises(TypeError,f) def f(): a.__shadowless__.notify('foo',lambda:None) self.assertRaises(TypeError,f)
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_shadow(self): """Source.__dict_shadow__""" from Tuke.context.source import Source def T(got,expected = True): self.assert_(expected == got,'got: %s expected: %s' % (got,expected)) s = Source() s.spam = 'spam' T(s.spam,'spam') s.__dict_shadow__['spam'] = 'ham' T(s.spam,'ham') s.spam = 'eggs' T(s.spam,'ham') T(s.__dict__['spam'],'eggs') # Deleting the attr shouldn't effect the shadow del s.spam T(s.spam,'ham') T(s.__dict__,{})
def test_Source_shadow(self): """Source.__dict_shadow__""" from Tuke.context.source import Source def T(got, expected=True): self.assert_(expected == got, 'got: %s expected: %s' % (got, expected)) s = Source() s.spam = 'spam' T(s.spam, 'spam') s.__dict_shadow__['spam'] = 'ham' T(s.spam, 'ham') s.spam = 'eggs' T(s.spam, 'ham') T(s.__dict__['spam'], 'eggs') # Deleting the attr shouldn't effect the shadow del s.spam T(s.spam, 'ham') T(s.__dict__, {})
def test_Source__shadowless__(self): """Source.__shadowless__""" def T(got, expected=True): self.assert_(expected == got, 'got: %s expected: %s' % (got, expected)) a = Source() a.__dict_shadow__['spam'] = 'ham' self.assertRaises(AttributeError, lambda: getattr(a.__shadowless__, 'spam')) a.spam = 'can' T(a.__shadowless__.spam, 'can') T(a.__shadowless__.__dict_shadow__.items(), []) # Should be completely read-only def f(): a.__shadowless__.spam = 10 self.assertRaises(TypeError, f) def f(): del a.__shadowless__.spam self.assertRaises(TypeError, f) def f(): a.__shadowless__.__dict_shadow__['foo'] = 'bar' self.assertRaises(TypeError, f) def f(): a.__shadowless__.notify('foo', lambda: None) self.assertRaises(TypeError, f)
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)))