From eaccc12aa986f92ea05f3f0a63cedbff78dd67f1 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Fri, 4 Dec 2020 15:22:12 +0000 Subject: [PATCH] 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. --- Lib/test/test_compile.py | 18 ++++++++++++++++++ Python/compile.c | 6 ++++++ Python/importlib.h | 2 +- Python/importlib_external.h | 6 +++--- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index 1e61f41c25b..0d11ce940f8 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -809,6 +809,24 @@ if 1: func(save_caller_frame) 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): # 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 diff --git a/Python/compile.c b/Python/compile.c index c67e8e885e4..ea9d6781b9c 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -6514,6 +6514,7 @@ is_exit_without_lineno(basicblock *b) { static int ensure_exits_have_lineno(struct compiler *c) { + basicblock *entry = NULL; /* 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) { @@ -6535,6 +6536,11 @@ ensure_exits_have_lineno(struct compiler *c) 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 * fall through, and thus can only have a single predecessor */ diff --git a/Python/importlib.h b/Python/importlib.h index c5ff9ece081..179f8df77a0 100644 --- a/Python/importlib.h +++ b/Python/importlib.h @@ -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, 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,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, 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, diff --git a/Python/importlib_external.h b/Python/importlib_external.h index c459bcf2e90..8f18d208d2e 100644 --- a/Python/importlib_external.h +++ b/Python/importlib_external.h @@ -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,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, - 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, 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, @@ -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, 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, - 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, 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, @@ -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, 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,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, 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,