Skip to content

Commit c50ff36

Browse files
committed
find last loc in finally body
1 parent 082625c commit c50ff36

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

Python/compile.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,10 +1652,6 @@ cfg_builder_addop_j(cfg_builder *g, struct location loc,
16521652
#define ADDOP_INPLACE(C, LOC, BINOP) \
16531653
RETURN_IF_FALSE(addop_binary((C), (LOC), (BINOP), true))
16541654

1655-
/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1656-
the ASDL name to synthesize the name of the C type and the visit function.
1657-
*/
1658-
16591655
#define ADD_YIELD_FROM(C, LOC, await) \
16601656
RETURN_IF_FALSE(compiler_add_yield_from((C), (LOC), (await)))
16611657

@@ -1665,6 +1661,10 @@ cfg_builder_addop_j(cfg_builder *g, struct location loc,
16651661
#define ADDOP_YIELD(C, LOC) \
16661662
RETURN_IF_FALSE(addop_yield((C), (LOC)))
16671663

1664+
/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1665+
the ASDL name to synthesize the name of the C type and the visit function.
1666+
*/
1667+
16681668
#define VISIT(C, TYPE, V) {\
16691669
if (!compiler_visit_ ## TYPE((C), (V))) \
16701670
return 0; \
@@ -3307,6 +3307,18 @@ compiler_continue(struct compiler *c, struct location loc)
33073307
}
33083308

33093309

3310+
static struct location
3311+
last_location_in_body(asdl_stmt_seq *stmts)
3312+
{
3313+
for (int i = asdl_seq_LEN(stmts) - 1; i >= 0; i++) {
3314+
struct location loc = LOC((stmt_ty)asdl_seq_GET(stmts, i));
3315+
if (loc.lineno > 0) {
3316+
return loc;
3317+
}
3318+
}
3319+
return NO_LOCATION;
3320+
}
3321+
33103322
/* Code generated for "try: <body> finally: <finalbody>" is as follows:
33113323
33123324
SETUP_FINALLY L
@@ -3362,6 +3374,7 @@ compiler_try_finally(struct compiler *c, stmt_ty s)
33623374
ADDOP(c, NO_LOCATION, POP_BLOCK);
33633375
compiler_pop_fblock(c, FINALLY_TRY, body);
33643376
VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3377+
33653378
ADDOP_JUMP(c, NO_LOCATION, JUMP, exit);
33663379
/* `finally` block */
33673380

@@ -3374,6 +3387,7 @@ compiler_try_finally(struct compiler *c, stmt_ty s)
33743387
if (!compiler_push_fblock(c, loc, FINALLY_END, end, NO_LABEL, NULL))
33753388
return 0;
33763389
VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3390+
loc = last_location_in_body(s->v.Try.finalbody);
33773391
compiler_pop_fblock(c, FINALLY_END, end);
33783392

33793393
ADDOP_I(c, loc, RERAISE, 0); // CHANGED
@@ -3412,6 +3426,7 @@ compiler_try_star_finally(struct compiler *c, stmt_ty s)
34123426
ADDOP(c, NO_LOCATION, POP_BLOCK);
34133427
compiler_pop_fblock(c, FINALLY_TRY, body);
34143428
VISIT_SEQ(c, stmt, s->v.TryStar.finalbody);
3429+
34153430
ADDOP_JUMP(c, NO_LOCATION, JUMP, exit);
34163431

34173432
/* `finally` block */
@@ -3425,6 +3440,8 @@ compiler_try_star_finally(struct compiler *c, stmt_ty s)
34253440
return 0;
34263441
}
34273442
VISIT_SEQ(c, stmt, s->v.TryStar.finalbody);
3443+
loc = last_location_in_body(s->v.Try.finalbody);
3444+
34283445
compiler_pop_fblock(c, FINALLY_END, end);
34293446
ADDOP_I(c, loc, RERAISE, 0); // CHANGED
34303447

0 commit comments

Comments
 (0)