Skip to content

Commit 59423b5

Browse files
committed
add test_constant_folding_small_int
1 parent 5ad6781 commit 59423b5

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Lib/test/test_peepholer.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,28 @@ def test_constant_folding(self):
473473
self.assertFalse(instr.opname.startswith('BUILD_'))
474474
self.check_lnotab(code)
475475

476+
def test_constant_folding_small_int(self):
477+
tests = [
478+
# subscript
479+
('(0, )[0]', 0),
480+
('(1 + 2, )[0]', 3),
481+
('(2 + 2 * 2, )[0]', 6),
482+
('(1, (1 + 2 + 3, ))[1][0]', 6),
483+
('(255, )[0]', 255),
484+
('(256, )[0]', None),
485+
('(1000, )[0]', None),
486+
('(1 - 2, )[0]', None),
487+
]
488+
489+
for expr, oparg in tests:
490+
with self.subTest(expr=expr, oparg=oparg):
491+
code = compile(expr, '', 'single')
492+
if oparg is not None:
493+
self.assertInBytecode(code, 'LOAD_SMALL_INT', oparg)
494+
else:
495+
self.assertNotInBytecode(code, 'LOAD_SMALL_INT')
496+
self.check_lnotab(code)
497+
476498
def test_folding_subscript(self):
477499
tests = [
478500
('(1, )[0]', False),

0 commit comments

Comments
 (0)