Mailing List Archive

bpo-34013: Don't consider a grouped expression when reporting legacy print syntax errors (GH-27521)
https://github.com/python/cpython/commit/208a7e957b812ad3b3733791845447677a704f3e
commit: 208a7e957b812ad3b3733791845447677a704f3e
branch: main
author: Pablo Galindo Salgado <Pablogsal@gmail.com>
committer: pablogsal <Pablogsal@gmail.com>
date: 2021-08-01T02:10:50+01:00
summary:

bpo-34013: Don't consider a grouped expression when reporting legacy print syntax errors (GH-27521)

files:
M Grammar/python.gram
M Lib/test/test_exceptions.py
M Parser/parser.c

diff --git a/Grammar/python.gram b/Grammar/python.gram
index 278ddfb2d1a30..8a21056d2a82b 100644
--- a/Grammar/python.gram
+++ b/Grammar/python.gram
@@ -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}

diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index 8ea415f45a6d4..b280cfea435fd 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -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)
diff --git a/Parser/parser.c b/Parser/parser.c
index c05fc31c2436e..7a106a437bc31 100644
--- a/Parser/parser.c
+++ b/Parser/parser.c
@@ -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)
{
@@ -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;
@@ -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:

_______________________________________________
Python-checkins mailing list
Python-checkins@python.org
https://mail.python.org/mailman/listinfo/python-checkins
bpo-34013: Don't consider a grouped expression when reporting legacy print syntax errors (GH-27521) [ In reply to ]
https://github.com/python/cpython/commit/35035bc35a9cb8617ab9fe9aac38aaf67c926aef
commit: 35035bc35a9cb8617ab9fe9aac38aaf67c926aef
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
committer: miss-islington <31488909+miss-islington@users.noreply.github.com>
date: 2021-07-31T18:31:44-07:00
summary:

bpo-34013: Don't consider a grouped expression when reporting legacy print syntax errors (GH-27521)

(cherry picked from commit 208a7e957b812ad3b3733791845447677a704f3e)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>

files:
M Grammar/python.gram
M Lib/test/test_exceptions.py
M Parser/parser.c

diff --git a/Grammar/python.gram b/Grammar/python.gram
index 70f2de8b9dece5..057bcb641a6666 100644
--- a/Grammar/python.gram
+++ b/Grammar/python.gram
@@ -849,7 +849,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}

diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index 8ffebebaeb83d1..339801ebaeaef2 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -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)
diff --git a/Parser/parser.c b/Parser/parser.c
index cda5c635276dde..c4a4eb651b3e78 100644
--- a/Parser/parser.c
+++ b/Parser/parser.c
@@ -18180,7 +18180,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)
{
@@ -18191,21 +18191,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;
@@ -18216,7 +18218,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:

_______________________________________________
Python-checkins mailing list
Python-checkins@python.org
https://mail.python.org/mailman/listinfo/python-checkins