From 10bf8d71d8f8f8058c2ad86bd7a8344669ac322a Mon Sep 17 00:00:00 2001 From: Ben Hsing Date: Wed, 19 Jun 2024 05:07:49 +0000 Subject: [PATCH 1/3] gh-120722: propogate instruction location in RETURN_VALUE optimization with LOAD_CONST --- Python/flowgraph.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Python/flowgraph.c b/Python/flowgraph.c index 8c1c20a0583c8c..19e2604c0c2713 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -116,6 +116,16 @@ is_jump(cfg_instr *i) _instr__ptr_->i_oparg = (ARG); \ } while (0); +/* One arg with location*/ +#define INSTR_SET_OP1_LOC(I, OP, ARG, LOC) \ + do { \ + assert(OPCODE_HAS_ARG(OP)); \ + cfg_instr *_instr__ptr_ = (I); \ + _instr__ptr_->i_opcode = (OP); \ + _instr__ptr_->i_oparg = (ARG); \ + _instr__ptr_->i_loc = (LOC); \ + } while (0); + /* No args*/ #define INSTR_SET_OP0(I, OP) \ do { \ @@ -1661,7 +1671,8 @@ basicblock_optimize_load_const(PyObject *const_cache, basicblock *bb, PyObject * case RETURN_VALUE: { INSTR_SET_OP0(inst, NOP); - INSTR_SET_OP1(&bb->b_instr[++i], RETURN_CONST, oparg); + INSTR_SET_OP1_LOC(&bb->b_instr[++i], RETURN_CONST, oparg, + inst->i_loc); break; } case TO_BOOL: From 3a7e863abbbb9de62b13dace6cd4fb9499034ef2 Mon Sep 17 00:00:00 2001 From: Ben Hsing Date: Wed, 19 Jun 2024 05:24:42 +0000 Subject: [PATCH 2/3] gh-120722: added test --- Lib/test/test_compile.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index e3e86b89246648..cb36fee3ae134d 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -2028,6 +2028,15 @@ def test_load_super_attr(self): code, "LOAD_GLOBAL", line=3, end_line=3, column=4, end_column=9 ) + def test_constant_lambda(self): + namespace = {} + source = "f = lambda: 1" + code = compile(source, "", "exec") + exec(code, namespace) + self.assertOpcodeSourcePositionIs( + namespace["f"].__code__, "RETURN_CONST", line=1, end_line=1, + column=12, end_column=13 + ) class TestExpectedAttributes(unittest.TestCase): From 482fa1780f08e1ca633faba67501e09bb4d1e299 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 19 Jun 2024 05:32:36 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2024-06-19-05-32-32.gh-issue-120722.4ZDtZr.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-06-19-05-32-32.gh-issue-120722.4ZDtZr.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-06-19-05-32-32.gh-issue-120722.4ZDtZr.rst b/Misc/NEWS.d/next/Core and Builtins/2024-06-19-05-32-32.gh-issue-120722.4ZDtZr.rst new file mode 100644 index 00000000000000..1aef266ad238c1 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-06-19-05-32-32.gh-issue-120722.4ZDtZr.rst @@ -0,0 +1 @@ +Fixed an issue where the ``RETURN_CONST`` instruction in the code object of a lambda function returning a constant value was not accompanied with correct location information.