Skip to content

Commit 742d461

Browse files
authored
GH-91409: Don't overwrite valid locations with NOP locations (GH-95067)
1 parent 41e0585 commit 742d461

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

Lib/test/test_compile.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,44 @@ def test_multiline_assert_rewritten_as_method_call(self):
12021202
tree.body[0] = new_node
12031203
compile(tree, "<test>", "exec")
12041204

1205+
def test_push_null_load_global_positions(self):
1206+
source_template = """
1207+
import abc, dis
1208+
import ast as art
1209+
1210+
abc = None
1211+
dix = dis
1212+
ast = art
1213+
1214+
def f():
1215+
{}
1216+
"""
1217+
for body in [
1218+
" abc.a()",
1219+
" art.a()",
1220+
" ast.a()",
1221+
" dis.a()",
1222+
" dix.a()",
1223+
" abc[...]()",
1224+
" art()()",
1225+
" (ast or ...)()",
1226+
" [dis]()",
1227+
" (dix + ...)()",
1228+
]:
1229+
with self.subTest(body):
1230+
namespace = {}
1231+
source = textwrap.dedent(source_template.format(body))
1232+
exec(source, namespace)
1233+
code = namespace["f"].__code__
1234+
self.assertOpcodeSourcePositionIs(
1235+
code,
1236+
"LOAD_GLOBAL",
1237+
line=10,
1238+
end_line=10,
1239+
column=4,
1240+
end_column=7,
1241+
)
1242+
12051243

12061244
class TestExpressionStackSize(unittest.TestCase):
12071245
# These tests check that the computed stack size for a code object
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix incorrect source location info caused by certain optimizations in the
2+
bytecode compiler.

Python/compile.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9278,7 +9278,10 @@ clean_basic_block(basicblock *bb) {
92789278
/* or, if the next instruction has same line number or no line number */
92799279
if (src < bb->b_iused - 1) {
92809280
int next_lineno = bb->b_instr[src+1].i_loc.lineno;
9281-
if (next_lineno < 0 || next_lineno == lineno) {
9281+
if (next_lineno == lineno) {
9282+
continue;
9283+
}
9284+
if (next_lineno < 0) {
92829285
bb->b_instr[src+1].i_loc = bb->b_instr[src].i_loc;
92839286
continue;
92849287
}

0 commit comments

Comments
 (0)