diff options
author | Christian Heimes <christian@cheimes.de> | 2008-02-23 15:01:05 (GMT) |
---|---|---|
committer | Christian Heimes <christian@cheimes.de> | 2008-02-23 15:01:05 (GMT) |
commit | 5224d28d38eb784f17c2fed3f48368285df6d17a (patch) | |
tree | b258202efc39ae5239d91dcf5b1898c6ce713f16 /Lib | |
parent | b12f0b581a6f268d0611c87012d1273aeca220b8 (diff) | |
download | cpython-5224d28d38eb784f17c2fed3f48368285df6d17a.zip cpython-5224d28d38eb784f17c2fed3f48368285df6d17a.tar.gz cpython-5224d28d38eb784f17c2fed3f48368285df6d17a.tar.bz2 |
Patch #1759: Backport of PEP 3129 class decorators
with some help from Georg
Diffstat (limited to 'Lib')
-rwxr-xr-x | Lib/symbol.py | 159 | ||||
-rw-r--r-- | Lib/test/test_ast.py | 2 | ||||
-rw-r--r-- | Lib/test/test_decorators.py | 36 | ||||
-rw-r--r-- | Lib/test/test_grammar.py | 10 |
4 files changed, 127 insertions, 80 deletions
diff --git a/Lib/symbol.py b/Lib/symbol.py index c650138..f25907b 100755 --- a/Lib/symbol.py +++ b/Lib/symbol.py @@ -15,85 +15,86 @@ file_input = 257 eval_input = 258 decorator = 259 decorators = 260 -funcdef = 261 -parameters = 262 -varargslist = 263 -fpdef = 264 -fplist = 265 -stmt = 266 -simple_stmt = 267 -small_stmt = 268 -expr_stmt = 269 -augassign = 270 -print_stmt = 271 -del_stmt = 272 -pass_stmt = 273 -flow_stmt = 274 -break_stmt = 275 -continue_stmt = 276 -return_stmt = 277 -yield_stmt = 278 -raise_stmt = 279 -import_stmt = 280 -import_name = 281 -import_from = 282 -import_as_name = 283 -dotted_as_name = 284 -import_as_names = 285 -dotted_as_names = 286 -dotted_name = 287 -global_stmt = 288 -exec_stmt = 289 -assert_stmt = 290 -compound_stmt = 291 -if_stmt = 292 -while_stmt = 293 -for_stmt = 294 -try_stmt = 295 -with_stmt = 296 -with_var = 297 -except_clause = 298 -suite = 299 -testlist_safe = 300 -old_test = 301 -old_lambdef = 302 -test = 303 -or_test = 304 -and_test = 305 -not_test = 306 -comparison = 307 -comp_op = 308 -expr = 309 -xor_expr = 310 -and_expr = 311 -shift_expr = 312 -arith_expr = 313 -term = 314 -factor = 315 -power = 316 -atom = 317 -listmaker = 318 -testlist_gexp = 319 -lambdef = 320 -trailer = 321 -subscriptlist = 322 -subscript = 323 -sliceop = 324 -exprlist = 325 -testlist = 326 -dictmaker = 327 -classdef = 328 -arglist = 329 -argument = 330 -list_iter = 331 -list_for = 332 -list_if = 333 -gen_iter = 334 -gen_for = 335 -gen_if = 336 -testlist1 = 337 -encoding_decl = 338 -yield_expr = 339 +decorated = 261 +funcdef = 262 +parameters = 263 +varargslist = 264 +fpdef = 265 +fplist = 266 +stmt = 267 +simple_stmt = 268 +small_stmt = 269 +expr_stmt = 270 +augassign = 271 +print_stmt = 272 +del_stmt = 273 +pass_stmt = 274 +flow_stmt = 275 +break_stmt = 276 +continue_stmt = 277 +return_stmt = 278 +yield_stmt = 279 +raise_stmt = 280 +import_stmt = 281 +import_name = 282 +import_from = 283 +import_as_name = 284 +dotted_as_name = 285 +import_as_names = 286 +dotted_as_names = 287 +dotted_name = 288 +global_stmt = 289 +exec_stmt = 290 +assert_stmt = 291 +compound_stmt = 292 +if_stmt = 293 +while_stmt = 294 +for_stmt = 295 +try_stmt = 296 +with_stmt = 297 +with_var = 298 +except_clause = 299 +suite = 300 +testlist_safe = 301 +old_test = 302 +old_lambdef = 303 +test = 304 +or_test = 305 +and_test = 306 +not_test = 307 +comparison = 308 +comp_op = 309 +expr = 310 +xor_expr = 311 +and_expr = 312 +shift_expr = 313 +arith_expr = 314 +term = 315 +factor = 316 +power = 317 +atom = 318 +listmaker = 319 +testlist_gexp = 320 +lambdef = 321 +trailer = 322 +subscriptlist = 323 +subscript = 324 +sliceop = 325 +exprlist = 326 +testlist = 327 +dictmaker = 328 +classdef = 329 +arglist = 330 +argument = 331 +list_iter = 332 +list_for = 333 +list_if = 334 +gen_iter = 335 +gen_for = 336 +gen_if = 337 +testlist1 = 338 +encoding_decl = 339 +yield_expr = 340 #--end constants-- sym_name = {} 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 @@ def run_tests(): #### 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 @@ class TestDecorators(unittest.TestCase): 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 @@ hello world 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 |