Skip to content

Commit 88dce26

Browse files
authored
Fix handling of line numbers around finally-blocks. (#17737)
1 parent 226e6e7 commit 88dce26

File tree

3 files changed

+1692
-1622
lines changed

3 files changed

+1692
-1622
lines changed

Lib/test/test_dis.py

+66
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,68 @@ def _fstring(a, b, c, d):
333333
28 RETURN_VALUE
334334
""" % (_fstring.__code__.co_firstlineno + 1,)
335335

336+
def _tryfinally(a, b):
337+
try:
338+
return a
339+
finally:
340+
b()
341+
342+
def _tryfinallyconst(b):
343+
try:
344+
return 1
345+
finally:
346+
b()
347+
348+
dis_tryfinally = """\
349+
%3d 0 SETUP_FINALLY 12 (to 14)
350+
351+
%3d 2 LOAD_FAST 0 (a)
352+
4 POP_BLOCK
353+
354+
%3d 6 LOAD_FAST 1 (b)
355+
8 CALL_FUNCTION 0
356+
10 POP_TOP
357+
358+
%3d 12 RETURN_VALUE
359+
360+
%3d >> 14 LOAD_FAST 1 (b)
361+
16 CALL_FUNCTION 0
362+
18 POP_TOP
363+
20 RERAISE
364+
22 LOAD_CONST 0 (None)
365+
24 RETURN_VALUE
366+
""" % (_tryfinally.__code__.co_firstlineno + 1,
367+
_tryfinally.__code__.co_firstlineno + 2,
368+
_tryfinally.__code__.co_firstlineno + 4,
369+
_tryfinally.__code__.co_firstlineno + 2,
370+
_tryfinally.__code__.co_firstlineno + 4,
371+
)
372+
373+
dis_tryfinallyconst = """\
374+
%3d 0 SETUP_FINALLY 12 (to 14)
375+
376+
%3d 2 POP_BLOCK
377+
378+
%3d 4 LOAD_FAST 0 (b)
379+
6 CALL_FUNCTION 0
380+
8 POP_TOP
381+
382+
%3d 10 LOAD_CONST 1 (1)
383+
12 RETURN_VALUE
384+
385+
%3d >> 14 LOAD_FAST 0 (b)
386+
16 CALL_FUNCTION 0
387+
18 POP_TOP
388+
20 RERAISE
389+
22 LOAD_CONST 0 (None)
390+
24 RETURN_VALUE
391+
""" % (_tryfinallyconst.__code__.co_firstlineno + 1,
392+
_tryfinallyconst.__code__.co_firstlineno + 2,
393+
_tryfinallyconst.__code__.co_firstlineno + 4,
394+
_tryfinallyconst.__code__.co_firstlineno + 2,
395+
_tryfinallyconst.__code__.co_firstlineno + 4,
396+
)
397+
336398
def _g(x):
337399
yield x
338400

@@ -563,6 +625,10 @@ def test_disassemble_coroutine(self):
563625
def test_disassemble_fstring(self):
564626
self.do_disassembly_test(_fstring, dis_fstring)
565627

628+
def test_disassemble_try_finally(self):
629+
self.do_disassembly_test(_tryfinally, dis_tryfinally)
630+
self.do_disassembly_test(_tryfinallyconst, dis_tryfinallyconst)
631+
566632
def test_dis_none(self):
567633
try:
568634
del sys.last_traceback

Python/compile.c

+4
Original file line numberDiff line numberDiff line change
@@ -1686,7 +1686,11 @@ compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
16861686
return 0;
16871687
}
16881688
}
1689+
/* Emit the finally block, restoring the line number when done */
1690+
int saved_lineno = c->u->u_lineno;
16891691
VISIT_SEQ(c, stmt, info->fb_datum);
1692+
c->u->u_lineno = saved_lineno;
1693+
c->u->u_lineno_set = 0;
16901694
if (preserve_tos) {
16911695
compiler_pop_fblock(c, POP_VALUE, NULL);
16921696
}

0 commit comments

Comments
 (0)