Skip to content

Commit aa0f056

Browse files
bpo-47212: Improve error messages for un-parenthesized generator expressions (GH-32302)
1 parent f1606a5 commit aa0f056

File tree

7 files changed

+28
-8
lines changed

7 files changed

+28
-8
lines changed

Grammar/python.gram

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,12 +1073,12 @@ func_type_comment[Token*]:
10731073
invalid_arguments:
10741074
| a=args ',' '*' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable argument unpacking follows keyword argument unpacking") }
10751075
| a=expression b=for_if_clauses ',' [args | expression for_if_clauses] {
1076-
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, PyPegen_last_item(b, comprehension_ty)->target, "Generator expression must be parenthesized") }
1076+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, _PyPegen_get_last_comprehension_item(PyPegen_last_item(b, comprehension_ty)), "Generator expression must be parenthesized") }
10771077
| a=NAME b='=' expression for_if_clauses {
10781078
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?")}
10791079
| a=args b=for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a, b) }
10801080
| args ',' a=expression b=for_if_clauses {
1081-
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, asdl_seq_GET(b, b->size-1)->target, "Generator expression must be parenthesized") }
1081+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, _PyPegen_get_last_comprehension_item(PyPegen_last_item(b, comprehension_ty)), "Generator expression must be parenthesized") }
10821082
| a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
10831083
invalid_kwarg:
10841084
| a[Token*]=('True'|'False'|'None') b='=' {
@@ -1257,7 +1257,7 @@ invalid_finally_stmt:
12571257
invalid_except_stmt_indent:
12581258
| a='except' expression ['as' NAME ] ':' NEWLINE !INDENT {
12591259
RAISE_INDENTATION_ERROR("expected an indented block after 'except' statement on line %d", a->lineno) }
1260-
| a='except' ':' NEWLINE !INDENT { RAISE_SYNTAX_ERROR("expected an indented block after except statement on line %d", a->lineno) }
1260+
| a='except' ':' NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block after 'except' statement on line %d", a->lineno) }
12611261
invalid_except_star_stmt_indent:
12621262
| a='except' '*' expression ['as' NAME ] ':' NEWLINE !INDENT {
12631263
RAISE_INDENTATION_ERROR("expected an indented block after 'except*' statement on line %d", a->lineno) }

Lib/test/test_exceptions.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,17 @@ def ckmsg(src, msg, exception=SyntaxError):
198198
s = '''if True:\n print()\n\texec "mixed tabs and spaces"'''
199199
ckmsg(s, "inconsistent use of tabs and spaces in indentation", TabError)
200200

201-
def check(self, src, lineno, offset, encoding='utf-8'):
201+
def check(self, src, lineno, offset, end_lineno=None, end_offset=None, encoding='utf-8'):
202202
with self.subTest(source=src, lineno=lineno, offset=offset):
203203
with self.assertRaises(SyntaxError) as cm:
204204
compile(src, '<fragment>', 'exec')
205205
self.assertEqual(cm.exception.lineno, lineno)
206206
self.assertEqual(cm.exception.offset, offset)
207+
if end_lineno is not None:
208+
self.assertEqual(cm.exception.end_lineno, end_lineno)
209+
if end_offset is not None:
210+
self.assertEqual(cm.exception.end_offset, end_offset)
211+
207212
if cm.exception.text is not None:
208213
if not isinstance(src, str):
209214
src = src.decode(encoding, 'replace')
@@ -235,6 +240,10 @@ def testSyntaxErrorOffset(self):
235240
check('match ...:\n case {**rest, "key": value}:\n ...', 2, 19)
236241
check("[a b c d e f]", 1, 2)
237242
check("for x yfff:", 1, 7)
243+
check("f(a for a in b, c)", 1, 3, 1, 15)
244+
check("f(a for a in b if a, c)", 1, 3, 1, 20)
245+
check("f(a, b for b in c)", 1, 6, 1, 18)
246+
check("f(a, b for b in c, d)", 1, 6, 1, 18)
238247

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

Lib/test/test_syntax.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,13 @@
13071307
Traceback (most recent call last):
13081308
IndentationError: expected an indented block after 'try' statement on line 1
13091309
1310+
>>> try:
1311+
... something()
1312+
... except:
1313+
... pass
1314+
Traceback (most recent call last):
1315+
IndentationError: expected an indented block after 'except' statement on line 3
1316+
13101317
>>> try:
13111318
... something()
13121319
... except A:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Raise :exc:`IndentationError` instead of :exc:`SyntaxError` for a bare
2+
``except`` with no following indent. Improve :exc:`SyntaxError` locations for
3+
an un-parenthesized generator used as arguments. Patch by Matthieu Dartiailh.

Parser/action_helpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ _PyPegen_get_expr_name(expr_ty e)
11451145
}
11461146
}
11471147

1148-
static inline expr_ty
1148+
expr_ty
11491149
_PyPegen_get_last_comprehension_item(comprehension_ty comprehension) {
11501150
if (comprehension->ifs == NULL || asdl_seq_LEN(comprehension->ifs) == 0) {
11511151
return comprehension->iter;

Parser/parser.c

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Parser/pegen.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ int _PyPegen_check_barry_as_flufl(Parser *, Token *);
324324
int _PyPegen_check_legacy_stmt(Parser *p, expr_ty t);
325325
mod_ty _PyPegen_make_module(Parser *, asdl_stmt_seq *);
326326
void *_PyPegen_arguments_parsing_error(Parser *, expr_ty);
327+
expr_ty _PyPegen_get_last_comprehension_item(comprehension_ty comprehension);
327328
void *_PyPegen_nonparen_genexp_in_call(Parser *p, expr_ty args, asdl_comprehension_seq *comprehensions);
328329

329330
// Parser API

0 commit comments

Comments
 (0)