Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix error for augassign
  • Loading branch information
pablogsal committed May 14, 2020
commit 4f2cb72b5ae98e5c24c3b4dfc94b0f121cc70769
19 changes: 6 additions & 13 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -641,20 +641,13 @@ invalid_assignment:
| a=expression ':' expression ['=' annotated_rhs] {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "illegal target for annotation") }
| 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=expression augassign (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))
)}
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
_PyPegen_get_invalid_target(a),
"cannot assign to %s", _PyPegen_get_expr_name(_PyPegen_get_invalid_target(a))) }
| a=expression augassign (yield_expr | star_expressions) { _PyPegen_raise_syntax_error_for_augassign(p, a) }
| a=star_expressions augassign (yield_expr | star_expressions) {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
a,
"illegal expression for augmented assignment"
)}
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "illegal expression for augmented assignment") }

invalid_block:
| NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
invalid_comprehension:
Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@

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

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

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

From compiler_complex_args():

Expand Down
2 changes: 1 addition & 1 deletion Parser/pegen/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -10878,7 +10878,7 @@ invalid_assignment_rule(Parser *p)
(_tmp_129_var = _tmp_129_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 ) ) );
_res = _PyPegen_raise_syntax_error_for_augassign ( p , a );
if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1;
return NULL;
Expand Down
16 changes: 15 additions & 1 deletion Parser/pegen/pegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -2093,4 +2093,18 @@ _PyPegen_get_invalid_target(expr_ty e)
default:
return e;
}
}
}

void *
_PyPegen_raise_syntax_error_for_augassign(Parser *p, expr_ty e) {
expr_ty invalid_target = _PyPegen_get_invalid_target(e);

if (invalid_target != NULL) {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(invalid_target,
"cannot assign to %s", _PyPegen_get_expr_name(invalid_target));
} else {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(e, "illegal expression for augmented assignment");
}
return NULL;
}

2 changes: 1 addition & 1 deletion Parser/pegen/pegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ mod_ty _PyPegen_make_module(Parser *, asdl_seq *);
// Error reporting helpers

expr_ty _PyPegen_get_invalid_target(expr_ty e);

void *_PyPegen_raise_syntax_error_for_augassign(Parser *p, expr_ty e);

void *_PyPegen_parse(Parser *);

Expand Down