Skip to content

Commit f0dd555

Browse files
committed
gh-129025: fix too wide source location for bytecodes emitted for and
1 parent 61b35f7 commit f0dd555

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

Lib/test/test_traceback.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2919,6 +2919,46 @@ def exc():
29192919
report = self.get_report(exc)
29202920
self.assertEqual(report, expected)
29212921

2922+
def test_except_star_lineno(self):
2923+
def exc():
2924+
class Bad(ExceptionGroup):
2925+
def split(*args):
2926+
1/0
2927+
2928+
try:
2929+
raise Bad("", [ValueError(), TypeError()])
2930+
except* ValueError:
2931+
1
2932+
2
2933+
3
2934+
2935+
expected = (f' + Exception Group Traceback (most recent call last):\n'
2936+
f' | File "{__file__}", line {exc.__code__.co_firstlineno + 6}, in exc\n'
2937+
f' | raise Bad("", [ValueError(), TypeError()])\n'
2938+
f' | test.test_traceback.BaseExceptionReportingTests.test_except_star_lineno.<locals>.exc.<locals>.Bad: (2 sub-exceptions)\n'
2939+
f' +-+---------------- 1 ----------------\n'
2940+
f' | ValueError\n'
2941+
f' +---------------- 2 ----------------\n'
2942+
f' | TypeError\n'
2943+
f' +------------------------------------\n'
2944+
f'\n'
2945+
f'During handling of the above exception, another exception occurred:\n'
2946+
f'\n'
2947+
f'Traceback (most recent call last):\n'
2948+
f' File "{__file__}", line '
2949+
f'{self.callable_line}, in get_exception\n'
2950+
f' exception_or_callable()\n'
2951+
f' ~~~~~~~~~~~~~~~~~~~~~^^\n'
2952+
f' File "{__file__}", line {exc.__code__.co_firstlineno + 7}, in exc\n'
2953+
f' except* ValueError:\n'
2954+
f' File "{__file__}", line {exc.__code__.co_firstlineno + 3}, in split\n'
2955+
f' 1/0\n'
2956+
f' ~^~\n'
2957+
f'ZeroDivisionError: division by zero\n')
2958+
2959+
report = self.get_report(exc)
2960+
self.assertEqual(report, expected)
2961+
29222962
def test_KeyboardInterrupt_at_first_line_of_frame(self):
29232963
# see GH-93249
29242964
def f():
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix too wide source locations of instructions emitted for ``except`` and
2+
``except*``.

Python/codegen.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,7 +2365,8 @@ codegen_try_except(compiler *c, stmt_ty s)
23652365
for (i = 0; i < n; i++) {
23662366
excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
23672367
s->v.Try.handlers, i);
2368-
location loc = LOC(handler);
2368+
/* Set location to the entire line of the except keyword */
2369+
location loc = LOCATION(handler->lineno, handler->lineno, -1, -1);
23692370
if (!handler->v.ExceptHandler.type && i < n-1) {
23702371
return _PyCompile_Error(c, loc, "default 'except:' must be last");
23712372
}
@@ -2546,7 +2547,8 @@ codegen_try_star_except(compiler *c, stmt_ty s)
25462547
for (Py_ssize_t i = 0; i < n; i++) {
25472548
excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
25482549
s->v.TryStar.handlers, i);
2549-
location loc = LOC(handler);
2550+
/* Set location to the entire line of the except keyword */
2551+
location loc = LOCATION(handler->lineno, handler->lineno, -1, -1);
25502552
NEW_JUMP_TARGET_LABEL(c, next_except);
25512553
except = next_except;
25522554
NEW_JUMP_TARGET_LABEL(c, except_with_error);

0 commit comments

Comments
 (0)