Skip to content

Commit 220bbf9

Browse files
committed
Handle SET_FUNCTION_ATTRIBUTE correctly
It leaves the function on the stack
1 parent 59b5886 commit 220bbf9

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

Lib/test/test_peepholer.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2637,6 +2637,32 @@ def test_format_simple(self):
26372637
]
26382638
self.check(insts, expected)
26392639

2640+
def test_set_function_attribute(self):
2641+
# SET_FUNCTION_ATTRIBUTE leaves the function on the stack
2642+
insts = [
2643+
("LOAD_CONST", 0, 1),
2644+
("LOAD_FAST", 0, 2),
2645+
("SET_FUNCTION_ATTRIBUTE", 2, 3),
2646+
("STORE_FAST", 1, 4),
2647+
("LOAD_CONST", 0, 5),
2648+
("RETURN_VALUE", None, 6)
2649+
]
2650+
self.cfg_optimization_test(insts, insts, consts=[None])
2651+
2652+
insts = [
2653+
("LOAD_CONST", 0, 1),
2654+
("LOAD_FAST", 0, 2),
2655+
("SET_FUNCTION_ATTRIBUTE", 2, 3),
2656+
("RETURN_VALUE", None, 4)
2657+
]
2658+
expected = [
2659+
("LOAD_CONST", 0, 1),
2660+
("LOAD_FAST_BORROW", 0, 2),
2661+
("SET_FUNCTION_ATTRIBUTE", 2, 3),
2662+
("RETURN_VALUE", None, 4)
2663+
]
2664+
self.cfg_optimization_test(insts, expected, consts=[None])
2665+
26402666
def test_del_in_finally(self):
26412667
# This loads `obj` onto the stack, executes `del obj`, then returns the
26422668
# `obj` from the stack. See gh-133371 for more details.

Python/flowgraph.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2944,6 +2944,16 @@ optimize_load_fast(cfg_builder *g)
29442944
break;
29452945
}
29462946

2947+
case SET_FUNCTION_ATTRIBUTE: {
2948+
assert(_PyOpcode_num_popped(opcode, oparg) == 2);
2949+
assert(_PyOpcode_num_pushed(opcode, oparg) == 1);
2950+
ref func = ref_stack_pop(&refs);
2951+
// Pop attr
2952+
ref_stack_pop(&refs);
2953+
PUSH_REF(func.instr, func.local);
2954+
break;
2955+
}
2956+
29472957
// Opcodes that consume all of their inputs
29482958
default: {
29492959
int num_popped = _PyOpcode_num_popped(opcode, oparg);

0 commit comments

Comments
 (0)