Index: Python/compile.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Python/compile.c,v retrieving revision 2.326 diff -u -r2.326 compile.c --- Python/compile.c 31 Aug 2004 10:07:13 -0000 2.326 +++ Python/compile.c 21 Sep 2004 03:59:09 -0000 @@ -388,6 +388,50 @@ #define CODESIZE(op) (HAS_ARG(op) ? 3 : 1) #define ISBASICBLOCK(blocks, start, bytes) (blocks[start]==blocks[start+bytes-1]) +static int +tuple_of_constants(unsigned char *codestr, int n, PyObject *consts) +{ + PyObject *newconst, *constant; + int i, arg, len_consts; + + /* Pre-conditions */ + assert(PyList_CheckExact(consts)); + assert(codestr[0] == LOAD_CONST); + assert(codestr[n*3] == BUILD_TUPLE); + assert(GETARG(codestr, (n*3)) == n); + + /* Verify chain of n load_constants */ + for (i=0 ; i= 0 && + codestr[h] == LOAD_CONST && + ISBASICBLOCK(blocks, h, 3*(j+1)) && + tuple_of_constants(&codestr[h], j, consts)) { + nops += 3 * j; + break; + } + /* Intentional fall-through */ case BUILD_LIST: j = GETARG(codestr, i); if (codestr[i+3] != UNPACK_SEQUENCE || @@ -4899,7 +4954,6 @@ if (sc.c_errors == 0) { PyObject *consts, *names, *varnames, *filename, *name, *freevars, *cellvars, *code; - consts = PyList_AsTuple(sc.c_consts); names = PyList_AsTuple(sc.c_names); varnames = PyList_AsTuple(sc.c_varnames); cellvars = dict_keys_inorder(sc.c_cellvars, 0); @@ -4907,7 +4961,8 @@ PyTuple_GET_SIZE(cellvars)); filename = PyString_InternFromString(sc.c_filename); name = PyString_InternFromString(sc.c_name); - code = optimize_code(sc.c_code, consts, names, sc.c_lnotab); + code = optimize_code(sc.c_code, sc.c_consts, names, sc.c_lnotab); + consts = PyList_AsTuple(sc.c_consts); if (!PyErr_Occurred()) co = PyCode_New(sc.c_argcount, sc.c_nlocals, Index: Lib/test/test_peepholer.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/test/test_peepholer.py,v retrieving revision 1.2 diff -u -r1.2 test_peepholer.py --- Lib/test/test_peepholer.py 26 Aug 2004 05:23:19 -0000 1.2 +++ Lib/test/test_peepholer.py 21 Sep 2004 03:59:09 -0000 @@ -64,15 +64,25 @@ def test_pack_unpack(self): for line, elem in ( - ('a, = 1,', 'LOAD_CONST',), - ('a, b = 1, 2', 'ROT_TWO',), - ('a, b, c = 1, 2, 3', 'ROT_THREE',), + ('a, = a,', 'LOAD_CONST',), + ('a, b = a, b', 'ROT_TWO',), + ('a, b, c = a, b, c', 'ROT_THREE',), ): asm = dis_single(line) self.assert_(elem in asm) self.assert_('BUILD_TUPLE' not in asm) self.assert_('UNPACK_TUPLE' not in asm) + def test_folding_of_tuples_of_constants(self): + for line, elem in ( + ('a = 1,2,3', '((1, 2, 3))',), + ('a = ("a","b","c")', "(('a', 'b', 'c'))",), + ('a, b, c = a, b, c', 'ROT_THREE',), + ): + asm = dis_single(line) + self.assert_(elem in asm) + self.assert_('BUILD_TUPLE' not in asm) + def test_elim_extra_return(self): # RETURN LOAD_CONST None RETURN --> RETURN def f(x):