Skip to content

Commit 1c01f8d

Browse files
authored
gh-101517: fix line number propagation in code generated for except* (#103550)
1 parent 518050c commit 1c01f8d

File tree

5 files changed

+27
-15
lines changed

5 files changed

+27
-15
lines changed

Lib/bdb.py

+2
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,8 @@ def format_stack_entry(self, frame_lineno, lprefix=': '):
574574
line = linecache.getline(filename, lineno, frame.f_globals)
575575
if line:
576576
s += lprefix + line.strip()
577+
else:
578+
s += f'{lprefix}Warning: lineno is None'
577579
return s
578580

579581
# The following methods can be called by clients to use

Lib/test/test_bdb.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,8 @@ def main():
12071207
class TestRegressions(unittest.TestCase):
12081208
def test_format_stack_entry_no_lineno(self):
12091209
# See gh-101517
1210-
Bdb().format_stack_entry((sys._getframe(), None))
1210+
self.assertIn('Warning: lineno is None',
1211+
Bdb().format_stack_entry((sys._getframe(), None)))
12111212

12121213

12131214
if __name__ == "__main__":

Lib/test/test_pdb.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1715,8 +1715,8 @@ def test_pdb_issue_gh_101517():
17151715
... 'continue'
17161716
... ]):
17171717
... test_function()
1718-
--Return--
1719-
> <doctest test.test_pdb.test_pdb_issue_gh_101517[0]>(None)test_function()->None
1718+
> <doctest test.test_pdb.test_pdb_issue_gh_101517[0]>(5)test_function()
1719+
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
17201720
(Pdb) continue
17211721
"""
17221722

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bug in line numbers of instructions emitted for :keyword:`except* <except_star>`.

Python/compile.c

+20-12
Original file line numberDiff line numberDiff line change
@@ -3045,20 +3045,24 @@ compiler_try_except(struct compiler *c, stmt_ty s)
30453045
[orig, res, exc] <evaluate E1>
30463046
[orig, res, exc, E1] CHECK_EG_MATCH
30473047
[orig, res, rest/exc, match?] COPY 1
3048-
[orig, res, rest/exc, match?, match?] POP_JUMP_IF_NOT_NONE H1
3049-
[orig, res, exc, None] POP_TOP
3050-
[orig, res, exc] JUMP L2
3048+
[orig, res, rest/exc, match?, match?] POP_JUMP_IF_NONE C1
30513049
3052-
[orig, res, rest, match] H1: <assign to V1> (or POP if no V1)
3050+
[orig, res, rest, match] <assign to V1> (or POP if no V1)
30533051
30543052
[orig, res, rest] SETUP_FINALLY R1
30553053
[orig, res, rest] <code for S1>
30563054
[orig, res, rest] JUMP L2
30573055
30583056
[orig, res, rest, i, v] R1: LIST_APPEND 3 ) exc raised in except* body - add to res
30593057
[orig, res, rest, i] POP
3058+
[orig, res, rest] JUMP LE2
30603059
3061-
[orig, res, rest] L2: <evaluate E2>
3060+
[orig, res, rest] L2: NOP ) for lineno
3061+
[orig, res, rest] JUMP LE2
3062+
3063+
[orig, res, rest/exc, None] C1: POP
3064+
3065+
[orig, res, rest] LE2: <evaluate E2>
30623066
.............................etc.......................
30633067
30643068
[orig, res, rest] Ln+1: LIST_APPEND 1 ) add unhandled exc to res (could be None)
@@ -3114,7 +3118,8 @@ compiler_try_star_except(struct compiler *c, stmt_ty s)
31143118
location loc = LOC(handler);
31153119
NEW_JUMP_TARGET_LABEL(c, next_except);
31163120
except = next_except;
3117-
NEW_JUMP_TARGET_LABEL(c, handle_match);
3121+
NEW_JUMP_TARGET_LABEL(c, except_with_error);
3122+
NEW_JUMP_TARGET_LABEL(c, no_match);
31183123
if (i == 0) {
31193124
/* create empty list for exceptions raised/reraise in the except* blocks */
31203125
/*
@@ -3132,13 +3137,9 @@ compiler_try_star_except(struct compiler *c, stmt_ty s)
31323137
VISIT(c, expr, handler->v.ExceptHandler.type);
31333138
ADDOP(c, loc, CHECK_EG_MATCH);
31343139
ADDOP_I(c, loc, COPY, 1);
3135-
ADDOP_JUMP(c, loc, POP_JUMP_IF_NOT_NONE, handle_match);
3136-
ADDOP(c, loc, POP_TOP); // match
3137-
ADDOP_JUMP(c, loc, JUMP, except);
3140+
ADDOP_JUMP(c, loc, POP_JUMP_IF_NONE, no_match);
31383141
}
31393142

3140-
USE_LABEL(c, handle_match);
3141-
31423143
NEW_JUMP_TARGET_LABEL(c, cleanup_end);
31433144
NEW_JUMP_TARGET_LABEL(c, cleanup_body);
31443145

@@ -3197,9 +3198,16 @@ compiler_try_star_except(struct compiler *c, stmt_ty s)
31973198
/* add exception raised to the res list */
31983199
ADDOP_I(c, NO_LOCATION, LIST_APPEND, 3); // exc
31993200
ADDOP(c, NO_LOCATION, POP_TOP); // lasti
3200-
ADDOP_JUMP(c, NO_LOCATION, JUMP, except);
3201+
ADDOP_JUMP(c, NO_LOCATION, JUMP, except_with_error);
32013202

32023203
USE_LABEL(c, except);
3204+
ADDOP(c, NO_LOCATION, NOP); // to hold a propagated location info
3205+
ADDOP_JUMP(c, NO_LOCATION, JUMP, except_with_error);
3206+
3207+
USE_LABEL(c, no_match);
3208+
ADDOP(c, loc, POP_TOP); // match (None)
3209+
3210+
USE_LABEL(c, except_with_error);
32033211

32043212
if (i == n - 1) {
32053213
/* Add exc to the list (if not None it's the unhandled part of the EG) */

0 commit comments

Comments
 (0)