Skip to content

Commit c72311d

Browse files
authored
[3.10] bpo-45727: Only trigger the 'did you forgot a comma' error suggestion if inside parentheses. (GH-29767)
Backport of GH-29757 Co-authored-by: Pablo Galindo <[email protected]>
1 parent cd85d91 commit c72311d

File tree

8 files changed

+15
-9
lines changed

8 files changed

+15
-9
lines changed

Grammar/python.gram

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -859,8 +859,8 @@ invalid_expression:
859859
# !(NAME STRING) is not matched so we don't show this error with some invalid string prefixes like: kf"dsfsdf"
860860
# Soft keywords need to also be ignored because they can be parsed as NAME NAME
861861
| !(NAME STRING | SOFT_KEYWORD) a=disjunction b=expression_without_invalid {
862-
_PyPegen_check_legacy_stmt(p, a) ? NULL : RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b,
863-
"invalid syntax. Perhaps you forgot a comma?") }
862+
_PyPegen_check_legacy_stmt(p, a) ? NULL : p->tokens[p->mark-1]->level == 0 ? NULL :
863+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") }
864864
| a=disjunction 'if' b=disjunction !('else'|':') { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected 'else' after 'if' expression") }
865865

866866
invalid_named_expression:

Lib/test/test_exceptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,14 @@ def testSyntaxErrorOffset(self):
226226
check(b'Python = "\xcf\xb3\xf2\xee\xed" +', 1, 18)
227227
check('x = "a', 1, 5)
228228
check('lambda x: x = 2', 1, 1)
229-
check('f{a + b + c}', 1, 1)
229+
check('f{a + b + c}', 1, 2)
230230
check('[file for str(file) in []\n])', 1, 11)
231231
check('a = « hello » « world »', 1, 5)
232232
check('[\nfile\nfor str(file)\nin\n[]\n]', 3, 5)
233233
check('[file for\n str(file) in []]', 2, 2)
234234
check("ages = {'Alice'=22, 'Bob'=23}", 1, 16)
235235
check('match ...:\n case {**rest, "key": value}:\n ...', 2, 19)
236-
check("a b c d e f", 1, 1)
236+
check("[a b c d e f]", 1, 2)
237237

238238
# Errors thrown by compile.c
239239
check('class foo:return 1', 1, 11)

Lib/test/test_fstring.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ def test_invalid_string_prefixes(self):
944944
"Bf''",
945945
"BF''",]
946946
double_quote_cases = [case.replace("'", '"') for case in single_quote_cases]
947-
self.assertAllRaise(SyntaxError, 'unexpected EOF while parsing',
947+
self.assertAllRaise(SyntaxError, 'invalid syntax',
948948
single_quote_cases + double_quote_cases)
949949

950950
def test_leading_trailing_spaces(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Refine the custom syntax error that suggests that a comma may be missing to
2+
trigger only when the expressions are detected between parentheses or
3+
brackets. Patch by Pablo Galindo

Parser/parser.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18288,7 +18288,7 @@ invalid_expression_rule(Parser *p)
1828818288
)
1828918289
{
1829018290
D(fprintf(stderr, "%*c+ invalid_expression[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "!(NAME STRING | SOFT_KEYWORD) disjunction expression_without_invalid"));
18291-
_res = _PyPegen_check_legacy_stmt ( p , a ) ? NULL : RAISE_SYNTAX_ERROR_KNOWN_RANGE ( a , b , "invalid syntax. Perhaps you forgot a comma?" );
18291+
_res = _PyPegen_check_legacy_stmt ( p , a ) ? NULL : p -> tokens [ p -> mark - 1 ] -> level == 0 ? NULL : RAISE_SYNTAX_ERROR_KNOWN_RANGE ( a , b , "invalid syntax. Perhaps you forgot a comma?" );
1829218292
if (_res == NULL && PyErr_Occurred()) {
1829318293
p->error_indicator = 1;
1829418294
D(p->level--);

Parser/pegen.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,8 @@ initialize_token(Parser *p, Token *token, const char *start, const char *end, in
692692
return -1;
693693
}
694694

695+
token->level = p->tok->level;
696+
695697
const char *line_start = token_type == STRING ? p->tok->multi_line_start : p->tok->line_start;
696698
int lineno = token_type == STRING ? p->tok->first_lineno : p->tok->lineno;
697699
int end_lineno = p->tok->lineno;
@@ -1357,7 +1359,7 @@ _PyPegen_run_parser(Parser *p)
13571359
if (p->fill == 0) {
13581360
RAISE_SYNTAX_ERROR("error at start before reading any input");
13591361
}
1360-
else if (p->tok->done == E_EOF) {
1362+
else if (last_token->type == ERRORTOKEN && p->tok->done == E_EOF) {
13611363
if (p->tok->level) {
13621364
raise_unclosed_parentheses_error(p);
13631365
} else {

Parser/pegen.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ typedef struct _memo {
3333
typedef struct {
3434
int type;
3535
PyObject *bytes;
36+
int level;
3637
int lineno, col_offset, end_lineno, end_col_offset;
3738
Memo *memo;
3839
} Token;

Parser/pegen_errors.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ _Pypegen_set_syntax_error(Parser* p, Token* last_token) {
399399
RAISE_SYNTAX_ERROR("error at start before reading any input");
400400
}
401401
// Parser encountered EOF (End of File) unexpectedtly
402-
if (p->tok->done == E_EOF) {
402+
if (last_token->type == ERRORTOKEN && p->tok->done == E_EOF) {
403403
if (p->tok->level) {
404404
raise_unclosed_parentheses_error(p);
405405
} else {
@@ -422,4 +422,4 @@ _Pypegen_set_syntax_error(Parser* p, Token* last_token) {
422422
// _PyPegen_tokenize_full_source_to_check_for_errors will override the existing
423423
// generic SyntaxError we just raised if errors are found.
424424
_PyPegen_tokenize_full_source_to_check_for_errors(p);
425-
}
425+
}

0 commit comments

Comments
 (0)