mirror of
https://github.com/django/django.git
synced 2025-08-04 02:48:35 +00:00
Fixed #29253 -- Made method_decorator(list) copy attributes.
This commit is contained in:
parent
a480ef89ad
commit
fdc936c913
2 changed files with 82 additions and 67 deletions
|
@ -168,7 +168,7 @@ def myattr_dec(func):
|
|||
def wrapper(*args, **kwargs):
|
||||
return func(*args, **kwargs)
|
||||
wrapper.myattr = True
|
||||
return wraps(func)(wrapper)
|
||||
return wrapper
|
||||
|
||||
|
||||
myattr_dec_m = method_decorator(myattr_dec)
|
||||
|
@ -178,7 +178,7 @@ def myattr2_dec(func):
|
|||
def wrapper(*args, **kwargs):
|
||||
return func(*args, **kwargs)
|
||||
wrapper.myattr2 = True
|
||||
return wraps(func)(wrapper)
|
||||
return wrapper
|
||||
|
||||
|
||||
myattr2_dec_m = method_decorator(myattr2_dec)
|
||||
|
@ -209,13 +209,23 @@ class MethodDecoratorTests(SimpleTestCase):
|
|||
|
||||
def test_preserve_attributes(self):
|
||||
# Sanity check myattr_dec and myattr2_dec
|
||||
@myattr_dec
|
||||
def func():
|
||||
pass
|
||||
self.assertIs(getattr(func, 'myattr', False), True)
|
||||
|
||||
@myattr2_dec
|
||||
def func():
|
||||
pass
|
||||
self.assertIs(getattr(func, 'myattr2', False), True)
|
||||
|
||||
@myattr_dec
|
||||
@myattr2_dec
|
||||
def func():
|
||||
pass
|
||||
|
||||
self.assertIs(getattr(func, 'myattr', False), True)
|
||||
self.assertIs(getattr(func, 'myattr2', False), True)
|
||||
self.assertIs(getattr(func, 'myattr2', False), False)
|
||||
|
||||
# Decorate using method_decorator() on the method.
|
||||
class TestPlain:
|
||||
|
@ -235,16 +245,23 @@ class MethodDecoratorTests(SimpleTestCase):
|
|||
"A method"
|
||||
pass
|
||||
|
||||
# Decorate using an iterable of decorators.
|
||||
decorators = (myattr_dec_m, myattr2_dec_m)
|
||||
|
||||
@method_decorator(decorators, "method")
|
||||
class TestIterable:
|
||||
# Decorate using an iterable of function decorators.
|
||||
@method_decorator((myattr_dec, myattr2_dec), 'method')
|
||||
class TestFunctionIterable:
|
||||
def method(self):
|
||||
"A method"
|
||||
pass
|
||||
|
||||
tests = (TestPlain, TestMethodAndClass, TestIterable)
|
||||
# Decorate using an iterable of method decorators.
|
||||
decorators = (myattr_dec_m, myattr2_dec_m)
|
||||
|
||||
@method_decorator(decorators, "method")
|
||||
class TestMethodIterable:
|
||||
def method(self):
|
||||
"A method"
|
||||
pass
|
||||
|
||||
tests = (TestPlain, TestMethodAndClass, TestFunctionIterable, TestMethodIterable)
|
||||
for Test in tests:
|
||||
with self.subTest(Test=Test):
|
||||
self.assertIs(getattr(Test().method, 'myattr', False), True)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue