给定一个蟒蛇.pyc文件的文件,是有一个工具,让我查看字节码?(Given a python .py

2019-06-25 16:10发布

一个Python模块被自动编译成的CPython解释器.pyc文件的文件。 的pyc文件文件,它包含字节码,为二进制格式(封代码?)。 是否有一个GUI(或命令行)工具,它让我看到了字节码?

Answer 1:

There's a visual python disassembler called PyChrisanthemum.

To do it the command-line way you can use module dis (python 2.7.3, python 3.2.3), as OP already found out.



Answer 2:

每个* .pyc文件文件是包含接下来的事情就一个二进制文件:

  • 一个四字节的幻数 - 它只是个字节,每条改变编组代码的变化;
  • 一个四字节的修改时间戳 - 是生成pyc文件,以便它可以如果源改变被重新编译的源文件的修改的Unix时间戳;
  • 因为版本Python3.3 +下四个字节是编码源文件作为长的尺寸字段;
  • 编组代码对象。

为什么不直接使用CPython`s内置功能完成这个任务?


一个文件view_pyc_file.py

import platform
import time
import sys
import binascii
import marshal
import dis
import struct


def view_pyc_file(path):
    """Read and display a content of the Python`s bytecode in a pyc-file."""

    file = open(path, 'rb')

    magic = file.read(4)
    timestamp = file.read(4)
    size = None

    if sys.version_info.major == 3 and sys.version_info.minor >= 3:
        size = file.read(4)
        size = struct.unpack('I', size)[0]

    code = marshal.load(file)

    magic = binascii.hexlify(magic).decode('utf-8')
    timestamp = time.asctime(time.localtime(struct.unpack('I', b'D\xa5\xc2X')[0]))

    dis.disassemble(code)

    print('-' * 80)
    print(
        'Python version: {}\nMagic code: {}\nTimestamp: {}\nSize: {}'
        .format(platform.python_version(), magic, timestamp, size)
    )

    file.close()


if __name__ == '__main__':
    view_pyc_file(sys.argv[1])

测试了下一个版本CPython`s:

  • 2.7.9
  • 3.4.2
  • 3.5.2

示范

文件的内容main.py

$ cat main.py
print("Never give up")

创建并通过python2.7阅读PYC文件

setivolkylany$~/Downloads/temp/temp$ python2.7 -m py_compile main.py 
setivolkylany$~/Downloads/temp/temp$ python2.7 view_pyc_file.py ./main.pyc
  1           0 LOAD_CONST               0 ('Never give up')
              3 PRINT_ITEM          
              4 PRINT_NEWLINE       
              5 LOAD_CONST               1 (None)
              8 RETURN_VALUE        
--------------------------------------------------------------------------------
Python version: 2.7.9
Magic code: 03f30d0a
Timestamp: Fri Mar 10 15:08:20 2017
Size: None

创建并通过python3.4阅读PYC文件

setivolkylany$~/Downloads/temp/temp$ python3.4 -m py_compile main.py 
setivolkylany$~/Downloads/temp/temp$ python3.4 view_pyc_file.py __pycache__/main.cpython-34.pyc 
  1           0 LOAD_NAME                0 (print)
              3 LOAD_CONST               0 ('Never give up')
              6 CALL_FUNCTION            1 (1 positional, 0 keyword pair)
              9 POP_TOP
             10 LOAD_CONST               1 (None)
             13 RETURN_VALUE
--------------------------------------------------------------------------------
Python version: 3.4.2
Magic code: ee0c0d0a
Timestamp: Fri Mar 10 15:08:20 2017
Size: 23

创建并通过python3.5阅读PYC文件

setivolkylany$~/Downloads/temp/temp$ python3.5 -m py_compile main.py 
setivolkylany$~/Downloads/temp/temp$ python3.5 view_pyc_file.py __pycache__/main.cpython-35.pyc 
  1           0 LOAD_NAME                0 (print)
              3 LOAD_CONST               0 ('Never give up')
              6 CALL_FUNCTION            1 (1 positional, 0 keyword pair)
              9 POP_TOP
             10 LOAD_CONST               1 (None)
             13 RETURN_VALUE
--------------------------------------------------------------------------------
Python version: 3.5.2
Magic code: 160d0d0a
Timestamp: Fri Mar 10 15:08:20 2017
Size: 23

基于:

  • 我怎么能理解一个.pyc文件文件内容
  • https://gist.github.com/anonymous/35c08092a6eb70cdd723
  • https://nedbatchelder.com/blog/200804/the_structure_of_pyc_files.html


文章来源: Given a python .pyc file, is there a tool that let me view the bytecode?