Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<test>", "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):

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
13 changes: 12 additions & 1 deletion Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 { \
Expand Down Expand Up @@ -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:
Expand Down