Re: [Python-Dev] Disassembly of generated comprehensions

2015年1月25日 07:33:54 -0800

On Sun, Jan 25, 2015 at 12:55 PM, Neil Girdhar <[email protected]> wrote:
> How do I disassemble a generated comprehension?
>
> For example, I am trying to debug the following:
>
>>>> dis.dis('{**{} for x in [{1:2}]}')
> 1 0 LOAD_CONST 0 (<code object <dictcomp> at
> 0x10160b7c0, file "<dis>", line 1>)
> 3 LOAD_CONST 1 ('<dictcomp>')
> 6 MAKE_FUNCTION 0
> 9 LOAD_CONST 2 (2)
> 12 LOAD_CONST 3 (1)
> 15 BUILD_MAP 1
> 18 BUILD_LIST 1
> 21 GET_ITER
> 22 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
> 25 RETURN_VALUE
>
> (This requires the new patch in issue 2292.)
>
> The code here looks fine to me, so I need to look into the code object
> <dictcomp>. How do I do that?
Put it in a function, then get it from the function's code's constants.
I don't have the patch applied but it should work like this even for
the new syntax:
>>> import dis
>>> def f(): return {{} for x in [{1:2}]}
...
>>> dis.dis(f)
 1 0 LOAD_CONST 1 (<code object <setcomp> at
0x7ff2c0647420, file "<stdin>", line 1>)
 3 LOAD_CONST 2 ('f.<locals>.<setcomp>')
 6 MAKE_FUNCTION 0
 9 BUILD_MAP 1
 12 LOAD_CONST 3 (2)
 15 LOAD_CONST 4 (1)
 18 STORE_MAP
 19 BUILD_LIST 1
 22 GET_ITER
 23 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
 26 RETURN_VALUE
>>> f.__code__.co_consts[1] # from "LOAD_CONST 1"
<code object <setcomp> at 0x7ff2c0647420, file "<stdin>", line 1>
>>> dis.dis(f.__code__.co_consts[1])
 1 0 BUILD_SET 0
 3 LOAD_FAST 0 (.0)
 >> 6 FOR_ITER 12 (to 21)
 9 STORE_FAST 1 (x)
 12 BUILD_MAP 0
 15 SET_ADD 2
 18 JUMP_ABSOLUTE 6
 >> 21 RETURN_VALUE
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to