Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-40334: Correctly identify invalid target in assignment errors #20076

Merged
merged 9 commits into from
May 15, 2020
13 changes: 11 additions & 2 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,17 @@ invalid_assignment:
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "only single target (not tuple) can be annotated") }
| a=expression ':' expression ['=' annotated_rhs] {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "illegal target for annotation") }
| a=expression ('=' | augassign) (yield_expr | star_expressions) {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot assign to %s", _PyPegen_get_expr_name(a)) }
| a=star_expressions '=' (yield_expr | star_expressions) {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
_PyPegen_get_invalid_target(a),
"cannot assign to %s", _PyPegen_get_expr_name(_PyPegen_get_invalid_target(a))) }
| a=star_expressions augassign (yield_expr | star_expressions) {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
a,
"'%s' is an illegal expression for augmented assignment",
_PyPegen_get_expr_name(a)
)}

invalid_block:
| NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
invalid_comprehension:
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_dictcomps.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_illegal_assignment(self):
compile("{x: y for y, x in ((1, 2), (3, 4))} = 5", "<test>",
"exec")

with self.assertRaisesRegex(SyntaxError, "cannot assign"):
with self.assertRaisesRegex(SyntaxError, "illegal expression"):
compile("{x: y for y, x in ((1, 2), (3, 4))} += 5", "<test>",
"exec")

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1921,7 +1921,7 @@ def printsolution(self, x):
>>> def f(): (yield bar) += y
Traceback (most recent call last):
...
SyntaxError: cannot assign to yield expression
SyntaxError: 'yield expression' is an illegal expression for augmented assignment


Now check some throw() conditions:
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_genexps.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
>>> (y for y in (1,2)) += 10
Traceback (most recent call last):
...
SyntaxError: cannot assign to generator expression
SyntaxError: 'generator expression' is an illegal expression for augmented assignment


########### Tests borrowed from or inspired by test_generators.py ############
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_peg_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ def f():
("(a, b): int", "only single target (not tuple) can be annotated"),
("[a, b]: int", "only single target (not list) can be annotated"),
("a(): int", "illegal target for annotation"),
("1 += 1", "cannot assign to literal"),
("1 += 1", "'literal' is an illegal expression for augmented assignment"),
("pass\n pass", "unexpected indent"),
("def f():\npass", "expected an indented block"),
("def f(*): pass", "named arguments must follow bare *"),
Expand Down
46 changes: 27 additions & 19 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,46 +100,53 @@
This test just checks a couple of cases rather than enumerating all of
them.

# All of the following also produce different error messages with pegen
# >>> (a, "b", c) = (1, 2, 3)
# Traceback (most recent call last):
# SyntaxError: cannot assign to literal
>>> (a, "b", c) = (1, 2, 3)
Traceback (most recent call last):
SyntaxError: cannot assign to literal

# >>> (a, True, c) = (1, 2, 3)
# Traceback (most recent call last):
# SyntaxError: cannot assign to True
>>> (a, True, c) = (1, 2, 3)
Traceback (most recent call last):
SyntaxError: cannot assign to True

>>> (a, __debug__, c) = (1, 2, 3)
Traceback (most recent call last):
SyntaxError: cannot assign to __debug__

# >>> (a, *True, c) = (1, 2, 3)
# Traceback (most recent call last):
# SyntaxError: cannot assign to True
>>> (a, *True, c) = (1, 2, 3)
Traceback (most recent call last):
SyntaxError: cannot assign to True

>>> (a, *__debug__, c) = (1, 2, 3)
Traceback (most recent call last):
SyntaxError: cannot assign to __debug__

# >>> [a, b, c + 1] = [1, 2, 3]
# Traceback (most recent call last):
# SyntaxError: cannot assign to operator
>>> [a, b, c + 1] = [1, 2, 3]
Traceback (most recent call last):
SyntaxError: cannot assign to operator

>>> [a, b[1], c + 1] = [1, 2, 3]
Traceback (most recent call last):
SyntaxError: cannot assign to operator

>>> [a, b.c.d, c + 1] = [1, 2, 3]
Traceback (most recent call last):
SyntaxError: cannot assign to operator

>>> a if 1 else b = 1
Traceback (most recent call last):
SyntaxError: cannot assign to conditional expression

>>> a, b += 1, 2
Traceback (most recent call last):
SyntaxError: invalid syntax
SyntaxError: 'tuple' is an illegal expression for augmented assignment

>>> (a, b) += 1, 2
Traceback (most recent call last):
SyntaxError: cannot assign to tuple
SyntaxError: 'tuple' is an illegal expression for augmented assignment

>>> [a, b] += 1, 2
Traceback (most recent call last):
SyntaxError: cannot assign to list
SyntaxError: 'list' is an illegal expression for augmented assignment

From compiler_complex_args():

Expand Down Expand Up @@ -346,16 +353,16 @@

>>> (x for x in x) += 1
Traceback (most recent call last):
SyntaxError: cannot assign to generator expression
SyntaxError: 'generator expression' is an illegal expression for augmented assignment
>>> None += 1
Traceback (most recent call last):
SyntaxError: cannot assign to None
SyntaxError: 'None' is an illegal expression for augmented assignment
>>> __debug__ += 1
Traceback (most recent call last):
SyntaxError: cannot assign to __debug__
>>> f() += 1
Traceback (most recent call last):
SyntaxError: cannot assign to function call
SyntaxError: 'function call' is an illegal expression for augmented assignment


Test continue in finally in weird combinations.
Expand Down Expand Up @@ -688,6 +695,7 @@ def _check_error(self, code, errtext,
def test_assign_call(self):
self._check_error("f() = 1", "assign")

@unittest.skipIf(support.use_old_parser(), "The old parser cannot generate these error messages")
def test_assign_del(self):
self._check_error("del (,)", "invalid syntax")
self._check_error("del 1", "delete literal")
Expand Down
50 changes: 36 additions & 14 deletions Parser/pegen/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -10747,7 +10747,8 @@ invalid_named_expression_rule(Parser *p)
// | tuple ':'
// | star_named_expression ',' star_named_expressions* ':'
// | expression ':' expression ['=' annotated_rhs]
// | expression ('=' | augassign) (yield_expr | star_expressions)
// | star_expressions '=' (yield_expr | star_expressions)
// | star_expressions augassign (yield_expr | star_expressions)
static void *
invalid_assignment_rule(Parser *p)
{
Expand Down Expand Up @@ -10841,19 +10842,40 @@ invalid_assignment_rule(Parser *p)
}
p->mark = _mark;
}
{ // expression ('=' | augassign) (yield_expr | star_expressions)
{ // star_expressions '=' (yield_expr | star_expressions)
Token * _literal;
void *_tmp_128_var;
expr_ty a;
if (
(a = star_expressions_rule(p)) // star_expressions
&&
(_literal = _PyPegen_expect_token(p, 22)) // token='='
&&
(_tmp_128_var = _tmp_128_rule(p)) // yield_expr | star_expressions
)
{
_res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( _PyPegen_get_invalid_target ( a ) , "cannot assign to %s" , _PyPegen_get_expr_name ( _PyPegen_get_invalid_target ( a ) ) );
if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1;
return NULL;
}
goto done;
}
p->mark = _mark;
}
{ // star_expressions augassign (yield_expr | star_expressions)
void *_tmp_129_var;
expr_ty a;
AugOperator* augassign_var;
if (
(a = expression_rule(p)) // expression
(a = star_expressions_rule(p)) // star_expressions
&&
(_tmp_128_var = _tmp_128_rule(p)) // '=' | augassign
(augassign_var = augassign_rule(p)) // augassign
&&
(_tmp_129_var = _tmp_129_rule(p)) // yield_expr | star_expressions
)
{
_res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "cannot assign to %s" , _PyPegen_get_expr_name ( a ) );
_res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "'%s' is an illegal expression for augmented assignment" , _PyPegen_get_expr_name ( a ) );
if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1;
return NULL;
Expand Down Expand Up @@ -16675,7 +16697,7 @@ _tmp_127_rule(Parser *p)
return _res;
}

// _tmp_128: '=' | augassign
// _tmp_128: yield_expr | star_expressions
static void *
_tmp_128_rule(Parser *p)
{
Expand All @@ -16684,24 +16706,24 @@ _tmp_128_rule(Parser *p)
}
void * _res = NULL;
int _mark = p->mark;
{ // '='
Token * _literal;
{ // yield_expr
expr_ty yield_expr_var;
if (
(_literal = _PyPegen_expect_token(p, 22)) // token='='
(yield_expr_var = yield_expr_rule(p)) // yield_expr
)
{
_res = _literal;
_res = yield_expr_var;
goto done;
}
p->mark = _mark;
}
{ // augassign
AugOperator* augassign_var;
{ // star_expressions
expr_ty star_expressions_var;
if (
(augassign_var = augassign_rule(p)) // augassign
(star_expressions_var = star_expressions_rule(p)) // star_expressions
)
{
_res = augassign_var;
_res = star_expressions_var;
goto done;
}
p->mark = _mark;
Expand Down
46 changes: 46 additions & 0 deletions Parser/pegen/pegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -2054,3 +2054,49 @@ _PyPegen_make_module(Parser *p, asdl_seq *a) {
}
return Module(a, type_ignores, p->arena);
}

// Error reporting helpers

expr_ty
_PyPegen_get_invalid_target(expr_ty e)
{
if (e == NULL) {
return NULL;
}

#define VISIT_CONTAINER(CONTAINER, TYPE) do { \
Py_ssize_t len = asdl_seq_LEN(CONTAINER->v.TYPE.elts);\
for (Py_ssize_t i = 0; i < len; i++) {\
expr_ty other = asdl_seq_GET(CONTAINER->v.TYPE.elts, i);\
expr_ty child = _PyPegen_get_invalid_target(other);\
if (child != NULL) {\
return child;\
}\
}\
} while (0)

// We only need to visit List and Tuple nodes recursively as those
// are the only ones that can contain valid names in targets when
// they are parsed as expressions. Any other kind of expression
// that is a container (like Sets or Dicts) is directly invalid and
// we don't need to visit it recursively.

switch (e->kind) {
pablogsal marked this conversation as resolved.
Show resolved Hide resolved
case List_kind: {
VISIT_CONTAINER(e, List);
return NULL;
}
case Tuple_kind: {
VISIT_CONTAINER(e, Tuple);
return NULL;
}
case Starred_kind:
return _PyPegen_get_invalid_target(e->v.Starred.value);
case Name_kind:
case Subscript_kind:
case Attribute_kind:
return NULL;
default:
return e;
}
}
4 changes: 4 additions & 0 deletions Parser/pegen/pegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ void *_PyPegen_arguments_parsing_error(Parser *, expr_ty);
int _PyPegen_check_barry_as_flufl(Parser *);
mod_ty _PyPegen_make_module(Parser *, asdl_seq *);

// Error reporting helpers

expr_ty _PyPegen_get_invalid_target(expr_ty e);

void *_PyPegen_parse(Parser *);

#endif
13 changes: 8 additions & 5 deletions Python/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -3164,10 +3164,7 @@ ast_for_expr_stmt(struct compiling *c, const node *n)
expr1 = ast_for_testlist(c, ch);
if (!expr1)
return NULL;
if(!set_context(c, expr1, Store, ch))
return NULL;
/* set_context checks that most expressions are not the left side.
Augmented assignments can only have a name, a subscript, or an
/* Augmented assignments can only have a name, a subscript, or an
attribute on the left, though, so we have to explicitly check for
those. */
switch (expr1->kind) {
Expand All @@ -3176,10 +3173,16 @@ ast_for_expr_stmt(struct compiling *c, const node *n)
case Subscript_kind:
break;
default:
ast_error(c, ch, "illegal expression for augmented assignment");
ast_error(c, ch, "'%s' is an illegal expression for augmented assignment",
get_expr_name(expr1));
return NULL;
}

/* set_context checks that most expressions are not the left side. */
if(!set_context(c, expr1, Store, ch)) {
return NULL;
}

ch = CHILD(n, 2);
if (TYPE(ch) == testlist)
expr2 = ast_for_testlist(c, ch);
Expand Down