Skip to content

Commit

Permalink
restore comments on the hijack/restore/original_attr funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbrowning committed Nov 13, 2014
1 parent b09fe74 commit 8d9e18d
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions doubles/target.py
Expand Up @@ -99,6 +99,13 @@ def _generate_attrs(self):
return attrs

def hijack_attr(self, attr_name):
"""
Hijack an attribute on the target object, updating the underlying class and delegating
the call to the instance. This allows specially-handled attributes like __call__,
__enter__, and __exit__ to be mocked on a per-instance basis.
:param str attr_name: the name of the attribute to hijack
"""
if not self._original_attr(attr_name):
setattr(
self.obj.__class__,
Expand All @@ -109,11 +116,23 @@ def hijack_attr(self, attr_name):
)

def restore_attr(self, attr_name):
"""
Restore an attribute back onto the target object.
:param str attr_name: the name of the attribute to restore
"""
original_attr = self._original_attr(attr_name)
if self._original_attr(attr_name):
setattr(self.obj.__class__, attr_name, original_attr)

def _original_attr(self, attr_name):
"""
Return the original attribute off of the proxy on the target object.
:param str attr_name: the name of the original attribute to return
:return: Func or None.
:rtype: func
"""
try:
return getattr(
getattr(self.obj.__class__, attr_name), '_doubles_target_method', None
Expand Down

0 comments on commit 8d9e18d

Please sign in to comment.