Coverage for delegator.py: 88%

22 statements  

« prev     ^ index     » next       coverage.py v7.2.5, created at 2023-05-11 13:22 -0700

1class Delegator: 

2 

3 def __init__(self, delegate=None): 

4 self.delegate = delegate 1a

5 self.__cache = set() 1a

6 # Cache is used to only remove added attributes 

7 # when changing the delegate. 

8 

9 def __getattr__(self, name): 

10 attr = getattr(self.delegate, name) # May raise AttributeError 1a

11 setattr(self, name, attr) 1a

12 self.__cache.add(name) 1a

13 return attr 1a

14 

15 def resetcache(self): 

16 "Removes added attributes while leaving original attributes." 

17 # Function is really about resetting delegator dict 

18 # to original state. Cache is just a means 

19 for key in self.__cache: 1a

20 try: 1a

21 delattr(self, key) 1a

22 except AttributeError: 1a

23 pass 1a

24 self.__cache.clear() 1a

25 

26 def setdelegate(self, delegate): 

27 "Reset attributes and change delegate." 

28 self.resetcache() 1a

29 self.delegate = delegate 1a

30 

31if __name__ == '__main__': 31 ↛ 32line 31 didn't jump to line 32, because the condition on line 31 was never true

32 from unittest import main 

33 main('idlelib.idle_test.test_delegator', verbosity=2)