summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrandt Bucher <brandtbucher@microsoft.com>2022-07-20 22:01:42 (GMT)
committerGitHub <noreply@github.com>2022-07-20 22:01:42 (GMT)
commite2fce3a8e77d33d52d47b567964ddebd6984a097 (patch)
tree52c224420a99a3c7fb4e928cc270985ea05fb52a
parent6515738c0e6b2ff3ed5ce40fb0d61632648efbe3 (diff)
downloadcpython-e2fce3a8e77d33d52d47b567964ddebd6984a097.zip
cpython-e2fce3a8e77d33d52d47b567964ddebd6984a097.tar.gz
cpython-e2fce3a8e77d33d52d47b567964ddebd6984a097.tar.bz2
[3.11] GH-91409: Don't overwrite valid locations with NOP locations (GH-95067) (GH-95068)
(cherry picked from commit 742d4614e1a645d765dbf76c19bd9a818239b1cb)
-rw-r--r--Lib/test/test_compile.py38
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2022-07-20-13-46-01.gh-issue-91409.dhL8Zo.rst2
-rw-r--r--Python/compile.c5
3 files changed, 44 insertions, 1 deletions
diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py
index 80b32de..e226964 100644
--- a/Lib/test/test_compile.py
+++ b/Lib/test/test_compile.py
@@ -1166,6 +1166,44 @@ f(
tree.body[0] = new_node
compile(tree, "<test>", "exec")
+ def test_push_null_load_global_positions(self):
+ source_template = """
+ import abc, dis
+ import ast as art
+
+ abc = None
+ dix = dis
+ ast = art
+
+ def f():
+ {}
+ """
+ for body in [
+ " abc.a()",
+ " art.a()",
+ " ast.a()",
+ " dis.a()",
+ " dix.a()",
+ " abc[...]()",
+ " art()()",
+ " (ast or ...)()",
+ " [dis]()",
+ " (dix + ...)()",
+ ]:
+ with self.subTest(body):
+ namespace = {}
+ source = textwrap.dedent(source_template.format(body))
+ exec(source, namespace)
+ code = namespace["f"].__code__
+ self.assertOpcodeSourcePositionIs(
+ code,
+ "LOAD_GLOBAL",
+ line=10,
+ end_line=10,
+ column=4,
+ end_column=7,
+ )
+
class TestExpressionStackSize(unittest.TestCase):
# These tests check that the computed stack size for a code object
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-07-20-13-46-01.gh-issue-91409.dhL8Zo.rst b/Misc/NEWS.d/next/Core and Builtins/2022-07-20-13-46-01.gh-issue-91409.dhL8Zo.rst
new file mode 100644
index 0000000..2bc0d82
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-07-20-13-46-01.gh-issue-91409.dhL8Zo.rst
@@ -0,0 +1,2 @@
+Fix incorrect source location info caused by certain optimizations in the
+bytecode compiler.
diff --git a/Python/compile.c b/Python/compile.c
index 3fe0930..e8acd85 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -9045,7 +9045,10 @@ clean_basic_block(basicblock *bb) {
/* or, if the next instruction has same line number or no line number */
if (src < bb->b_iused - 1) {
int next_lineno = bb->b_instr[src+1].i_lineno;
- if (next_lineno < 0 || next_lineno == lineno) {
+ if (next_lineno == lineno) {
+ continue;
+ }
+ if (next_lineno < 0) {
COPY_INSTR_LOC(bb->b_instr[src], bb->b_instr[src+1]);
continue;
}