summaryrefslogtreecommitdiffstats
path: root/Python/ast.c
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2011-05-27 18:58:08 (GMT)
committerBenjamin Peterson <benjamin@python.org>2011-05-27 18:58:08 (GMT)
commitbf1bbc145299964a37cfae5bc5565177192f68ad (patch)
treece4eccef9452efe19126ae83e22d006a300328c0 /Python/ast.c
parent05010706697ce9c18e7f8a8e571753b0bcfd6548 (diff)
downloadcpython-bf1bbc145299964a37cfae5bc5565177192f68ad.zip
cpython-bf1bbc145299964a37cfae5bc5565177192f68ad.tar.gz
cpython-bf1bbc145299964a37cfae5bc5565177192f68ad.tar.bz2
reflect with statements with multiple items in the AST (closes #12106)
Diffstat (limited to 'Python/ast.c')
-rw-r--r--Python/ast.c43
1 files changed, 16 insertions, 27 deletions
diff --git a/Python/ast.c b/Python/ast.c
index 5b12da8..882452b 100644
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -2967,8 +2967,8 @@ ast_for_try_stmt(struct compiling *c, const node *n)
}
/* with_item: test ['as' expr] */
-static stmt_ty
-ast_for_with_item(struct compiling *c, const node *n, asdl_seq *content)
+static withitem_ty
+ast_for_with_item(struct compiling *c, const node *n)
{
expr_ty context_expr, optional_vars = NULL;
@@ -2987,43 +2987,32 @@ ast_for_with_item(struct compiling *c, const node *n, asdl_seq *content)
}
}
- return With(context_expr, optional_vars, content, LINENO(n),
- n->n_col_offset, c->c_arena);
+ return withitem(context_expr, optional_vars, c->c_arena);
}
/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
static stmt_ty
ast_for_with_stmt(struct compiling *c, const node *n)
{
- int i;
- stmt_ty ret;
- asdl_seq *inner;
+ int i, n_items;
+ asdl_seq *items, *body;
REQ(n, with_stmt);
- /* process the with items inside-out */
- i = NCH(n) - 1;
- /* the suite of the innermost with item is the suite of the with stmt */
- inner = ast_for_suite(c, CHILD(n, i));
- if (!inner)
- return NULL;
-
- for (;;) {
- i -= 2;
- ret = ast_for_with_item(c, CHILD(n, i), inner);
- if (!ret)
+ n_items = (NCH(n) - 2) / 2;
+ items = asdl_seq_new(n_items, c->c_arena);
+ for (i = 1; i < NCH(n) - 2; i += 2) {
+ withitem_ty item = ast_for_with_item(c, CHILD(n, i));
+ if (!item)
return NULL;
- /* was this the last item? */
- if (i == 1)
- break;
- /* if not, wrap the result so far in a new sequence */
- inner = asdl_seq_new(1, c->c_arena);
- if (!inner)
- return NULL;
- asdl_seq_SET(inner, 0, ret);
+ asdl_seq_SET(items, (i - 1) / 2, item);
}
- return ret;
+ body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
+ if (!body)
+ return NULL;
+
+ return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
}
static stmt_ty