ANN compiler2 : Produce bytecode from Python 2.5 Abstract Syntax Trees

sébastien martini sebastien.martini at gmail.com
Sat Oct 28 17:07:36 EDT 2006


Hi,

> I was primarily talking about language support. For quite some time,
> the compiler package wasn't able to compile the Python standard library,
> until Guido van Rossum (and others) brought it back to work at the last
> PyCon. It would simply reject certain more recent language constructs.
> In the process of fixing it, it was also found to deviate from the
> normal language definition, i.e. it would generate bad code.
>
> Many of these are fixed, but it wouldn't surprise me if there are
> still bugs remaining.

I don't know if it can hide some bugs or if the module has just never
been updated to support this bytecode but LIST_APPEND is never emitted.
In the module compiler, list comprehensions are implemented without
emitting this bytecode, howewer the current implementation seems to be
correct from syntax and execution point of view.

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

Cordially,

sébastien martini

-- 
http://seb.dbzteam.com




More information about the Python-list mailing list