summaryrefslogtreecommitdiffstats
path: root/Python/ast.c
diff options
context:
space:
mode:
Diffstat (limited to 'Python/ast.c')
-rw-r--r--Python/ast.c72
1 files changed, 28 insertions, 44 deletions
diff --git a/Python/ast.c b/Python/ast.c
index aa72cdb..2080e49 100644
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -109,22 +109,14 @@ validate_arguments(arguments_ty args)
{
if (!validate_args(args->args))
return 0;
- if (args->varargannotation) {
- if (!args->vararg) {
- PyErr_SetString(PyExc_ValueError, "varargannotation but no vararg on arguments");
- return 0;
- }
- if (!validate_expr(args->varargannotation, Load))
+ if (args->vararg && args->vararg->annotation
+ && !validate_expr(args->vararg->annotation, Load)) {
return 0;
}
if (!validate_args(args->kwonlyargs))
return 0;
- if (args->kwargannotation) {
- if (!args->kwarg) {
- PyErr_SetString(PyExc_ValueError, "kwargannotation but no kwarg on arguments");
- return 0;
- }
- if (!validate_expr(args->kwargannotation, Load))
+ if (args->kwarg && args->kwarg->annotation
+ && !validate_expr(args->kwarg->annotation, Load)) {
return 0;
}
if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
@@ -1138,7 +1130,13 @@ ast_for_arg(struct compiling *c, const node *n)
return NULL;
}
- return arg(name, annotation, c->c_arena);
+ arg_ty tmp = arg(name, annotation, c->c_arena);
+ if (!tmp)
+ return NULL;
+
+ tmp->lineno = LINENO(n);
+ tmp->col_offset = n->n_col_offset;
+ return tmp;
}
/* returns -1 if failed to handle keyword only arguments
@@ -1234,15 +1232,13 @@ ast_for_arguments(struct compiling *c, const node *n)
int i, j, k, nposargs = 0, nkwonlyargs = 0;
int nposdefaults = 0, found_default = 0;
asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
- identifier vararg = NULL, kwarg = NULL;
+ arg_ty vararg = NULL, kwarg = NULL;
arg_ty arg;
- expr_ty varargannotation = NULL, kwargannotation = NULL;
node *ch;
if (TYPE(n) == parameters) {
if (NCH(n) == 2) /* () as argument list */
- return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL,
- NULL, c->c_arena);
+ return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
n = CHILD(n, 1);
}
assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
@@ -1348,17 +1344,10 @@ ast_for_arguments(struct compiling *c, const node *n)
i = res; /* res has new position to process */
}
else {
- vararg = NEW_IDENTIFIER(CHILD(ch, 0));
+ vararg = ast_for_arg(c, ch);
if (!vararg)
return NULL;
- if (forbidden_name(c, vararg, CHILD(ch, 0), 0))
- return NULL;
- if (NCH(ch) > 1) {
- /* there is an annotation on the vararg */
- varargannotation = ast_for_expr(c, CHILD(ch, 2));
- if (!varargannotation)
- return NULL;
- }
+
i += 3;
if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
|| TYPE(CHILD(n, i)) == vfpdef)) {
@@ -1373,17 +1362,9 @@ ast_for_arguments(struct compiling *c, const node *n)
case DOUBLESTAR:
ch = CHILD(n, i+1); /* tfpdef */
assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
- kwarg = NEW_IDENTIFIER(CHILD(ch, 0));
+ kwarg = ast_for_arg(c, ch);
if (!kwarg)
return NULL;
- if (NCH(ch) > 1) {
- /* there is an annotation on the kwarg */
- kwargannotation = ast_for_expr(c, CHILD(ch, 2));
- if (!kwargannotation)
- return NULL;
- }
- if (forbidden_name(c, kwarg, CHILD(ch, 0), 0))
- return NULL;
i += 3;
break;
default:
@@ -1393,8 +1374,7 @@ ast_for_arguments(struct compiling *c, const node *n)
return NULL;
}
}
- return arguments(posargs, vararg, varargannotation, kwonlyargs, kwarg,
- kwargannotation, posdefaults, kwdefaults, c->c_arena);
+ return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
}
static expr_ty
@@ -1559,8 +1539,7 @@ ast_for_lambdef(struct compiling *c, const node *n)
expr_ty expression;
if (NCH(n) == 3) {
- args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL,
- NULL, c->c_arena);
+ args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
if (!args)
return NULL;
expression = ast_for_expr(c, CHILD(n, 2));
@@ -2107,15 +2086,22 @@ ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
if (NCH(n) == 2)
return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
n->n_col_offset, c->c_arena);
- else
- return ast_for_call(c, CHILD(n, 1), left_expr);
+ else {
+ expr_ty tmp = ast_for_call(c, CHILD(n, 1), left_expr);
+ if (!tmp)
+ return NULL;
+
+ tmp->lineno = LINENO(n);
+ tmp->col_offset = n->n_col_offset;
+ return tmp;
+ }
}
else if (TYPE(CHILD(n, 0)) == DOT ) {
PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
if (!attr_id)
return NULL;
return Attribute(left_expr, attr_id, Load,
- LINENO(n), n->n_col_offset, c->c_arena);
+ LINENO(CHILD(n, 1)), CHILD(n, 1)->n_col_offset, c->c_arena);
}
else {
REQ(CHILD(n, 0), LSQB);
@@ -2216,8 +2202,6 @@ ast_for_power(struct compiling *c, const node *n)
tmp = ast_for_trailer(c, ch, e);
if (!tmp)
return NULL;
- tmp->lineno = e->lineno;
- tmp->col_offset = e->col_offset;
e = tmp;
}
if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {