Skip to content

Commit cb6f3d7

Browse files
authored
bpo-43933: Force RETURN_VALUE bytecodes to have line numbers (GH-26054)
1 parent 6574334 commit cb6f3d7

File tree

4 files changed

+2990
-2939
lines changed

4 files changed

+2990
-2939
lines changed

Lib/test/test_sys_settrace.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,26 @@ class A:
976976
(3, 'return'),
977977
(1, 'return')])
978978

979+
def test_try_in_try(self):
980+
def func():
981+
try:
982+
try:
983+
pass
984+
except Exception as ex:
985+
pass
986+
except Exception:
987+
pass
988+
989+
# This doesn't conform to PEP 626
990+
self.run_and_compare(func,
991+
[(0, 'call'),
992+
(1, 'line'),
993+
(2, 'line'),
994+
(3, 'line'),
995+
(5, 'line'),
996+
(5, 'return')])
997+
998+
979999
class SkipLineEventsTraceTestCase(TraceTestCase):
9801000
"""Repeat the trace tests, but with per-line events skipped"""
9811001

@@ -1647,6 +1667,7 @@ async def test_no_jump_forwards_into_async_for_block(output):
16471667
output.append(1)
16481668
async for i in asynciter([1, 2]):
16491669
output.append(3)
1670+
pass
16501671

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

Python/compile.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7276,6 +7276,34 @@ insert_generator_prefix(struct compiler *c, basicblock *entryblock) {
72767276
return 0;
72777277
}
72787278

7279+
/* Make sure that all returns have a line number, even if early passes
7280+
* have failed to propagate a correct line number.
7281+
* The resulting line number may not be correct according to PEP 626,
7282+
* but should be "good enough", and no worse than in older versions. */
7283+
static void
7284+
guarantee_lineno_for_exits(struct assembler *a, int firstlineno) {
7285+
int lineno = firstlineno;
7286+
assert(lineno > 0);
7287+
for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7288+
if (b->b_iused == 0) {
7289+
continue;
7290+
}
7291+
struct instr *last = &b->b_instr[b->b_iused-1];
7292+
if (last->i_lineno < 0) {
7293+
if (last->i_opcode == RETURN_VALUE)
7294+
{
7295+
for (int i = 0; i < b->b_iused; i++) {
7296+
assert(b->b_instr[i].i_lineno < 0);
7297+
b->b_instr[i].i_lineno = lineno;
7298+
}
7299+
}
7300+
}
7301+
else {
7302+
lineno = last->i_lineno;
7303+
}
7304+
}
7305+
}
7306+
72797307
static PyCodeObject *
72807308
assemble(struct compiler *c, int addNone)
72817309
{
@@ -7338,6 +7366,7 @@ assemble(struct compiler *c, int addNone)
73387366
if (optimize_cfg(c, &a, consts)) {
73397367
goto error;
73407368
}
7369+
guarantee_lineno_for_exits(&a, c->u->u_firstlineno);
73417370

73427371
int maxdepth = stackdepth(c);
73437372
if (maxdepth < 0) {

0 commit comments

Comments
 (0)