8000 [3.11] GH-91409: Don't overwrite valid locations with NOP locations (… · python/cpython@e2fce3a · GitHub
[go: up one dir, main page]

Skip to content

Commit e2fce3a

Browse files
authored
[3.11] GH-91409: Don't overwrite valid locations with NOP locations (GH-95067) (GH-95068)
(cherry picked from commit 742d461)
1 parent 6515738 commit e2fce3a

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
@@ -1166,6 +1166,44 @@ def test_multiline_assert_rewritten_as_method_call(self):
11661166
tree.body[0] = new_node
11671167
compile(tree, "<test>", "exec")
11681168

1169+
def test_push_null_load_global_positions(self):
1170+
source_template = """
1171+
import abc, dis
1172+
import ast as art
1173+
1174+
abc = None
1175+
dix = dis
1176+
ast = art
1177+
1178+
def f():
1179+
{}
1180+
"""
1181+
for body in [
1182+
" abc.a()",
1183+
" art.a()",
1184+
" ast.a()",
1185+
" dis.a()",
1186+
" dix.a()",
1187+
" abc[...]()",
1188+
" art()()",
1189+
" (ast or ...)()",
1190+
" [dis]()",
1191+
" (dix + ...)()",
1192+
]:
1193+
with self.subTest(body):
1194+
namespace = {}
1195+
source = textwrap.dedent(source_template.format(body))
1196+
exec(source, namespace)
1197+
code = namespace["f"].__code__
1198+
self.assertOpcodeSourcePositionIs(
1199+
code,
1200+
"LOAD_GLOBAL",
1201+
line=10,
1202+
end_line=10,
1203+
column=4,
1204+
end_column=7,
1205+
)
1206+
11691207

11701208
class TestExpressionStackSize(unittest.TestCase):
11711209
# 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
@@ -9045,7 +9045,10 @@ clean_basic_block(basicblock *bb) {
90459045
/* or, if the next instruction has same line number or no line number */
90469046
if (src < bb->b_iused - 1) {
90479047
int next_lineno = bb->b_instr[src+1].i_lineno;
9048-
if (next_lineno < 0 || next_lineno == lineno) {
9048+
if (next_lineno == lineno) {
9049+
continue;
9050+
}
9051+
if (next_lineno < 0) {
90499052
COPY_INSTR_LOC(bb->b_instr[src], bb->b_instr[src+1]);
90509053
continue;
90519054
}

0 commit comments

Comments
 (0)
0