首頁  >  文章  >  後端開發  >  python虛擬機器pyc檔案結構是什麼

python虛擬機器pyc檔案結構是什麼

王林
王林轉載
2023-05-27 21:01:581424瀏覽

    PYC 文件

    pyc 檔案是Python 在解釋執行原始碼時產生的一種字節碼文件,它包含了源代碼的編譯結果和相關的元資料訊息,以便於Python 可以更快地載入和執行程式碼。

    與編譯型語言不同,Python 是一種解釋型語言,不會將原始碼直接編譯成機器碼並執行。在執行程式碼之前,Python 解釋器會先將原始程式碼編譯成字節碼,接著將字節碼進行解釋執行。 .pyc 檔案就是這個過程中產生的字節碼檔。

    當 Python 解釋器首次執行一個 .py 檔案時,它會在同一目錄下產生一個對應的 .pyc 文件,以便於下次載入該檔案時可以更快地執行。當來源檔案被修改並重新載入時,解釋器會重新產生 .pyc 檔案以更新快取的字節碼。

    產生 PYC 檔案

    正常的 python 檔案需要透過編譯器變成字節碼,然後將字節碼交給 python 虛擬機,然後 python 虛擬機會執行字節碼。整體流程如下所示:

    python虛擬機器pyc檔案結構是什麼

    我們可以直接使用 compile all 模組來產生對應檔案的 pyc 檔案。

    ➜  pvm ls
    demo.py  hello.py
    ➜  pvm python -m compileall .
    Listing '.'...
    Listing './.idea'...
    Listing './.idea/inspectionProfiles'...
    Compiling './demo.py'...
    Compiling './hello.py'...
    ➜  pvm ls
    __pycache__ demo.py     hello.py
    ➜  pvm ls __pycache__ 
    demo.cpython-310.pyc  hello.cpython-310.pyc

    python -m compileall . 指令將遞歸掃描目前目錄下面的 py 文件,並且產生對應文件的 pyc 檔案。

    PYC 檔案佈局

    python虛擬機器pyc檔案結構是什麼

    第一部分魔數由兩個部分組成:

    python虛擬機器pyc檔案結構是什麼

    第一部分魔術是由一個2 位元組的整數和另外兩個字元回車換行組成的, "\r\n" 也佔用兩個位元組,一共是四個位元組。這個兩個位元組的整數在不同的python 版本還不一樣,比如說在python3.5 當中這個值為3351 等值,在python3.9 當中這個值為3420,3421,3422,3423,3424等值(在python 3.9 的小版本)。

    第二部分 Bit Field 這個欄位的主要功能是為了將來能夠實現復現編譯結果,但是在 python3.9a2 時,這個欄位的值還全部是 0 。請參考PEP552-確定性pyc的詳細內容。這個欄位在 python2 和 python3 早期版本並沒有(python3.5 還沒有),在 python3 的後期版本這個欄位才出現的。

    第三部分 就是整個 py 原始檔的大小了。

    第四部分 也是整個 pyc 檔案當中最重要的一個部分,最後一個部分就是一個 CodeObject 物件序列化之後的數據,我們稍後再來仔細分析這個物件相關的數據。

    我們現在來具體分析一個pyc 文件,對應的python 程式碼為:

    def f():
        x = 1
        return 2

    pyc 檔案的十六進位形式如下所示:

    ➜  __pycache__ hexdump -C hello.cpython-310.pyc
    00000000  6f 0d 0d 0a 00 00 00 00  b9 48 21 64 20 00 00 00  |o........H!d ...|
    00000010  e3 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    00000020  00 02 00 00 00 40 00 00  00 73 0c 00 00 00 64 00  |.....@...s....d.|
    00000030  64 01 84 00 5a 00 64 02  53 00 29 03 63 00 00 00  |d...Z.d.S.).c...|
    00000040  00 00 00 00 00 00 00 00  00 01 00 00 00 01 00 00  |................|
    00000050  00 43 00 00 00 73 08 00  00 00 64 01 7d 00 64 02  |.C...s....d.}.d.|
    00000060  53 00 29 03 4e e9 01 00  00 00 e9 02 00 00 00 a9  |S.).N...........|
    00000070  00 29 01 da 01 78 72 03  00 00 00 72 03 00 00 00  |.)...xr....r....|
    00000080  fa 0a 2e 2f 68 65 6c 6c  6f 2e 70 79 da 01 66 01  |.../hello.py..f.|
    00000090  00 00 00 73 04 00 00 00  04 01 04 01 72 06 00 00  |...s........r...|
    000000a0  00 4e 29 01 72 06 00 00  00 72 03 00 00 00 72 03  |.N).r....r....r.|
    000000b0  00 00 00 72 03 00 00 00  72 05 00 00 00 da 08 3c  |...r....r......<|
    000000c0  6d 6f 64 75 6c 65 3e 01  00 00 00 73 02 00 00 00  |module>....s....|
    000000d0  0c 00                                             |..|
    000000d2

    因為資料使用小端表示方式,因此對於上面的資料:

    • 第一部分魔數為:0xa0d0d6f 。

    • 第二部分 Bit Field 為:0x0 。

    • 第三部分最後一次修改日期為:0x642148b9 。

    • 第四部分檔案大小為:0x20 字節,也就是說 hello.py 這個檔案的大小是 32 位元組。

    下面是一個小的程式碼片段用來讀取pyc 檔案的頭部元資訊:

    import struct
    import time
    import binascii
    fname = "./__pycache__/hello.cpython-310.pyc"
    f = open(fname, "rb")
    magic = struct.unpack(&#39;<l&#39;, f.read(4))[0]
    bit_filed = f.read(4)
    print(f"bit field = {binascii.hexlify(bit_filed)}")
    moddate = f.read(4)
    filesz = f.read(4)
    modtime = time.asctime(time.localtime(struct.unpack(&#39;<l&#39;, moddate)[0]))
    filesz = struct.unpack(&#39;<L&#39;, filesz)
    print("magic %s" % (hex(magic)))
    print("moddate (%s)" % (modtime))
    print("File Size %d" % filesz)
    f.close()

    上面的程式碼輸出結果如下所示:

    bit field = b'00000000'
    magic 0xa0d0d6f
    moddate (Mon Mar 27 15:41:45 2023)
    File Size 32

    有關pyc檔案的詳細操作可以查看python 標準函式庫importlib/_bootstrap_external.py 檔案原始碼。

    CODEOBJECT

    在CPython 中,CodeObject 是一個對象,它包含了Python 程式碼的字節碼、常數、變數、位置參數、關鍵字參數等信息,以及一些用於運行程式碼的元數據,如檔案名稱、程式碼行號等。

    在 CPython 中,當我們執行一個 Python 模組或函數時,解譯器會先將其程式碼編譯為 CodeObject,然後再執行。在編譯過程中,解釋器會將 Python 程式碼轉換為字節碼,並將其保存在 CodeObject 物件中。此後,每當我們呼叫該模組或函數時,解釋器都會使用 CodeObject 中的字節碼來執行程式碼。

    CodeObject 物件是不可變的,一旦建立就不能被修改。這是因為 Python 程式碼的字節碼是不可變的,而 CodeObject 物件包含了這些字節碼,所以也是不可變的。

    在本篇文章當中主要介紹 code object 當中主要的內容,以及簡單介紹他們的作用,在後續的文章當中會仔細分析 code object 對應的源代碼以及對應的字段的詳細作用。

    现在举一个例子来分析一下 pycdemo.py 的 pyc 文件,pycdemo.py 的源程序如下所示:

    if __name__ == &#39;__main__&#39;:
        a = 100
        print(a)

    下面的代码是一个用于加载 pycdemo01.cpython-39.pyc 文件(也就是 hello.py 对应的 pyc 文件)的代码,使用 marshal 读取 pyc 文件里面的 code object 。

    import marshal
    import dis
    import struct
    import time
    import types
    import binascii
    def print_metadata(fp):
        magic = struct.unpack(&#39;<l&#39;, fp.read(4))[0]
        print(f"magic number = {hex(magic)}")
        bit_field = struct.unpack(&#39;<l&#39;, fp.read(4))[0]
        print(f"bit filed = {bit_field}")
        t = struct.unpack(&#39;<l&#39;, fp.read(4))[0]
        print(f"time = {time.asctime(time.localtime(t))}")
        file_size = struct.unpack(&#39;<l&#39;, fp.read(4))[0]
        print(f"file size = {file_size}")
    def show_code(code, indent=&#39;&#39;):
        print ("%scode" % indent)
        indent += &#39;   &#39;
        print ("%sargcount %d" % (indent, code.co_argcount))
        print ("%snlocals %d" % (indent, code.co_nlocals))
        print ("%sstacksize %d" % (indent, code.co_stacksize))
        print ("%sflags %04x" % (indent, code.co_flags))
        show_hex("code", code.co_code, indent=indent)
        dis.disassemble(code)
        print ("%sconsts" % indent)
        for const in code.co_consts:
            if type(const) == types.CodeType:
                show_code(const, indent+&#39;   &#39;)
            else:
                print("   %s%r" % (indent, const))
        print("%snames %r" % (indent, code.co_names))
        print("%svarnames %r" % (indent, code.co_varnames))
        print("%sfreevars %r" % (indent, code.co_freevars))
        print("%scellvars %r" % (indent, code.co_cellvars))
        print("%sfilename %r" % (indent, code.co_filename))
        print("%sname %r" % (indent, code.co_name))
        print("%sfirstlineno %d" % (indent, code.co_firstlineno))
        show_hex("lnotab", code.co_lnotab, indent=indent)
    def show_hex(label, h, indent):
        h = binascii.hexlify(h)
        if len(h) < 60:
            print("%s%s %s" % (indent, label, h))
        else:
            print("%s%s" % (indent, label))
            for i in range(0, len(h), 60):
                print("%s   %s" % (indent, h[i:i+60]))
    if __name__ == &#39;__main__&#39;:
        filename = "./__pycache__/pycdemo01.cpython-39.pyc"
        with open(filename, "rb") as fp:
            print_metadata(fp)
            code_object = marshal.load(fp)
            show_code(code_object)

    执行上面的程序输出结果如下所示:

    magic number = 0xa0d0d61
    bit filed = 0
    time = Tue Mar 28 02:40:20 2023
    file size = 54
    code
       argcount 0
       nlocals 0
       stacksize 2
       flags 0040
       code b&#39;650064006b02721464015a01650265018301010064025300&#39;
      3           0 LOAD_NAME                0 (__name__)
                  2 LOAD_CONST               0 (&#39;__main__&#39;)
                  4 COMPARE_OP               2 (==)
                  6 POP_JUMP_IF_FALSE       20
      4           8 LOAD_CONST               1 (100)
                 10 STORE_NAME               1 (a)
      5          12 LOAD_NAME                2 (print)
                 14 LOAD_NAME                1 (a)
                 16 CALL_FUNCTION            1
                 18 POP_TOP
            >>   20 LOAD_CONST               2 (None)
                 22 RETURN_VALUE
       consts
          &#39;__main__&#39;
          100
          None
       names (&#39;__name__&#39;, &#39;a&#39;, &#39;print&#39;)
       varnames ()
       freevars ()
       cellvars ()
       filename &#39;./pycdemo01.py&#39;
       name &#39;<module>&#39;
       firstlineno 3
       lnotab b&#39;08010401&#39;

    下面是 code object 当中各个字段的作用:

    • 首先需要了解一下代码块这个概念,所谓代码块就是一个小的 python 代码,被当做一个小的单元整体执行。在 Python 中常见的代码块包括函数体、类的定义和模块。

    • argcount,这个表示一个代码块的参数个数,这个参数只对函数体代码块有用,因为函数可能会有参数,比如上面的 pycdemo.py 是一个模块而不是一个函数,因此这个参数对应的值为 0 。

    • co_code,这个对象的具体内容就是一个字节序列,存储真实的 python 字节码,主要是用于 python 虚拟机执行的,在本篇文章当中暂时不详细分析。

    • co_consts,这个字段是一个列表类型的字段,主要是包含一些字符串常量和数值常量,比如上面的 ";main" 和 100 。

    • co_filename,这个字段的含义就是对应的源文件的文件名。

    • co_firstlineno,这个字段的含义为在 python 源文件当中第一行代码出现的行数,这个字段在进行调试的时候非常重要。

    • 主要含义是标识该 code object 的类型的字段是 co_flags。0x0080 表示这个 block 是一个协程,0x0010 表示这个 code object 是嵌套的等等。

    • co_lnotab,这个字段的含义主要是用于计算每个字节码指令对应的源代码行数。

    • The main purpose of the field "co_varnames" is to indicate a name defined locally in a code object.。

    • co_names,和 co_varnames 相反,表示非本地定义但是在 code object 当中使用的名字。

    • co_nlocals,这个字段表示在一个 code object 当中本地使用的变量个数。

    • co_stackszie,因为 python 虚拟机是一个栈式计算机,这个参数的值表示这个栈需要的最大的值。

    • co_cellvars,co_freevars,这两个字段主要和嵌套函数和函数闭包有关。

    以上是python虛擬機器pyc檔案結構是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

    陳述:
    本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除