Patch #1759: Backport of PEP 3129 class decorators

with some help from Georg
This commit is contained in:
Christian Heimes 2008-02-23 15:01:05 +00:00
parent b12f0b581a
commit 5224d28d38
15 changed files with 1591 additions and 1465 deletions

View file

@ -33,7 +33,8 @@ eval_input: testlist NEWLINE* ENDMARKER
decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
decorators: decorator+
funcdef: [decorators] 'def' NAME parameters ':' suite
decorated: decorators (classdef | funcdef)
funcdef: 'def' NAME parameters ':' suite
parameters: '(' [varargslist] ')'
varargslist: ((fpdef ['=' test] ',')*
('*' NAME [',' '**' NAME] | '**' NAME) |
@ -73,7 +74,7 @@ global_stmt: 'global' NAME (',' NAME)*
exec_stmt: 'exec' expr ['in' 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]
while_stmt: 'while' test ':' suite ['else' ':' suite]
for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]

View file

@ -73,13 +73,14 @@ struct _stmt {
identifier name;
arguments_ty args;
asdl_seq *body;
asdl_seq *decorators;
asdl_seq *decorator_list;
} FunctionDef;
struct {
identifier name;
asdl_seq *bases;
asdl_seq *body;
asdl_seq *decorator_list;
} ClassDef;
struct {
@ -359,11 +360,12 @@ mod_ty _Py_Expression(expr_ty 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)
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);
#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,
asdl_seq * decorator_list, int lineno, int col_offset,
PyArena *arena);
#define ClassDef(a0, a1, a2, a3, a4, a5) _Py_ClassDef(a0, a1, a2, a3, a4, a5)
stmt_ty _Py_ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, int
lineno, int col_offset, PyArena *arena);
#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);
#define Delete(a0, a1, a2, a3) _Py_Delete(a0, a1, a2, a3)

View file

@ -3,82 +3,83 @@
#define eval_input 258
#define decorator 259
#define decorators 260
#define funcdef 261
#define parameters 262
#define varargslist 263
#define fpdef 264
#define fplist 265
#define stmt 266
#define simple_stmt 267
#define small_stmt 268
#define expr_stmt 269
#define augassign 270
#define print_stmt 271
#define del_stmt 272
#define pass_stmt 273
#define flow_stmt 274
#define break_stmt 275
#define continue_stmt 276
#define return_stmt 277
#define yield_stmt 278
#define raise_stmt 279
#define import_stmt 280
#define import_name 281
#define import_from 282
#define import_as_name 283
#define dotted_as_name 284
#define import_as_names 285
#define dotted_as_names 286
#define dotted_name 287
#define global_stmt 288
#define exec_stmt 289
#define assert_stmt 290
#define compound_stmt 291
#define if_stmt 292
#define while_stmt 293
#define for_stmt 294
#define try_stmt 295
#define with_stmt 296
#define with_var 297
#define except_clause 298
#define suite 299
#define testlist_safe 300
#define old_test 301
#define old_lambdef 302
#define test 303
#define or_test 304
#define and_test 305
#define not_test 306
#define comparison 307
#define comp_op 308
#define expr 309
#define xor_expr 310
#define and_expr 311
#define shift_expr 312
#define arith_expr 313
#define term 314
#define factor 315
#define power 316
#define atom 317
#define listmaker 318
#define testlist_gexp 319
#define lambdef 320
#define trailer 321
#define subscriptlist 322
#define subscript 323
#define sliceop 324
#define exprlist 325
#define testlist 326
#define dictmaker 327
#define classdef 328
#define arglist 329
#define argument 330
#define list_iter 331
#define list_for 332
#define list_if 333
#define gen_iter 334
#define gen_for 335
#define gen_if 336
#define testlist1 337
#define encoding_decl 338
#define yield_expr 339
#define decorated 261
#define funcdef 262
#define parameters 263
#define varargslist 264
#define fpdef 265
#define fplist 266
#define stmt 267
#define simple_stmt 268
#define small_stmt 269
#define expr_stmt 270
#define augassign 271
#define print_stmt 272
#define del_stmt 273
#define pass_stmt 274
#define flow_stmt 275
#define break_stmt 276
#define continue_stmt 277
#define return_stmt 278
#define yield_stmt 279
#define raise_stmt 280
#define import_stmt 281
#define import_name 282
#define import_from 283
#define import_as_name 284
#define dotted_as_name 285
#define import_as_names 286
#define dotted_as_names 287
#define dotted_name 288
#define global_stmt 289
#define exec_stmt 290
#define assert_stmt 291
#define compound_stmt 292
#define if_stmt 293
#define while_stmt 294
#define for_stmt 295
#define try_stmt 296
#define with_stmt 297
#define with_var 298
#define except_clause 299
#define suite 300
#define testlist_safe 301
#define old_test 302
#define old_lambdef 303
#define test 304
#define or_test 305
#define and_test 306
#define not_test 307
#define comparison 308
#define comp_op 309
#define expr 310
#define xor_expr 311
#define and_expr 312
#define shift_expr 313
#define arith_expr 314
#define term 315
#define factor 316
#define power 317
#define atom 318
#define listmaker 319
#define testlist_gexp 320
#define lambdef 321
#define trailer 322
#define subscriptlist 323
#define subscript 324
#define sliceop 325
#define exprlist 326
#define testlist 327
#define dictmaker 328
#define classdef 329
#define arglist 330
#define argument 331
#define list_iter 332
#define list_for 333
#define list_if 334
#define gen_iter 335
#define gen_for 336
#define gen_if 337
#define testlist1 338
#define encoding_decl 339
#define yield_expr 340

View file

@ -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 = {}

View file

@ -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))]),

View file

@ -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()

View file

@ -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

View file

@ -12,6 +12,8 @@ What's New in Python 2.6 alpha 1?
Core and builtins
-----------------
- Patch #1759: Backport of PEP 3129 class decorators
- Issue #1881: An internal parser limit has been increased. Also see
issue 215555 for a discussion.

View file

@ -1498,7 +1498,7 @@ validate_small_stmt(node *tree)
/* 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
validate_compound_stmt(node *tree)
@ -1517,7 +1517,8 @@ validate_compound_stmt(node *tree)
|| (ntype == for_stmt)
|| (ntype == try_stmt)
|| (ntype == funcdef)
|| (ntype == classdef))
|| (ntype == classdef)
|| (ntype == decorated))
res = validate_node(tree);
else {
res = 0;
@ -1527,7 +1528,6 @@ validate_compound_stmt(node *tree)
return (res);
}
static int
validate_yield_or_testlist(node *tree)
{
@ -2558,28 +2558,40 @@ validate_decorators(node *tree)
/* funcdef:
*
* -6 -5 -4 -3 -2 -1
* [decorators] 'def' NAME parameters ':' suite
* -5 -4 -3 -2 -1
* 'def' NAME parameters ':' suite
*/
static int
validate_funcdef(node *tree)
{
int nch = NCH(tree);
int ok = (validate_ntype(tree, funcdef)
&& ((nch == 5) || (nch == 6))
&& (nch == 5)
&& validate_name(RCHILD(tree, -5), "def")
&& validate_ntype(RCHILD(tree, -4), NAME)
&& validate_colon(RCHILD(tree, -2))
&& validate_parameters(RCHILD(tree, -3))
&& validate_suite(RCHILD(tree, -1)));
if (ok && (nch == 6))
ok = validate_decorators(CHILD(tree, 0));
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
validate_lambdef(node *tree)
{
@ -2923,6 +2935,9 @@ validate_node(node *tree)
case classdef:
res = validate_class(tree);
break;
case decorated:
res = validate_decorated(tree);
break;
/*
* "Trivial" parse tree nodes.
* (Why did I call these trivial?)

View file

@ -10,8 +10,8 @@ module Python version "$Revision$"
| Suite(stmt* body)
stmt = FunctionDef(identifier name, arguments args,
stmt* body, expr* decorators)
| ClassDef(identifier name, expr* bases, stmt* body)
stmt* body, expr* decorator_list)
| ClassDef(identifier name, expr* bases, stmt* body, expr *decorator_list)
| Return(expr? value)
| Delete(expr* targets)

View file

@ -42,13 +42,14 @@ static char *FunctionDef_fields[]={
"name",
"args",
"body",
"decorators",
"decorator_list",
};
static PyTypeObject *ClassDef_type;
static char *ClassDef_fields[]={
"name",
"bases",
"body",
"decorator_list",
};
static PyTypeObject *Return_type;
static char *Return_fields[]={
@ -469,7 +470,7 @@ static int init_types(void)
FunctionDef_type = make_type("FunctionDef", stmt_type,
FunctionDef_fields, 4);
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;
Return_type = make_type("Return", stmt_type, Return_fields, 1);
if (!Return_type) return 0;
@ -790,7 +791,7 @@ Suite(asdl_seq * body, PyArena *arena)
stmt_ty
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;
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.args = args;
p->v.FunctionDef.body = body;
p->v.FunctionDef.decorators = decorators;
p->v.FunctionDef.decorator_list = decorator_list;
p->lineno = lineno;
p->col_offset = col_offset;
return p;
}
stmt_ty
ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, int lineno, int
col_offset, PyArena *arena)
ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, asdl_seq *
decorator_list, int lineno, int col_offset, PyArena *arena)
{
stmt_ty p;
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.bases = bases;
p->v.ClassDef.body = body;
p->v.ClassDef.decorator_list = decorator_list;
p->lineno = lineno;
p->col_offset = col_offset;
return p;
@ -1906,9 +1908,11 @@ ast2obj_stmt(void* _o)
if (PyObject_SetAttrString(result, "body", value) == -1)
goto failed;
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 (PyObject_SetAttrString(result, "decorators", value) == -1)
if (PyObject_SetAttrString(result, "decorator_list", value) ==
-1)
goto failed;
Py_DECREF(value);
break;
@ -1930,6 +1934,13 @@ ast2obj_stmt(void* _o)
if (PyObject_SetAttrString(result, "body", value) == -1)
goto failed;
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;
case Return_kind:
result = PyType_GenericNew(Return_type, NULL, NULL);

View file

@ -29,6 +29,7 @@ static asdl_seq *ast_for_suite(struct compiling *, const node *);
static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
expr_context_ty);
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 *);
/* Note different signature for ast_for_call */
@ -828,27 +829,16 @@ ast_for_decorators(struct compiling *c, const node *n)
}
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;
arguments_ty args;
asdl_seq *body;
asdl_seq *decorator_seq = NULL;
int name_i;
int name_i = 1;
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));
if (!name)
return NULL;
@ -867,6 +857,36 @@ ast_for_funcdef(struct compiling *c, const node *n)
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
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
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 */
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));
if (!s)
return NULL;
return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
n->n_col_offset, c->c_arena);
return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, decorator_seq,
LINENO(n), n->n_col_offset, c->c_arena);
}
/* check for empty base list */
if (TYPE(CHILD(n,3)) == RPAR) {
s = ast_for_suite(c, CHILD(n,5));
if (!s)
return NULL;
return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
n->n_col_offset, c->c_arena);
return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, decorator_seq,
LINENO(n), n->n_col_offset, c->c_arena);
}
/* 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));
if (!s)
return NULL;
return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
n->n_col_offset, c->c_arena);
return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, decorator_seq,
LINENO(n), n->n_col_offset, c->c_arena);
}
static stmt_ty
@ -3054,7 +3074,7 @@ ast_for_stmt(struct compiling *c, const node *n)
}
else {
/* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
| funcdef | classdef
| funcdef | classdef | decorated
*/
node *ch = CHILD(n, 0);
REQ(n, compound_stmt);
@ -3070,9 +3090,11 @@ ast_for_stmt(struct compiling *c, const node *n)
case with_stmt:
return ast_for_with_stmt(c, ch);
case funcdef:
return ast_for_funcdef(c, ch);
return ast_for_funcdef(c, ch, NULL);
case classdef:
return ast_for_classdef(c, ch);
return ast_for_classdef(c, ch, NULL);
case decorated:
return ast_for_decorated(c, ch);
default:
PyErr_Format(PyExc_SystemError,
"unhandled small_stmt: TYPE=%d NCH=%d\n",

View file

@ -1362,7 +1362,7 @@ compiler_function(struct compiler *c, stmt_ty s)
PyCodeObject *co;
PyObject *first_const = Py_None;
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;
int i, n, docstring;
@ -1413,9 +1413,14 @@ compiler_function(struct compiler *c, stmt_ty s)
static int
compiler_class(struct compiler *c, stmt_ty s)
{
int n;
int n, i;
PyCodeObject *co;
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 */
ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
/* 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(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))
return 0;
return 1;

File diff suppressed because it is too large Load diff

View file

@ -931,8 +931,8 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
return 0;
if (s->v.FunctionDef.args->defaults)
VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
if (s->v.FunctionDef.decorators)
VISIT_SEQ(st, expr, s->v.FunctionDef.decorators);
if (s->v.FunctionDef.decorator_list)
VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
if (!symtable_enter_block(st, s->v.FunctionDef.name,
FunctionBlock, (void *)s, s->lineno))
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))
return 0;
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,
(void *)s, s->lineno))
return 0;