Skip to content

Commit 88a7797

Browse files
committed
add tests
1 parent ed4820b commit 88a7797

File tree

2 files changed

+199
-0
lines changed

2 files changed

+199
-0
lines changed

Lib/test/support/bytecode_helper.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ def assertInstructionsMatch(self, actual_seq, expected):
7777
self.assertIsInstance(expected, list)
7878
actual = actual_seq.get_instructions()
7979
expected = self.seq_from_insts(expected).get_instructions()
80+
if len(expected) == 9 and len(actual) == 7:
81+
print('actual', actual)
82+
print('expected', expected)
8083
self.assertEqual(len(actual), len(expected))
8184

8285
# compare instructions

Lib/test/test_peepholer.py

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,202 @@ def test_optimize_if_const_set(self):
12611261
]
12621262
self.cfg_optimization_test(same, same, consts=[])
12631263

1264+
def test_optimize_literal_list_for_iter(self):
1265+
# for _ in [1, 2]: pass ==> for _ in (1, 2): pass
1266+
before = [
1267+
('LOAD_SMALL_INT', 1, 0),
1268+
('LOAD_SMALL_INT', 2, 0),
1269+
('BUILD_LIST', 2, 0),
1270+
('GET_ITER', None, 0),
1271+
start := self.Label(),
1272+
('FOR_ITER', end := self.Label(), 0),
1273+
('STORE_FAST', 0, 0),
1274+
('JUMP', start, 0),
1275+
end,
1276+
('END_FOR', None, 0),
1277+
('POP_ITER', None, 0),
1278+
('LOAD_CONST', 0, 0),
1279+
('RETURN_VALUE', None, 0),
1280+
]
1281+
after = [
1282+
('LOAD_CONST', 1, 0),
1283+
('GET_ITER', None, 0),
1284+
start := self.Label(),
1285+
('FOR_ITER', end := self.Label(), 0),
1286+
('STORE_FAST', 0, 0),
1287+
('JUMP', start, 0),
1288+
end,
1289+
('END_FOR', None, 0),
1290+
('POP_ITER', None, 0),
1291+
('LOAD_CONST', 0, 0),
1292+
('RETURN_VALUE', None, 0),
1293+
]
1294+
self.cfg_optimization_test(before, after, consts=[None], expected_consts=[None, (1, 2)])
1295+
1296+
# for _ in [1, x]: pass ==> for _ in (1, x): pass
1297+
before = [
1298+
('LOAD_SMALL_INT', 1, 0),
1299+
('LOAD_NAME', 0, 0),
1300+
('BUILD_LIST', 2, 0),
1301+
('GET_ITER', None, 0),
1302+
start := self.Label(),
1303+
('FOR_ITER', end := self.Label(), 0),
1304+
('STORE_FAST', 0, 0),
1305+
('JUMP', start, 0),
1306+
end,
1307+
('END_FOR', None, 0),
1308+
('POP_ITER', None, 0),
1309+
('LOAD_CONST', 0, 0),
1310+
('RETURN_VALUE', None, 0),
1311+
]
1312+
after = [
1313+
('LOAD_SMALL_INT', 1, 0),
1314+
('LOAD_NAME', 0, 0),
1315+
('BUILD_TUPLE', 2, 0),
1316+
('GET_ITER', None, 0),
1317+
start := self.Label(),
1318+
('FOR_ITER', end := self.Label(), 0),
1319+
('STORE_FAST', 0, 0),
1320+
('JUMP', start, 0),
1321+
end,
1322+
('END_FOR', None, 0),
1323+
('POP_ITER', None, 0),
1324+
('LOAD_CONST', 0, 0),
1325+
('RETURN_VALUE', None, 0),
1326+
]
1327+
self.cfg_optimization_test(before, after, consts=[None], expected_consts=[None])
1328+
1329+
def test_optimize_literal_set_for_iter(self):
1330+
# for _ in {1, 2}: pass ==> for _ in (1, 2): pass
1331+
before = [
1332+
('LOAD_SMALL_INT', 1, 0),
1333+
('LOAD_SMALL_INT', 2, 0),
1334+
('BUILD_SET', 2, 0),
1335+
('GET_ITER', None, 0),
1336+
start := self.Label(),
1337+
('FOR_ITER', end := self.Label(), 0),
1338+
('STORE_FAST', 0, 0),
1339+
('JUMP', start, 0),
1340+
end,
1341+
('END_FOR', None, 0),
1342+
('POP_ITER', None, 0),
1343+
('LOAD_CONST', 0, 0),
1344+
('RETURN_VALUE', None, 0),
1345+
]
1346+
after = [
1347+
('LOAD_CONST', 1, 0),
1348+
('GET_ITER', None, 0),
1349+
start := self.Label(),
1350+
('FOR_ITER', end := self.Label(), 0),
1351+
('STORE_FAST', 0, 0),
1352+
('JUMP', start, 0),
1353+
end,
1354+
('END_FOR', None, 0),
1355+
('POP_ITER', None, 0),
1356+
('LOAD_CONST', 0, 0),
1357+
('RETURN_VALUE', None, 0),
1358+
]
1359+
self.cfg_optimization_test(before, after, consts=[None], expected_consts=[None, frozenset({1, 2})])
1360+
1361+
# non constant literal set is not changed
1362+
# for _ in {1, x}: pass ==> for _ in {1, x}: pass
1363+
same = [
1364+
('LOAD_SMALL_INT', 1, 0),
1365+
('LOAD_NAME', 0, 0),
1366+
('BUILD_SET', 2, 0),
1367+
('GET_ITER', None, 0),
1368+
start := self.Label(),
1369+
('FOR_ITER', end := self.Label(), 0),
1370+
('STORE_FAST', 0, 0),
1371+
('JUMP', start, 0),
1372+
end,
1373+
('END_FOR', None, 0),
1374+
('POP_ITER', None, 0),
1375+
('LOAD_CONST', 0, 0),
1376+
('RETURN_VALUE', None, 0),
1377+
]
1378+
self.cfg_optimization_test(same, same, consts=[None], expected_consts=[None])
1379+
1380+
def test_optimize_literal_list_contains(self):
1381+
# x in [1, 2] ==> x in (1, 2)
1382+
before = [
1383+
('LOAD_NAME', 0, 0),
1384+
('LOAD_SMALL_INT', 1, 0),
1385+
('LOAD_SMALL_INT', 2, 0),
1386+
('BUILD_LIST', 2, 0),
1387+
('CONTAINS_OP', 0, 0),
1388+
('POP_TOP', None, 0),
1389+
('LOAD_CONST', 0, 0),
1390+
('RETURN_VALUE', None, 0),
1391+
]
1392+
after = [
1393+
('LOAD_NAME', 0, 0),
1394+
('LOAD_CONST', 1, 0),
1395+
('CONTAINS_OP', 0, 0),
1396+
('POP_TOP', None, 0),
1397+
('LOAD_CONST', 0, 0),
1398+
('RETURN_VALUE', None, 0),
1399+
]
1400+
self.cfg_optimization_test(before, after, consts=[None], expected_consts=[None, (1, 2)])
1401+
1402+
# x in [1, y] ==> x in (1, y)
1403+
before = [
1404+
('LOAD_NAME', 0, 0),
1405+
('LOAD_SMALL_INT', 1, 0),
1406+
('LOAD_NAME', 1, 0),
1407+
('BUILD_LIST', 2, 0),
1408+
('CONTAINS_OP', 0, 0),
1409+
('POP_TOP', None, 0),
1410+
('LOAD_CONST', 0, 0),
1411+
('RETURN_VALUE', None, 0),
1412+
]
1413+
after = [
1414+
('LOAD_NAME', 0, 0),
1415+
('LOAD_SMALL_INT', 1, 0),
1416+
('LOAD_NAME', 1, 0),
1417+
('BUILD_TUPLE', 2, 0),
1418+
('CONTAINS_OP', 0, 0),
1419+
('POP_TOP', None, 0),
1420+
('LOAD_CONST', 0, 0),
1421+
('RETURN_VALUE', None, 0),
1422+
]
1423+
self.cfg_optimization_test(before, after, consts=[None], expected_consts=[None])
1424+
1425+
def test_optimize_literal_set_contains(self):
1426+
# x in {1, 2} ==> x in (1, 2)
1427+
before = [
1428+
('LOAD_NAME', 0, 0),
1429+
('LOAD_SMALL_INT', 1, 0),
1430+
('LOAD_SMALL_INT', 2, 0),
1431+
('BUILD_SET', 2, 0),
1432+
('CONTAINS_OP', 0, 0),
1433+
('POP_TOP', None, 0),
1434+
('LOAD_CONST', 0, 0),
1435+
('RETURN_VALUE', None, 0),
1436+
]
1437+
after = [
1438+
('LOAD_NAME', 0, 0),
1439+
('LOAD_CONST', 1, 0),
1440+
('CONTAINS_OP', 0, 0),
1441+
('POP_TOP', None, 0),
1442+
('LOAD_CONST', 0, 0),
1443+
('RETURN_VALUE', None, 0),
1444+
]
1445+
self.cfg_optimization_test(before, after, consts=[None], expected_consts=[None, frozenset({1, 2})])
1446+
1447+
# non constant literal set is not changed
1448+
# x in {1, y} ==> x in {1, y}
1449+
same = [
1450+
('LOAD_NAME', 0, 0),
1451+
('LOAD_SMALL_INT', 1, 0),
1452+
('LOAD_NAME', 1, 0),
1453+
('BUILD_SET', 2, 0),
1454+
('CONTAINS_OP', 0, 0),
1455+
('POP_TOP', None, 0),
1456+
('LOAD_CONST', 0, 0),
1457+
('RETURN_VALUE', None, 0),
1458+
]
1459+
self.cfg_optimization_test(same, same, consts=[None], expected_consts=[None])
12641460

12651461
def test_conditional_jump_forward_const_condition(self):
12661462
# The unreachable branch of the jump is removed, the jump

0 commit comments

Comments
 (0)