Showing content from http://mail.python.org/pipermail/python-dev/attachments/20150125/7e17c44c/attachment.html below:
<div dir="ltr">Perfect, thanks!</div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jan 25, 2015 at 7:08 AM, Petr Viktorin <span dir="ltr"><<a href="mailto:encukou@gmail.com" target="_blank">encukou@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Sun, Jan 25, 2015 at 12:55 PM, Neil Girdhar <<a href="mailto:mistersheik@gmail.com">mistersheik@gmail.com</a>> wrote:<br>
> How do I disassemble a generated comprehension?<br>
><br>
> For example, I am trying to debug the following:<br>
><br>
>>>> dis.dis('{**{} for x in [{1:2}]}')<br>
>Â Â 1Â Â Â Â Â Â 0 LOAD_CONSTÂ Â Â Â Â Â Â Â 0 (<code object <dictcomp> at<br>
> 0x10160b7c0, file "<dis>", line 1>)<br>
>Â Â Â Â Â Â Â Â 3 LOAD_CONSTÂ Â Â Â Â Â Â Â 1 ('<dictcomp>')<br>
>Â Â Â Â Â Â Â Â 6 MAKE_FUNCTIONÂ Â Â Â Â Â 0<br>
>Â Â Â Â Â Â Â Â 9 LOAD_CONSTÂ Â Â Â Â Â Â Â 2 (2)<br>
>Â Â Â Â Â Â Â 12 LOAD_CONSTÂ Â Â Â Â Â Â Â 3 (1)<br>
>Â Â Â Â Â Â Â 15 BUILD_MAPÂ Â Â Â Â Â Â Â 1<br>
>Â Â Â Â Â Â Â 18 BUILD_LISTÂ Â Â Â Â Â Â Â 1<br>
>Â Â Â Â Â Â Â 21 GET_ITER<br>
>Â Â Â Â Â Â Â 22 CALL_FUNCTIONÂ Â Â Â Â Â 1 (1 positional, 0 keyword pair)<br>
>Â Â Â Â Â Â Â 25 RETURN_VALUE<br>
><br>
> (This requires the new patch in issue 2292.)<br>
><br>
> The code here looks fine to me, so I need to look into the code object<br>
> <dictcomp>. How do I do that?<br>
<br>
</div></div>Put it in a function, then get it from the function's code's constants.<br>
I don't have the patch applied but it should work like this even for<br>
the new syntax:<br>
<br>
>>> import dis<br>
>>> def f(): return {{} for x in [{1:2}]}<br>
...<br>
>>> dis.dis(f)<br>
 1      0 LOAD_CONST        1 (<code object <setcomp> at<br>
0x7ff2c0647420, file "<stdin>", line 1>)<br>
       3 LOAD_CONST        2 ('f.<locals>.<setcomp>')<br>
       6 MAKE_FUNCTION      0<br>
       9 BUILD_MAP        1<br>
       12 LOAD_CONST        3 (2)<br>
       15 LOAD_CONST        4 (1)<br>
       18 STORE_MAP<br>
       19 BUILD_LIST        1<br>
       22 GET_ITER<br>
       23 CALL_FUNCTION      1 (1 positional, 0 keyword pair)<br>
       26 RETURN_VALUE<br>
>>> f.__code__.co_consts[1]Â # from "LOAD_CONST 1"<br>
<code object <setcomp> at 0x7ff2c0647420, file "<stdin>", line 1><br>
>>> dis.dis(f.__code__.co_consts[1])<br>
 1      0 BUILD_SET        0<br>
       3 LOAD_FAST        0 (.0)<br>
    >>  6 FOR_ITER        12 (to 21)<br>
       9 STORE_FAST        1 (x)<br>
       12 BUILD_MAP        0<br>
       15 SET_ADD         2<br>
       18 JUMP_ABSOLUTE      6<br>
    >>  21 RETURN_VALUE<br>
</blockquote></div><br></div>
RetroSearch is an open source project built by @garambo
| Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4