Skip to content

Commit

Permalink
bpo-40631: Improve error message when deleting single starred element
Browse files Browse the repository at this point in the history
  • Loading branch information
pablogsal committed Jan 21, 2021
1 parent dcea78f commit 6035080
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Grammar/python.gram
Expand Up @@ -724,6 +724,8 @@ invalid_for_target:
RAISE_SYNTAX_ERROR_INVALID_TARGET(FOR_TARGETS, a) }

invalid_group:
| '(' a=star_expression ')' {
RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
| '(' a=starred_expression ')' {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "can't use starred expression here") }
invalid_import_from_targets:
Expand Down
5 changes: 4 additions & 1 deletion Lib/test/test_syntax.py
Expand Up @@ -789,6 +789,7 @@ def _check_error(self, code, errtext,
try:
compile(code, filename, mode)
except SyntaxError as err:
print(err)
if subclass and not isinstance(err, subclass):
self.fail("SyntaxError is not a %s" % subclass.__name__)
mo = re.search(errtext, str(err))
Expand Down Expand Up @@ -821,7 +822,9 @@ def test_assign_del(self):
self._check_error("del (1, 2)", "delete literal")
self._check_error("del None", "delete None")
self._check_error("del *x", "delete starred")
self._check_error("del (*x)", "use starred expression")
self._check_error("del (*x)", "delete starred")
self._check_error("del (*x, *y)", "delete starred")
self._check_error("del (x, *y)", "delete starred")
self._check_error("del (*x,)", "delete starred")
self._check_error("del [*x,]", "delete starred")
self._check_error("del f()", "delete function call")
Expand Down
32 changes: 31 additions & 1 deletion Parser/parser.c
Expand Up @@ -15777,7 +15777,7 @@ invalid_for_target_rule(Parser *p)
return _res;
}

// invalid_group: '(' starred_expression ')'
// invalid_group: '(' star_expression ')' | '(' starred_expression ')'
static void *
invalid_group_rule(Parser *p)
{
Expand All @@ -15788,6 +15788,36 @@ invalid_group_rule(Parser *p)
}
void * _res = NULL;
int _mark = p->mark;
{ // '(' star_expression ')'
if (p->error_indicator) {
D(p->level--);
return NULL;
}
D(fprintf(stderr, "%*c> invalid_group[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'(' star_expression ')'"));
Token * _literal;
Token * _literal_1;
expr_ty a;
if (
(_literal = _PyPegen_expect_token(p, 7)) // token='('
&&
(a = star_expression_rule(p)) // star_expression
&&
(_literal_1 = _PyPegen_expect_token(p, 8)) // token=')'
)
{
D(fprintf(stderr, "%*c+ invalid_group[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'(' star_expression ')'"));
_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 invalid_group[%d-%d]: %s failed!\n", p->level, ' ',
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'(' star_expression ')'"));
}
{ // '(' starred_expression ')'
if (p->error_indicator) {
D(p->level--);
Expand Down

0 comments on commit 6035080

Please sign in to comment.