mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Fix parsing of subscriptlist.
(Armin's SF bug report). d = {} d[1,] = 1 Now handled correctly
This commit is contained in:
parent
02cbf4ae4b
commit
c7d37264bb
1 changed files with 30 additions and 4 deletions
32
Python/ast.c
32
Python/ast.c
|
@ -1388,7 +1388,10 @@ ast_for_binop(struct compiling *c, const node *n)
|
||||||
static expr_ty
|
static expr_ty
|
||||||
ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
|
ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
|
||||||
{
|
{
|
||||||
/* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME */
|
/* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
|
||||||
|
subscriptlist: subscript (',' subscript)* [',']
|
||||||
|
subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
|
||||||
|
*/
|
||||||
REQ(n, trailer);
|
REQ(n, trailer);
|
||||||
if (TYPE(CHILD(n, 0)) == LPAR) {
|
if (TYPE(CHILD(n, 0)) == LPAR) {
|
||||||
if (NCH(n) == 2)
|
if (NCH(n) == 2)
|
||||||
|
@ -1404,27 +1407,50 @@ ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
|
||||||
REQ(CHILD(n, 0), LSQB);
|
REQ(CHILD(n, 0), LSQB);
|
||||||
REQ(CHILD(n, 2), RSQB);
|
REQ(CHILD(n, 2), RSQB);
|
||||||
n = CHILD(n, 1);
|
n = CHILD(n, 1);
|
||||||
if (NCH(n) <= 2) {
|
if (NCH(n) == 1) {
|
||||||
slice_ty slc = ast_for_slice(c, CHILD(n, 0));
|
slice_ty slc = ast_for_slice(c, CHILD(n, 0));
|
||||||
if (!slc)
|
if (!slc)
|
||||||
return NULL;
|
return NULL;
|
||||||
return Subscript(left_expr, slc, Load, LINENO(n), c->c_arena);
|
return Subscript(left_expr, slc, Load, LINENO(n), c->c_arena);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
/* The grammar is ambiguous here. The ambiguity is resolved
|
||||||
|
by treating the sequence as a tuple literal if there are
|
||||||
|
no slice features.
|
||||||
|
*/
|
||||||
int j;
|
int j;
|
||||||
slice_ty slc;
|
slice_ty slc;
|
||||||
asdl_seq *slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
|
expr_ty e;
|
||||||
|
bool simple;
|
||||||
|
asdl_seq *slices, *elts;
|
||||||
|
slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
|
||||||
if (!slices)
|
if (!slices)
|
||||||
return NULL;
|
return NULL;
|
||||||
for (j = 0; j < NCH(n); j += 2) {
|
for (j = 0; j < NCH(n); j += 2) {
|
||||||
slc = ast_for_slice(c, CHILD(n, j));
|
slc = ast_for_slice(c, CHILD(n, j));
|
||||||
if (!slc)
|
if (!slc)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
if (slc->kind != Index_kind)
|
||||||
|
simple = false;
|
||||||
asdl_seq_SET(slices, j / 2, slc);
|
asdl_seq_SET(slices, j / 2, slc);
|
||||||
}
|
}
|
||||||
|
if (!simple) {
|
||||||
return Subscript(left_expr, ExtSlice(slices, c->c_arena),
|
return Subscript(left_expr, ExtSlice(slices, c->c_arena),
|
||||||
Load, LINENO(n), c->c_arena);
|
Load, LINENO(n), c->c_arena);
|
||||||
}
|
}
|
||||||
|
/* extract Index values and put them in a Tuple */
|
||||||
|
elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
|
||||||
|
for (j = 0; j < asdl_seq_LEN(slices); ++j) {
|
||||||
|
slc = (slice_ty)asdl_seq_GET(slices, j);
|
||||||
|
assert(slc->kind == Index_kind && slc->v.Index.value);
|
||||||
|
asdl_seq_SET(elts, j, slc->v.Index.value);
|
||||||
|
}
|
||||||
|
e = Tuple(elts, Load, LINENO(n), c->c_arena);
|
||||||
|
if (!e)
|
||||||
|
return NULL;
|
||||||
|
return Subscript(left_expr, Index(e, c->c_arena),
|
||||||
|
Load, LINENO(n), c->c_arena);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue