Skip to content

Commit

Permalink
Merge f9f52cf into c16f28a
Browse files Browse the repository at this point in the history
  • Loading branch information
charlax committed Oct 5, 2018
2 parents c16f28a + f9f52cf commit e6c0831
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 120 deletions.
30 changes: 18 additions & 12 deletions doubles/proxy_method.py
Expand Up @@ -8,7 +8,7 @@


def double_name(name):
return 'double_of_' + name
return "double_of_" + name


def _restore__new__(target, original_method):
Expand All @@ -27,6 +27,7 @@ def _restore__new__(target, original_method):
:param func original_method: The method to set __new__ to
"""
if isbuiltin(original_method):

@wraps(original_method)
def _new(cls, *args, **kwargs):
return original_method(cls)
Expand Down Expand Up @@ -85,7 +86,7 @@ def __get__(self, instance, owner):
:rtype: object, ProxyMethod
"""

if self._attr.kind == 'property':
if self._attr.kind == "property":
return self.__call__()

return self
Expand All @@ -98,25 +99,31 @@ def __name__(self):
def __doc__(self):
return self._original_method.__doc__

@property
def __wrapped__(self):
return self._original_method

def restore_original_method(self):
"""Replaces the proxy method on the target object with its original value."""

if self._target.is_class_or_module():
setattr(self._target.obj, self._method_name, self._original_method)
if self._method_name == '__new__' and sys.version_info >= (3, 0):
if self._method_name == "__new__" and sys.version_info >= (3, 0):
_restore__new__(self._target.obj, self._original_method)
else:
setattr(self._target.obj, self._method_name, self._original_method)
elif self._attr.kind == 'property':
setattr(self._target.obj.__class__, self._method_name, self._original_method)
elif self._attr.kind == "property":
setattr(
self._target.obj.__class__, self._method_name, self._original_method
)
del self._target.obj.__dict__[double_name(self._method_name)]
elif self._attr.kind == 'attribute':
elif self._attr.kind == "attribute":
self._target.obj.__dict__[self._method_name] = self._original_method
else:
# TODO: Could there ever have been a value here that needs to be restored?
del self._target.obj.__dict__[self._method_name]

if self._method_name in ['__call__', '__enter__', '__exit__']:
if self._method_name in ["__call__", "__enter__", "__exit__"]:
self._target.restore_attr(self._method_name)

def _capture_original_method(self):
Expand All @@ -129,17 +136,16 @@ def _hijack_target(self):

if self._target.is_class_or_module():
setattr(self._target.obj, self._method_name, self)
elif self._attr.kind == 'property':
elif self._attr.kind == "property":
proxy_property = ProxyProperty(
double_name(self._method_name),
self._original_method,
double_name(self._method_name), self._original_method
)
setattr(self._target.obj.__class__, self._method_name, proxy_property)
self._target.obj.__dict__[double_name(self._method_name)] = self
else:
self._target.obj.__dict__[self._method_name] = self

if self._method_name in ['__call__', '__enter__', '__exit__']:
if self._method_name in ["__call__", "__enter__", "__exit__"]:
self._target.hijack_attr(self._method_name)

def _raise_exception(self, args, kwargs):
Expand All @@ -157,6 +163,6 @@ def _raise_exception(self, args, kwargs):
error_message.format(
self._method_name,
self._target.obj,
build_argument_repr_string(args, kwargs)
build_argument_repr_string(args, kwargs),
)
)

0 comments on commit e6c0831

Please sign in to comment.