mirror of
https://github.com/python/cpython.git
synced 2025-07-28 21:55:21 +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
|
@ -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);
|
||||
|
|
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 *,
|
||||
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",
|
||||
|
|
|
@ -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;
|
||||
|
|
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;
|
||||
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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue