Skip to content

Commit

Permalink
bpo-34013: Don't consider a grouped expression when reporting legacy …
Browse files Browse the repository at this point in the history
…print syntax errors (pythonGH-27521)
  • Loading branch information
pablogsal authored Aug 1, 2021
1 parent b08c48e commit 208a7e9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ expression_without_invalid[expr_ty]:
| disjunction
| lambdef
invalid_legacy_expression:
| a=NAME b=star_expressions {
| a=NAME !'(' b=star_expressions {
_PyPegen_check_legacy_stmt(p, a) ? RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b,
"Missing parentheses in call to '%U'. Did you mean %U(...)?", a->v.Name.id, a->v.Name.id) : NULL}

Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ def ckmsg(src, msg, exception=SyntaxError):
s = 'exec f(a+b,c)'
ckmsg(s, "Missing parentheses in call to 'exec'. Did you mean exec(...)?")

# Check that we don't incorrectly identify '(...)' as an expression to the right
# of 'print'

s = 'print (a+b,c) $ 42'
ckmsg(s, "invalid syntax")

s = 'exec (a+b,c) $ 42'
ckmsg(s, "invalid syntax")

# should not apply to subclasses, see issue #31161
s = '''if True:\nprint "No indent"'''
ckmsg(s, "expected an indented block after 'if' statement on line 1", IndentationError)
Expand Down
12 changes: 7 additions & 5 deletions Parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -18158,7 +18158,7 @@ expression_without_invalid_rule(Parser *p)
return _res;
}

// invalid_legacy_expression: NAME star_expressions
// invalid_legacy_expression: NAME !'(' star_expressions
static void *
invalid_legacy_expression_rule(Parser *p)
{
Expand All @@ -18169,21 +18169,23 @@ invalid_legacy_expression_rule(Parser *p)
}
void * _res = NULL;
int _mark = p->mark;
{ // NAME star_expressions
{ // NAME !'(' star_expressions
if (p->error_indicator) {
D(p->level--);
return NULL;
}
D(fprintf(stderr, "%*c> invalid_legacy_expression[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "NAME star_expressions"));
D(fprintf(stderr, "%*c> invalid_legacy_expression[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "NAME !'(' star_expressions"));
expr_ty a;
expr_ty b;
if (
(a = _PyPegen_name_token(p)) // NAME
&&
_PyPegen_lookahead_with_int(0, _PyPegen_expect_token, p, 7) // token='('
&&
(b = star_expressions_rule(p)) // star_expressions
)
{
D(fprintf(stderr, "%*c+ invalid_legacy_expression[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "NAME star_expressions"));
D(fprintf(stderr, "%*c+ invalid_legacy_expression[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "NAME !'(' star_expressions"));
_res = _PyPegen_check_legacy_stmt ( p , a ) ? RAISE_SYNTAX_ERROR_KNOWN_RANGE ( a , b , "Missing parentheses in call to '%U'. Did you mean %U(...)?" , a -> v . Name . id , a -> v . Name . id ) : NULL;
if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1;
Expand All @@ -18194,7 +18196,7 @@ invalid_legacy_expression_rule(Parser *p)
}
p->mark = _mark;
D(fprintf(stderr, "%*c%s invalid_legacy_expression[%d-%d]: %s failed!\n", p->level, ' ',
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "NAME star_expressions"));
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "NAME !'(' star_expressions"));
}
_res = NULL;
done:
Expand Down

0 comments on commit 208a7e9

Please sign in to comment.