Skip to content

Commit e62d5d9

Browse files
committed
pythongh-101517: fix line number propagation in code generated for except* (python#103550)
1 parent c8de883 commit e62d5d9

File tree

5 files changed

+48
-15
lines changed

5 files changed

+48
-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
@@ -1206,7 +1206,8 @@ def main():
12061206
class TestRegressions(unittest.TestCase):
12071207
def test_format_stack_entry_no_lineno(self):
12081208
# See gh-101517
1209-
Bdb().format_stack_entry((sys._getframe(), None))
1209+
self.assertIn('Warning: lineno is None',
1210+
Bdb().format_stack_entry((sys._getframe(), None)))
12101211

12111212

12121213
if __name__ == "__main__":

Lib/test/test_pdb.py

+19
Original file line numberDiff line numberDiff line change
@@ -1653,6 +1653,25 @@ def test_pdb_issue_gh_101673():
16531653
(Pdb) continue
16541654
"""
16551655

1656+
def test_pdb_issue_gh_101517():
1657+
"""See GH-101517
1658+
1659+
Make sure pdb doesn't crash when the exception is caught in a try/except* block
1660+
1661+
>>> def test_function():
1662+
... try:
1663+
... raise KeyError
1664+
... except* Exception as e:
1665+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
1666+
1667+
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
1668+
... 'continue'
1669+
... ]):
1670+
... test_function()
1671+
> <doctest test.test_pdb.test_pdb_issue_gh_101517[0]>(5)test_function()
1672+
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
1673+
(Pdb) continue
1674+
"""
16561675

16571676
@support.requires_subprocess()
16581677
class PdbTestCase(unittest.TestCase):
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

+24-14
Original file line numberDiff line numberDiff line change
@@ -3613,21 +3613,25 @@ compiler_try_except(struct compiler *c, stmt_ty s)
36133613
36143614
[orig, res, exc] <evaluate E1>
36153615
[orig, res, exc, E1] CHECK_EG_MATCH
3616-
[orig, red, rest/exc, match?] COPY 1
3617-
[orig, red, rest/exc, match?, match?] POP_JUMP_IF_NOT_NONE H1
3618-
[orig, red, exc, None] POP_TOP
3619-
[orig, red, exc] JUMP L2
3616+
[orig, res, rest/exc, match?] COPY 1
3617+
[orig, res, rest/exc, match?, match?] POP_JUMP_IF_NONE C1
36203618
3621-
[orig, res, rest, match] H1: <assign to V1> (or POP if no V1)
3619+
[orig, res, rest, match] <assign to V1> (or POP if no V1)
36223620
36233621
[orig, res, rest] SETUP_FINALLY R1
36243622
[orig, res, rest] <code for S1>
36253623
[orig, res, rest] JUMP L2
36263624
36273625
[orig, res, rest, i, v] R1: LIST_APPEND 3 ) exc raised in except* body - add to res
36283626
[orig, res, rest, i] POP
3627+
[orig, res, rest] JUMP LE2
36293628
3630-
[orig, res, rest] L2: <evaluate E2>
3629+
[orig, res, rest] L2: NOP ) for lineno
3630+
[orig, res, rest] JUMP LE2
3631+
3632+
[orig, res, rest/exc, None] C1: POP
3633+
3634+
[orig, res, rest] LE2: <evaluate E2>
36313635
.............................etc.......................
36323636
36333637
[orig, res, rest] Ln+1: LIST_APPEND 1 ) add unhandled exc to res (could be None)
@@ -3700,8 +3704,12 @@ compiler_try_star_except(struct compiler *c, stmt_ty s)
37003704
if (except == NULL) {
37013705
return 0;
37023706
}
3703-
basicblock *handle_match = compiler_new_block(c);
3704-
if (handle_match == NULL) {
3707+
basicblock *except_with_error = compiler_new_block(c);
3708+
if (except_with_error == NULL) {
3709+
return 0;
3710+
}
3711+
basicblock *no_match = compiler_new_block(c);
3712+
if (no_match == NULL) {
37053713
return 0;
37063714
}
37073715
if (i == 0) {
@@ -3725,13 +3733,9 @@ compiler_try_star_except(struct compiler *c, stmt_ty s)
37253733
VISIT(c, expr, handler->v.ExceptHandler.type);
37263734
ADDOP(c, CHECK_EG_MATCH);
37273735
ADDOP_I(c, COPY, 1);
3728-
ADDOP_JUMP(c, POP_JUMP_IF_NOT_NONE, handle_match);
3729-
ADDOP(c, POP_TOP); // match
3730-
ADDOP_JUMP(c, JUMP, except);
3736+
ADDOP_JUMP(c, POP_JUMP_IF_NONE, no_match);
37313737
}
37323738

3733-
compiler_use_next_block(c, handle_match);
3734-
37353739
basicblock *cleanup_end = compiler_new_block(c);
37363740
if (cleanup_end == NULL) {
37373741
return 0;
@@ -3793,8 +3797,14 @@ compiler_try_star_except(struct compiler *c, stmt_ty s)
37933797
ADDOP_I(c, LIST_APPEND, 3); // exc
37943798
ADDOP(c, POP_TOP); // lasti
37953799

3796-
ADDOP_JUMP(c, JUMP, except);
3800+
ADDOP_JUMP(c, JUMP, except_with_error);
37973801
compiler_use_next_block(c, except);
3802+
ADDOP(c, NOP); // to hold a propagated location info
3803+
ADDOP_JUMP(c, JUMP, except_with_error);
3804+
compiler_use_next_block(c, no_match);
3805+
ADDOP(c, POP_TOP); // match (None)
3806+
3807+
compiler_use_next_block(c, except_with_error);
37983808

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

0 commit comments

Comments
 (0)