Index: Python/peephole.c =================================================================== --- Python/peephole.c (revision 77447) +++ Python/peephole.c (working copy) @@ -31,7 +31,8 @@ new constant (c1, c2, ... cn) can be appended. Called with codestr pointing to the first LOAD_CONST. Bails out with no change if one or more of the LOAD_CONSTs is missing. - Also works for BUILD_LIST when followed by an "in" or "not in" test. + Also works for BUILD_LIST and BUILT_SET when followed by an "in" or "not in" + test; for BUILD_SET it assembles a frozenset rather than a tuple. */ static int tuple_of_constants(unsigned char *codestr, Py_ssize_t n, PyObject *consts) @@ -41,7 +42,7 @@ /* Pre-conditions */ assert(PyList_CheckExact(consts)); - assert(codestr[n*3] == BUILD_TUPLE || codestr[n*3] == BUILD_LIST); + assert(codestr[n*3] == BUILD_TUPLE || codestr[n*3] == BUILD_LIST || codestr[n*3] == BUILD_SET); assert(GETARG(codestr, (n*3)) == n); for (i=0 ; i= 0 && j <= lastlc && ((opcode == BUILD_TUPLE && ISBASICBLOCK(blocks, h, 3*(j+1))) || - (opcode == BUILD_LIST && + ((opcode == BUILD_LIST || opcode == BUILD_SET) && codestr[i+3]==COMPARE_OP && ISBASICBLOCK(blocks, h, 3*(j+2)) && (GETARG(codestr,i+3)==6 || Index: Lib/test/test_peepholer.py =================================================================== --- Lib/test/test_peepholer.py (revision 77447) +++ Lib/test/test_peepholer.py (working copy) @@ -115,6 +115,55 @@ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ],) + def test_folding_of_lists_of_constants(self): + for line, elem in ( + # in/not in constants with BUILD_LIST should be folded to a tuple: + ('a in [1,2,3]', '(1, 2, 3)'), + ('a not in ["a","b","c"]', "(('a', 'b', 'c'))"), + ('a in [None, 1, None]', '((None, 1, None))'), + ('a not in [(1, 2), 3, 4]', '(((1, 2), 3, 4))'), + ): + asm = dis_single(line) + self.assertIn(elem, asm) + self.assertNotIn('BUILD_LIST', asm) + + def test_folding_of_sets_of_constants(self): + for line, elem in ( + # in/not in constants with BUILD_SET should be folded to a frozenset: + ('a in {1,2,3}', frozenset({1, 2, 3})), + ('a not in {"a","b","c"}', frozenset({'a', 'c', 'b'})), + ('a in {None, 1, None}', frozenset({1, None})), + ('a not in {(1, 2), 3, 4}', frozenset({(1, 2), 3, 4})), + ('a in {1, 2, 3, 3, 2, 1}', frozenset({1, 2, 3})), + ): + asm = dis_single(line) + self.assertNotIn('BUILD_SET', asm) + + # Verify that the frozenset 'elem' is in the disassembly + # The ordering of the elements in repr( frozenset ) isn't + # guaranteed, so we jump through some hoops to ensure that we have + # the frozenset we expect: + self.assertIn('frozenset', asm) + # Extract the frozenset literal from the disassembly: + import re + m = re.match(r'.*(frozenset\({.*}\)).*', asm, re.DOTALL) + self.assertTrue(m) + self.assertEqual(eval(m.group(1)), elem) + + # Ensure that the resulting code actually works: + def f(a): + return a in {1, 2, 3} + + def g(a): + return a not in {1, 2, 3} + + self.assertTrue(f(3)) + self.assertTrue(not f(4)) + + self.assertTrue(not g(3)) + self.assertTrue(g(4)) + + def test_folding_of_binops_on_constants(self): for line, elem in ( ('a = 2+3+4', '(9)'), # chained fold