9

特别是,它输出:

>>> dis.dis(None)
 22           0 LOAD_FAST                0 (x)
              3 LOAD_CONST               1 (None)
              6 COMPARE_OP               8 (is)
              9 POP_JUMP_IF_FALSE       23

 23          12 LOAD_GLOBAL              1 (distb)
             15 CALL_FUNCTION            0
             18 POP_TOP             

 24          19 LOAD_CONST               1 (None)
             22 RETURN_VALUE        

 25     >>   23 LOAD_GLOBAL              2 (isinstance)
             26 LOAD_FAST                0 (x)
             29 LOAD_GLOBAL              3 (types)
             32 LOAD_ATTR                4 (InstanceType)
             35 CALL_FUNCTION            2
             38 POP_JUMP_IF_FALSE       53

 26          41 LOAD_FAST                0 (x)
             44 LOAD_ATTR                5 (__class__)
             47 STORE_FAST               0 (x)
             50 JUMP_FORWARD             0 (to 53)

 27     >>   53 LOAD_GLOBAL              6 (hasattr)
             56 LOAD_FAST                0 (x)
             59 LOAD_CONST               2 ('im_func')
             62 CALL_FUNCTION            2
             65 POP_JUMP_IF_FALSE       80

 28          68 LOAD_FAST                0 (x)
             71 LOAD_ATTR                7 (im_func)
             74 STORE_FAST               0 (x)
             77 JUMP_FORWARD             0 (to 80)

 29     >>   80 LOAD_GLOBAL              6 (hasattr)
             83 LOAD_FAST                0 (x)
             86 LOAD_CONST               3 ('func_code')
             89 CALL_FUNCTION            2
             92 POP_JUMP_IF_FALSE      107

 30          95 LOAD_FAST                0 (x)
             98 LOAD_ATTR                8 (func_code)
            101 STORE_FAST               0 (x)
            104 JUMP_FORWARD             0 (to 107)

 31     >>  107 LOAD_GLOBAL              6 (hasattr)
            110 LOAD_FAST                0 (x)
            113 LOAD_CONST               4 ('__dict__')
            116 CALL_FUNCTION            2
            119 POP_JUMP_IF_FALSE      246

 32         122 LOAD_FAST                0 (x)
            125 LOAD_ATTR                9 (__dict__)
            128 LOAD_ATTR               10 (items)
            131 CALL_FUNCTION            0
            134 STORE_FAST               1 (items)

 33         137 LOAD_FAST                1 (items)
            140 LOAD_ATTR               11 (sort)
            143 CALL_FUNCTION            0
            146 POP_TOP             

 34         147 SETUP_LOOP             174 (to 324)
            150 LOAD_FAST                1 (items)
            153 GET_ITER            
        >>  154 FOR_ITER                85 (to 242)
            157 UNPACK_SEQUENCE          2
            160 STORE_FAST               2 (name)
            163 STORE_FAST               3 (x1)

 35         166 LOAD_GLOBAL              2 (isinstance)
            169 LOAD_FAST                3 (x1)
            172 LOAD_GLOBAL             12 (_have_code)
            175 CALL_FUNCTION            2
            178 POP_JUMP_IF_FALSE      154

 36         181 LOAD_CONST               5 ('Disassembly of %s:')
            184 LOAD_FAST                2 (name)
            187 BINARY_MODULO       
            188 PRINT_ITEM          
            189 PRINT_NEWLINE       

 37         190 SETUP_EXCEPT            14 (to 207)

 38         193 LOAD_GLOBAL             13 (dis)
            196 LOAD_FAST                3 (x1)
            199 CALL_FUNCTION            1
            202 POP_TOP             
            203 POP_BLOCK           
            204 JUMP_FORWARD            28 (to 235)

 39     >>  207 DUP_TOP             
            208 LOAD_GLOBAL             14 (TypeError)
            211 COMPARE_OP              10 (exception match)
            214 POP_JUMP_IF_FALSE      234
            217 POP_TOP             
            218 STORE_FAST               4 (msg)
            221 POP_TOP             

 40         222 LOAD_CONST               6 ('Sorry:')
            225 PRINT_ITEM          
            226 LOAD_FAST                4 (msg)
            229 PRINT_ITEM          
            230 PRINT_NEWLINE       
            231 JUMP_FORWARD             1 (to 235)
        >>  234 END_FINALLY         

 41     >>  235 PRINT_NEWLINE       
            236 JUMP_ABSOLUTE          154
            239 JUMP_ABSOLUTE          154
        >>  242 POP_BLOCK           
            243 JUMP_FORWARD            78 (to 324)

 42     >>  246 LOAD_GLOBAL              6 (hasattr)
            249 LOAD_FAST                0 (x)
            252 LOAD_CONST               7 ('co_code')
            255 CALL_FUNCTION            2
            258 POP_JUMP_IF_FALSE      274

 43         261 LOAD_GLOBAL             15 (disassemble)
            264 LOAD_FAST                0 (x)
            267 CALL_FUNCTION            1
            270 POP_TOP             
            271 JUMP_FORWARD            50 (to 324)

 44     >>  274 LOAD_GLOBAL              2 (isinstance)
            277 LOAD_FAST                0 (x)
            280 LOAD_GLOBAL             16 (str)
            283 CALL_FUNCTION            2
            286 POP_JUMP_IF_FALSE      302

 45         289 LOAD_GLOBAL             17 (disassemble_string)
            292 LOAD_FAST                0 (x)
            295 CALL_FUNCTION            1
            298 POP_TOP             
            299 JUMP_FORWARD            22 (to 324)

 47     >>  302 LOAD_GLOBAL             14 (TypeError)

 48         305 LOAD_CONST               8 ("don't know how to disassemble %s objects")

 49         308 LOAD_GLOBAL             18 (type)
            311 LOAD_FAST                0 (x)
            314 CALL_FUNCTION            1
            317 LOAD_ATTR               19 (__name__)
            320 BINARY_MODULO       
    -->     321 RAISE_VARARGS            2
        >>  324 LOAD_CONST               1 (None)
            327 RETURN_VALUE 

为什么?

4

1 回答 1

22

文档中:

dis.dis([字节源])

反汇编字节源对象。bytesource 可以表示模块、类、方法、函数或代码对象。对于一个模块,它会反汇编所有功能。对于一个类,它反汇编所有方法。对于单个代码序列,它每条字节码指令打印一行。如果没有提供对象,它会反汇编最后的 traceback

强调我的。


如果你在一个新的解释器中尝试它,没有最后的回溯,所以你会得到一个错误:

>>> import dis
>>> dis.dis(None)

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    dis.dis(None)
  File "C:\Python27\lib\dis.py", line 23, in dis
    distb()
  File "C:\Python27\lib\dis.py", line 57, in distb
    raise RuntimeError, "no last traceback to disassemble"
RuntimeError: no last traceback to disassemble

但是现在如果你尝试相同的命令,它会反汇编RuntimeError你刚刚得到的:

>>> dis.dis(None)
 53           0 LOAD_FAST                0 (tb)
              3 LOAD_CONST               2 (None)
              6 COMPARE_OP               8 (is)
              9 POP_JUMP_IF_FALSE       82

 54          12 SETUP_EXCEPT            13 (to 28)

 55          15 LOAD_GLOBAL              1 (sys)
             18 LOAD_ATTR                2 (last_traceback)
             21 STORE_FAST               0 (tb)
             24 POP_BLOCK           
             25 JUMP_FORWARD            26 (to 54)

 56     >>   28 DUP_TOP             
             29 LOAD_GLOBAL              3 (AttributeError)
             32 COMPARE_OP              10 (exception match)
             35 POP_JUMP_IF_FALSE       53
             38 POP_TOP             
             39 POP_TOP             
             40 POP_TOP             

 57          41 LOAD_GLOBAL              4 (RuntimeError)
             44 LOAD_CONST               1 ('no last traceback to disassemble')
    -->      47 RAISE_VARARGS            2
             50 JUMP_FORWARD             1 (to 54)
于 2012-07-25T00:43:54.363 回答