Skip to content

Commit 56aa20f

Browse files
authored
Don't generate spurious line number in try-except-finally. (#23760)
1 parent 674fa0a commit 56aa20f

File tree

4 files changed

+1751
-1732
lines changed

4 files changed

+1751
-1732
lines changed

Lib/test/test_sys_settrace.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,23 @@ def func():
629629
(3, 'line'),
630630
(3, 'return')])
631631

632+
def test_try_except_no_exception(self):
633+
634+
def func():
635+
try:
636+
2
637+
except:
638+
4
639+
finally:
640+
6
641+
642+
self.run_and_compare(func,
643+
[(0, 'call'),
644+
(1, 'line'),
645+
(2, 'line'),
646+
(6, 'line'),
647+
(6, 'return')])
648+
632649

633650
class SkipLineEventsTraceTestCase(TraceTestCase):
634651
"""Repeat the trace tests, but with per-line events skipped"""

Python/compile.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3007,6 +3007,8 @@ compiler_try_finally(struct compiler *c, stmt_ty s)
30073007
else {
30083008
VISIT_SEQ(c, stmt, s->v.Try.body);
30093009
}
3010+
/* Mark code as artificial */
3011+
c->u->u_lineno = -1;
30103012
ADDOP(c, POP_BLOCK);
30113013
compiler_pop_fblock(c, FINALLY_TRY, body);
30123014
VISIT_SEQ(c, stmt, s->v.Try.finalbody);

0 commit comments

Comments
 (0)