mirror of
https://github.com/python/cpython.git
synced 2025-07-28 13:44:43 +00:00
Patch #1759: Backport of PEP 3129 class decorators
with some help from Georg
This commit is contained in:
parent
b12f0b581a
commit
5224d28d38
15 changed files with 1591 additions and 1465 deletions
|
@ -33,7 +33,8 @@ eval_input: testlist NEWLINE* ENDMARKER
|
||||||
|
|
||||||
decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
|
decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
|
||||||
decorators: decorator+
|
decorators: decorator+
|
||||||
funcdef: [decorators] 'def' NAME parameters ':' suite
|
decorated: decorators (classdef | funcdef)
|
||||||
|
funcdef: 'def' NAME parameters ':' suite
|
||||||
parameters: '(' [varargslist] ')'
|
parameters: '(' [varargslist] ')'
|
||||||
varargslist: ((fpdef ['=' test] ',')*
|
varargslist: ((fpdef ['=' test] ',')*
|
||||||
('*' NAME [',' '**' NAME] | '**' NAME) |
|
('*' NAME [',' '**' NAME] | '**' NAME) |
|
||||||
|
@ -73,7 +74,7 @@ global_stmt: 'global' NAME (',' NAME)*
|
||||||
exec_stmt: 'exec' expr ['in' test [',' test]]
|
exec_stmt: 'exec' expr ['in' test [',' test]]
|
||||||
assert_stmt: 'assert' test [',' test]
|
assert_stmt: 'assert' test [',' test]
|
||||||
|
|
||||||
compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef
|
compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
|
||||||
if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
|
if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
|
||||||
while_stmt: 'while' test ':' suite ['else' ':' suite]
|
while_stmt: 'while' test ':' suite ['else' ':' suite]
|
||||||
for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
|
for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
|
||||||
|
|
|
@ -73,13 +73,14 @@ struct _stmt {
|
||||||
identifier name;
|
identifier name;
|
||||||
arguments_ty args;
|
arguments_ty args;
|
||||||
asdl_seq *body;
|
asdl_seq *body;
|
||||||
asdl_seq *decorators;
|
asdl_seq *decorator_list;
|
||||||
} FunctionDef;
|
} FunctionDef;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
identifier name;
|
identifier name;
|
||||||
asdl_seq *bases;
|
asdl_seq *bases;
|
||||||
asdl_seq *body;
|
asdl_seq *body;
|
||||||
|
asdl_seq *decorator_list;
|
||||||
} ClassDef;
|
} ClassDef;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
|
@ -359,11 +360,12 @@ mod_ty _Py_Expression(expr_ty body, PyArena *arena);
|
||||||
mod_ty _Py_Suite(asdl_seq * body, PyArena *arena);
|
mod_ty _Py_Suite(asdl_seq * body, PyArena *arena);
|
||||||
#define FunctionDef(a0, a1, a2, a3, a4, a5, a6) _Py_FunctionDef(a0, a1, a2, a3, a4, a5, a6)
|
#define FunctionDef(a0, a1, a2, a3, a4, a5, a6) _Py_FunctionDef(a0, a1, a2, a3, a4, a5, a6)
|
||||||
stmt_ty _Py_FunctionDef(identifier name, arguments_ty args, asdl_seq * body,
|
stmt_ty _Py_FunctionDef(identifier name, arguments_ty args, asdl_seq * body,
|
||||||
asdl_seq * decorators, int lineno, int col_offset,
|
asdl_seq * decorator_list, int lineno, int col_offset,
|
||||||
PyArena *arena);
|
PyArena *arena);
|
||||||
#define ClassDef(a0, a1, a2, a3, a4, a5) _Py_ClassDef(a0, a1, a2, a3, a4, a5)
|
#define ClassDef(a0, a1, a2, a3, a4, a5, a6) _Py_ClassDef(a0, a1, a2, a3, a4, a5, a6)
|
||||||
stmt_ty _Py_ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, int
|
stmt_ty _Py_ClassDef(identifier name, asdl_seq * bases, asdl_seq * body,
|
||||||
lineno, int col_offset, PyArena *arena);
|
asdl_seq * decorator_list, int lineno, int col_offset,
|
||||||
|
PyArena *arena);
|
||||||
#define Return(a0, a1, a2, a3) _Py_Return(a0, a1, a2, a3)
|
#define Return(a0, a1, a2, a3) _Py_Return(a0, a1, a2, a3)
|
||||||
stmt_ty _Py_Return(expr_ty value, int lineno, int col_offset, PyArena *arena);
|
stmt_ty _Py_Return(expr_ty value, int lineno, int col_offset, PyArena *arena);
|
||||||
#define Delete(a0, a1, a2, a3) _Py_Delete(a0, a1, a2, a3)
|
#define Delete(a0, a1, a2, a3) _Py_Delete(a0, a1, a2, a3)
|
||||||
|
|
|
@ -3,82 +3,83 @@
|
||||||
#define eval_input 258
|
#define eval_input 258
|
||||||
#define decorator 259
|
#define decorator 259
|
||||||
#define decorators 260
|
#define decorators 260
|
||||||
#define funcdef 261
|
#define decorated 261
|
||||||
#define parameters 262
|
#define funcdef 262
|
||||||
#define varargslist 263
|
#define parameters 263
|
||||||
#define fpdef 264
|
#define varargslist 264
|
||||||
#define fplist 265
|
#define fpdef 265
|
||||||
#define stmt 266
|
#define fplist 266
|
||||||
#define simple_stmt 267
|
#define stmt 267
|
||||||
#define small_stmt 268
|
#define simple_stmt 268
|
||||||
#define expr_stmt 269
|
#define small_stmt 269
|
||||||
#define augassign 270
|
#define expr_stmt 270
|
||||||
#define print_stmt 271
|
#define augassign 271
|
||||||
#define del_stmt 272
|
#define print_stmt 272
|
||||||
#define pass_stmt 273
|
#define del_stmt 273
|
||||||
#define flow_stmt 274
|
#define pass_stmt 274
|
||||||
#define break_stmt 275
|
#define flow_stmt 275
|
||||||
#define continue_stmt 276
|
#define break_stmt 276
|
||||||
#define return_stmt 277
|
#define continue_stmt 277
|
||||||
#define yield_stmt 278
|
#define return_stmt 278
|
||||||
#define raise_stmt 279
|
#define yield_stmt 279
|
||||||
#define import_stmt 280
|
#define raise_stmt 280
|
||||||
#define import_name 281
|
#define import_stmt 281
|
||||||
#define import_from 282
|
#define import_name 282
|
||||||
#define import_as_name 283
|
#define import_from 283
|
||||||
#define dotted_as_name 284
|
#define import_as_name 284
|
||||||
#define import_as_names 285
|
#define dotted_as_name 285
|
||||||
#define dotted_as_names 286
|
#define import_as_names 286
|
||||||
#define dotted_name 287
|
#define dotted_as_names 287
|
||||||
#define global_stmt 288
|
#define dotted_name 288
|
||||||
#define exec_stmt 289
|
#define global_stmt 289
|
||||||
#define assert_stmt 290
|
#define exec_stmt 290
|
||||||
#define compound_stmt 291
|
#define assert_stmt 291
|
||||||
#define if_stmt 292
|
#define compound_stmt 292
|
||||||
#define while_stmt 293
|
#define if_stmt 293
|
||||||
#define for_stmt 294
|
#define while_stmt 294
|
||||||
#define try_stmt 295
|
#define for_stmt 295
|
||||||
#define with_stmt 296
|
#define try_stmt 296
|
||||||
#define with_var 297
|
#define with_stmt 297
|
||||||
#define except_clause 298
|
#define with_var 298
|
||||||
#define suite 299
|
#define except_clause 299
|
||||||
#define testlist_safe 300
|
#define suite 300
|
||||||
#define old_test 301
|
#define testlist_safe 301
|
||||||
#define old_lambdef 302
|
#define old_test 302
|
||||||
#define test 303
|
#define old_lambdef 303
|
||||||
#define or_test 304
|
#define test 304
|
||||||
#define and_test 305
|
#define or_test 305
|
||||||
#define not_test 306
|
#define and_test 306
|
||||||
#define comparison 307
|
#define not_test 307
|
||||||
#define comp_op 308
|
#define comparison 308
|
||||||
#define expr 309
|
#define comp_op 309
|
||||||
#define xor_expr 310
|
#define expr 310
|
||||||
#define and_expr 311
|
#define xor_expr 311
|
||||||
#define shift_expr 312
|
#define and_expr 312
|
||||||
#define arith_expr 313
|
#define shift_expr 313
|
||||||
#define term 314
|
#define arith_expr 314
|
||||||
#define factor 315
|
#define term 315
|
||||||
#define power 316
|
#define factor 316
|
||||||
#define atom 317
|
#define power 317
|
||||||
#define listmaker 318
|
#define atom 318
|
||||||
#define testlist_gexp 319
|
#define listmaker 319
|
||||||
#define lambdef 320
|
#define testlist_gexp 320
|
||||||
#define trailer 321
|
#define lambdef 321
|
||||||
#define subscriptlist 322
|
#define trailer 322
|
||||||
#define subscript 323
|
#define subscriptlist 323
|
||||||
#define sliceop 324
|
#define subscript 324
|
||||||
#define exprlist 325
|
#define sliceop 325
|
||||||
#define testlist 326
|
#define exprlist 326
|
||||||
#define dictmaker 327
|
#define testlist 327
|
||||||
#define classdef 328
|
#define dictmaker 328
|
||||||
#define arglist 329
|
#define classdef 329
|
||||||
#define argument 330
|
#define arglist 330
|
||||||
#define list_iter 331
|
#define argument 331
|
||||||
#define list_for 332
|
#define list_iter 332
|
||||||
#define list_if 333
|
#define list_for 333
|
||||||
#define gen_iter 334
|
#define list_if 334
|
||||||
#define gen_for 335
|
#define gen_iter 335
|
||||||
#define gen_if 336
|
#define gen_for 336
|
||||||
#define testlist1 337
|
#define gen_if 337
|
||||||
#define encoding_decl 338
|
#define testlist1 338
|
||||||
#define yield_expr 339
|
#define encoding_decl 339
|
||||||
|
#define yield_expr 340
|
||||||
|
|
159
Lib/symbol.py
159
Lib/symbol.py
|
@ -15,85 +15,86 @@ file_input = 257
|
||||||
eval_input = 258
|
eval_input = 258
|
||||||
decorator = 259
|
decorator = 259
|
||||||
decorators = 260
|
decorators = 260
|
||||||
funcdef = 261
|
decorated = 261
|
||||||
parameters = 262
|
funcdef = 262
|
||||||
varargslist = 263
|
parameters = 263
|
||||||
fpdef = 264
|
varargslist = 264
|
||||||
fplist = 265
|
fpdef = 265
|
||||||
stmt = 266
|
fplist = 266
|
||||||
simple_stmt = 267
|
stmt = 267
|
||||||
small_stmt = 268
|
simple_stmt = 268
|
||||||
expr_stmt = 269
|
small_stmt = 269
|
||||||
augassign = 270
|
expr_stmt = 270
|
||||||
print_stmt = 271
|
augassign = 271
|
||||||
del_stmt = 272
|
print_stmt = 272
|
||||||
pass_stmt = 273
|
del_stmt = 273
|
||||||
flow_stmt = 274
|
pass_stmt = 274
|
||||||
break_stmt = 275
|
flow_stmt = 275
|
||||||
continue_stmt = 276
|
break_stmt = 276
|
||||||
return_stmt = 277
|
continue_stmt = 277
|
||||||
yield_stmt = 278
|
return_stmt = 278
|
||||||
raise_stmt = 279
|
yield_stmt = 279
|
||||||
import_stmt = 280
|
raise_stmt = 280
|
||||||
import_name = 281
|
import_stmt = 281
|
||||||
import_from = 282
|
import_name = 282
|
||||||
import_as_name = 283
|
import_from = 283
|
||||||
dotted_as_name = 284
|
import_as_name = 284
|
||||||
import_as_names = 285
|
dotted_as_name = 285
|
||||||
dotted_as_names = 286
|
import_as_names = 286
|
||||||
dotted_name = 287
|
dotted_as_names = 287
|
||||||
global_stmt = 288
|
dotted_name = 288
|
||||||
exec_stmt = 289
|
global_stmt = 289
|
||||||
assert_stmt = 290
|
exec_stmt = 290
|
||||||
compound_stmt = 291
|
assert_stmt = 291
|
||||||
if_stmt = 292
|
compound_stmt = 292
|
||||||
while_stmt = 293
|
if_stmt = 293
|
||||||
for_stmt = 294
|
while_stmt = 294
|
||||||
try_stmt = 295
|
for_stmt = 295
|
||||||
with_stmt = 296
|
try_stmt = 296
|
||||||
with_var = 297
|
with_stmt = 297
|
||||||
except_clause = 298
|
with_var = 298
|
||||||
suite = 299
|
except_clause = 299
|
||||||
testlist_safe = 300
|
suite = 300
|
||||||
old_test = 301
|
testlist_safe = 301
|
||||||
old_lambdef = 302
|
old_test = 302
|
||||||
test = 303
|
old_lambdef = 303
|
||||||
or_test = 304
|
test = 304
|
||||||
and_test = 305
|
or_test = 305
|
||||||
not_test = 306
|
and_test = 306
|
||||||
comparison = 307
|
not_test = 307
|
||||||
comp_op = 308
|
comparison = 308
|
||||||
expr = 309
|
comp_op = 309
|
||||||
xor_expr = 310
|
expr = 310
|
||||||
and_expr = 311
|
xor_expr = 311
|
||||||
shift_expr = 312
|
and_expr = 312
|
||||||
arith_expr = 313
|
shift_expr = 313
|
||||||
term = 314
|
arith_expr = 314
|
||||||
factor = 315
|
term = 315
|
||||||
power = 316
|
factor = 316
|
||||||
atom = 317
|
power = 317
|
||||||
listmaker = 318
|
atom = 318
|
||||||
testlist_gexp = 319
|
listmaker = 319
|
||||||
lambdef = 320
|
testlist_gexp = 320
|
||||||
trailer = 321
|
lambdef = 321
|
||||||
subscriptlist = 322
|
trailer = 322
|
||||||
subscript = 323
|
subscriptlist = 323
|
||||||
sliceop = 324
|
subscript = 324
|
||||||
exprlist = 325
|
sliceop = 325
|
||||||
testlist = 326
|
exprlist = 326
|
||||||
dictmaker = 327
|
testlist = 327
|
||||||
classdef = 328
|
dictmaker = 328
|
||||||
arglist = 329
|
classdef = 329
|
||||||
argument = 330
|
arglist = 330
|
||||||
list_iter = 331
|
argument = 331
|
||||||
list_for = 332
|
list_iter = 332
|
||||||
list_if = 333
|
list_for = 333
|
||||||
gen_iter = 334
|
list_if = 334
|
||||||
gen_for = 335
|
gen_iter = 335
|
||||||
gen_if = 336
|
gen_for = 336
|
||||||
testlist1 = 337
|
gen_if = 337
|
||||||
encoding_decl = 338
|
testlist1 = 338
|
||||||
yield_expr = 339
|
encoding_decl = 339
|
||||||
|
yield_expr = 340
|
||||||
#--end constants--
|
#--end constants--
|
||||||
|
|
||||||
sym_name = {}
|
sym_name = {}
|
||||||
|
|
|
@ -156,7 +156,7 @@ def run_tests():
|
||||||
#### EVERYTHING BELOW IS GENERATED #####
|
#### EVERYTHING BELOW IS GENERATED #####
|
||||||
exec_results = [
|
exec_results = [
|
||||||
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, []), [('Pass', (1, 9))], [])]),
|
('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', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, []), [('Return', (1, 8), ('Num', (1, 15), 1))], [])]),
|
||||||
('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]),
|
('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]),
|
||||||
('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]),
|
('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]),
|
||||||
|
|
|
@ -266,8 +266,44 @@ class TestDecorators(unittest.TestCase):
|
||||||
self.assertEqual(bar(), 42)
|
self.assertEqual(bar(), 42)
|
||||||
self.assertEqual(actions, expected_actions)
|
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():
|
def test_main():
|
||||||
test_support.run_unittest(TestDecorators)
|
test_support.run_unittest(TestDecorators)
|
||||||
|
test_support.run_unittest(TestClassDecorators)
|
||||||
|
|
||||||
if __name__=="__main__":
|
if __name__=="__main__":
|
||||||
test_main()
|
test_main()
|
||||||
|
|
|
@ -779,6 +779,16 @@ hello world
|
||||||
def meth1(self): pass
|
def meth1(self): pass
|
||||||
def meth2(self, arg): pass
|
def meth2(self, arg): pass
|
||||||
def meth3(self, a1, a2): 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):
|
def testListcomps(self):
|
||||||
# list comprehension tests
|
# list comprehension tests
|
||||||
|
|
|
@ -12,6 +12,8 @@ What's New in Python 2.6 alpha 1?
|
||||||
Core and builtins
|
Core and builtins
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
- Patch #1759: Backport of PEP 3129 class decorators
|
||||||
|
|
||||||
- Issue #1881: An internal parser limit has been increased. Also see
|
- Issue #1881: An internal parser limit has been increased. Also see
|
||||||
issue 215555 for a discussion.
|
issue 215555 for a discussion.
|
||||||
|
|
||||||
|
|
|
@ -1498,7 +1498,7 @@ validate_small_stmt(node *tree)
|
||||||
|
|
||||||
|
|
||||||
/* compound_stmt:
|
/* compound_stmt:
|
||||||
* if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
|
* if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef | decorated
|
||||||
*/
|
*/
|
||||||
static int
|
static int
|
||||||
validate_compound_stmt(node *tree)
|
validate_compound_stmt(node *tree)
|
||||||
|
@ -1517,7 +1517,8 @@ validate_compound_stmt(node *tree)
|
||||||
|| (ntype == for_stmt)
|
|| (ntype == for_stmt)
|
||||||
|| (ntype == try_stmt)
|
|| (ntype == try_stmt)
|
||||||
|| (ntype == funcdef)
|
|| (ntype == funcdef)
|
||||||
|| (ntype == classdef))
|
|| (ntype == classdef)
|
||||||
|
|| (ntype == decorated))
|
||||||
res = validate_node(tree);
|
res = validate_node(tree);
|
||||||
else {
|
else {
|
||||||
res = 0;
|
res = 0;
|
||||||
|
@ -1527,7 +1528,6 @@ validate_compound_stmt(node *tree)
|
||||||
return (res);
|
return (res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
validate_yield_or_testlist(node *tree)
|
validate_yield_or_testlist(node *tree)
|
||||||
{
|
{
|
||||||
|
@ -2558,28 +2558,40 @@ validate_decorators(node *tree)
|
||||||
|
|
||||||
/* funcdef:
|
/* funcdef:
|
||||||
*
|
*
|
||||||
* -6 -5 -4 -3 -2 -1
|
* -5 -4 -3 -2 -1
|
||||||
* [decorators] 'def' NAME parameters ':' suite
|
* 'def' NAME parameters ':' suite
|
||||||
*/
|
*/
|
||||||
static int
|
static int
|
||||||
validate_funcdef(node *tree)
|
validate_funcdef(node *tree)
|
||||||
{
|
{
|
||||||
int nch = NCH(tree);
|
int nch = NCH(tree);
|
||||||
int ok = (validate_ntype(tree, funcdef)
|
int ok = (validate_ntype(tree, funcdef)
|
||||||
&& ((nch == 5) || (nch == 6))
|
&& (nch == 5)
|
||||||
&& validate_name(RCHILD(tree, -5), "def")
|
&& validate_name(RCHILD(tree, -5), "def")
|
||||||
&& validate_ntype(RCHILD(tree, -4), NAME)
|
&& validate_ntype(RCHILD(tree, -4), NAME)
|
||||||
&& validate_colon(RCHILD(tree, -2))
|
&& validate_colon(RCHILD(tree, -2))
|
||||||
&& validate_parameters(RCHILD(tree, -3))
|
&& validate_parameters(RCHILD(tree, -3))
|
||||||
&& validate_suite(RCHILD(tree, -1)));
|
&& validate_suite(RCHILD(tree, -1)));
|
||||||
|
|
||||||
if (ok && (nch == 6))
|
|
||||||
ok = validate_decorators(CHILD(tree, 0));
|
|
||||||
|
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* decorated
|
||||||
|
* decorators (classdef | funcdef)
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
validate_decorated(node *tree)
|
||||||
|
{
|
||||||
|
int nch = NCH(tree);
|
||||||
|
int ok = (validate_ntype(tree, decorated)
|
||||||
|
&& (nch == 2)
|
||||||
|
&& validate_decorators(RCHILD(tree, -2))
|
||||||
|
&& (validate_funcdef(RCHILD(tree, -1))
|
||||||
|
|| validate_class(RCHILD(tree, -1)))
|
||||||
|
);
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
validate_lambdef(node *tree)
|
validate_lambdef(node *tree)
|
||||||
{
|
{
|
||||||
|
@ -2923,6 +2935,9 @@ validate_node(node *tree)
|
||||||
case classdef:
|
case classdef:
|
||||||
res = validate_class(tree);
|
res = validate_class(tree);
|
||||||
break;
|
break;
|
||||||
|
case decorated:
|
||||||
|
res = validate_decorated(tree);
|
||||||
|
break;
|
||||||
/*
|
/*
|
||||||
* "Trivial" parse tree nodes.
|
* "Trivial" parse tree nodes.
|
||||||
* (Why did I call these trivial?)
|
* (Why did I call these trivial?)
|
||||||
|
|
|
@ -10,8 +10,8 @@ module Python version "$Revision$"
|
||||||
| Suite(stmt* body)
|
| Suite(stmt* body)
|
||||||
|
|
||||||
stmt = FunctionDef(identifier name, arguments args,
|
stmt = FunctionDef(identifier name, arguments args,
|
||||||
stmt* body, expr* decorators)
|
stmt* body, expr* decorator_list)
|
||||||
| ClassDef(identifier name, expr* bases, stmt* body)
|
| ClassDef(identifier name, expr* bases, stmt* body, expr *decorator_list)
|
||||||
| Return(expr? value)
|
| Return(expr? value)
|
||||||
|
|
||||||
| Delete(expr* targets)
|
| Delete(expr* targets)
|
||||||
|
|
|
@ -42,13 +42,14 @@ static char *FunctionDef_fields[]={
|
||||||
"name",
|
"name",
|
||||||
"args",
|
"args",
|
||||||
"body",
|
"body",
|
||||||
"decorators",
|
"decorator_list",
|
||||||
};
|
};
|
||||||
static PyTypeObject *ClassDef_type;
|
static PyTypeObject *ClassDef_type;
|
||||||
static char *ClassDef_fields[]={
|
static char *ClassDef_fields[]={
|
||||||
"name",
|
"name",
|
||||||
"bases",
|
"bases",
|
||||||
"body",
|
"body",
|
||||||
|
"decorator_list",
|
||||||
};
|
};
|
||||||
static PyTypeObject *Return_type;
|
static PyTypeObject *Return_type;
|
||||||
static char *Return_fields[]={
|
static char *Return_fields[]={
|
||||||
|
@ -469,7 +470,7 @@ static int init_types(void)
|
||||||
FunctionDef_type = make_type("FunctionDef", stmt_type,
|
FunctionDef_type = make_type("FunctionDef", stmt_type,
|
||||||
FunctionDef_fields, 4);
|
FunctionDef_fields, 4);
|
||||||
if (!FunctionDef_type) return 0;
|
if (!FunctionDef_type) return 0;
|
||||||
ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 3);
|
ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 4);
|
||||||
if (!ClassDef_type) return 0;
|
if (!ClassDef_type) return 0;
|
||||||
Return_type = make_type("Return", stmt_type, Return_fields, 1);
|
Return_type = make_type("Return", stmt_type, Return_fields, 1);
|
||||||
if (!Return_type) return 0;
|
if (!Return_type) return 0;
|
||||||
|
@ -790,7 +791,7 @@ Suite(asdl_seq * body, PyArena *arena)
|
||||||
|
|
||||||
stmt_ty
|
stmt_ty
|
||||||
FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
|
FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
|
||||||
decorators, int lineno, int col_offset, PyArena *arena)
|
decorator_list, int lineno, int col_offset, PyArena *arena)
|
||||||
{
|
{
|
||||||
stmt_ty p;
|
stmt_ty p;
|
||||||
if (!name) {
|
if (!name) {
|
||||||
|
@ -810,15 +811,15 @@ FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
|
||||||
p->v.FunctionDef.name = name;
|
p->v.FunctionDef.name = name;
|
||||||
p->v.FunctionDef.args = args;
|
p->v.FunctionDef.args = args;
|
||||||
p->v.FunctionDef.body = body;
|
p->v.FunctionDef.body = body;
|
||||||
p->v.FunctionDef.decorators = decorators;
|
p->v.FunctionDef.decorator_list = decorator_list;
|
||||||
p->lineno = lineno;
|
p->lineno = lineno;
|
||||||
p->col_offset = col_offset;
|
p->col_offset = col_offset;
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
stmt_ty
|
stmt_ty
|
||||||
ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, int lineno, int
|
ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, asdl_seq *
|
||||||
col_offset, PyArena *arena)
|
decorator_list, int lineno, int col_offset, PyArena *arena)
|
||||||
{
|
{
|
||||||
stmt_ty p;
|
stmt_ty p;
|
||||||
if (!name) {
|
if (!name) {
|
||||||
|
@ -833,6 +834,7 @@ ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, int lineno, int
|
||||||
p->v.ClassDef.name = name;
|
p->v.ClassDef.name = name;
|
||||||
p->v.ClassDef.bases = bases;
|
p->v.ClassDef.bases = bases;
|
||||||
p->v.ClassDef.body = body;
|
p->v.ClassDef.body = body;
|
||||||
|
p->v.ClassDef.decorator_list = decorator_list;
|
||||||
p->lineno = lineno;
|
p->lineno = lineno;
|
||||||
p->col_offset = col_offset;
|
p->col_offset = col_offset;
|
||||||
return p;
|
return p;
|
||||||
|
@ -1906,9 +1908,11 @@ ast2obj_stmt(void* _o)
|
||||||
if (PyObject_SetAttrString(result, "body", value) == -1)
|
if (PyObject_SetAttrString(result, "body", value) == -1)
|
||||||
goto failed;
|
goto failed;
|
||||||
Py_DECREF(value);
|
Py_DECREF(value);
|
||||||
value = ast2obj_list(o->v.FunctionDef.decorators, ast2obj_expr);
|
value = ast2obj_list(o->v.FunctionDef.decorator_list,
|
||||||
|
ast2obj_expr);
|
||||||
if (!value) goto failed;
|
if (!value) goto failed;
|
||||||
if (PyObject_SetAttrString(result, "decorators", value) == -1)
|
if (PyObject_SetAttrString(result, "decorator_list", value) ==
|
||||||
|
-1)
|
||||||
goto failed;
|
goto failed;
|
||||||
Py_DECREF(value);
|
Py_DECREF(value);
|
||||||
break;
|
break;
|
||||||
|
@ -1930,6 +1934,13 @@ ast2obj_stmt(void* _o)
|
||||||
if (PyObject_SetAttrString(result, "body", value) == -1)
|
if (PyObject_SetAttrString(result, "body", value) == -1)
|
||||||
goto failed;
|
goto failed;
|
||||||
Py_DECREF(value);
|
Py_DECREF(value);
|
||||||
|
value = ast2obj_list(o->v.ClassDef.decorator_list,
|
||||||
|
ast2obj_expr);
|
||||||
|
if (!value) goto failed;
|
||||||
|
if (PyObject_SetAttrString(result, "decorator_list", value) ==
|
||||||
|
-1)
|
||||||
|
goto failed;
|
||||||
|
Py_DECREF(value);
|
||||||
break;
|
break;
|
||||||
case Return_kind:
|
case Return_kind:
|
||||||
result = PyType_GenericNew(Return_type, NULL, NULL);
|
result = PyType_GenericNew(Return_type, NULL, NULL);
|
||||||
|
|
70
Python/ast.c
70
Python/ast.c
|
@ -29,6 +29,7 @@ static asdl_seq *ast_for_suite(struct compiling *, const node *);
|
||||||
static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
|
static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
|
||||||
expr_context_ty);
|
expr_context_ty);
|
||||||
static expr_ty ast_for_testlist(struct compiling *, const node *);
|
static expr_ty ast_for_testlist(struct compiling *, const node *);
|
||||||
|
static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
|
||||||
static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
|
static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
|
||||||
|
|
||||||
/* Note different signature for ast_for_call */
|
/* Note different signature for ast_for_call */
|
||||||
|
@ -828,27 +829,16 @@ ast_for_decorators(struct compiling *c, const node *n)
|
||||||
}
|
}
|
||||||
|
|
||||||
static stmt_ty
|
static stmt_ty
|
||||||
ast_for_funcdef(struct compiling *c, const node *n)
|
ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
|
||||||
{
|
{
|
||||||
/* funcdef: 'def' [decorators] NAME parameters ':' suite */
|
/* funcdef: 'def' NAME parameters ':' suite */
|
||||||
identifier name;
|
identifier name;
|
||||||
arguments_ty args;
|
arguments_ty args;
|
||||||
asdl_seq *body;
|
asdl_seq *body;
|
||||||
asdl_seq *decorator_seq = NULL;
|
int name_i = 1;
|
||||||
int name_i;
|
|
||||||
|
|
||||||
REQ(n, funcdef);
|
REQ(n, funcdef);
|
||||||
|
|
||||||
if (NCH(n) == 6) { /* decorators are present */
|
|
||||||
decorator_seq = ast_for_decorators(c, CHILD(n, 0));
|
|
||||||
if (!decorator_seq)
|
|
||||||
return NULL;
|
|
||||||
name_i = 2;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
name_i = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
name = NEW_IDENTIFIER(CHILD(n, name_i));
|
name = NEW_IDENTIFIER(CHILD(n, name_i));
|
||||||
if (!name)
|
if (!name)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -867,6 +857,36 @@ ast_for_funcdef(struct compiling *c, const node *n)
|
||||||
n->n_col_offset, c->c_arena);
|
n->n_col_offset, c->c_arena);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static stmt_ty
|
||||||
|
ast_for_decorated(struct compiling *c, const node *n)
|
||||||
|
{
|
||||||
|
/* decorated: decorators (classdef | funcdef) */
|
||||||
|
stmt_ty thing = NULL;
|
||||||
|
asdl_seq *decorator_seq = NULL;
|
||||||
|
|
||||||
|
REQ(n, decorated);
|
||||||
|
|
||||||
|
decorator_seq = ast_for_decorators(c, CHILD(n, 0));
|
||||||
|
if (!decorator_seq)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
assert(TYPE(CHILD(n, 1)) == funcdef ||
|
||||||
|
TYPE(CHILD(n, 1)) == classdef);
|
||||||
|
|
||||||
|
if (TYPE(CHILD(n, 1)) == funcdef) {
|
||||||
|
thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
|
||||||
|
} else if (TYPE(CHILD(n, 1)) == classdef) {
|
||||||
|
thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
|
||||||
|
}
|
||||||
|
/* we count the decorators in when talking about the class' or
|
||||||
|
function's line number */
|
||||||
|
if (thing) {
|
||||||
|
thing->lineno = LINENO(n);
|
||||||
|
thing->col_offset = n->n_col_offset;
|
||||||
|
}
|
||||||
|
return thing;
|
||||||
|
}
|
||||||
|
|
||||||
static expr_ty
|
static expr_ty
|
||||||
ast_for_lambdef(struct compiling *c, const node *n)
|
ast_for_lambdef(struct compiling *c, const node *n)
|
||||||
{
|
{
|
||||||
|
@ -2968,7 +2988,7 @@ ast_for_with_stmt(struct compiling *c, const node *n)
|
||||||
}
|
}
|
||||||
|
|
||||||
static stmt_ty
|
static stmt_ty
|
||||||
ast_for_classdef(struct compiling *c, const node *n)
|
ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
|
||||||
{
|
{
|
||||||
/* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
|
/* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
|
||||||
asdl_seq *bases, *s;
|
asdl_seq *bases, *s;
|
||||||
|
@ -2984,16 +3004,16 @@ ast_for_classdef(struct compiling *c, const node *n)
|
||||||
s = ast_for_suite(c, CHILD(n, 3));
|
s = ast_for_suite(c, CHILD(n, 3));
|
||||||
if (!s)
|
if (!s)
|
||||||
return NULL;
|
return NULL;
|
||||||
return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
|
return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, decorator_seq,
|
||||||
n->n_col_offset, c->c_arena);
|
LINENO(n), n->n_col_offset, c->c_arena);
|
||||||
}
|
}
|
||||||
/* check for empty base list */
|
/* check for empty base list */
|
||||||
if (TYPE(CHILD(n,3)) == RPAR) {
|
if (TYPE(CHILD(n,3)) == RPAR) {
|
||||||
s = ast_for_suite(c, CHILD(n,5));
|
s = ast_for_suite(c, CHILD(n,5));
|
||||||
if (!s)
|
if (!s)
|
||||||
return NULL;
|
return NULL;
|
||||||
return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
|
return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, decorator_seq,
|
||||||
n->n_col_offset, c->c_arena);
|
LINENO(n), n->n_col_offset, c->c_arena);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* else handle the base class list */
|
/* else handle the base class list */
|
||||||
|
@ -3004,8 +3024,8 @@ ast_for_classdef(struct compiling *c, const node *n)
|
||||||
s = ast_for_suite(c, CHILD(n, 6));
|
s = ast_for_suite(c, CHILD(n, 6));
|
||||||
if (!s)
|
if (!s)
|
||||||
return NULL;
|
return NULL;
|
||||||
return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
|
return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, decorator_seq,
|
||||||
n->n_col_offset, c->c_arena);
|
LINENO(n), n->n_col_offset, c->c_arena);
|
||||||
}
|
}
|
||||||
|
|
||||||
static stmt_ty
|
static stmt_ty
|
||||||
|
@ -3054,7 +3074,7 @@ ast_for_stmt(struct compiling *c, const node *n)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
/* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
|
/* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
|
||||||
| funcdef | classdef
|
| funcdef | classdef | decorated
|
||||||
*/
|
*/
|
||||||
node *ch = CHILD(n, 0);
|
node *ch = CHILD(n, 0);
|
||||||
REQ(n, compound_stmt);
|
REQ(n, compound_stmt);
|
||||||
|
@ -3070,9 +3090,11 @@ ast_for_stmt(struct compiling *c, const node *n)
|
||||||
case with_stmt:
|
case with_stmt:
|
||||||
return ast_for_with_stmt(c, ch);
|
return ast_for_with_stmt(c, ch);
|
||||||
case funcdef:
|
case funcdef:
|
||||||
return ast_for_funcdef(c, ch);
|
return ast_for_funcdef(c, ch, NULL);
|
||||||
case classdef:
|
case classdef:
|
||||||
return ast_for_classdef(c, ch);
|
return ast_for_classdef(c, ch, NULL);
|
||||||
|
case decorated:
|
||||||
|
return ast_for_decorated(c, ch);
|
||||||
default:
|
default:
|
||||||
PyErr_Format(PyExc_SystemError,
|
PyErr_Format(PyExc_SystemError,
|
||||||
"unhandled small_stmt: TYPE=%d NCH=%d\n",
|
"unhandled small_stmt: TYPE=%d NCH=%d\n",
|
||||||
|
|
|
@ -1362,7 +1362,7 @@ compiler_function(struct compiler *c, stmt_ty s)
|
||||||
PyCodeObject *co;
|
PyCodeObject *co;
|
||||||
PyObject *first_const = Py_None;
|
PyObject *first_const = Py_None;
|
||||||
arguments_ty args = s->v.FunctionDef.args;
|
arguments_ty args = s->v.FunctionDef.args;
|
||||||
asdl_seq* decos = s->v.FunctionDef.decorators;
|
asdl_seq* decos = s->v.FunctionDef.decorator_list;
|
||||||
stmt_ty st;
|
stmt_ty st;
|
||||||
int i, n, docstring;
|
int i, n, docstring;
|
||||||
|
|
||||||
|
@ -1413,9 +1413,14 @@ compiler_function(struct compiler *c, stmt_ty s)
|
||||||
static int
|
static int
|
||||||
compiler_class(struct compiler *c, stmt_ty s)
|
compiler_class(struct compiler *c, stmt_ty s)
|
||||||
{
|
{
|
||||||
int n;
|
int n, i;
|
||||||
PyCodeObject *co;
|
PyCodeObject *co;
|
||||||
PyObject *str;
|
PyObject *str;
|
||||||
|
asdl_seq* decos = s->v.ClassDef.decorator_list;
|
||||||
|
|
||||||
|
if (!compiler_decorators(c, decos))
|
||||||
|
return 0;
|
||||||
|
|
||||||
/* push class name on stack, needed by BUILD_CLASS */
|
/* push class name on stack, needed by BUILD_CLASS */
|
||||||
ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
|
ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
|
||||||
/* push the tuple of base classes on the stack */
|
/* push the tuple of base classes on the stack */
|
||||||
|
@ -1461,6 +1466,10 @@ compiler_class(struct compiler *c, stmt_ty s)
|
||||||
|
|
||||||
ADDOP_I(c, CALL_FUNCTION, 0);
|
ADDOP_I(c, CALL_FUNCTION, 0);
|
||||||
ADDOP(c, BUILD_CLASS);
|
ADDOP(c, BUILD_CLASS);
|
||||||
|
/* apply decorators */
|
||||||
|
for (i = 0; i < asdl_seq_LEN(decos); i++) {
|
||||||
|
ADDOP_I(c, CALL_FUNCTION, 1);
|
||||||
|
}
|
||||||
if (!compiler_nameop(c, s->v.ClassDef.name, Store))
|
if (!compiler_nameop(c, s->v.ClassDef.name, Store))
|
||||||
return 0;
|
return 0;
|
||||||
return 1;
|
return 1;
|
||||||
|
|
2516
Python/graminit.c
2516
Python/graminit.c
File diff suppressed because it is too large
Load diff
|
@ -931,8 +931,8 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
|
||||||
return 0;
|
return 0;
|
||||||
if (s->v.FunctionDef.args->defaults)
|
if (s->v.FunctionDef.args->defaults)
|
||||||
VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
|
VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
|
||||||
if (s->v.FunctionDef.decorators)
|
if (s->v.FunctionDef.decorator_list)
|
||||||
VISIT_SEQ(st, expr, s->v.FunctionDef.decorators);
|
VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
|
||||||
if (!symtable_enter_block(st, s->v.FunctionDef.name,
|
if (!symtable_enter_block(st, s->v.FunctionDef.name,
|
||||||
FunctionBlock, (void *)s, s->lineno))
|
FunctionBlock, (void *)s, s->lineno))
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -946,6 +946,8 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
|
||||||
if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
|
if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
|
||||||
return 0;
|
return 0;
|
||||||
VISIT_SEQ(st, expr, s->v.ClassDef.bases);
|
VISIT_SEQ(st, expr, s->v.ClassDef.bases);
|
||||||
|
if (s->v.ClassDef.decorator_list)
|
||||||
|
VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
|
||||||
if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
|
if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
|
||||||
(void *)s, s->lineno))
|
(void *)s, s->lineno))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue