Index: Python/ast.c =================================================================== --- Python/ast.c (revision 53773) +++ Python/ast.c (working copy) @@ -2537,6 +2537,19 @@ return seq; } +static Else_ty +ast_for_else(struct compiling *c, const node *e, const node *b) +{ + asdl_seq *body; + REQ(n, Else_ty); + + body = ast_for_suite(c, b); + if (!body) + return NULL; + + return Else(body, LINENO(e), e->n_col_offset, c->c_arena); +} + static stmt_ty ast_for_if_stmt(struct compiling *c, const node *n) { @@ -2568,7 +2581,8 @@ */ if (s[2] == 's') { expr_ty expression; - asdl_seq *seq1, *seq2; + Else_ty orelse; + asdl_seq *seq1; expression = ast_for_expr(c, CHILD(n, 1)); if (!expression) @@ -2576,15 +2590,15 @@ seq1 = ast_for_suite(c, CHILD(n, 3)); if (!seq1) return NULL; - seq2 = ast_for_suite(c, CHILD(n, 6)); - if (!seq2) + orelse = ast_for_else(c, CHILD(n, 4), CHILD(n, 6)); + if (!orelse) return NULL; - return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena); + return If(expression, seq1, orelse, LINENO(n), n->n_col_offset, c->c_arena); } else if (s[2] == 'i') { int i, n_elif, has_else = 0; - asdl_seq *orelse = NULL; + Else_ty orelse = NULL; n_elif = NCH(n) - 4; /* must reference the child n_elif+1 since 'else' token is third, not fourth, child from the end. */ @@ -2596,11 +2610,13 @@ n_elif /= 4; if (has_else) { + int lno, col; + Else_ty if_else; expr_ty expression; - asdl_seq *seq1, *seq2; + asdl_seq *seq1, *else_body; - orelse = asdl_seq_new(1, c->c_arena); - if (!orelse) + else_body = asdl_seq_new(1, c->c_arena); + if (!else_body) return NULL; expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); if (!expression) @@ -2608,18 +2624,20 @@ seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4)); if (!seq1) return NULL; - seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); - if (!seq2) + if_else = ast_for_else(c, CHILD(n, NCH(n) - 3), CHILD(n, NCH(n) - 1)); + if (!if_else) return NULL; - asdl_seq_SET(orelse, 0, If(expression, seq1, seq2, - LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset, - c->c_arena)); + lno = LINENO(CHILD(n, NCH(n) - 6)); + col = CHILD(n, NCH(n) - 6)->n_col_offset; + asdl_seq_SET(else_body, 0, If(expression, seq1, if_else, lno, col, c->c_arena)); + orelse = Else(else_body, lno, col, c->c_arena); /* the just-created orelse handled the last elif */ n_elif--; } for (i = 0; i < n_elif; i++) { + int col, lno; int off = 5 + (n_elif - i - 1) * 4; expr_ty expression; asdl_seq *suite_seq; @@ -2633,10 +2651,10 @@ if (!suite_seq) return NULL; - asdl_seq_SET(newobj, 0, - If(expression, suite_seq, orelse, - LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena)); - orelse = newobj; + lno = LINENO(CHILD(n, off)); + col = CHILD(n, off)->n_col_offset; + asdl_seq_SET(newobj, 0, If(expression, suite_seq, orelse, lno, col, c->c_arena)); + orelse = Else(newobj, lno, col, c->c_arena); } return If(ast_for_expr(c, CHILD(n, 1)), ast_for_suite(c, CHILD(n, 3)), @@ -2668,7 +2686,8 @@ } else if (NCH(n) == 7) { expr_ty expression; - asdl_seq *seq1, *seq2; + Else_ty orelse; + asdl_seq *seq1; expression = ast_for_expr(c, CHILD(n, 1)); if (!expression) @@ -2676,11 +2695,11 @@ seq1 = ast_for_suite(c, CHILD(n, 3)); if (!seq1) return NULL; - seq2 = ast_for_suite(c, CHILD(n, 6)); - if (!seq2) + orelse = ast_for_else(c, CHILD(n, 4), CHILD(n, 6)); + if (!orelse) return NULL; - return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena); + return While(expression, seq1, orelse, LINENO(n), n->n_col_offset, c->c_arena); } PyErr_Format(PyExc_SystemError, @@ -2692,16 +2711,17 @@ static stmt_ty ast_for_for_stmt(struct compiling *c, const node *n) { - asdl_seq *_target, *seq = NULL, *suite_seq; + asdl_seq *_target, *suite_seq; expr_ty expression; expr_ty target; + Else_ty orelse = NULL; const node *node_target; /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */ REQ(n, for_stmt); if (NCH(n) == 9) { - seq = ast_for_suite(c, CHILD(n, 8)); - if (!seq) + orelse = ast_for_else(c, CHILD(n, 6), CHILD(n, 8)); + if (!orelse) return NULL; } @@ -2723,8 +2743,8 @@ if (!suite_seq) return NULL; - return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset, - c->c_arena); + return For(target, expression, suite_seq, orelse, LINENO(n), + n->n_col_offset, c->c_arena); } static excepthandler_ty @@ -2786,7 +2806,8 @@ { const int nch = NCH(n); int n_except = (nch - 3)/3; - asdl_seq *body, *orelse = NULL, *finally = NULL; + asdl_seq *body, *finally = NULL; + Else_ty orelse = NULL; REQ(n, try_stmt); @@ -2800,7 +2821,7 @@ /* we can assume it's an "else", because nch >= 9 for try-else-finally and it would otherwise have a type of except_clause */ - orelse = ast_for_suite(c, CHILD(n, nch - 4)); + orelse = ast_for_else(c, CHILD(n, nch - 6), CHILD(n, nch - 4)); if (orelse == NULL) return NULL; n_except--; @@ -2814,7 +2835,7 @@ else { /* we can assume it's an "else", otherwise it would have a type of except_clause */ - orelse = ast_for_suite(c, CHILD(n, nch - 1)); + orelse = ast_for_else(c, CHILD(n, nch - 3), CHILD(n, nch - 1)); if (orelse == NULL) return NULL; n_except--; Index: Python/symtable.c =================================================================== --- Python/symtable.c (revision 53773) +++ Python/symtable.c (working copy) @@ -928,84 +928,84 @@ { switch (s->kind) { case FunctionDef_kind: - if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL)) - 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 (!symtable_enter_block(st, s->v.FunctionDef.name, - FunctionBlock, (void *)s, s->lineno)) - return 0; - VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s); - VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s); - if (!symtable_exit_block(st, s)) - return 0; - break; + if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL)) + 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 (!symtable_enter_block(st, s->v.FunctionDef.name, + FunctionBlock, (void *)s, s->lineno)) + return 0; + VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s); + VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s); + if (!symtable_exit_block(st, s)) + return 0; + break; case ClassDef_kind: { - PyObject *tmp; - if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL)) - return 0; - VISIT_SEQ(st, expr, s->v.ClassDef.bases); - if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock, - (void *)s, s->lineno)) - return 0; - tmp = st->st_private; - st->st_private = s->v.ClassDef.name; - VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s); - st->st_private = tmp; - if (!symtable_exit_block(st, s)) - return 0; - break; - } + PyObject *tmp; + if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL)) + return 0; + VISIT_SEQ(st, expr, s->v.ClassDef.bases); + if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock, + (void *)s, s->lineno)) + return 0; + tmp = st->st_private; + st->st_private = s->v.ClassDef.name; + VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s); + st->st_private = tmp; + if (!symtable_exit_block(st, s)) + return 0; + break; + } case Return_kind: - if (s->v.Return.value) { - VISIT(st, expr, s->v.Return.value); - st->st_cur->ste_returns_value = 1; - if (st->st_cur->ste_generator) { - PyErr_SetString(PyExc_SyntaxError, - RETURN_VAL_IN_GENERATOR); - PyErr_SyntaxLocation(st->st_filename, - s->lineno); - return 0; - } - } - break; + if (s->v.Return.value) { + VISIT(st, expr, s->v.Return.value); + st->st_cur->ste_returns_value = 1; + if (st->st_cur->ste_generator) { + PyErr_SetString(PyExc_SyntaxError, + RETURN_VAL_IN_GENERATOR); + PyErr_SyntaxLocation(st->st_filename, + s->lineno); + return 0; + } + } + break; case Delete_kind: - VISIT_SEQ(st, expr, s->v.Delete.targets); - break; + VISIT_SEQ(st, expr, s->v.Delete.targets); + break; case Assign_kind: - VISIT_SEQ(st, expr, s->v.Assign.targets); - VISIT(st, expr, s->v.Assign.value); - break; + VISIT_SEQ(st, expr, s->v.Assign.targets); + VISIT(st, expr, s->v.Assign.value); + break; case AugAssign_kind: - VISIT(st, expr, s->v.AugAssign.target); - VISIT(st, expr, s->v.AugAssign.value); - break; + VISIT(st, expr, s->v.AugAssign.target); + VISIT(st, expr, s->v.AugAssign.value); + break; case Print_kind: - if (s->v.Print.dest) - VISIT(st, expr, s->v.Print.dest); - VISIT_SEQ(st, expr, s->v.Print.values); - break; + if (s->v.Print.dest) + VISIT(st, expr, s->v.Print.dest); + VISIT_SEQ(st, expr, s->v.Print.values); + break; case For_kind: - VISIT(st, expr, s->v.For.target); - VISIT(st, expr, s->v.For.iter); - VISIT_SEQ(st, stmt, s->v.For.body); - if (s->v.For.orelse) - VISIT_SEQ(st, stmt, s->v.For.orelse); - break; + VISIT(st, expr, s->v.For.target); + VISIT(st, expr, s->v.For.iter); + VISIT_SEQ(st, stmt, s->v.For.body); + if (s->v.For.orelse) + VISIT_SEQ(st, stmt, s->v.For.orelse->body); + break; case While_kind: VISIT(st, expr, s->v.While.test); VISIT_SEQ(st, stmt, s->v.While.body); if (s->v.While.orelse) - VISIT_SEQ(st, stmt, s->v.While.orelse); + VISIT_SEQ(st, stmt, s->v.While.orelse->body); break; case If_kind: /* XXX if 0: and lookup_yield() hacks */ VISIT(st, expr, s->v.If.test); VISIT_SEQ(st, stmt, s->v.If.body); if (s->v.If.orelse) - VISIT_SEQ(st, stmt, s->v.If.orelse); + VISIT_SEQ(st, stmt, s->v.If.orelse->body); break; case Raise_kind: if (s->v.Raise.type) { @@ -1019,7 +1019,8 @@ break; case TryExcept_kind: VISIT_SEQ(st, stmt, s->v.TryExcept.body); - VISIT_SEQ(st, stmt, s->v.TryExcept.orelse); + if (s->v.TryExcept.orelse) + VISIT_SEQ(st, stmt, s->v.TryExcept.orelse->body); VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers); break; case TryFinally_kind: @@ -1113,101 +1114,101 @@ { switch (e->kind) { case BoolOp_kind: - VISIT_SEQ(st, expr, e->v.BoolOp.values); - break; + VISIT_SEQ(st, expr, e->v.BoolOp.values); + break; case BinOp_kind: - VISIT(st, expr, e->v.BinOp.left); - VISIT(st, expr, e->v.BinOp.right); - break; + VISIT(st, expr, e->v.BinOp.left); + VISIT(st, expr, e->v.BinOp.right); + break; case UnaryOp_kind: - VISIT(st, expr, e->v.UnaryOp.operand); - break; + VISIT(st, expr, e->v.UnaryOp.operand); + break; case Lambda_kind: { - if (!GET_IDENTIFIER(lambda) || - !symtable_add_def(st, lambda, DEF_LOCAL)) - return 0; - if (e->v.Lambda.args->defaults) - VISIT_SEQ(st, expr, e->v.Lambda.args->defaults); - /* XXX how to get line numbers for expressions */ - if (!symtable_enter_block(st, lambda, - FunctionBlock, (void *)e, 0)) - return 0; - VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e); - VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e); - if (!symtable_exit_block(st, (void *)e)) - return 0; - break; - } - case IfExp_kind: - VISIT(st, expr, e->v.IfExp.test); - VISIT(st, expr, e->v.IfExp.body); - VISIT(st, expr, e->v.IfExp.orelse); - break; + if (!GET_IDENTIFIER(lambda) || + !symtable_add_def(st, lambda, DEF_LOCAL)) + return 0; + if (e->v.Lambda.args->defaults) + VISIT_SEQ(st, expr, e->v.Lambda.args->defaults); + /* XXX how to get line numbers for expressions */ + if (!symtable_enter_block(st, lambda, + FunctionBlock, (void *)e, 0)) + return 0; + VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e); + VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e); + if (!symtable_exit_block(st, (void *)e)) + return 0; + break; + } + case IfExp_kind: + VISIT(st, expr, e->v.IfExp.test); + VISIT(st, expr, e->v.IfExp.body); + VISIT(st, expr, e->v.IfExp.orelse); + break; case Dict_kind: - VISIT_SEQ(st, expr, e->v.Dict.keys); - VISIT_SEQ(st, expr, e->v.Dict.values); - break; + VISIT_SEQ(st, expr, e->v.Dict.keys); + VISIT_SEQ(st, expr, e->v.Dict.values); + break; case ListComp_kind: - if (!symtable_new_tmpname(st)) - return 0; - VISIT(st, expr, e->v.ListComp.elt); - VISIT_SEQ(st, comprehension, e->v.ListComp.generators); - break; + if (!symtable_new_tmpname(st)) + return 0; + VISIT(st, expr, e->v.ListComp.elt); + VISIT_SEQ(st, comprehension, e->v.ListComp.generators); + break; case GeneratorExp_kind: - if (!symtable_visit_genexp(st, e)) - return 0; - break; + if (!symtable_visit_genexp(st, e)) + return 0; + break; case Yield_kind: - if (e->v.Yield.value) - VISIT(st, expr, e->v.Yield.value); - st->st_cur->ste_generator = 1; - if (st->st_cur->ste_returns_value) { - PyErr_SetString(PyExc_SyntaxError, - RETURN_VAL_IN_GENERATOR); - PyErr_SyntaxLocation(st->st_filename, - e->lineno); - return 0; - } - break; + if (e->v.Yield.value) + VISIT(st, expr, e->v.Yield.value); + st->st_cur->ste_generator = 1; + if (st->st_cur->ste_returns_value) { + PyErr_SetString(PyExc_SyntaxError, + RETURN_VAL_IN_GENERATOR); + PyErr_SyntaxLocation(st->st_filename, + e->lineno); + return 0; + } + break; case Compare_kind: - VISIT(st, expr, e->v.Compare.left); - VISIT_SEQ(st, expr, e->v.Compare.comparators); - break; + VISIT(st, expr, e->v.Compare.left); + VISIT_SEQ(st, expr, e->v.Compare.comparators); + break; case Call_kind: - VISIT(st, expr, e->v.Call.func); - VISIT_SEQ(st, expr, e->v.Call.args); - VISIT_SEQ(st, keyword, e->v.Call.keywords); - if (e->v.Call.starargs) - VISIT(st, expr, e->v.Call.starargs); - if (e->v.Call.kwargs) - VISIT(st, expr, e->v.Call.kwargs); - break; + VISIT(st, expr, e->v.Call.func); + VISIT_SEQ(st, expr, e->v.Call.args); + VISIT_SEQ(st, keyword, e->v.Call.keywords); + if (e->v.Call.starargs) + VISIT(st, expr, e->v.Call.starargs); + if (e->v.Call.kwargs) + VISIT(st, expr, e->v.Call.kwargs); + break; case Repr_kind: - VISIT(st, expr, e->v.Repr.value); - break; + VISIT(st, expr, e->v.Repr.value); + break; case Num_kind: case Str_kind: - /* Nothing to do here. */ - break; - /* The following exprs can be assignment targets. */ + /* Nothing to do here. */ + break; + /* The following exprs can be assignment targets. */ case Attribute_kind: - VISIT(st, expr, e->v.Attribute.value); - break; + VISIT(st, expr, e->v.Attribute.value); + break; case Subscript_kind: - VISIT(st, expr, e->v.Subscript.value); - VISIT(st, slice, e->v.Subscript.slice); - break; + VISIT(st, expr, e->v.Subscript.value); + VISIT(st, slice, e->v.Subscript.slice); + break; case Name_kind: - if (!symtable_add_def(st, e->v.Name.id, - e->v.Name.ctx == Load ? USE : DEF_LOCAL)) - return 0; - break; - /* child nodes of List and Tuple will have expr_context set */ + if (!symtable_add_def(st, e->v.Name.id, + e->v.Name.ctx == Load ? USE : DEF_LOCAL)) + return 0; + break; + /* child nodes of List and Tuple will have expr_context set */ case List_kind: - VISIT_SEQ(st, expr, e->v.List.elts); - break; + VISIT_SEQ(st, expr, e->v.List.elts); + break; case Tuple_kind: - VISIT_SEQ(st, expr, e->v.Tuple.elts); + VISIT_SEQ(st, expr, e->v.Tuple.elts); break; } return 1; Index: Python/compile.c =================================================================== --- Python/compile.c (revision 53773) +++ Python/compile.c (working copy) @@ -1516,7 +1516,7 @@ * constant = -1: rest */ if (constant == 0) { if (s->v.If.orelse) - VISIT_SEQ(c, stmt, s->v.If.orelse); + VISIT_SEQ(c, stmt, s->v.If.orelse->body); } else if (constant == 1) { VISIT_SEQ(c, stmt, s->v.If.body); } else { @@ -1528,7 +1528,7 @@ compiler_use_next_block(c, next); ADDOP(c, POP_TOP); if (s->v.If.orelse) - VISIT_SEQ(c, stmt, s->v.If.orelse); + VISIT_SEQ(c, stmt, s->v.If.orelse->body); } compiler_use_next_block(c, end); return 1; @@ -1561,7 +1561,8 @@ compiler_use_next_block(c, cleanup); ADDOP(c, POP_BLOCK); compiler_pop_fblock(c, LOOP, start); - VISIT_SEQ(c, stmt, s->v.For.orelse); + if (s->v.For.orelse) + VISIT_SEQ(c, stmt, s->v.For.orelse->body); compiler_use_next_block(c, end); return 1; } @@ -1614,7 +1615,7 @@ } compiler_pop_fblock(c, LOOP, loop); if (orelse != NULL) /* what if orelse is just pass? */ - VISIT_SEQ(c, stmt, s->v.While.orelse); + VISIT_SEQ(c, stmt, s->v.While.orelse->body); compiler_use_next_block(c, end); return 1; @@ -1804,7 +1805,8 @@ } ADDOP(c, END_FINALLY); compiler_use_next_block(c, orelse); - VISIT_SEQ(c, stmt, s->v.TryExcept.orelse); + if (s->v.TryExcept.orelse) + VISIT_SEQ(c, stmt, s->v.TryExcept.orelse->body); compiler_use_next_block(c, end); return 1; } Index: Python/Python-ast.c =================================================================== --- Python/Python-ast.c (revision 53773) +++ Python/Python-ast.c (working copy) @@ -255,8 +255,6 @@ "ctx", }; static PyTypeObject *expr_context_type; -static PyObject *Load_singleton, *Store_singleton, *Del_singleton, -*AugLoad_singleton, *AugStore_singleton, *Param_singleton; static PyObject* ast2obj_expr_context(expr_context_ty); static PyTypeObject *Load_type; static PyTypeObject *Store_type; @@ -282,15 +280,10 @@ "value", }; static PyTypeObject *boolop_type; -static PyObject *And_singleton, *Or_singleton; static PyObject* ast2obj_boolop(boolop_ty); static PyTypeObject *And_type; static PyTypeObject *Or_type; static PyTypeObject *operator_type; -static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton, -*Div_singleton, *Mod_singleton, *Pow_singleton, *LShift_singleton, -*RShift_singleton, *BitOr_singleton, *BitXor_singleton, *BitAnd_singleton, -*FloorDiv_singleton; static PyObject* ast2obj_operator(operator_ty); static PyTypeObject *Add_type; static PyTypeObject *Sub_type; @@ -305,17 +298,12 @@ static PyTypeObject *BitAnd_type; static PyTypeObject *FloorDiv_type; static PyTypeObject *unaryop_type; -static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton, -*USub_singleton; static PyObject* ast2obj_unaryop(unaryop_ty); static PyTypeObject *Invert_type; static PyTypeObject *Not_type; static PyTypeObject *UAdd_type; static PyTypeObject *USub_type; static PyTypeObject *cmpop_type; -static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton, -*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton, -*NotIn_singleton; static PyObject* ast2obj_cmpop(cmpop_ty); static PyTypeObject *Eq_type; static PyTypeObject *NotEq_type; @@ -334,6 +322,13 @@ "iter", "ifs", }; +static PyTypeObject *Else_type; +static PyObject* ast2obj_Else(void*); +static char *Else_fields[]={ + "body", + "lineno", + "col_offset", +}; static PyTypeObject *excepthandler_type; static PyObject* ast2obj_excepthandler(void*); static char *excepthandler_fields[]={ @@ -369,13 +364,8 @@ { PyObject *fnames, *result; int i; - if (num_fields) { - fnames = PyTuple_New(num_fields); - if (!fnames) return NULL; - } else { - fnames = Py_None; - Py_INCREF(Py_None); - } + fnames = PyTuple_New(num_fields); + if (!fnames) return NULL; for(i=0; i < num_fields; i++) { PyObject *field = PyString_FromString(fields[i]); if (!field) { @@ -562,28 +552,16 @@ if (!add_attributes(expr_context_type, NULL, 0)) return 0; Load_type = make_type("Load", expr_context_type, NULL, 0); if (!Load_type) return 0; - Load_singleton = PyType_GenericNew(Load_type, NULL, NULL); - if (!Load_singleton) return 0; Store_type = make_type("Store", expr_context_type, NULL, 0); if (!Store_type) return 0; - Store_singleton = PyType_GenericNew(Store_type, NULL, NULL); - if (!Store_singleton) return 0; Del_type = make_type("Del", expr_context_type, NULL, 0); if (!Del_type) return 0; - Del_singleton = PyType_GenericNew(Del_type, NULL, NULL); - if (!Del_singleton) return 0; AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0); if (!AugLoad_type) return 0; - AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL); - if (!AugLoad_singleton) return 0; AugStore_type = make_type("AugStore", expr_context_type, NULL, 0); if (!AugStore_type) return 0; - AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL); - if (!AugStore_singleton) return 0; Param_type = make_type("Param", expr_context_type, NULL, 0); if (!Param_type) return 0; - Param_singleton = PyType_GenericNew(Param_type, NULL, NULL); - if (!Param_singleton) return 0; slice_type = make_type("slice", AST_type, NULL, 0); if (!slice_type) return 0; if (!add_attributes(slice_type, NULL, 0)) return 0; @@ -600,128 +578,74 @@ if (!add_attributes(boolop_type, NULL, 0)) return 0; And_type = make_type("And", boolop_type, NULL, 0); if (!And_type) return 0; - And_singleton = PyType_GenericNew(And_type, NULL, NULL); - if (!And_singleton) return 0; Or_type = make_type("Or", boolop_type, NULL, 0); if (!Or_type) return 0; - Or_singleton = PyType_GenericNew(Or_type, NULL, NULL); - if (!Or_singleton) return 0; operator_type = make_type("operator", AST_type, NULL, 0); if (!operator_type) return 0; if (!add_attributes(operator_type, NULL, 0)) return 0; Add_type = make_type("Add", operator_type, NULL, 0); if (!Add_type) return 0; - Add_singleton = PyType_GenericNew(Add_type, NULL, NULL); - if (!Add_singleton) return 0; Sub_type = make_type("Sub", operator_type, NULL, 0); if (!Sub_type) return 0; - Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL); - if (!Sub_singleton) return 0; Mult_type = make_type("Mult", operator_type, NULL, 0); if (!Mult_type) return 0; - Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL); - if (!Mult_singleton) return 0; Div_type = make_type("Div", operator_type, NULL, 0); if (!Div_type) return 0; - Div_singleton = PyType_GenericNew(Div_type, NULL, NULL); - if (!Div_singleton) return 0; Mod_type = make_type("Mod", operator_type, NULL, 0); if (!Mod_type) return 0; - Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL); - if (!Mod_singleton) return 0; Pow_type = make_type("Pow", operator_type, NULL, 0); if (!Pow_type) return 0; - Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL); - if (!Pow_singleton) return 0; LShift_type = make_type("LShift", operator_type, NULL, 0); if (!LShift_type) return 0; - LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL); - if (!LShift_singleton) return 0; RShift_type = make_type("RShift", operator_type, NULL, 0); if (!RShift_type) return 0; - RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL); - if (!RShift_singleton) return 0; BitOr_type = make_type("BitOr", operator_type, NULL, 0); if (!BitOr_type) return 0; - BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL); - if (!BitOr_singleton) return 0; BitXor_type = make_type("BitXor", operator_type, NULL, 0); if (!BitXor_type) return 0; - BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL); - if (!BitXor_singleton) return 0; BitAnd_type = make_type("BitAnd", operator_type, NULL, 0); if (!BitAnd_type) return 0; - BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL); - if (!BitAnd_singleton) return 0; FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0); if (!FloorDiv_type) return 0; - FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL); - if (!FloorDiv_singleton) return 0; unaryop_type = make_type("unaryop", AST_type, NULL, 0); if (!unaryop_type) return 0; if (!add_attributes(unaryop_type, NULL, 0)) return 0; Invert_type = make_type("Invert", unaryop_type, NULL, 0); if (!Invert_type) return 0; - Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL); - if (!Invert_singleton) return 0; Not_type = make_type("Not", unaryop_type, NULL, 0); if (!Not_type) return 0; - Not_singleton = PyType_GenericNew(Not_type, NULL, NULL); - if (!Not_singleton) return 0; UAdd_type = make_type("UAdd", unaryop_type, NULL, 0); if (!UAdd_type) return 0; - UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL); - if (!UAdd_singleton) return 0; USub_type = make_type("USub", unaryop_type, NULL, 0); if (!USub_type) return 0; - USub_singleton = PyType_GenericNew(USub_type, NULL, NULL); - if (!USub_singleton) return 0; cmpop_type = make_type("cmpop", AST_type, NULL, 0); if (!cmpop_type) return 0; if (!add_attributes(cmpop_type, NULL, 0)) return 0; Eq_type = make_type("Eq", cmpop_type, NULL, 0); if (!Eq_type) return 0; - Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL); - if (!Eq_singleton) return 0; NotEq_type = make_type("NotEq", cmpop_type, NULL, 0); if (!NotEq_type) return 0; - NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL); - if (!NotEq_singleton) return 0; Lt_type = make_type("Lt", cmpop_type, NULL, 0); if (!Lt_type) return 0; - Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL); - if (!Lt_singleton) return 0; LtE_type = make_type("LtE", cmpop_type, NULL, 0); if (!LtE_type) return 0; - LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL); - if (!LtE_singleton) return 0; Gt_type = make_type("Gt", cmpop_type, NULL, 0); if (!Gt_type) return 0; - Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL); - if (!Gt_singleton) return 0; GtE_type = make_type("GtE", cmpop_type, NULL, 0); if (!GtE_type) return 0; - GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL); - if (!GtE_singleton) return 0; Is_type = make_type("Is", cmpop_type, NULL, 0); if (!Is_type) return 0; - Is_singleton = PyType_GenericNew(Is_type, NULL, NULL); - if (!Is_singleton) return 0; IsNot_type = make_type("IsNot", cmpop_type, NULL, 0); if (!IsNot_type) return 0; - IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL); - if (!IsNot_singleton) return 0; In_type = make_type("In", cmpop_type, NULL, 0); if (!In_type) return 0; - In_singleton = PyType_GenericNew(In_type, NULL, NULL); - if (!In_singleton) return 0; NotIn_type = make_type("NotIn", cmpop_type, NULL, 0); if (!NotIn_type) return 0; - NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL); - if (!NotIn_singleton) return 0; comprehension_type = make_type("comprehension", AST_type, comprehension_fields, 3); if (!comprehension_type) return 0; + Else_type = make_type("Else", AST_type, Else_fields, 3); + if (!Else_type) return 0; excepthandler_type = make_type("excepthandler", AST_type, excepthandler_fields, 5); if (!excepthandler_type) return 0; @@ -959,8 +883,8 @@ } stmt_ty -For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int - lineno, int col_offset, PyArena *arena) +For(expr_ty target, expr_ty iter, asdl_seq * body, Else_ty orelse, int lineno, + int col_offset, PyArena *arena) { stmt_ty p; if (!target) { @@ -989,7 +913,7 @@ } stmt_ty -While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int +While(expr_ty test, asdl_seq * body, Else_ty orelse, int lineno, int col_offset, PyArena *arena) { stmt_ty p; @@ -1013,8 +937,8 @@ } stmt_ty -If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int - col_offset, PyArena *arena) +If(expr_ty test, asdl_seq * body, Else_ty orelse, int lineno, int col_offset, + PyArena *arena) { stmt_ty p; if (!test) { @@ -1080,8 +1004,8 @@ } stmt_ty -TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno, - int col_offset, PyArena *arena) +TryExcept(asdl_seq * body, asdl_seq * handlers, Else_ty orelse, int lineno, int + col_offset, PyArena *arena) { stmt_ty p; p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); @@ -1853,6 +1777,21 @@ return p; } +Else_ty +Else(asdl_seq * body, int lineno, int col_offset, PyArena *arena) +{ + Else_ty p; + p = (Else_ty)PyArena_Malloc(arena, sizeof(*p)); + if (!p) { + PyErr_NoMemory(); + return NULL; + } + p->body = body; + p->lineno = lineno; + p->col_offset = col_offset; + return p; +} + excepthandler_ty excepthandler(expr_ty type, expr_ty name, asdl_seq * body, int lineno, int col_offset, PyArena *arena) @@ -2129,7 +2068,7 @@ if (PyObject_SetAttrString(result, "body", value) == -1) goto failed; Py_DECREF(value); - value = ast2obj_list(o->v.For.orelse, ast2obj_stmt); + value = ast2obj_Else(o->v.For.orelse); if (!value) goto failed; if (PyObject_SetAttrString(result, "orelse", value) == -1) goto failed; @@ -2148,7 +2087,7 @@ if (PyObject_SetAttrString(result, "body", value) == -1) goto failed; Py_DECREF(value); - value = ast2obj_list(o->v.While.orelse, ast2obj_stmt); + value = ast2obj_Else(o->v.While.orelse); if (!value) goto failed; if (PyObject_SetAttrString(result, "orelse", value) == -1) goto failed; @@ -2167,7 +2106,7 @@ if (PyObject_SetAttrString(result, "body", value) == -1) goto failed; Py_DECREF(value); - value = ast2obj_list(o->v.If.orelse, ast2obj_stmt); + value = ast2obj_Else(o->v.If.orelse); if (!value) goto failed; if (PyObject_SetAttrString(result, "orelse", value) == -1) goto failed; @@ -2226,7 +2165,7 @@ if (PyObject_SetAttrString(result, "handlers", value) == -1) goto failed; Py_DECREF(value); - value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt); + value = ast2obj_Else(o->v.TryExcept.orelse); if (!value) goto failed; if (PyObject_SetAttrString(result, "orelse", value) == -1) goto failed; @@ -2682,23 +2621,23 @@ { switch(o) { case Load: - Py_INCREF(Load_singleton); - return Load_singleton; + Py_INCREF(Load_type); + return (PyObject *)Load_type; case Store: - Py_INCREF(Store_singleton); - return Store_singleton; + Py_INCREF(Store_type); + return (PyObject *)Store_type; case Del: - Py_INCREF(Del_singleton); - return Del_singleton; + Py_INCREF(Del_type); + return (PyObject *)Del_type; case AugLoad: - Py_INCREF(AugLoad_singleton); - return AugLoad_singleton; + Py_INCREF(AugLoad_type); + return (PyObject *)AugLoad_type; case AugStore: - Py_INCREF(AugStore_singleton); - return AugStore_singleton; + Py_INCREF(AugStore_type); + return (PyObject *)AugStore_type; case Param: - Py_INCREF(Param_singleton); - return Param_singleton; + Py_INCREF(Param_type); + return (PyObject *)Param_type; } return NULL; /* cannot happen */ } @@ -2766,11 +2705,11 @@ { switch(o) { case And: - Py_INCREF(And_singleton); - return And_singleton; + Py_INCREF(And_type); + return (PyObject *)And_type; case Or: - Py_INCREF(Or_singleton); - return Or_singleton; + Py_INCREF(Or_type); + return (PyObject *)Or_type; } return NULL; /* cannot happen */ } @@ -2778,41 +2717,41 @@ { switch(o) { case Add: - Py_INCREF(Add_singleton); - return Add_singleton; + Py_INCREF(Add_type); + return (PyObject *)Add_type; case Sub: - Py_INCREF(Sub_singleton); - return Sub_singleton; + Py_INCREF(Sub_type); + return (PyObject *)Sub_type; case Mult: - Py_INCREF(Mult_singleton); - return Mult_singleton; + Py_INCREF(Mult_type); + return (PyObject *)Mult_type; case Div: - Py_INCREF(Div_singleton); - return Div_singleton; + Py_INCREF(Div_type); + return (PyObject *)Div_type; case Mod: - Py_INCREF(Mod_singleton); - return Mod_singleton; + Py_INCREF(Mod_type); + return (PyObject *)Mod_type; case Pow: - Py_INCREF(Pow_singleton); - return Pow_singleton; + Py_INCREF(Pow_type); + return (PyObject *)Pow_type; case LShift: - Py_INCREF(LShift_singleton); - return LShift_singleton; + Py_INCREF(LShift_type); + return (PyObject *)LShift_type; case RShift: - Py_INCREF(RShift_singleton); - return RShift_singleton; + Py_INCREF(RShift_type); + return (PyObject *)RShift_type; case BitOr: - Py_INCREF(BitOr_singleton); - return BitOr_singleton; + Py_INCREF(BitOr_type); + return (PyObject *)BitOr_type; case BitXor: - Py_INCREF(BitXor_singleton); - return BitXor_singleton; + Py_INCREF(BitXor_type); + return (PyObject *)BitXor_type; case BitAnd: - Py_INCREF(BitAnd_singleton); - return BitAnd_singleton; + Py_INCREF(BitAnd_type); + return (PyObject *)BitAnd_type; case FloorDiv: - Py_INCREF(FloorDiv_singleton); - return FloorDiv_singleton; + Py_INCREF(FloorDiv_type); + return (PyObject *)FloorDiv_type; } return NULL; /* cannot happen */ } @@ -2820,17 +2759,17 @@ { switch(o) { case Invert: - Py_INCREF(Invert_singleton); - return Invert_singleton; + Py_INCREF(Invert_type); + return (PyObject *)Invert_type; case Not: - Py_INCREF(Not_singleton); - return Not_singleton; + Py_INCREF(Not_type); + return (PyObject *)Not_type; case UAdd: - Py_INCREF(UAdd_singleton); - return UAdd_singleton; + Py_INCREF(UAdd_type); + return (PyObject *)UAdd_type; case USub: - Py_INCREF(USub_singleton); - return USub_singleton; + Py_INCREF(USub_type); + return (PyObject *)USub_type; } return NULL; /* cannot happen */ } @@ -2838,35 +2777,35 @@ { switch(o) { case Eq: - Py_INCREF(Eq_singleton); - return Eq_singleton; + Py_INCREF(Eq_type); + return (PyObject *)Eq_type; case NotEq: - Py_INCREF(NotEq_singleton); - return NotEq_singleton; + Py_INCREF(NotEq_type); + return (PyObject *)NotEq_type; case Lt: - Py_INCREF(Lt_singleton); - return Lt_singleton; + Py_INCREF(Lt_type); + return (PyObject *)Lt_type; case LtE: - Py_INCREF(LtE_singleton); - return LtE_singleton; + Py_INCREF(LtE_type); + return (PyObject *)LtE_type; case Gt: - Py_INCREF(Gt_singleton); - return Gt_singleton; + Py_INCREF(Gt_type); + return (PyObject *)Gt_type; case GtE: - Py_INCREF(GtE_singleton); - return GtE_singleton; + Py_INCREF(GtE_type); + return (PyObject *)GtE_type; case Is: - Py_INCREF(Is_singleton); - return Is_singleton; + Py_INCREF(Is_type); + return (PyObject *)Is_type; case IsNot: - Py_INCREF(IsNot_singleton); - return IsNot_singleton; + Py_INCREF(IsNot_type); + return (PyObject *)IsNot_type; case In: - Py_INCREF(In_singleton); - return In_singleton; + Py_INCREF(In_type); + return (PyObject *)In_type; case NotIn: - Py_INCREF(NotIn_singleton); - return NotIn_singleton; + Py_INCREF(NotIn_type); + return (PyObject *)NotIn_type; } return NULL; /* cannot happen */ } @@ -2905,6 +2844,40 @@ } PyObject* +ast2obj_Else(void* _o) +{ + Else_ty o = (Else_ty)_o; + PyObject *result = NULL, *value = NULL; + if (!o) { + Py_INCREF(Py_None); + return Py_None; + } + + result = PyType_GenericNew(Else_type, NULL, NULL); + if (!result) return NULL; + value = ast2obj_list(o->body, ast2obj_stmt); + if (!value) goto failed; + if (PyObject_SetAttrString(result, "body", value) == -1) + goto failed; + Py_DECREF(value); + value = ast2obj_int(o->lineno); + if (!value) goto failed; + if (PyObject_SetAttrString(result, "lineno", value) == -1) + goto failed; + Py_DECREF(value); + value = ast2obj_int(o->col_offset); + if (!value) goto failed; + if (PyObject_SetAttrString(result, "col_offset", value) == -1) + goto failed; + Py_DECREF(value); + return result; +failed: + Py_XDECREF(value); + Py_XDECREF(result); + return NULL; +} + +PyObject* ast2obj_excepthandler(void* _o) { excepthandler_ty o = (excepthandler_ty)_o; @@ -3192,6 +3165,7 @@ if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return; if (PyDict_SetItemString(d, "comprehension", (PyObject*)comprehension_type) < 0) return; + if (PyDict_SetItemString(d, "Else", (PyObject*)Else_type) < 0) return; if (PyDict_SetItemString(d, "excepthandler", (PyObject*)excepthandler_type) < 0) return; if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < Index: Include/Python-ast.h =================================================================== --- Include/Python-ast.h (revision 53773) +++ Include/Python-ast.h (working copy) @@ -26,6 +26,8 @@ typedef struct _comprehension *comprehension_ty; +typedef struct _Else *Else_ty; + typedef struct _excepthandler *excepthandler_ty; typedef struct _arguments *arguments_ty; @@ -111,19 +113,19 @@ expr_ty target; expr_ty iter; asdl_seq *body; - asdl_seq *orelse; + Else_ty orelse; } For; struct { expr_ty test; asdl_seq *body; - asdl_seq *orelse; + Else_ty orelse; } While; struct { expr_ty test; asdl_seq *body; - asdl_seq *orelse; + Else_ty orelse; } If; struct { @@ -141,7 +143,7 @@ struct { asdl_seq *body; asdl_seq *handlers; - asdl_seq *orelse; + Else_ty orelse; } TryExcept; struct { @@ -323,6 +325,12 @@ asdl_seq *ifs; }; +struct _Else { + asdl_seq *body; + int lineno; + int col_offset; +}; + struct _excepthandler { expr_ty type; expr_ty name; @@ -379,14 +387,14 @@ stmt_ty _Py_Print(expr_ty dest, asdl_seq * values, bool nl, int lineno, int col_offset, PyArena *arena); #define For(a0, a1, a2, a3, a4, a5, a6) _Py_For(a0, a1, a2, a3, a4, a5, a6) -stmt_ty _Py_For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * - orelse, int lineno, int col_offset, PyArena *arena); +stmt_ty _Py_For(expr_ty target, expr_ty iter, asdl_seq * body, Else_ty orelse, + int lineno, int col_offset, PyArena *arena); #define While(a0, a1, a2, a3, a4, a5) _Py_While(a0, a1, a2, a3, a4, a5) -stmt_ty _Py_While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, +stmt_ty _Py_While(expr_ty test, asdl_seq * body, Else_ty orelse, int lineno, int col_offset, PyArena *arena); #define If(a0, a1, a2, a3, a4, a5) _Py_If(a0, a1, a2, a3, a4, a5) -stmt_ty _Py_If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, - int col_offset, PyArena *arena); +stmt_ty _Py_If(expr_ty test, asdl_seq * body, Else_ty orelse, int lineno, int + col_offset, PyArena *arena); #define With(a0, a1, a2, a3, a4, a5) _Py_With(a0, a1, a2, a3, a4, a5) stmt_ty _Py_With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno, int col_offset, PyArena *arena); @@ -394,8 +402,8 @@ stmt_ty _Py_Raise(expr_ty type, expr_ty inst, expr_ty tback, int lineno, int col_offset, PyArena *arena); #define TryExcept(a0, a1, a2, a3, a4, a5) _Py_TryExcept(a0, a1, a2, a3, a4, a5) -stmt_ty _Py_TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, - int lineno, int col_offset, PyArena *arena); +stmt_ty _Py_TryExcept(asdl_seq * body, asdl_seq * handlers, Else_ty orelse, int + lineno, int col_offset, PyArena *arena); #define TryFinally(a0, a1, a2, a3, a4) _Py_TryFinally(a0, a1, a2, a3, a4) stmt_ty _Py_TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset, PyArena *arena); @@ -487,6 +495,8 @@ #define comprehension(a0, a1, a2, a3) _Py_comprehension(a0, a1, a2, a3) comprehension_ty _Py_comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena); +#define Else(a0, a1, a2, a3) _Py_Else(a0, a1, a2, a3) +Else_ty _Py_Else(asdl_seq * body, int lineno, int col_offset, PyArena *arena); #define excepthandler(a0, a1, a2, a3, a4, a5) _Py_excepthandler(a0, a1, a2, a3, a4, a5) excepthandler_ty _Py_excepthandler(expr_ty type, expr_ty name, asdl_seq * body, int lineno, int col_offset, PyArena *arena); Index: Parser/Python.asdl =================================================================== --- Parser/Python.asdl (revision 53773) +++ Parser/Python.asdl (working copy) @@ -22,14 +22,14 @@ | Print(expr? dest, expr* values, bool nl) -- use 'orelse' because else is a keyword in target languages - | For(expr target, expr iter, stmt* body, stmt* orelse) - | While(expr test, stmt* body, stmt* orelse) - | If(expr test, stmt* body, stmt* orelse) + | For(expr target, expr iter, stmt* body, Else? orelse) + | While(expr test, stmt* body, Else? orelse) + | If(expr test, stmt* body, Else? orelse) | With(expr context_expr, expr? optional_vars, stmt* body) -- 'type' is a bad name | Raise(expr? type, expr? inst, expr? tback) - | TryExcept(stmt* body, excepthandler* handlers, stmt* orelse) + | TryExcept(stmt* body, excepthandler* handlers, Else? orelse) | TryFinally(stmt* body, stmt* finalbody) | Assert(expr test, expr? msg) @@ -96,6 +96,8 @@ cmpop = Eq | NotEq | Lt | LtE | Gt | GtE | Is | IsNot | In | NotIn comprehension = (expr target, expr iter, expr* ifs) + + Else = (stmt* body, int lineno, int col_offset) -- not sure what to call the first argument for raise and except -- TODO(jhylton): Figure out if there is a better way to handle Index: Parser/asdl_c.py =================================================================== --- Parser/asdl_c.py (revision 53773) +++ Parser/asdl_c.py (working copy) @@ -378,11 +378,6 @@ ptype = "void*" if is_simple(sum): ptype = get_c_type(name) - tnames = [] - for t in sum.types: - tnames.append(str(t.name)+"_singleton") - tnames = ", *".join(tnames) - self.emit("static PyObject *%s;" % tnames, 0) self.emit("static PyObject* ast2obj_%s(%s);" % (name, ptype), 0) for t in sum.types: self.visitConstructor(t, name) @@ -403,13 +398,8 @@ { PyObject *fnames, *result; int i; - if (num_fields) { - fnames = PyTuple_New(num_fields); - if (!fnames) return NULL; - } else { - fnames = Py_None; - Py_INCREF(Py_None); - } + fnames = PyTuple_New(num_fields); + if (!fnames) return NULL; for(i=0; i < num_fields; i++) { PyObject *field = PyString_FromString(fields[i]); if (!field) { @@ -520,10 +510,6 @@ self.emit('%s_type = make_type("%s", %s_type, %s, %d);' % (cons.name, cons.name, name, fields, len(cons.fields)), 1) self.emit("if (!%s_type) return 0;" % cons.name, 1) - if simple: - self.emit("%s_singleton = PyType_GenericNew(%s_type, NULL, NULL);" % - (cons.name, cons.name), 1) - self.emit("if (!%s_singleton) return 0;" % cons.name, 1) def parse_version(mod): return mod.version.value[12:-3] @@ -637,8 +623,10 @@ self.emit("switch(o) {", 1) for t in sum.types: self.emit("case %s:" % t.name, 2) - self.emit("Py_INCREF(%s_singleton);" % t.name, 3) - self.emit("return %s_singleton;" % t.name, 3) + #self.emit("Py_INCREF(%s_singleton);" % t.name, 3) + #self.emit("return %s_singleton;" % t.name, 3) + self.emit("Py_INCREF(%s_type);" % t.name, 3) + self.emit("return (PyObject *)%s_type;" % t.name, 3) self.emit("}", 1) self.emit("return NULL; /* cannot happen */", 1) self.emit("}", 0) Index: Lib/test/test_ast.py =================================================================== --- Lib/test/test_ast.py (revision 53773) +++ Lib/test/test_ast.py (working copy) @@ -6,10 +6,13 @@ return t elif isinstance(t, list): return [to_tuple(e) for e in t] - result = [t.__class__.__name__] + if isinstance(t, type): + result = [t.__name__] + else: + result = [t.__class__.__name__] if hasattr(t, 'lineno') and hasattr(t, 'col_offset'): result.append((t.lineno, t.col_offset)) - if t._fields is None: + if t._fields == (): return tuple(result) for f in t._fields: result.append(to_tuple(getattr(t, f))) @@ -130,7 +133,7 @@ def test_order(ast_node, parent_pos): - if not isinstance(ast_node, _ast.AST) or ast_node._fields == None: + if not isinstance(ast_node, _ast.AST) or ast_node._fields == (): return if isinstance(ast_node, (_ast.expr, _ast.stmt, _ast.excepthandler)): node_pos = (ast_node.lineno, ast_node.col_offset) @@ -150,7 +153,13 @@ (eval_tests, eval_results, "eval")): for i, o in itertools.izip(input, output): ast_tree = compile(i, "?", kind, 0x400) - assert to_tuple(ast_tree) == o + try: + assert to_tuple(ast_tree) == o + except AssertionError: + print to_tuple(ast_tree) + print o + raw_input() + raise test_order(ast_tree, (0, 0)) #### EVERYTHING BELOW IS GENERATED ##### @@ -162,11 +171,11 @@ ('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]), ('Module', [('AugAssign', (1, 0), ('Name', (1, 0), 'v', ('Store',)), ('Add',), ('Num', (1, 5), 1))]), ('Module', [('Print', (1, 0), ('Name', (1, 8), 'f', ('Load',)), [('Num', (1, 11), 1)], False)]), -('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Pass', (1, 11))], [])]), -('Module', [('While', (1, 0), ('Name', (1, 6), 'v', ('Load',)), [('Pass', (1, 8))], [])]), -('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [])]), +('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Pass', (1, 11))], None)]), +('Module', [('While', (1, 0), ('Name', (1, 6), 'v', ('Load',)), [('Pass', (1, 8))], None)]), +('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], None)]), ('Module', [('Raise', (1, 0), ('Name', (1, 6), 'Exception', ('Load',)), ('Str', (1, 17), 'string'), None)]), -('Module', [('TryExcept', (1, 0), [('Pass', (2, 2))], [('excepthandler', (3, 0), ('Name', (3, 7), 'Exception', ('Load',)), None, [('Pass', (4, 2))], 3, 0)], [])]), +('Module', [('TryExcept', (1, 0), [('Pass', (2, 2))], [('excepthandler', (3, 0), ('Name', (3, 7), 'Exception', ('Load',)), None, [('Pass', (4, 2))], 3, 0)], None)]), ('Module', [('TryFinally', (1, 0), [('Pass', (2, 2))], [('Pass', (4, 2))])]), ('Module', [('Assert', (1, 0), ('Name', (1, 7), 'v', ('Load',)), None)]), ('Module', [('Import', (1, 0), [('alias', 'sys', None)])]),