Skip to content

Commit 24c10d2

Browse files
authored
bpo-45727: Only trigger the 'did you forgot a comma' error suggestion if inside parentheses (GH-29757)
1 parent f4afc53 commit 24c10d2

File tree

8 files changed

+15
-8
lines changed

8 files changed

+15
-8
lines changed

Grammar/python.gram

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

10901091
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
@@ -18298,7 +18298,7 @@ invalid_expression_rule(Parser *p)
1829818298
)
1829918299
{
1830018300
D(fprintf(stderr, "%*c+ invalid_expression[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "!(NAME STRING | SOFT_KEYWORD) disjunction expression_without_invalid"));
18301-
_res = _PyPegen_check_legacy_stmt ( p , a ) ? NULL : RAISE_SYNTAX_ERROR_KNOWN_RANGE ( a , b , "invalid syntax. Perhaps you forgot a comma?" );
18301+
_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?" );
1830218302
if (_res == NULL && PyErr_Occurred()) {
1830318303
p->error_indicator = 1;
1830418304
D(p->level--);

Parser/pegen.c

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

173+
token->level = p->tok->level;
174+
173175
const char *line_start = token_type == STRING ? p->tok->multi_line_start : p->tok->line_start;
174176
int lineno = token_type == STRING ? p->tok->first_lineno : p->tok->lineno;
175177
int end_lineno = p->tok->lineno;
@@ -946,4 +948,4 @@ _PyPegen_run_parser_from_string(const char *str, int start_rule, PyObject *filen
946948
error:
947949
_PyTokenizer_Free(tok);
948950
return result;
949-
}
951+
}

Parser/pegen.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ typedef struct _memo {
3535
typedef struct {
3636
int type;
3737
PyObject *bytes;
38+
int level;
3839
int lineno, col_offset, end_lineno, end_col_offset;
3940
Memo *memo;
4041
} 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)