Skip to content

Commit 8fe23b8

Browse files
authored
Update test_peepholer.py to 3.14.5 (RustPython#7864)
1 parent ff05dae commit 8fe23b8

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

Lib/test/test_peepholer.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ast
12
import dis
23
import gc
34
from itertools import combinations, product
@@ -1125,6 +1126,53 @@ def trace(frame, event, arg):
11251126

11261127
class DirectCfgOptimizerTests(CfgOptimizationTestCase):
11271128

1129+
def test_optimize_cfg_const_index_out_of_range(self):
1130+
insts = [
1131+
('LOAD_CONST', 2, 0),
1132+
('RETURN_VALUE', None, 0),
1133+
]
1134+
seq = self.seq_from_insts(insts)
1135+
with self.assertRaisesRegex(ValueError, "out of range"):
1136+
_testinternalcapi.optimize_cfg(seq, [0, 1], 0)
1137+
1138+
def test_optimize_cfg_consts_must_be_list(self):
1139+
insts = [
1140+
('LOAD_CONST', 0, 0),
1141+
('RETURN_VALUE', None, 0),
1142+
]
1143+
seq = self.seq_from_insts(insts)
1144+
with self.assertRaisesRegex(TypeError, "consts must be a list"):
1145+
_testinternalcapi.optimize_cfg(seq, (0,), 0)
1146+
1147+
def test_compiler_codegen_metadata_consts_roundtrips_optimize_cfg(self):
1148+
tree = ast.parse("x = (1, 2)", mode="exec", optimize=1)
1149+
insts, meta = _testinternalcapi.compiler_codegen(tree, "<s>", 0)
1150+
consts = meta["consts"]
1151+
self.assertIsInstance(consts, list)
1152+
_testinternalcapi.optimize_cfg(insts, consts, 0)
1153+
1154+
def test_compiler_codegen_consts_include_none_required_for_implicit_return(self):
1155+
# Module "pass" only needs the const table entry for None once
1156+
# _PyCodegen_AddReturnAtEnd runs. If metadata["consts"] were taken
1157+
# before that, the list would not match LOAD_CONST opargs (here: 0
1158+
# for None), and optimize_cfg would read out of range.
1159+
tree = ast.parse("pass", mode="exec", optimize=1)
1160+
insts, meta = _testinternalcapi.compiler_codegen(tree, "<s>", 0)
1161+
consts = meta["consts"]
1162+
self.assertEqual(consts, [None])
1163+
1164+
load_const = opcode.opmap["LOAD_CONST"]
1165+
self.assertEqual(
1166+
[t[1] for t in insts.get_instructions() if t[0] == load_const],
1167+
[0],
1168+
)
1169+
1170+
# As if consts were snapshotted before AddReturnAtEnd: still LOAD_CONST 0, no row.
1171+
with self.assertRaisesRegex(ValueError, "out of range"):
1172+
_testinternalcapi.optimize_cfg(insts, [], 0)
1173+
1174+
_testinternalcapi.optimize_cfg(insts, list(consts), 0)
1175+
11281176
def cfg_optimization_test(self, insts, expected_insts,
11291177
consts=None, expected_consts=None,
11301178
nlocals=0):

0 commit comments

Comments
 (0)