summaryrefslogtreecommitdiffstats
path: root/Python/ast.c
diff options
context:
space:
mode:
Diffstat (limited to 'Python/ast.c')
-rw-r--r--Python/ast.c82
1 files changed, 47 insertions, 35 deletions
diff --git a/Python/ast.c b/Python/ast.c
index 83572ee..d81f001 100644
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -362,12 +362,12 @@ static const char* FORBIDDEN[] = {
};
static int
-forbidden_name(expr_ty e, const node *n)
+forbidden_name(identifier name, const node *n)
{
const char **p;
- assert(PyUnicode_Check(e->v.Name.id));
+ assert(PyUnicode_Check(name));
for (p = FORBIDDEN; *p; p++) {
- if (PyUnicode_CompareWithASCIIString(e->v.Name.id, *p) == 0) {
+ if (PyUnicode_CompareWithASCIIString(name, *p) == 0) {
ast_error(n, "assignment to keyword");
return 1;
}
@@ -414,7 +414,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(e, n))
+ if (forbidden_name(e->v.Name.id, n))
return 0; /* forbidden_name() calls ast_error() */
}
e->v.Name.ctx = ctx;
@@ -424,10 +424,13 @@ set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
s = e->v.List.elts;
break;
case Tuple_kind:
- if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
- return ast_error(n, "can't assign to ()");
- e->v.Tuple.ctx = ctx;
- s = e->v.Tuple.elts;
+ if (asdl_seq_LEN(e->v.Tuple.elts)) {
+ e->v.Tuple.ctx = ctx;
+ s = e->v.Tuple.elts;
+ }
+ else {
+ expr_name = "()";
+ }
break;
case Lambda_kind:
expr_name = "lambda";
@@ -1378,7 +1381,7 @@ ast_for_atom(struct compiling *c, const node *n)
/* testlist_comp: test ( comp_for | (',' test)* [','] ) */
if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
return ast_for_genexp(c, ch);
-
+
return ast_for_testlist(c, ch);
case LSQB: /* list (or list comprehension) */
ch = CHILD(n, 1);
@@ -1513,14 +1516,7 @@ ast_for_slice(struct compiling *c, const node *n)
ch = CHILD(n, NCH(n) - 1);
if (TYPE(ch) == sliceop) {
- if (NCH(ch) == 1) {
- /* No expression, so step is None */
- ch = CHILD(ch, 0);
- step = Name(new_identifier("None", c->c_arena), Load,
- LINENO(ch), ch->n_col_offset, c->c_arena);
- if (!step)
- return NULL;
- } else {
+ if (NCH(ch) != 1) {
ch = CHILD(ch, 1);
if (TYPE(ch) == test) {
step = ast_for_expr(c, ch);
@@ -2014,7 +2010,7 @@ ast_for_call(struct compiling *c, const node *n, expr_ty func)
} else if (e->kind != Name_kind) {
ast_error(CHILD(ch, 0), "keyword can't be an expression");
return NULL;
- } else if (forbidden_name(e, ch)) {
+ } else if (forbidden_name(e->v.Name.id, ch)) {
return NULL;
}
key = e->v.Name.id;
@@ -2160,6 +2156,7 @@ ast_for_expr_stmt(struct compiling *c, const node *n)
}
}
+
static asdl_seq *
ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
{
@@ -2260,49 +2257,64 @@ ast_for_flow_stmt(struct compiling *c, const node *n)
}
static alias_ty
-alias_for_import_name(struct compiling *c, const node *n)
+alias_for_import_name(struct compiling *c, const node *n, int store)
{
/*
import_as_name: NAME ['as' NAME]
dotted_as_name: dotted_name ['as' NAME]
dotted_name: NAME ('.' NAME)*
*/
- PyObject *str, *name;
+ identifier str, name;
loop:
switch (TYPE(n)) {
- case import_as_name:
+ case import_as_name: {
+ node *name_node = CHILD(n, 0);
str = NULL;
+ name = NEW_IDENTIFIER(name_node);
+ if (!name)
+ return NULL;
if (NCH(n) == 3) {
- str = NEW_IDENTIFIER(CHILD(n, 2));
+ node *str_node = CHILD(n, 2);
+ str = NEW_IDENTIFIER(str_node);
if (!str)
return NULL;
+ if (store && forbidden_name(str, str_node))
+ return NULL;
+ }
+ else {
+ if (forbidden_name(name, name_node))
+ return NULL;
}
- name = NEW_IDENTIFIER(CHILD(n, 0));
- if (!name)
- return NULL;
return alias(name, str, c->c_arena);
+ }
case dotted_as_name:
if (NCH(n) == 1) {
n = CHILD(n, 0);
goto loop;
}
else {
- alias_ty a = alias_for_import_name(c, CHILD(n, 0));
+ node *asname_node = CHILD(n, 2);
+ alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
if (!a)
return NULL;
assert(!a->asname);
- a->asname = NEW_IDENTIFIER(CHILD(n, 2));
+ a->asname = NEW_IDENTIFIER(asname_node);
if (!a->asname)
return NULL;
+ if (forbidden_name(a->asname, asname_node))
+ return NULL;
return a;
}
break;
case dotted_name:
if (NCH(n) == 1) {
- name = NEW_IDENTIFIER(CHILD(n, 0));
+ node *name_node = CHILD(n, 0);
+ name = NEW_IDENTIFIER(name_node);
if (!name)
return NULL;
+ if (store && forbidden_name(name, name_node))
+ return NULL;
return alias(name, NULL, c->c_arena);
}
else {
@@ -2382,7 +2394,7 @@ ast_for_import_stmt(struct compiling *c, const node *n)
if (!aliases)
return NULL;
for (i = 0; i < NCH(n); i += 2) {
- alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
+ alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
if (!import_alias)
return NULL;
asdl_seq_SET(aliases, i / 2, import_alias);
@@ -2393,13 +2405,15 @@ ast_for_import_stmt(struct compiling *c, const node *n)
int n_children;
int idx, ndots = 0;
alias_ty mod = NULL;
- identifier modname;
+ identifier modname = NULL;
/* Count the number of dots (for relative imports) and check for the
optional module name */
for (idx = 1; idx < NCH(n); idx++) {
if (TYPE(CHILD(n, idx)) == dotted_name) {
- mod = alias_for_import_name(c, CHILD(n, idx));
+ mod = alias_for_import_name(c, CHILD(n, idx), 0);
+ if (!mod)
+ return NULL;
idx++;
break;
} else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
@@ -2444,14 +2458,14 @@ ast_for_import_stmt(struct compiling *c, const node *n)
/* handle "from ... import *" special b/c there's no children */
if (TYPE(n) == STAR) {
- alias_ty import_alias = alias_for_import_name(c, n);
+ alias_ty import_alias = alias_for_import_name(c, n, 1);
if (!import_alias)
return NULL;
asdl_seq_SET(aliases, 0, import_alias);
}
else {
for (i = 0; i < NCH(n); i += 2) {
- alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
+ alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
if (!import_alias)
return NULL;
asdl_seq_SET(aliases, i / 2, import_alias);
@@ -2459,8 +2473,6 @@ ast_for_import_stmt(struct compiling *c, const node *n)
}
if (mod != NULL)
modname = mod->name;
- else
- modname = new_identifier("", c->c_arena);
return ImportFrom(modname, aliases, ndots, lineno, col_offset,
c->c_arena);
}