8000 Guarantee that line number is set for returns. · python/cpython@23a5a31 · GitHub
[go: up one dir, main page]

Skip to content

Commit 23a5a31

Browse files
committed
Guarantee that line number is set for returns.
1 parent 7e59ef1 commit 23a5a31

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Lib/test/test_sys_settrace.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,6 +1647,7 @@ async def test_no_jump_forwards_into_async_for_block(output):
16471647
output.append(1)
16481648
async for i in asynciter([1, 2]):
16491649
output.append(3)
1650+
pass
16501651

16511652
@jump_test(3, 2, [2, 2], (ValueError, 'into'))
16521653
def test_no_jump_backwards_into_for_block(output):

Python/compile.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6960,6 +6960,36 @@ insert_generator_prefix(struct compiler *c, basicblock *entryblock) {
69606960
return 0;
69616961
}
69626962

6963+
/* Make sure that all returns have a line number, even if early passes
6964+
* have failed to propagate a correct line number.
6965+
* The resulting line number may not be correct according to PEP 626,
6966+
* but should be "good enough", and no worse than in older versions. */
6967+
static void
6968+
guarantee_lineno_for_exits(struct assembler *a, int firstlineno) {
6969+
int lineno = firstlineno;
6970+
assert(lineno > 0);
6971+
for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
6972+
if (b->b_iused == 0) {
6973+
continue;
6974+
}
6975+
struct instr *last = &b->b_instr[b->b_iused-1];
6976+
if (last->i_lineno < 0) {
6977+
/* A return in a block by itself can't have a linenumber, or
6978+
* we can mess up frame.setlineno */
6979+
if (last->i_opcode == RETURN_VALUE)
6980+
{
6981+
for (int i = 0; i < b->b_iused; i++) {
6982+
assert(b->b_instr[i].i_lineno < 0);
6983+
b->b_instr[i].i_lineno = lineno;
6984+
}
6985+
}
6986+
}
6987+
else {
6988+
lineno = last->i_lineno;
6989+
}
6990+
}
6991+
}
6992+
69636993
static PyCodeObject *
69646994
assemble(struct compiler *c, int addNone)
69656995
{
@@ -7022,6 +7052,7 @@ assemble(struct compiler *c, int addNone)
70227052
if (optimize_cfg(c, &a, consts)) {
70237053
goto error;
70247054
}
7055+
guarantee_lineno_for_exits(&a, c->u->u_firstlineno);
70257056

70267057
/* Can't modify the bytecode after computing jump offsets. */
70277058
assemble_jump_offsets(&a, c);

0 commit comments

Comments
 (0)
0