如何在 Python 中去除函数的修饰符

假设我有以下几点:

def with_connection(f):
def decorated(*args, **kwargs):
f(get_connection(...), *args, **kwargs)
return decorated


@with_connection
def spam(connection):
# Do something

我想要测试 spam函数,而不必经历设置连接的麻烦(或者装饰器正在做的任何事情)。

给定 spam,我如何从它去掉装饰符并得到底层的“未装饰”函数?

34318 次浏览

In the general case, you can't, because

@with_connection
def spam(connection):
# Do something

is equivalent to

def spam(connection):
# Do something


spam = with_connection(spam)

which means that the "original" spam might not even exist anymore. A (not too pretty) hack would be this:

def with_connection(f):
def decorated(*args, **kwargs):
f(get_connection(...), *args, **kwargs)
decorated._original = f
return decorated


@with_connection
def spam(connection):
# Do something


spam._original(testcon) # calls the undecorated function

Behold, FuglyHackThatWillWorkForYourExampleButICantPromiseAnythingElse:

 orig_spam = spam.func_closure[0].cell_contents

Edit: For functions/methods decorated more than once and with more complicated decorators you can try using the following code. It relies on the fact, that decorated functions are __name__d differently than the original function.

def search_for_orig(decorated, orig_name):
for obj in (c.cell_contents for c in decorated.__closure__):
if hasattr(obj, "__name__") and obj.__name__ == orig_name:
return obj
if hasattr(obj, "__closure__") and obj.__closure__:
found = search_for_orig(obj, orig_name)
if found:
return found
return None


>>> search_for_orig(spam, "spam")
<function spam at 0x027ACD70>

It's not fool proof though. It will fail if the name of the function returned from a decorator is the same as the decorated one. The order of hasattr() checks is also a heuristic, there are decoration chains that return wrong results in any case.

balpha's solution can be made more generalizable with this meta-decorator:

def include_original(dec):
def meta_decorator(f):
decorated = dec(f)
decorated._original = f
return decorated
return meta_decorator

Then you can decorate your decorators with @include_original, and every one will have a testable (undecorated) version tucked away inside it.

@include_original
def shout(f):
def _():
string = f()
return string.upper()
return _






@shout
def function():
return "hello world"


>>> print function()
HELLO_WORLD
>>> print function._original()
hello world

The usual approach to testing such functions is to make any dependencies, such as get_connection, configurable. Then you can override it with a mock while testing. Basically the same as dependency injection in the Java world but a lot simpler thanks to Pythons dynamic nature.

Code for it might look something like this:

# decorator definition
def with_connection(f):
def decorated(*args, **kwargs):
f(with_connection.connection_getter(), *args, **kwargs)
return decorated


# normal configuration
with_connection.connection_getter = lambda: get_connection(...)


# inside testsuite setup override it
with_connection.connection_getter = lambda: "a mock connection"

Depending on your code you could find a better object than the decorator to stick the factory function on. The issue with having it on the decorator is that you'd have to remember to restore it to the old value in the teardown method.

Instead of doing...

def with_connection(f):
def decorated(*args, **kwargs):
f(get_connection(...), *args, **kwargs)
return decorated


@with_connection
def spam(connection):
# Do something


orig_spam = magic_hack_of_a_function(spam)

You could just do...

def with_connection(f):
...


def spam_f(connection):
...


spam = with_connection(spam_f)

...which is all the @decorator syntax does - you can then obviously access the original spam_f normally.

Add a do-nothing decorator:

def do_nothing(f):
return f

After defining or importing with_connection but before you get to the methods that use it as a decorator, add:

if TESTING:
with_connection = do_nothing

Then if you set the global TESTING to True, you will have replaced with_connection with a do-nothing decorator.

There's been a bit of an update for this question. If you're using Python 3, and @functools.wraps you can use __wrapped__ property for decorators from stdlib.

Here's an example from Python Cookbook, 3rd edition, section 9.3 Unwrapping decorators

>>> @somedecorator
>>> def add(x, y):
...     return x + y
...
>>> orig_add = add.__wrapped__
>>> orig_add(3, 4)
7
>>>

If you are trying to unwrap a function from custom decorator, the decorator function needs to use wraps function from functools See discussion in Python Cookbook, 3rd edition, section 9.2 Preserving function metadata when writing decorators

>>> from functools import wraps
>>> def somedecorator(func):
...    @wraps(func)
...    def wrapper(*args, **kwargs):
...       # decorator implementation here
...       # ......
...       return func(*args, **kwargs)
...
...    return wrapper

You can now use the undecorated package:

>>> from undecorated import undecorated
>>> undecorated(spam)

It goes through the hassle of digging through all the layers of different decorators until it reaches the bottom function and doesn't require changing the original decorators. It works on both Python 2 and Python 3.

It's good practice to decorate decorators with functools.wraps like so:

import functools


def with_connection(f):
@functools.wraps(f)
def decorated(*args, **kwargs):
f(get_connection(...), *args, **kwargs)
return decorated


@with_connection
def spam(connection):
# Do something

As of Python 3.2, this will automatically add a __wrapped__ attribute that lets you retrieve the original, undecorated function:

>>> spam.__wrapped__
<function spam at 0x7fe4e6dfc048>

However, instead of manually accessing the __wrapped__ attribute, it's better to use inspect.unwrap:

>>> inspect.unwrap(spam)
<function spam at 0x7fe4e6dfc048>

the original function is stored in spam.__closure__[0].cell_contents.
Decorator uses closure to bind original function with extra layer of functionality. The original function must be stored in a closure cell kept by one of the functions in the nested structure of decorator.
Example:

>>> def add(f):
...     def _decorator(*args, **kargs):
...             print('hello_world')
...             return f(*args, **kargs)
...     return _decorator
...
>>> @add
... def f(msg):
...     print('f ==>', msg)
...
>>> f('alice')
hello_world
f ==> alice
>>> f.__closure__[0].cell_contents
<function f at 0x7f5d205991e0>
>>> f.__closure__[0].cell_contents('alice')
f ==> alice

this is the core principle of undecorated, you could refer to the source code for more details.