python 2中奇怪的dis.dis行为

时间:2019-06-07 08:05:48

标签: python-2.7

我在传递字符串时从Python 2.7.14在dis.dis()中发现了一个奇怪的错误:

>>> dis.dis("import dis")
          0 BUILD_MAP       28781
          3 JUMP_IF_FALSE_OR_POP 29810
          6 SLICE+2
          7 LOAD_CONST      29545 (29545)
>>> dis.dis("import os")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\dis.py", line 45, in dis
    disassemble_string(x)
  File "C:\Python27\lib\dis.py", line 112, in disassemble_string
    labels = findlabels(code)
  File "C:\Python27\lib\dis.py", line 166, in findlabels
    oparg = ord(code[i]) + ord(code[i+1])*256
IndexError: string index out of range

在测试其他几个模块之后,我发现只有当模块名称的长度可以被3整除时,它才有效。在Python 3.7中,一切正常(通过python.org的在线shell测试)

0 个答案:

没有答案