From 8fbf4fa9edcb9efd1ca5d650ed235eb4770dea3a Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Thu, 6 Dec 2012 17:41:04 -0500 Subject: create NameConstant AST class for None, True, and False literals (closes #16619) --- Python/ast.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'Python/ast.c') diff --git a/Python/ast.c b/Python/ast.c index 7657b22459..50603033d5 100644 --- a/Python/ast.c +++ b/Python/ast.c @@ -282,6 +282,7 @@ validate_expr(expr_ty exp, expr_context_ty ctx) return validate_exprs(exp->v.Tuple.elts, ctx, 0); /* These last cases don't have any checking. */ case Name_kind: + case NameConstant_kind: case Ellipsis_kind: return 1; default: @@ -903,7 +904,7 @@ set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n) break; case Name_kind: if (ctx == Store) { - if (forbidden_name(c, e->v.Name.id, n, 1)) + if (forbidden_name(c, e->v.Name.id, n, 0)) return 0; /* forbidden_name() calls ast_error() */ } e->v.Name.ctx = ctx; @@ -955,6 +956,9 @@ set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n) case Bytes_kind: expr_name = "literal"; break; + case NameConstant_kind: + expr_name = "keyword"; + break; case Ellipsis_kind: expr_name = "Ellipsis"; break; @@ -1819,11 +1823,21 @@ ast_for_atom(struct compiling *c, const node *n) switch (TYPE(ch)) { case NAME: { - /* All names start in Load context, but may later be - changed. */ - PyObject *name = NEW_IDENTIFIER(ch); + PyObject *name; + const char *s = STR(ch); + size_t len = strlen(s); + if (len >= 4 && len <= 5) { + if (!strcmp(s, "None")) + return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena); + if (!strcmp(s, "True")) + return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena); + if (!strcmp(s, "False")) + return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena); + } + name = new_identifier(s, c); if (!name) return NULL; + /* All names start in Load context, but may later be changed. */ return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena); } case STRING: { -- cgit v1.2.1 From 7eadff200c3acb1286b95d417edfcc56cee617ee Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Mon, 18 Mar 2013 10:48:58 -0700 Subject: unify some ast.argument's attrs; change Attribute column offset (closes #16795) Patch from Sven Brauch. --- Python/ast.c | 72 +++++++++++++++++++++++------------------------------------- 1 file changed, 28 insertions(+), 44 deletions(-) (limited to 'Python/ast.c') diff --git a/Python/ast.c b/Python/ast.c index aa72cdbb1e..2080e4991a 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) { -- cgit v1.2.1 From 8703704e3ab4dc0e40996d1796e75fea5df1da32 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Mon, 18 Mar 2013 10:59:41 -0700 Subject: move declaration to top of block --- Python/ast.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'Python/ast.c') diff --git a/Python/ast.c b/Python/ast.c index 2080e4991a..4d517b7b54 100644 --- a/Python/ast.c +++ b/Python/ast.c @@ -1115,6 +1115,7 @@ ast_for_arg(struct compiling *c, const node *n) identifier name; expr_ty annotation = NULL; node *ch; + arg_ty tmp; assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef); ch = CHILD(n, 0); @@ -1130,7 +1131,7 @@ ast_for_arg(struct compiling *c, const node *n) return NULL; } - arg_ty tmp = arg(name, annotation, c->c_arena); + tmp = arg(name, annotation, c->c_arena); if (!tmp) return NULL; -- cgit v1.2.1 From f5fd332a1c4abc1b374c7c618aa59857da29ce91 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 17 Jul 2013 00:57:58 +0200 Subject: Issue #18408: Handle PyArena_AddPyObject() failure in ast.c PyList_Append() (called by PyArena_AddPyObject()) can fail because of a MemoryError for example. --- Python/ast.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'Python/ast.c') diff --git a/Python/ast.c b/Python/ast.c index 4d517b7b54..afdb199f04 100644 --- a/Python/ast.c +++ b/Python/ast.c @@ -560,7 +560,10 @@ new_identifier(const char *n, struct compiling *c) id = id2; } PyUnicode_InternInPlace(&id); - PyArena_AddPyObject(c->c_arena, id); + if (PyArena_AddPyObject(c->c_arena, id) < 0) { + Py_DECREF(id); + return NULL; + } return id; } @@ -1847,7 +1850,10 @@ ast_for_atom(struct compiling *c, const node *n) } return NULL; } - PyArena_AddPyObject(c->c_arena, str); + if (PyArena_AddPyObject(c->c_arena, str) < 0) { + Py_DECREF(str); + return NULL; + } if (bytesmode) return Bytes(str, LINENO(n), n->n_col_offset, c->c_arena); else @@ -1858,7 +1864,10 @@ ast_for_atom(struct compiling *c, const node *n) if (!pynum) return NULL; - PyArena_AddPyObject(c->c_arena, pynum); + if (PyArena_AddPyObject(c->c_arena, pynum) < 0) { + Py_DECREF(pynum); + return NULL; + } return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena); } case ELLIPSIS: /* Ellipsis */ @@ -2845,13 +2854,19 @@ alias_for_import_name(struct compiling *c, const node *n, int store) return NULL; str = uni; PyUnicode_InternInPlace(&str); - PyArena_AddPyObject(c->c_arena, str); + if (PyArena_AddPyObject(c->c_arena, str) < 0) { + Py_DECREF(str); + return NULL; + } return alias(str, NULL, c->c_arena); } break; case STAR: str = PyUnicode_InternFromString("*"); - PyArena_AddPyObject(c->c_arena, str); + if (PyArena_AddPyObject(c->c_arena, str) < 0) { + Py_DECREF(str); + return NULL; + } return alias(str, NULL, c->c_arena); default: PyErr_Format(PyExc_SystemError, -- cgit v1.2.1 From ccc9c4bcff5d09d6abbb33960f1cd44406615cb4 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 17 Jul 2013 21:51:42 +0200 Subject: Issue #18408: Fix ast_for_atom(), PyErr_Fetch(&type, &value, &tback) can set value to NULL --- Python/ast.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Python/ast.c') diff --git a/Python/ast.c b/Python/ast.c index afdb199f04..f1c2347ebc 100644 --- a/Python/ast.c +++ b/Python/ast.c @@ -1845,7 +1845,7 @@ ast_for_atom(struct compiling *c, const node *n) } ast_error(c, n, buf); Py_DECREF(type); - Py_DECREF(value); + Py_XDECREF(value); Py_XDECREF(tback); } return NULL; -- cgit v1.2.1