bpo-40631: Improve error message when deleting single starred element

This commit is contained in:
Pablo Galindo 2020-12-30 21:22:43 +00:00
parent 2edfc86f69
commit a9176efbcd
2 changed files with 30 additions and 1 deletions

View File

@ -129,6 +129,8 @@ yield_stmt[stmt_ty]: y=yield_expr { _Py_Expr(y, EXTRA) }
assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _Py_Assert(a, b, EXTRA) }
del_stmt[stmt_ty]:
| 'del' a=star_element {
RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
| 'del' a=del_targets &(';' | NEWLINE) { _Py_Delete(a, EXTRA) }
| invalid_del_stmt

View File

@ -2829,7 +2829,7 @@ assert_stmt_rule(Parser *p)
return _res;
}
// del_stmt: 'del' del_targets &(';' | NEWLINE) | invalid_del_stmt
// del_stmt: 'del' star_element | 'del' del_targets &(';' | NEWLINE) | invalid_del_stmt
static stmt_ty
del_stmt_rule(Parser *p)
{
@ -2849,6 +2849,33 @@ del_stmt_rule(Parser *p)
UNUSED(_start_lineno); // Only used by EXTRA macro
int _start_col_offset = p->tokens[_mark]->col_offset;
UNUSED(_start_col_offset); // Only used by EXTRA macro
{ // 'del' star_element
if (p->error_indicator) {
D(p->level--);
return NULL;
}
D(fprintf(stderr, "%*c> del_stmt[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'del' star_element"));
Token * _keyword;
expr_ty a;
if (
(_keyword = _PyPegen_expect_token(p, 503)) // token='del'
&&
(a = star_element_rule(p)) // star_element
)
{
D(fprintf(stderr, "%*c+ del_stmt[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'del' star_element"));
_res = RAISE_SYNTAX_ERROR_INVALID_TARGET ( DEL_TARGETS , a );
if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1;
D(p->level--);
return NULL;
}
goto done;
}
p->mark = _mark;
D(fprintf(stderr, "%*c%s del_stmt[%d-%d]: %s failed!\n", p->level, ' ',
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'del' star_element"));
}
{ // 'del' del_targets &(';' | NEWLINE)
if (p->error_indicator) {
D(p->level--);