Merge pull request #1997 from dpwrussell/method_decorator_args_fix

Used available_attrs in method_decorator
This commit is contained in:
Marc Tamlyn 2014-02-09 10:12:22 +00:00
commit 46a87214ca
2 changed files with 27 additions and 3 deletions

View File

@ -39,9 +39,13 @@ def method_decorator(decorator):
update_wrapper(_wrapper, func) update_wrapper(_wrapper, func)
return _wrapper return _wrapper
update_wrapper(_dec, decorator)
update_wrapper(_dec, decorator, assigned=available_attrs(decorator))
# Change the name to aid debugging. # Change the name to aid debugging.
_dec.__name__ = 'method_decorator(%s)' % decorator.__name__ if hasattr(decorator, '__name__'):
_dec.__name__ = 'method_decorator(%s)' % decorator.__name__
else:
_dec.__name__ = 'method_decorator(%s)' % decorator.__class__.__name__
return _dec return _dec

View File

@ -1,4 +1,4 @@
from functools import wraps from functools import wraps, update_wrapper
from unittest import TestCase from unittest import TestCase
import warnings import warnings
@ -174,6 +174,16 @@ def myattr2_dec(func):
myattr2_dec_m = method_decorator(myattr2_dec) myattr2_dec_m = method_decorator(myattr2_dec)
class ClsDec(object):
def __init__(self, myattr):
self.myattr = myattr
def __call__(self, f):
def wrapped():
return f() and self.myattr
return update_wrapper(wrapped, f)
class MethodDecoratorTests(TestCase): class MethodDecoratorTests(TestCase):
""" """
@ -214,6 +224,16 @@ class MethodDecoratorTests(TestCase):
self.assertEqual(Test.method.__doc__, 'A method') self.assertEqual(Test.method.__doc__, 'A method')
self.assertEqual(Test.method.__name__, 'method') self.assertEqual(Test.method.__name__, 'method')
# Test for argumented decorator
def test_argumented(self):
class Test(object):
@method_decorator(ClsDec(False))
def method(self):
return True
# t = Test()
self.assertEqual(Test().method(), False)
class XFrameOptionsDecoratorsTests(TestCase): class XFrameOptionsDecoratorsTests(TestCase):
""" """