[Python-checkins] Correct location for syntax error in try-except (GH-25939)
markshannon
webhook-mailer at python.org
Thu May 6 08:38:58 EDT 2021
https://github.com/python/cpython/commit/8d4b18429de504445a1338077e495090d91044b4
commit: 8d4b18429de504445a1338077e495090d91044b4
branch: 3.10
author: Mark Shannon <mark at hotpy.org>
committer: markshannon <mark at hotpy.org>
date: 2021-05-06T13:38:50+01:00
summary:
Correct location for syntax error in try-except (GH-25939)
files:
A Misc/NEWS.d/next/Core and Builtins/2021-05-06-12-43-04.bpo-44056.4LWcJW.rst
M Lib/test/test_exceptions.py
M Python/compile.c
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index 3c427fed561183..1fe479fbe83092 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -218,7 +218,7 @@ def testSyntaxErrorOffset(self):
check('class foo:return 1', 1, 11)
check('def f():\n continue', 2, 3)
check('def f():\n break', 2, 3)
- check('try:\n pass\nexcept:\n pass\nexcept ValueError:\n pass', 2, 3)
+ check('try:\n pass\nexcept:\n pass\nexcept ValueError:\n pass', 3, 1)
# Errors thrown by tokenizer.c
check('(0x+1)', 1, 3)
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-05-06-12-43-04.bpo-44056.4LWcJW.rst b/Misc/NEWS.d/next/Core and Builtins/2021-05-06-12-43-04.bpo-44056.4LWcJW.rst
new file mode 100644
index 00000000000000..24a6581bebf327
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-05-06-12-43-04.bpo-44056.4LWcJW.rst
@@ -0,0 +1,2 @@
+Syntax errors when default ``except`` is not the last ``except`` are
+reported with the correct location. Patch by Mark Shannon.
diff --git a/Python/compile.c b/Python/compile.c
index 4fc8b38d978289..d6bcca1c02483d 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -3165,9 +3165,9 @@ compiler_try_except(struct compiler *c, stmt_ty s)
for (i = 0; i < n; i++) {
excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
s->v.Try.handlers, i);
+ SET_LOC(c, handler);
if (!handler->v.ExceptHandler.type && i < n-1)
return compiler_error(c, "default 'except:' must be last");
- SET_LOC(c, handler);
except = compiler_new_block(c);
if (except == NULL)
return 0;
More information about the Python-checkins
mailing list