Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eaccc12

Browse files
authored
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.
1 parent f24b810 commit eaccc12

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

Lib/test/test_compile.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,24 @@ def save_caller_frame():
809809
func(save_caller_frame)
810810
self.assertEqual(frame.f_lineno-frame.f_code.co_firstlineno, lastline)
811811

812+
def test_lineno_after_no_code(self):
813+
def no_code1():
814+
"doc string"
815+
816+
def no_code2():
817+
a: int
818+
819+
for func in (no_code1, no_code2):
820+
with self.subTest(func=func):
821+
code = func.__code__
822+
lines = list(code.co_lines())
823+
self.assertEqual(len(lines), 1)
824+
start, end, line = lines[0]
825+
self.assertEqual(start, 0)
826+
self.assertEqual(end, len(code.co_code))
827+
self.assertEqual(line, code.co_firstlineno)
828+
829+
812830
def test_big_dict_literal(self):
813831
# The compiler has a flushing point in "compiler_dict" that calls compiles
814832
# a portion of the dictionary literal when the loop that iterates over the items

Python/compile.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6514,6 +6514,7 @@ is_exit_without_lineno(basicblock *b) {
65146514
static int
65156515
ensure_exits_have_lineno(struct compiler *c)
65166516
{
6517+
basicblock *entry = NULL;
65176518
/* Copy all exit blocks without line number that are targets of a jump.
65186519
*/
65196520
for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
@@ -6535,6 +6536,11 @@ ensure_exits_have_lineno(struct compiler *c)
65356536
b->b_instr[b->b_iused-1].i_target = new_target;
65366537
}
65376538
}
6539+
entry = b;
6540+
}
6541+
assert(entry != NULL);
6542+
if (is_exit_without_lineno(entry)) {
6543+
entry->b_instr[0].i_lineno = c->u->u_firstlineno;
65386544
}
65396545
/* Any remaining reachable exit blocks without line number can only be reached by
65406546
* fall through, and thus can only have a single predecessor */

Python/importlib.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/importlib_external.h

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)