summaryrefslogtreecommitdiffstats
path: root/Python/ast.c
diff options
context:
space:
mode:
authorAlexandre Vassalotti <alexandre@peadrop.com>2010-01-09 23:35:54 (GMT)
committerAlexandre Vassalotti <alexandre@peadrop.com>2010-01-09 23:35:54 (GMT)
commitee936a21308679654b2d458166ff094ed735fef7 (patch)
tree612cd109e8ede4080f58f30ece3212d8e0f996d2 /Python/ast.c
parente36561352895170f28f77f0b4ec4292e35d1cc01 (diff)
downloadcpython-ee936a21308679654b2d458166ff094ed735fef7.zip
cpython-ee936a21308679654b2d458166ff094ed735fef7.tar.gz
cpython-ee936a21308679654b2d458166ff094ed735fef7.tar.bz2
Issue #2335: Backport set literals syntax from Python 3.x.
Diffstat (limited to 'Python/ast.c')
-rw-r--r--Python/ast.c65
1 files changed, 44 insertions, 21 deletions
diff --git a/Python/ast.c b/Python/ast.c
index c8875e0..01d1f7a 100644
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -1383,36 +1383,59 @@ ast_for_atom(struct compiling *c, const node *n)
else
return ast_for_listcomp(c, ch);
case LBRACE: {
- /* dictmaker: test ':' test (',' test ':' test)* [','] */
+ /* dictorsetmaker: test ':' test (',' test ':' test)* [','] |
+ * test (',' test)* [','])
+ */
int i, size;
asdl_seq *keys, *values;
-
+
ch = CHILD(n, 1);
- size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
- keys = asdl_seq_new(size, c->c_arena);
- if (!keys)
- return NULL;
-
- values = asdl_seq_new(size, c->c_arena);
- if (!values)
- return NULL;
-
- for (i = 0; i < NCH(ch); i += 4) {
- expr_ty expression;
+ if (TYPE(ch) == RBRACE) {
+ /* it's an empty dict */
+ return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
+ } else if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
+ /* it's a simple set */
+ asdl_seq *elts;
+ size = (NCH(ch) + 1) / 2; /* +1 in case no trailing comma */
+ elts = asdl_seq_new(size, c->c_arena);
+ if (!elts)
+ return NULL;
+ for (i = 0; i < NCH(ch); i += 2) {
+ expr_ty expression;
+ expression = ast_for_expr(c, CHILD(ch, i));
+ if (!expression)
+ return NULL;
+ asdl_seq_SET(elts, i / 2, expression);
+ }
+ return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
+ } else {
+ /* it's a dict */
+ size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
+ keys = asdl_seq_new(size, c->c_arena);
+ if (!keys)
+ return NULL;
- expression = ast_for_expr(c, CHILD(ch, i));
- if (!expression)
+ values = asdl_seq_new(size, c->c_arena);
+ if (!values)
return NULL;
+
+ for (i = 0; i < NCH(ch); i += 4) {
+ expr_ty expression;
+
+ expression = ast_for_expr(c, CHILD(ch, i));
+ if (!expression)
+ return NULL;
- asdl_seq_SET(keys, i / 4, expression);
+ asdl_seq_SET(keys, i / 4, expression);
- expression = ast_for_expr(c, CHILD(ch, i + 2));
- if (!expression)
- return NULL;
+ expression = ast_for_expr(c, CHILD(ch, i + 2));
+ if (!expression)
+ return NULL;
- asdl_seq_SET(values, i / 4, expression);
+ asdl_seq_SET(values, i / 4, expression);
+ }
+ return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
}
- return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
}
case BACKQUOTE: { /* repr */
expr_ty expression;