Patch #1759: Backport of PEP 3129 class decorators
with some help from Georg
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index 14fc010..03dd5b5 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -156,7 +156,7 @@
#### EVERYTHING BELOW IS GENERATED #####
exec_results = [
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, []), [('Pass', (1, 9))], [])]),
-('Module', [('ClassDef', (1, 0), 'C', [], [('Pass', (1, 8))])]),
+('Module', [('ClassDef', (1, 0), 'C', [], [('Pass', (1, 8))], [])]),
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, []), [('Return', (1, 8), ('Num', (1, 15), 1))], [])]),
('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]),
('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]),
diff --git a/Lib/test/test_decorators.py b/Lib/test/test_decorators.py
index 56aa5e1..cf2bc30 100644
--- a/Lib/test/test_decorators.py
+++ b/Lib/test/test_decorators.py
@@ -266,8 +266,44 @@
self.assertEqual(bar(), 42)
self.assertEqual(actions, expected_actions)
+class TestClassDecorators(unittest.TestCase):
+
+ def test_simple(self):
+ def plain(x):
+ x.extra = 'Hello'
+ return x
+ @plain
+ class C(object): pass
+ self.assertEqual(C.extra, 'Hello')
+
+ def test_double(self):
+ def ten(x):
+ x.extra = 10
+ return x
+ def add_five(x):
+ x.extra += 5
+ return x
+
+ @add_five
+ @ten
+ class C(object): pass
+ self.assertEqual(C.extra, 15)
+
+ def test_order(self):
+ def applied_first(x):
+ x.extra = 'first'
+ return x
+ def applied_second(x):
+ x.extra = 'second'
+ return x
+ @applied_second
+ @applied_first
+ class C(object): pass
+ self.assertEqual(C.extra, 'second')
+
def test_main():
test_support.run_unittest(TestDecorators)
+ test_support.run_unittest(TestClassDecorators)
if __name__=="__main__":
test_main()
diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py
index d4bcfda..e2bb3eb 100644
--- a/Lib/test/test_grammar.py
+++ b/Lib/test/test_grammar.py
@@ -779,6 +779,16 @@
def meth1(self): pass
def meth2(self, arg): pass
def meth3(self, a1, a2): pass
+ # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
+ # decorators: decorator+
+ # decorated: decorators (classdef | funcdef)
+ def class_decorator(x):
+ x.decorated = True
+ return x
+ @class_decorator
+ class G:
+ pass
+ self.assertEqual(G.decorated, True)
def testListcomps(self):
# list comprehension tests