bpo-42246: Don't forget the entry block when ensuring that all exits have a line number (GH-23636)

Don't forget the entry block when ensuring that all exits have a line number.
This commit is contained in:
Mark Shannon 2020-12-04 15:22:12 +00:00 committed by GitHub
parent f24b8101a0
commit eaccc12aa9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 4 deletions

View File

@ -809,6 +809,24 @@ if 1:
func(save_caller_frame) func(save_caller_frame)
self.assertEqual(frame.f_lineno-frame.f_code.co_firstlineno, lastline) self.assertEqual(frame.f_lineno-frame.f_code.co_firstlineno, lastline)
def test_lineno_after_no_code(self):
def no_code1():
"doc string"
def no_code2():
a: int
for func in (no_code1, no_code2):
with self.subTest(func=func):
code = func.__code__
lines = list(code.co_lines())
self.assertEqual(len(lines), 1)
start, end, line = lines[0]
self.assertEqual(start, 0)
self.assertEqual(end, len(code.co_code))
self.assertEqual(line, code.co_firstlineno)
def test_big_dict_literal(self): def test_big_dict_literal(self):
# The compiler has a flushing point in "compiler_dict" that calls compiles # The compiler has a flushing point in "compiler_dict" that calls compiles
# a portion of the dictionary literal when the loop that iterates over the items # a portion of the dictionary literal when the loop that iterates over the items

View File

@ -6514,6 +6514,7 @@ is_exit_without_lineno(basicblock *b) {
static int static int
ensure_exits_have_lineno(struct compiler *c) ensure_exits_have_lineno(struct compiler *c)
{ {
basicblock *entry = NULL;
/* Copy all exit blocks without line number that are targets of a jump. /* Copy all exit blocks without line number that are targets of a jump.
*/ */
for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
@ -6535,6 +6536,11 @@ ensure_exits_have_lineno(struct compiler *c)
b->b_instr[b->b_iused-1].i_target = new_target; b->b_instr[b->b_iused-1].i_target = new_target;
} }
} }
entry = b;
}
assert(entry != NULL);
if (is_exit_without_lineno(entry)) {
entry->b_instr[0].i_lineno = c->u->u_firstlineno;
} }
/* Any remaining reachable exit blocks without line number can only be reached by /* Any remaining reachable exit blocks without line number can only be reached by
* fall through, and thus can only have a single predecessor */ * fall through, and thus can only have a single predecessor */

2
Python/importlib.h generated
View File

@ -1243,7 +1243,7 @@ const unsigned char _Py_M__importlib_bootstrap[] = {
114,32,109,111,100,117,108,101,32,99,114,101,97,116,105,111, 114,32,109,111,100,117,108,101,32,99,114,101,97,116,105,111,
110,46,78,114,10,0,0,0,114,161,0,0,0,114,10,0, 110,46,78,114,10,0,0,0,114,161,0,0,0,114,10,0,
0,0,114,10,0,0,0,114,11,0,0,0,114,150,0,0, 0,0,114,10,0,0,0,114,11,0,0,0,114,150,0,0,
0,66,3,0,0,115,4,0,0,0,4,128,255,128,122,28, 0,66,3,0,0,115,4,0,0,0,4,0,255,128,122,28,
70,114,111,122,101,110,73,109,112,111,114,116,101,114,46,99, 70,114,111,122,101,110,73,109,112,111,114,116,101,114,46,99,
114,101,97,116,101,95,109,111,100,117,108,101,99,1,0,0, 114,101,97,116,101,95,109,111,100,117,108,101,99,1,0,0,
0,0,0,0,0,0,0,0,0,3,0,0,0,4,0,0, 0,0,0,0,0,0,0,0,0,3,0,0,0,4,0,0,

View File

@ -1153,7 +1153,7 @@ const unsigned char _Py_M__importlib_bootstrap_external[] = {
0,169,2,114,123,0,0,0,114,191,0,0,0,114,7,0, 0,169,2,114,123,0,0,0,114,191,0,0,0,114,7,0,
0,0,114,7,0,0,0,114,8,0,0,0,218,13,99,114, 0,0,114,7,0,0,0,114,8,0,0,0,218,13,99,114,
101,97,116,101,95,109,111,100,117,108,101,55,3,0,0,115, 101,97,116,101,95,109,111,100,117,108,101,55,3,0,0,115,
4,0,0,0,4,128,255,128,122,27,95,76,111,97,100,101, 4,0,0,0,4,0,255,128,122,27,95,76,111,97,100,101,
114,66,97,115,105,99,115,46,99,114,101,97,116,101,95,109, 114,66,97,115,105,99,115,46,99,114,101,97,116,101,95,109,
111,100,117,108,101,99,2,0,0,0,0,0,0,0,0,0, 111,100,117,108,101,99,2,0,0,0,0,0,0,0,0,0,
0,0,3,0,0,0,5,0,0,0,67,0,0,0,115,56, 0,0,3,0,0,0,5,0,0,0,67,0,0,0,115,56,
@ -1292,7 +1292,7 @@ const unsigned char _Py_M__importlib_bootstrap_external[] = {
32,32,32,78,114,7,0,0,0,41,3,114,123,0,0,0, 32,32,32,78,114,7,0,0,0,41,3,114,123,0,0,0,
114,52,0,0,0,114,37,0,0,0,114,7,0,0,0,114, 114,52,0,0,0,114,37,0,0,0,114,7,0,0,0,114,
7,0,0,0,114,8,0,0,0,114,232,0,0,0,105,3, 7,0,0,0,114,8,0,0,0,114,232,0,0,0,105,3,
0,0,115,4,0,0,0,4,128,255,128,122,21,83,111,117, 0,0,115,4,0,0,0,4,0,255,128,122,21,83,111,117,
114,99,101,76,111,97,100,101,114,46,115,101,116,95,100,97, 114,99,101,76,111,97,100,101,114,46,115,101,116,95,100,97,
116,97,99,2,0,0,0,0,0,0,0,0,0,0,0,5, 116,97,99,2,0,0,0,0,0,0,0,0,0,0,0,5,
0,0,0,10,0,0,0,67,0,0,0,115,70,0,0,0, 0,0,0,10,0,0,0,67,0,0,0,115,70,0,0,0,
@ -2019,7 +2019,7 @@ const unsigned char _Py_M__importlib_bootstrap_external[] = {
0,115,4,0,0,0,100,1,83,0,114,217,0,0,0,114, 0,115,4,0,0,0,100,1,83,0,114,217,0,0,0,114,
7,0,0,0,114,218,0,0,0,114,7,0,0,0,114,7, 7,0,0,0,114,218,0,0,0,114,7,0,0,0,114,7,
0,0,0,114,8,0,0,0,114,219,0,0,0,217,4,0, 0,0,0,114,8,0,0,0,114,219,0,0,0,217,4,0,
0,115,4,0,0,0,4,128,255,128,122,30,95,78,97,109, 0,115,4,0,0,0,4,0,255,128,122,30,95,78,97,109,
101,115,112,97,99,101,76,111,97,100,101,114,46,99,114,101, 101,115,112,97,99,101,76,111,97,100,101,114,46,99,114,101,
97,116,101,95,109,111,100,117,108,101,99,2,0,0,0,0, 97,116,101,95,109,111,100,117,108,101,99,2,0,0,0,0,
0,0,0,0,0,0,0,2,0,0,0,1,0,0,0,67, 0,0,0,0,0,0,0,2,0,0,0,1,0,0,0,67,