Skip to content

Commit 8d4b184

Browse files
authored
Correct location for syntax error in try-except (GH-25939)
1 parent 60ba0b6 commit 8d4b184

File tree

3 files changed

+4
-2
lines changed

3 files changed

+4
-2
lines changed

Lib/test/test_exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def testSyntaxErrorOffset(self):
218218
check('class foo:return 1', 1, 11)
219219
check('def f():\n continue', 2, 3)
220220
check('def f():\n break', 2, 3)
221-
check('try:\n pass\nexcept:\n pass\nexcept ValueError:\n pass', 2, 3)
221+
check('try:\n pass\nexcept:\n pass\nexcept ValueError:\n pass', 3, 1)
222222

223223
# Errors thrown by tokenizer.c
224224
check('(0x+1)', 1, 3)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Syntax errors when default ``except`` is not the last ``except`` are
2+
reported with the correct location. Patch by Mark Shannon.

Python/compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3165,9 +3165,9 @@ compiler_try_except(struct compiler *c, stmt_ty s)
31653165
for (i = 0; i < n; i++) {
31663166
excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
31673167
s->v.Try.handlers, i);
3168+
SET_LOC(c, handler);
31683169
if (!handler->v.ExceptHandler.type && i < n-1)
31693170
return compiler_error(c, "default 'except:' must be last");
3170-
SET_LOC(c, handler);
31713171
except = compiler_new_block(c);
31723172
if (except == NULL)
31733173
return 0;

0 commit comments

Comments
 (0)