Skip to content
This repository was archived by the owner on Mar 2, 2022. It is now read-only.

Commit e046cdc

Browse files
committed
Fix peephole tests to avoid not emitting AST folded constants
Summary: Our existing peep hole tests just load things which get folded into constants. Under the 3.6 compiler the folded constants stick around, there's an optimization at code gen type where we remove constants, then the peep hole optimizer runs and turns the tuples into constants, but the LOAD_CONST/POP still remains. On 3.7 the AST optimizer runs first, turns these into LOAD_CONST's, and then they get excluded from code gen. By turning them into a STORE_... the code generator can't remove them, and the tests pass on both 3.6 and 3.7. Test Plan: ./python -m test.test_compiler
1 parent 8f91dab commit e046cdc

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

test_compiler/test_peephole.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -381,17 +381,17 @@ def test_pack_unpack(self):
381381
def test_folding_of_tuples_of_constants(self):
382382
for line, elem in (
383383
("a = 1,2,3", (1, 2, 3)),
384-
('("a","b","c")', ("a", "b", "c")),
384+
('a = ("a","b","c")', ("a", "b", "c")),
385385
("a,b,c = 1,2,3", (1, 2, 3)),
386-
("(None, 1, None)", (None, 1, None)),
387-
("((1, 2), 3, 4)", ((1, 2), 3, 4)),
386+
("a = (None, 1, None)", (None, 1, None)),
387+
("a = ((1, 2), 3, 4)", ((1, 2), 3, 4)),
388388
):
389389
code = self.peephole_compile(line)
390390
code.assert_added("LOAD_CONST", elem)
391391
code.assert_removed("BUILD_TUPLE")
392392

393393
# Long tuples should be folded too.
394-
code = self.peephole_compile(repr(tuple(range(10000))))
394+
code = self.peephole_compile("x=" + repr(tuple(range(10000))))
395395
code.assert_removed("BUILD_TUPLE")
396396
# One LOAD_CONST for the tuple, one for the None return value
397397
code.assert_instr_count("LOAD_CONST", 10001, 2)
@@ -547,7 +547,7 @@ def g(a):
547547
def test_folding_of_binops_on_constants(self):
548548
for line, elem in (
549549
("a = 2+3+4", 9), # chained fold
550-
('"@"*4', "@@@@"), # check string ops
550+
('a = "@"*4', "@@@@"), # check string ops
551551
('a="abc" + "def"', "abcdef"), # check string ops
552552
("a = 3**4", 81), # binary power
553553
("a = 3*4", 12), # binary multiply
@@ -585,31 +585,31 @@ def test_folding_of_binops_on_constants(self):
585585

586586
def test_binary_subscr_on_unicode(self):
587587
# valid code get optimized
588-
code = self.peephole_compile('"foo"[0]')
588+
code = self.peephole_compile('x = "foo"[0]')
589589
code.assert_added("LOAD_CONST", "f")
590590
code.assert_removed("BINARY_SUBSCR")
591-
code = self.peephole_compile('"\u0061\uffff"[1]')
591+
code = self.peephole_compile('x = "\u0061\uffff"[1]')
592592
code.assert_added("LOAD_CONST", "\uffff")
593593
code.assert_removed("BINARY_SUBSCR")
594594

595595
# With PEP 393, non-BMP char get optimized
596-
code = self.peephole_compile('"\U00012345"[0]')
596+
code = self.peephole_compile('x = "\U00012345"[0]')
597597
code.assert_both("LOAD_CONST", "\U00012345")
598598
code.assert_removed("BINARY_SUBSCR")
599599

600600
# invalid code doesn't get optimized
601601
# out of range
602-
code = self.peephole_compile('"fuu"[10]')
602+
code = self.peephole_compile('x = "fuu"[10]')
603603
code.assert_both("BINARY_SUBSCR")
604604

605605
def test_folding_of_unaryops_on_constants(self):
606606
for line, elem in (
607-
("-0.5", -0.5), # unary negative
608-
("-0.0", -0.0), # -0.0
609-
("-(1.0-1.0)", -0.0), # -0.0 after folding
610-
("-0", 0), # -0
611-
("~-2", 1), # unary invert
612-
("+1", 1), # unary positive
607+
("x = -0.5", -0.5), # unary negative
608+
("x = -0.0", -0.0), # -0.0
609+
("x = -(1.0-1.0)", -0.0), # -0.0 after folding
610+
("x = -0", 0), # -0
611+
("x = ~-2", 1), # unary invert
612+
("x = +1", 1), # unary positive
613613
):
614614
code = self.peephole_compile(line)
615615
# can't assert added here because -0/0 compares equal

0 commit comments

Comments
 (0)