Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c7d3726

Browse files
committed
Fix parsing of subscriptlist.
(Armin's SF bug report). d = {} d[1,] = 1 Now handled correctly
1 parent 02cbf4a commit c7d3726

1 file changed

Lines changed: 30 additions & 4 deletions

File tree

Python/ast.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,10 @@ ast_for_binop(struct compiling *c, const node *n)
13881388
static expr_ty
13891389
ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
13901390
{
1391-
/* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME */
1391+
/* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1392+
subscriptlist: subscript (',' subscript)* [',']
1393+
subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1394+
*/
13921395
REQ(n, trailer);
13931396
if (TYPE(CHILD(n, 0)) == LPAR) {
13941397
if (NCH(n) == 2)
@@ -1404,25 +1407,48 @@ ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
14041407
REQ(CHILD(n, 0), LSQB);
14051408
REQ(CHILD(n, 2), RSQB);
14061409
n = CHILD(n, 1);
1407-
if (NCH(n) <= 2) {
1410+
if (NCH(n) == 1) {
14081411
slice_ty slc = ast_for_slice(c, CHILD(n, 0));
14091412
if (!slc)
14101413
return NULL;
14111414
return Subscript(left_expr, slc, Load, LINENO(n), c->c_arena);
14121415
}
14131416
else {
1417+
/* The grammar is ambiguous here. The ambiguity is resolved
1418+
by treating the sequence as a tuple literal if there are
1419+
no slice features.
1420+
*/
14141421
int j;
14151422
slice_ty slc;
1416-
asdl_seq *slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1423+
expr_ty e;
1424+
bool simple;
1425+
asdl_seq *slices, *elts;
1426+
slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
14171427
if (!slices)
14181428
return NULL;
14191429
for (j = 0; j < NCH(n); j += 2) {
14201430
slc = ast_for_slice(c, CHILD(n, j));
14211431
if (!slc)
14221432
return NULL;
1433+
if (slc->kind != Index_kind)
1434+
simple = false;
14231435
asdl_seq_SET(slices, j / 2, slc);
14241436
}
1425-
return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1437+
if (!simple) {
1438+
return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1439+
Load, LINENO(n), c->c_arena);
1440+
}
1441+
/* extract Index values and put them in a Tuple */
1442+
elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1443+
for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1444+
slc = (slice_ty)asdl_seq_GET(slices, j);
1445+
assert(slc->kind == Index_kind && slc->v.Index.value);
1446+
asdl_seq_SET(elts, j, slc->v.Index.value);
1447+
}
1448+
e = Tuple(elts, Load, LINENO(n), c->c_arena);
1449+
if (!e)
1450+
return NULL;
1451+
return Subscript(left_expr, Index(e, c->c_arena),
14261452
Load, LINENO(n), c->c_arena);
14271453
}
14281454
}

0 commit comments

Comments
 (0)