Fix handling of line numbers around finally-blocks. (#17737)

This commit is contained in:
Mark Shannon 2019-12-30 09:53:36 +00:00 committed by GitHub
parent 226e6e7d43
commit 88dce26da6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 1695 additions and 1625 deletions

View File

@ -333,6 +333,68 @@ dis_fstring = """\
28 RETURN_VALUE
""" % (_fstring.__code__.co_firstlineno + 1,)
def _tryfinally(a, b):
try:
return a
finally:
b()
def _tryfinallyconst(b):
try:
return 1
finally:
b()
dis_tryfinally = """\
%3d 0 SETUP_FINALLY 12 (to 14)
%3d 2 LOAD_FAST 0 (a)
4 POP_BLOCK
%3d 6 LOAD_FAST 1 (b)
8 CALL_FUNCTION 0
10 POP_TOP
%3d 12 RETURN_VALUE
%3d >> 14 LOAD_FAST 1 (b)
16 CALL_FUNCTION 0
18 POP_TOP
20 RERAISE
22 LOAD_CONST 0 (None)
24 RETURN_VALUE
""" % (_tryfinally.__code__.co_firstlineno + 1,
_tryfinally.__code__.co_firstlineno + 2,
_tryfinally.__code__.co_firstlineno + 4,
_tryfinally.__code__.co_firstlineno + 2,
_tryfinally.__code__.co_firstlineno + 4,
)
dis_tryfinallyconst = """\
%3d 0 SETUP_FINALLY 12 (to 14)
%3d 2 POP_BLOCK
%3d 4 LOAD_FAST 0 (b)
6 CALL_FUNCTION 0
8 POP_TOP
%3d 10 LOAD_CONST 1 (1)
12 RETURN_VALUE
%3d >> 14 LOAD_FAST 0 (b)
16 CALL_FUNCTION 0
18 POP_TOP
20 RERAISE
22 LOAD_CONST 0 (None)
24 RETURN_VALUE
""" % (_tryfinallyconst.__code__.co_firstlineno + 1,
_tryfinallyconst.__code__.co_firstlineno + 2,
_tryfinallyconst.__code__.co_firstlineno + 4,
_tryfinallyconst.__code__.co_firstlineno + 2,
_tryfinallyconst.__code__.co_firstlineno + 4,
)
def _g(x):
yield x
@ -563,6 +625,10 @@ class DisTests(unittest.TestCase):
def test_disassemble_fstring(self):
self.do_disassembly_test(_fstring, dis_fstring)
def test_disassemble_try_finally(self):
self.do_disassembly_test(_tryfinally, dis_tryfinally)
self.do_disassembly_test(_tryfinallyconst, dis_tryfinallyconst)
def test_dis_none(self):
try:
del sys.last_traceback

View File

@ -1686,7 +1686,11 @@ compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
return 0;
}
}
/* Emit the finally block, restoring the line number when done */
int saved_lineno = c->u->u_lineno;
VISIT_SEQ(c, stmt, info->fb_datum);
c->u->u_lineno = saved_lineno;
c->u->u_lineno_set = 0;
if (preserve_tos) {
compiler_pop_fblock(c, POP_VALUE, NULL);
}

3250
Python/importlib.h generated

File diff suppressed because it is too large Load Diff