def preserve_signature(func):
"""Preserve the original function signature and attributes in decorator wrappers."""
def fix_signature(wrapper):
exec_scope = {}
parameters = formatargspec(*getargspec(func), formatvalue=lambda value: "")
exec "def {0}{1}: return wrapper{1}".format(func.__name__, parameters) in {'wrapper': wrapper}, exec_scope # can't use tuple form here (see https://bugs.python.org/issue21591)
new_wrapper = exec_scope.pop(func.__name__)
new_wrapper.__name__ = func.__name__
new_wrapper.__doc__ = func.__doc__
new_wrapper.__module__ = func.__module__
new_wrapper.__defaults__ = func.__defaults__
new_wrapper.__dict__.update(func.__dict__)
return new_wrapper
return fix_signature
评论列表
文章目录