Modification de propriétés sur . ___________________________________________________________________ Modifié : svnmerge-integrated - /python/trunk:1-61437,61439-61441,61443-61453,61455-61474,61476-61477,61479-61485,61487-61488,61490,61493-61528,61530-61563,61565-61569,61571-61576,61578-61584,61586,61588-61589,61591-61778,61780-61809,61811-61866,61868-61870,61872-61874,61876-61877,61883-61903,61905-61935,61938-61939,61941-62004,62007-62014,62016-62018,62021-62022,62024,62027,62029,62031-62066,62068-62074,62076-62094,62096,62098,62100-62102,62104-62122,62124-62125,62127-62137,62139-62142,62144-62194,62196-62222,62224-62308,62311-62397,62399-62430,62432-62510,62512-62516,62519-62535,62537-62598,62600-62665,62667-62683,62685-62719,62721,62723-62792,62794-62796,62798-62860,62862-62864,62866-62878,62880-62883,62885-62887,62889-62899,62901-62911,62913-62916,62918-62919,62921-62922,62924-62942,62944-62959,62961,62963-62967,62969-62970,62972-62973,62975-62976,62978-62982,62984,62987-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63045,63047-63054,63056-63079,63081-63085,63087-63097,63099,63101-63104,63106-63107,63109-63113,63115-63128,63130-63133,63135-63144,63146-63148,63151-63152,63155-63165,63167-63176,63181-63186,63188-63190,63192-63194,63196-63206,63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63248,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63342,63344-63346,63348-63353,63355-63357,63359-63373,63375,63377-63380,63383,63385-63391,63393-63402,63404-63407,63411-63412,63416-63420,63423-63424,63427,63429,63431-63433,63435-63439,63441-63457,63459-63468,63470-63480,63482-63484,63492,63496-63497,63499-63512,63514-63527,63529-63536,63538-63544,63546,63548,63550-63567,63569-63585,63590-63596,63602,63608-63610,63612-63616,63619-63629,63631-63648,63650-63659,63661-63664,63666-63671,63673-63674,63676,63678,63680-63684,63687-63690,63692-63703,63705-63713,63715-63717,63720,63722-63724,63726-63735,63737,63739-63741,63743-63744,63746-63766,63768-63775,63777-63798,63801-63813,63815-63845,63847,63850-63860,63862,63864-63872,63874-63898,63900-63909,63911-63913,63915-63931,63933-63941,63944,63946-63960,63964,63966,63968-63981,63983-64015,64017,64020-64027,64029-64030,64032-64039,64041-64043,64046-64047,64049-64056,64059-64061,64063-64067,64070-64079,64081-64085,64087-64089,64091-64092,64094-64096,64098-64104,64106-64127,64130-64132,64134-64141,64143-64168,64170-64205,64207-64247,64249,64251-64253,64255-64370,64372-64385,64387-64391,64393-64397,64399,64401-64412,64414-64423,64425-64428,64430,64432-64435,64440-64452,64454-64455,64457-64461,64463-64470,64472-64488,64490-64497,64499-64507,64509-64510,64512-64515,64517-64519,64521-64526,64528-64534,64536-64545,64548-64553,64556-64571,64573-64577,64579,64581-64594,64596-64632,64634,64636-64648,64650-64672,64674-64701,64703-64761,64763-64790,64792-64853,64856-64857,64860,64862-64864,64867-64909,64911-64912,64914-64921,64923-64929,64931-64952,64954-64957,64959-64961,64963-64985,64987-65004,65006-65015,65017-65019,65021-65031,65033-65041,65043,65045,65047-65068,65071-65074,65076-65081,65083-65084,65086-65101,65103-65108,65110,65113-65171,65173-65192,65194-65197,65199-65204,65206-65216,65218-65265,65267-65283,65285-65288,65290-65309,65311,65313-65317,65319-65322,65324-65345,65347-65348,65350,65352,65354,65356-65381,65384-65385,65388-65390,65392,65394,65396-65401,65403-65421,65424,65426-65437,65439,65441,65443,65445,65447-65448,65450,65452,65454,65456,65458-65461,65463,65465-65466,65468-65476,65478-65487,65489-65505,65507,65509,65511,65513,65515,65517-65518,65520-65521,65523,65525-65562,65564-65577,65579-65583,65585,65587,65589-65605,65607-65612,65614,65616,65618,65620-65641,65643-65654,65656-65662,65664,65666-65678,65680-65709,65711-65715,65717-65718,65720,65722-65732,65734-65740,65743-65747,65749-65761,65763-65767,65769-65797,65799-65801,65804-65805,65807-65813,65815-65821,65823-65838,65840-65864,65866-65871,65873,65875-65879,65881-65890,65892,65894-65911,65913,65915-65916,65918-65921,65923-65925,65927-65994,65996-66003,66005-66012,66014-66038,66040-66042,66044-66057,66059-66087,66089-66099,66101-66113,66115-66118,66120-66122,66124-66126,66128-66134,66136,66138-66141,66143-66180,66183-66191,66193-66195,66198-66228,66230-66231,66233-66274,66276-66280,66283,66285-66300,66302-66309,66311-66315,66317-66318,66320-66331,66333-66385,66387-66389,66391-66414,66417-66421,66423-66446,66448-66449,66451-66497,66499-66501,66503,66505,66507-66553,66555-66565,66567,66570-66619,66621-66656,66658-66675,66677-66681,66683-66688,66690-66713,66715-66720,66722-66743,66745-66755,66757-66762,66766-66767,66769-66808,66811-66821,66823-66877,66879-67001,67003-67012,67014-67040,67042-67059,67062-67162,67164-67165,67167-67265,67267-67286,67288-67302,67304-67306,67308-67325,67327-67331,67333-67335,67337-67348,67350-67352,67354-67369,67371-67375,67377-67395,67397-67406,67408-67410,67412-67497,67499-67520,67522-67580,67582,67585-67586,67588-67600,67602-67613,67615-67618,67620-67640,67658-67659,67666-67675,67678-67680,67682-67685,67688,67707,67717,67750-67751,67777-67779,67790,67806 + /python/trunk:1-61437,61439-61441,61443-61453,61455-61474,61476-61477,61479-61485,61487-61488,61490,61493-61528,61530-61563,61565-61569,61571-61576,61578-61584,61586,61588-61589,61591-61778,61780-61809,61811-61866,61868-61870,61872-61874,61876-61877,61883-61903,61905-61935,61938-61939,61941-62004,62007-62014,62016-62018,62021-62022,62024,62027,62029,62031-62066,62068-62074,62076-62094,62096,62098,62100-62102,62104-62122,62124-62125,62127-62137,62139-62142,62144-62194,62196-62222,62224-62308,62311-62397,62399-62430,62432-62510,62512-62516,62519-62535,62537-62598,62600-62665,62667-62683,62685-62719,62721,62723-62792,62794-62796,62798-62860,62862-62864,62866-62878,62880-62883,62885-62887,62889-62899,62901-62911,62913-62916,62918-62919,62921-62922,62924-62942,62944-62959,62961,62963-62967,62969-62970,62972-62973,62975-62976,62978-62982,62984,62987-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63045,63047-63054,63056-63079,63081-63085,63087-63097,63099,63101-63104,63106-63107,63109-63113,63115-63128,63130-63133,63135-63144,63146-63148,63151-63152,63155-63165,63167-63176,63181-63186,63188-63190,63192-63194,63196-63206,63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63248,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63342,63344-63346,63348-63353,63355-63357,63359-63373,63375,63377-63380,63383,63385-63391,63393-63402,63404-63407,63411-63412,63416-63420,63423-63424,63427,63429,63431-63433,63435-63439,63441-63457,63459-63468,63470-63480,63482-63484,63492,63496-63497,63499-63512,63514-63527,63529-63536,63538-63544,63546,63548,63550-63567,63569-63585,63590-63596,63602,63608-63610,63612-63616,63619-63629,63631-63648,63650-63659,63661-63664,63666-63671,63673-63674,63676,63678,63680-63684,63687-63690,63692-63703,63705-63713,63715-63717,63720,63722-63724,63726-63735,63737,63739-63741,63743-63744,63746-63766,63768-63775,63777-63798,63801-63813,63815-63845,63847,63850-63860,63862,63864-63872,63874-63898,63900-63909,63911-63913,63915-63931,63933-63941,63944,63946-63960,63964,63966,63968-63981,63983-64015,64017,64020-64027,64029-64030,64032-64039,64041-64043,64046-64047,64049-64056,64059-64061,64063-64067,64070-64079,64081-64085,64087-64089,64091-64092,64094-64096,64098-64104,64106-64127,64130-64132,64134-64141,64143-64168,64170-64205,64207-64247,64249,64251-64253,64255-64370,64372-64385,64387-64391,64393-64397,64399,64401-64412,64414-64423,64425-64428,64430,64432-64435,64440-64452,64454-64455,64457-64461,64463-64470,64472-64488,64490-64497,64499-64507,64509-64510,64512-64515,64517-64519,64521-64526,64528-64534,64536-64545,64548-64553,64556-64571,64573-64577,64579,64581-64594,64596-64632,64634,64636-64648,64650-64672,64674-64701,64703-64761,64763-64790,64792-64853,64856-64857,64860,64862-64864,64867-64909,64911-64912,64914-64921,64923-64929,64931-64952,64954-64957,64959-64961,64963-64985,64987-65004,65006-65015,65017-65019,65021-65031,65033-65041,65043,65045,65047-65068,65071-65074,65076-65081,65083-65084,65086-65101,65103-65108,65110,65113-65171,65173-65192,65194-65197,65199-65204,65206-65216,65218-65265,65267-65283,65285-65288,65290-65309,65311,65313-65317,65319-65322,65324-65345,65347-65348,65350,65352,65354,65356-65381,65384-65385,65388-65390,65392,65394,65396-65401,65403-65421,65424,65426-65437,65439,65441,65443,65445,65447-65448,65450,65452,65454,65456,65458-65461,65463,65465-65466,65468-65476,65478-65487,65489-65505,65507,65509,65511,65513,65515,65517-65518,65520-65521,65523,65525-65562,65564-65577,65579-65583,65585,65587,65589-65605,65607-65612,65614,65616,65618,65620-65641,65643-65654,65656-65662,65664,65666-65678,65680-65709,65711-65715,65717-65718,65720,65722-65732,65734-65740,65743-65747,65749-65761,65763-65767,65769-65797,65799-65801,65804-65805,65807-65813,65815-65821,65823-65838,65840-65864,65866-65871,65873,65875-65879,65881-65890,65892,65894-65911,65913,65915-65916,65918-65921,65923-65925,65927-65994,65996-66003,66005-66012,66014-66038,66040-66042,66044-66057,66059-66087,66089-66099,66101-66113,66115-66118,66120-66122,66124-66126,66128-66134,66136,66138-66141,66143-66180,66183-66191,66193-66195,66198-66228,66230-66231,66233-66274,66276-66280,66283,66285-66300,66302-66309,66311-66315,66317-66318,66320-66331,66333-66385,66387-66389,66391-66414,66417-66421,66423-66446,66448-66449,66451-66497,66499-66501,66503,66505,66507-66553,66555-66565,66567,66570-66619,66621-66656,66658-66675,66677-66681,66683-66688,66690-66713,66715-66720,66722-66743,66745-66755,66757-66762,66766-66767,66769-66808,66811-66821,66823-66877,66879-67001,67003-67012,67014-67040,67042-67059,67062-67162,67164-67165,67167-67265,67267-67286,67288-67302,67304-67306,67308-67325,67327-67331,67333-67335,67337-67348,67350-67352,67354-67369,67371-67375,67377-67395,67397-67406,67408-67410,67412-67497,67499-67520,67522-67580,67582,67585-67586,67588-67600,67602-67613,67615-67618,67620-67640,67658-67659,67666-67675,67678-67680,67682-67685,67688,67707,67717,67750-67751,67777-67779,67790,67806,67818 Index: Python/ceval.c =================================================================== --- Python/ceval.c (révision 67818) +++ Python/ceval.c (copie de travail) @@ -1306,9 +1306,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); @@ -1318,9 +1317,8 @@ case SET_ADD: w = POP(); - v = POP(); + v = stack_pointer[-oparg]; err = PySet_Add(v, w); - Py_DECREF(v); Py_DECREF(w); if (err == 0) { PREDICT(JUMP_ABSOLUTE); @@ -1935,6 +1933,21 @@ if (err == 0) continue; break; + case MAP_ADD: + w = TOP(); /* key */ + u = SECOND(); /* value */ + STACKADJ(-2); + v = stack_pointer[-oparg]; /* dict */ + assert (PyDict_CheckExact(v)); + err = PyDict_SetItem(v, w, u); /* v[w] = u */ + Py_DECREF(u); + Py_DECREF(w); + if (err == 0) { + PREDICT(JUMP_ABSOLUTE); + continue; + } + break; + case LOAD_ATTR: w = GETITEM(names, oparg); v = TOP(); Index: Python/import.c =================================================================== --- Python/import.c (révision 67818) +++ Python/import.c (copie de travail) @@ -87,8 +87,10 @@ 3102 (__file__ points to source file) Python 3.0a4: 3110 (WITH_CLEANUP optimization). Python 3.0a5: 3130 (lexical exception stacking, including POP_EXCEPT) + Python 3.1a0: 3140 (optimize list, set and dict comprehensions: + change LIST_APPEND and SET_ADD, add MAP_ADD) */ -#define MAGIC (3130 | ((long)'\r'<<16) | ((long)'\n'<<24)) +#define MAGIC (3140 | ((long)'\r'<<16) | ((long)'\n'<<24)) /* Magic word as global; note that _PyImport_Init() can change the value of this global to accommodate for alterations of how the Index: Python/compile.c =================================================================== --- Python/compile.c (révision 67818) +++ Python/compile.c (copie de travail) @@ -707,6 +707,8 @@ case SET_ADD: case LIST_APPEND: + return -1; + case MAP_ADD: return -2; case BINARY_POWER: @@ -2823,7 +2825,7 @@ */ static int -compiler_comprehension_generator(struct compiler *c, PyObject *tmpname, +compiler_comprehension_generator(struct compiler *c, asdl_seq *generators, int gen_index, expr_ty elt, expr_ty val, int type) { @@ -2871,7 +2873,7 @@ } if (++gen_index < asdl_seq_LEN(generators)) - if (!compiler_comprehension_generator(c, tmpname, + if (!compiler_comprehension_generator(c, generators, gen_index, elt, val, type)) return 0; @@ -2886,27 +2888,19 @@ ADDOP(c, POP_TOP); break; case COMP_LISTCOMP: - if (!compiler_nameop(c, tmpname, Load)) - return 0; VISIT(c, expr, elt); - ADDOP(c, LIST_APPEND); + ADDOP_I(c, LIST_APPEND, gen_index + 1); break; case COMP_SETCOMP: - if (!compiler_nameop(c, tmpname, Load)) - return 0; VISIT(c, expr, elt); - ADDOP(c, SET_ADD); + ADDOP_I(c, SET_ADD, gen_index + 1); break; case COMP_DICTCOMP: - if (!compiler_nameop(c, tmpname, Load)) - return 0; /* With 'd[k] = v', v is evaluated before k, so we do - the same. STORE_SUBSCR requires (item, map, key), - so we still end up ROTing once. */ + the same. */ VISIT(c, expr, val); - ADDOP(c, ROT_TWO); VISIT(c, expr, elt); - ADDOP(c, STORE_SUBSCR); + ADDOP_I(c, MAP_ADD, gen_index + 1); break; default: return 0; @@ -2932,7 +2926,6 @@ asdl_seq *generators, expr_ty elt, expr_ty val) { PyCodeObject *co = NULL; - identifier tmp = NULL; expr_ty outermost_iter; outermost_iter = ((comprehension_ty) @@ -2943,9 +2936,6 @@ if (type != COMP_GENEXP) { int op; - tmp = compiler_new_tmpname(c); - if (!tmp) - goto error_in_scope; switch (type) { case COMP_LISTCOMP: op = BUILD_LIST; @@ -2963,12 +2953,9 @@ } ADDOP_I(c, op, 0); - ADDOP(c, DUP_TOP); - if (!compiler_nameop(c, tmp, Store)) - goto error_in_scope; } - if (!compiler_comprehension_generator(c, tmp, generators, 0, elt, + if (!compiler_comprehension_generator(c, generators, 0, elt, val, type)) goto error_in_scope; @@ -2984,7 +2971,6 @@ if (!compiler_make_closure(c, co, 0)) goto error; Py_DECREF(co); - Py_XDECREF(tmp); VISIT(c, expr, outermost_iter); ADDOP(c, GET_ITER); @@ -2994,7 +2980,6 @@ compiler_exit_scope(c); error: Py_XDECREF(co); - Py_XDECREF(tmp); return 0; } Index: Include/opcode.h =================================================================== --- Include/opcode.h (révision 67818) +++ Include/opcode.h (copie de travail) @@ -21,8 +21,6 @@ #define UNARY_INVERT 15 -#define SET_ADD 17 -#define LIST_APPEND 18 #define BINARY_POWER 19 #define BINARY_MULTIPLY 20 @@ -133,7 +131,11 @@ /* Support for opargs more than 16 bits long */ #define EXTENDED_ARG 143 +#define LIST_APPEND 145 +#define SET_ADD 146 +#define MAP_ADD 147 + /* EXCEPT_HANDLER is a special, implicit block type which is created when entering an except handler. It is not an opcode but we define it here as we want it to be available to both frameobject.c and ceval.c, while Index: Misc/NEWS =================================================================== --- Misc/NEWS (révision 67818) +++ Misc/NEWS (copie de travail) @@ -12,6 +12,9 @@ Core and Builtins ----------------- +- Issue #2183: Simplify and optimize bytecode for list, dict and set + comprehensions. Original patch for list comprehensions by Neal Norwitz. + - Issue #2173: When getting device encoding, check that return value of nl_langinfo is not the empty string. This was causing silent build failures on OS X. Index: Doc/library/dis.rst =================================================================== --- Doc/library/dis.rst (révision 67818) +++ Doc/library/dis.rst (copie de travail) @@ -357,16 +357,27 @@ address to jump to (which should be a ``FOR_ITER`` instruction). -.. opcode:: SET_ADD () +.. opcode:: SET_ADD (i) - Calls ``set.add(TOS1, TOS)``. Used to implement set comprehensions. + Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions. -.. opcode:: LIST_APPEND () +.. opcode:: LIST_APPEND (i) - Calls ``list.append(TOS1, TOS)``. Used to implement list comprehensions. + Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions. +.. opcode:: MAP_ADD (i) + + Calls ``dict.setitem(TOS1[-i], TOS, TOS1)``. Used to implement dict + comprehensions. + + +For all of the SET_ADD, LIST_APPEND and MAP_ADD instructions, while the +added value or key/value pair is popped off, the container object remains on +the stack so that it is available for further iterations of the loop. + + .. opcode:: LOAD_LOCALS () Pushes a reference to the locals of the current scope on the stack. This is used Index: Lib/opcode.py =================================================================== --- Lib/opcode.py (révision 67818) +++ Lib/opcode.py (copie de travail) @@ -57,8 +57,6 @@ def_op('UNARY_INVERT', 15) -def_op('SET_ADD', 17) -def_op('LIST_APPEND', 18) def_op('BINARY_POWER', 19) def_op('BINARY_MULTIPLY', 20) @@ -169,4 +167,9 @@ def_op('EXTENDED_ARG', 143) EXTENDED_ARG = 143 +def_op('LIST_APPEND', 145) +def_op('SET_ADD', 146) +def_op('MAP_ADD', 147) + + del def_op, name_op, jrel_op, jabs_op Index: Lib/test/test_dis.py =================================================================== --- Lib/test/test_dis.py (révision 67818) +++ Lib/test/test_dis.py (copie de travail) @@ -55,29 +55,25 @@ dis_bug1333982 = """\ %-4d 0 LOAD_CONST 1 (0) - 3 JUMP_IF_TRUE 41 (to 47) + 3 JUMP_IF_TRUE 33 (to 39) 6 POP_TOP 7 LOAD_GLOBAL 0 (AssertionError) 10 BUILD_LIST 0 - 13 DUP_TOP - 14 STORE_FAST 1 (_[1]) - 17 LOAD_FAST 0 (x) - 20 GET_ITER - >> 21 FOR_ITER 13 (to 37) - 24 STORE_FAST 2 (s) - 27 LOAD_FAST 1 (_[1]) - 30 LOAD_FAST 2 (s) - 33 LIST_APPEND - 34 JUMP_ABSOLUTE 21 - >> 37 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 40 LOAD_CONST 2 (1) - 43 BINARY_ADD - 44 RAISE_VARARGS 2 - >> 47 POP_TOP + %-4d >> 32 LOAD_CONST 2 (1) + 35 BINARY_ADD + 36 RAISE_VARARGS 2 + >> 39 POP_TOP - %-4d 48 LOAD_CONST 0 (None) - 51 RETURN_VALUE + %-4d 40 LOAD_CONST 0 (None) + 43 RETURN_VALUE """%(bug1333982.__code__.co_firstlineno + 1, bug1333982.__code__.co_firstlineno + 2, bug1333982.__code__.co_firstlineno + 3)