This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: compiler module dont emit LIST_APPEND w/ list comprehension
Type: Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: georg.brandl, seb_martini
Priority: normal Keywords:

Created on 2006-10-28 22:58 by seb_martini, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg30406 - (view) Author: sebastien Martini (seb_martini) Date: 2006-10-28 22:58
In the module compiler, list comprehensions are
implemented without emitting this bytecode.

For example:
>>> src = "[a for a in range(3)]"
>>> co = compiler.compile(src, 'lc1', 'exec')
>>> co

<code object <module> at 0x404927b8, file "lc1", line 1>
>>> dis.dis(co)

  1           0 BUILD_LIST               0
              3 DUP_TOP
              4 LOAD_ATTR                0 (append)
              7 STORE_NAME               1 ($append0)
             10 LOAD_NAME                2 (range)
             13 LOAD_CONST               1 (3)
             16 CALL_FUNCTION            1
             19 GET_ITER
        >>   20 FOR_ITER                16 (to 39)
             23 STORE_NAME               3 (a)
             26 LOAD_NAME                1 ($append0)
             29 LOAD_NAME                3 (a)
             32 CALL_FUNCTION            1
             35 POP_TOP
             36 JUMP_ABSOLUTE           20
        >>   39 DELETE_NAME              1 ($append0)
             42 POP_TOP
             43 LOAD_CONST               0 (None)
             46 RETURN_VALUE
>>> co2 = compile(src, 'lc2', 'exec')
>>> co2

<code object <module> at 0x40492770, file "lc2", line 1>
>>> dis.dis(co2)

  1           0 BUILD_LIST               0
              3 DUP_TOP
              4 STORE_NAME               0 (_[1])
              7 LOAD_NAME                1 (range)
             10 LOAD_CONST               0 (3)
             13 CALL_FUNCTION            1
             16 GET_ITER
        >>   17 FOR_ITER                13 (to 33)
             20 STORE_NAME               2 (a)
             23 LOAD_NAME                0 (_[1])
             26 LOAD_NAME                2 (a)
             29 LIST_APPEND
             30 JUMP_ABSOLUTE           17
        >>   33 DELETE_NAME              0 (_[1])
             36 POP_TOP
             37 LOAD_CONST               1 (None)
             40 RETURN_VALUE 

-- 
sébastien martini
msg30407 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-10-29 08:54
Logged In: YES 
user_id=849994

I "fixed" this in rev. 52520, now the compiler module
generates the same bytecode for listcomps as the builtin
compiler.

Not backporting this, as it isn't really a bug.
History
Date User Action Args
2022-04-11 14:56:21adminsetgithub: 44174
2006-10-28 22:58:33seb_martinicreate