Index: Python/ceval.c =================================================================== --- Python/ceval.c (revision 42780) +++ Python/ceval.c (working copy) @@ -1245,9 +1245,8 @@ case LIST_APPEND: w = POP(); - v = POP(); + v = *(stack_pointer - oparg); err = PyList_Append(v, w); - Py_DECREF(v); Py_DECREF(w); if (err == 0) { PREDICT(JUMP_ABSOLUTE); Index: Python/compile.c =================================================================== --- Python/compile.c (revision 42780) +++ Python/compile.c (working copy) @@ -1407,6 +1407,9 @@ case FOR_ITER: return 1; + case LIST_APPEND: + return -1; + case STORE_ATTR: return -2; case DELETE_ATTR: @@ -3101,9 +3104,8 @@ } static int -compiler_listcomp_generator(struct compiler *c, PyObject *tmpname, - asdl_seq *generators, int gen_index, - expr_ty elt) +compiler_listcomp_generator(struct compiler *c, asdl_seq *generators, + int gen_index, expr_ty elt) { /* generate code for the iterator, then each of the ifs, and then write to the element */ @@ -3140,17 +3142,15 @@ } if (++gen_index < asdl_seq_LEN(generators)) - if (!compiler_listcomp_generator(c, tmpname, - generators, gen_index, elt)) + if (!compiler_listcomp_generator(c, generators, gen_index, elt)) return 0; /* only append after the last for generator */ if (gen_index >= asdl_seq_LEN(generators)) { - if (!compiler_nameop(c, tmpname, Load)) - return 0; VISIT(c, expr, elt); - ADDOP_I(c, CALL_FUNCTION, 1); - ADDOP(c, POP_TOP); + /* Each generator puts an iterator on the stack; add another one + to get to the list itself */ + ADDOP_I(c, LIST_APPEND, 1 + asdl_seq_LEN(generators)); compiler_use_next_block(c, skip); } @@ -3162,10 +3162,6 @@ } ADDOP_JABS(c, JUMP_ABSOLUTE, start); compiler_use_next_block(c, anchor); - /* delete the append method added to locals */ - if (gen_index == 1) - if (!compiler_nameop(c, tmpname, Del)) - return 0; return 1; } @@ -3173,28 +3169,12 @@ static int compiler_listcomp(struct compiler *c, expr_ty e) { - identifier tmp; - int rc = 0; - static identifier append; asdl_seq *generators = e->v.ListComp.generators; assert(e->kind == ListComp_kind); - if (!append) { - append = PyString_InternFromString("append"); - if (!append) - return 0; - } - tmp = compiler_new_tmpname(c); - if (!tmp) - return 0; + ADDOP_I(c, BUILD_LIST, 0); - ADDOP(c, DUP_TOP); - ADDOP_O(c, LOAD_ATTR, append, names); - if (compiler_nameop(c, tmp, Store)) - rc = compiler_listcomp_generator(c, tmp, generators, 0, - e->v.ListComp.elt); - Py_DECREF(tmp); - return rc; + return compiler_listcomp_generator(c, generators, 0, e->v.ListComp.elt); } static int Index: Include/opcode.h =================================================================== --- Include/opcode.h (revision 42780) +++ Include/opcode.h (working copy) @@ -22,7 +22,6 @@ #define UNARY_INVERT 15 -#define LIST_APPEND 18 #define BINARY_POWER 19 #define BINARY_MULTIPLY 20 @@ -89,6 +88,8 @@ #define UNPACK_SEQUENCE 92 /* Number of sequence items */ #define FOR_ITER 93 +#define LIST_APPEND 94 /* Number of stack items to look back */ + #define STORE_ATTR 95 /* Index in name list */ #define DELETE_ATTR 96 /* "" */ #define STORE_GLOBAL 97 /* "" */ Index: Lib/opcode.py =================================================================== --- Lib/opcode.py (revision 42780) +++ Lib/opcode.py (working copy) @@ -58,7 +58,6 @@ def_op('UNARY_INVERT', 15) -def_op('LIST_APPEND', 18) def_op('BINARY_POWER', 19) def_op('BINARY_MULTIPLY', 20) def_op('BINARY_DIVIDE', 21) @@ -128,6 +127,7 @@ def_op('UNPACK_SEQUENCE', 92) # Number of tuple items jrel_op('FOR_ITER', 93) +def_op('LIST_APPEND', 94) # Number of stack items to look back name_op('STORE_ATTR', 95) # Index in name list name_op('DELETE_ATTR', 96) # "" name_op('STORE_GLOBAL', 97) # "" Index: Lib/test/test_dis.py =================================================================== --- Lib/test/test_dis.py (revision 42780) +++ Lib/test/test_dis.py (working copy) @@ -54,31 +54,25 @@ dis_bug1333982 = """\ %-4d 0 LOAD_CONST 1 (0) - 3 JUMP_IF_TRUE 47 (to 53) + 3 JUMP_IF_TRUE 33 (to 39) 6 POP_TOP 7 LOAD_GLOBAL 0 (AssertionError) 10 BUILD_LIST 0 - 13 DUP_TOP - 14 LOAD_ATTR 1 (append) - 17 STORE_FAST 1 (_[1]) - 20 LOAD_FAST 0 (x) - 23 GET_ITER - >> 24 FOR_ITER 16 (to 43) - 27 STORE_FAST 2 (s) - 30 LOAD_FAST 1 (_[1]) - 33 LOAD_FAST 2 (s) - 36 CALL_FUNCTION 1 - 39 POP_TOP - 40 JUMP_ABSOLUTE 24 - >> 43 DELETE_FAST 1 (_[1]) + 13 LOAD_FAST 0 (x) + 16 GET_ITER + >> 17 FOR_ITER 12 (to 32) + 20 STORE_FAST 1 (s) + 23 LOAD_FAST 1 (s) + 26 LIST_APPEND 2 + 29 JUMP_ABSOLUTE 17 - %-4d 46 LOAD_CONST 2 (1) - 49 BINARY_ADD - 50 RAISE_VARARGS 2 - >> 53 POP_TOP + %-4d >> 32 LOAD_CONST 2 (1) + 35 BINARY_ADD + 36 RAISE_VARARGS 2 + >> 39 POP_TOP - %-4d 54 LOAD_CONST 0 (None) - 57 RETURN_VALUE + %-4d 40 LOAD_CONST 0 (None) + 43 RETURN_VALUE """%(bug1333982.func_code.co_firstlineno + 1, bug1333982.func_code.co_firstlineno + 2, bug1333982.func_code.co_firstlineno + 3)