@@ -43,12 +43,12 @@ class Spy(object):
43
43
44
44
def __init__ (self , mock ):
45
45
self .__dict__ ["_%s__mock" % self .__class__ .__name__ ] = mock
46
- self .__dict__ ["_%s__wrapped" % self .__class__ .__name__ ] = self .__mock ._mock_wraps
47
46
48
47
def __getattr__ (self , item ):
49
48
attr = getattr (self .__mock , item )
50
49
51
50
mock_wraps = attr ._mock_wraps
51
+ wrapped = self .__mock ._mock_wraps
52
52
if mock_wraps is not None :
53
53
# Is the wrapped value present?
54
54
if isinstance (mock_wraps , types .MethodType ):
@@ -58,16 +58,15 @@ def __getattr__(self, item):
58
58
# If the method belongs to a Mock and method is not class method
59
59
# Rebind mock method to use this Spy as self
60
60
# This will allow self.method() to go back into the spy and into the Mock to be tracked
61
- attr ._mock_wraps = types .MethodType (attr . _mock_wraps .__func__ , self )
61
+ attr ._mock_wraps = types .MethodType (mock_wraps .__func__ , self )
62
62
else :
63
63
# This attribute is not a method
64
- if not isinstance (mock_wraps , types .FunctionType ) and hasattr (self . __wrapped , item ):
64
+ if not isinstance (mock_wraps , types .FunctionType ) and hasattr (wrapped , item ):
65
65
# If wrapped is not a function (e.g. static method) and the underlying wrapped
66
66
# has this attribute then simply return the value of that attribute directly
67
- return getattr (self . __wrapped , item )
67
+ return getattr (wrapped , item )
68
68
else :
69
- if attr ._mock_return_value is DEFAULT and hasattr (self .__wrapped , item ) and getattr (self .__wrapped ,
70
- item ) is None :
69
+ if attr ._mock_return_value is DEFAULT and hasattr (wrapped , item ) and getattr (wrapped , item ) is None :
71
70
# This attribute is not wrapped, and if it doesn't have a return value
72
71
# and is None then just return None
73
72
return None
@@ -76,20 +75,22 @@ def __getattr__(self, item):
76
75
return attr
77
76
78
77
def __setattr__ (self , key : str , value ):
78
+ mock = self .__mock
79
79
try :
80
- attr = getattr (self . __mock , key )
80
+ attr = getattr (mock , key )
81
81
mock_wraps = attr ._mock_wraps
82
+ wrapped = mock ._mock_wraps
82
83
if mock_wraps is None or \
83
- isinstance (mock_wraps , types .MethodType ) and mock_wraps .__self__ in (self , self . __wrapped ):
84
+ isinstance (mock_wraps , types .MethodType ) and mock_wraps .__self__ in (self , wrapped ):
84
85
# If attribute is not wrapped or is a method of the wrapped object and method is bound
85
86
# to Spy or spied mock then delegate to Mock
86
- return setattr (self . __mock , key , value )
87
+ return setattr (mock , key , value )
87
88
88
89
# Otherwise set the value directly on the object that is wrapped and is spied on
89
- setattr (self . __wrapped , key , value )
90
+ setattr (wrapped , key , value )
90
91
except AttributeError :
91
92
# If Mock doesn't have this attribute delegate to Mock
92
- return setattr (self . __mock , key , value )
93
+ return setattr (mock , key , value )
93
94
94
95
95
96
def magic_spy (obj ):
0 commit comments