Skip to content
Merged
2 changes: 1 addition & 1 deletion Lib/test/support/bytecode_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def normalize_insts(self, insts):
if isinstance(oparg, self.Label):
arg = oparg.value
else:
arg = oparg if opcode in self.HAS_ARG else None
arg = oparg if opcode in self.HAS_ARG else 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? And why do some of the 'NOP's below in test_peepholer.py have a None operand and some have 0?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that was just messy. I've tidied it up now. Thanks.

opcode = dis.opname[opcode]
res.append((opcode, arg, *loc))
return res
Expand Down
39 changes: 26 additions & 13 deletions Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,15 +995,20 @@ def test_conditional_jump_forward_non_const_condition(self):
('LOAD_CONST', 2, 13),
lbl,
('LOAD_CONST', 3, 14),
('RETURN_VALUE', 14),
]
expected = [
expected_insts = [
('LOAD_NAME', 1, 11),
('POP_JUMP_IF_TRUE', lbl := self.Label(), 12),
('LOAD_CONST', 2, 13),
('LOAD_CONST', 1, 13),
lbl,
('LOAD_CONST', 3, 14)
('NOP', 0, 14),
('RETURN_CONST', 2, 0),
]
self.cfg_optimization_test(insts, expected, consts=list(range(5)))
self.cfg_optimization_test(insts,
expected_insts,
consts=[0, 1, 2, 3, 4],
expected_consts=[0, 2, 3])

def test_conditional_jump_forward_const_condition(self):
# The unreachable branch of the jump is removed, the jump
Expand All @@ -1015,43 +1020,51 @@ def test_conditional_jump_forward_const_condition(self):
('LOAD_CONST', 2, 13),
lbl,
('LOAD_CONST', 3, 14),
('RETURN_VALUE', 14),
]
expected = [
expected_insts = [
('NOP', None, 11),
('NOP', None, 12),
('LOAD_CONST', 3, 14)
('NOP', 0, 14),
('RETURN_CONST', 1, 0),
]
self.cfg_optimization_test(insts, expected, consts=list(range(5)))
self.cfg_optimization_test(insts,
expected_insts,
consts=[0, 1, 2, 3, 4],
expected_consts=[0, 3])

def test_conditional_jump_backward_non_const_condition(self):
insts = [
lbl1 := self.Label(),
('LOAD_NAME', 1, 11),
('POP_JUMP_IF_TRUE', lbl1, 12),
('LOAD_CONST', 2, 13),
('LOAD_NAME', 2, 13),
('RETURN_VALUE', 13),
]
expected = [
lbl := self.Label(),
('LOAD_NAME', 1, 11),
('POP_JUMP_IF_TRUE', lbl, 12),
('LOAD_CONST', 2, 13)
('LOAD_NAME', 2, 13),
('RETURN_VALUE', 13),
]
self.cfg_optimization_test(insts, expected, consts=list(range(5)))

def test_conditional_jump_backward_const_condition(self):
# The unreachable branch of the jump is removed
insts = [
lbl1 := self.Label(),
('LOAD_CONST', 1, 11),
('LOAD_CONST', 3, 11),
('POP_JUMP_IF_TRUE', lbl1, 12),
('LOAD_CONST', 2, 13),
('RETURN_VALUE', 13),
]
expected = [
expected_insts = [
lbl := self.Label(),
('NOP', None, 11),
('JUMP', lbl, 12)
('JUMP', lbl, 12),
]
self.cfg_optimization_test(insts, expected, consts=list(range(5)))
self.cfg_optimization_test(insts, expected_insts, consts=list(range(5)))


if __name__ == "__main__":
Expand Down
Loading