Python pyc解析记录

准备加强python学习,在网上翻了几篇文章想了解一下pyc结构

我的python 是3.7.1 x64

写了一个简单的python3代码


def add(a,b):
    return a+b

def mul(a,b):
    return a*b

def plu(a,b):
    return a-b

def div(a,b):
    return a/b

保存成lib.py 编译生成pyc

python -m lib

注意这个地方千万不要加后缀 .py,不然会报错

Error while finding module specification for 'lib.py' (ModuleNotFoundError: __path__ attribute not found on 'lib' while trying to find 'lib.py')

我也是在网上找了很久才发现这个问题解决办法,有很多文章是有加py后缀,也可能有些版本是没有问题的,我的python版本是3.7.1,如下图

编译完成后会生成__pycache__/lib.cpython-37.pyc文件

在网上找了一篇分析pyc的python代码

import dis, marshal, struct, sys, time, types, binascii
def show_file(fname):
    f = open(fname, "rb")
    magic = f.read(4)
    moddate = f.read(4)
    filesz = f.read(4)
    modtime = time.asctime(time.localtime(struct.unpack('=L', moddate)[0]))
    filesz = struct.unpack('=L', filesz)
    print ("magic %s" % (binascii.hexlify(magic)))
    print ("moddate %s (%s)" % (binascii.hexlify(moddate), modtime))
    print ("files sz %d" % filesz)
    code = marshal.load(f)
    show_code(code)
def show_code(code, indent=''):
    print ("%scode" % indent)
    indent += '   '
    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+'   ')
        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]))
#show_file(sys.argv[1])
show_file("D:\\wksp_py\\__pycache__\\lib.cpython-37.pyc")

运行报错

magic b'420d0d0a'
moddate b'00000000' (Thu Jan  1 08:00:00 1970)
Traceback (most recent call last):
  File "ppyc.py", line 46, in <module>
    show_file("D:\\wksp_py\\__pycache__\\lib.cpython-37.pyc")
  File "ppyc.py", line 12, in show_file
    code = marshal.load(f)
ValueError: bad marshal data (unknown type code)

这个问题,折腾很长时间,我也在网上查了,这段代码网上很多地方都有,分析二进制,查看python源码,其中艰辛暂时不表,直接说结果。

最终在pep552上找到答案

PEP 552 -- Deterministic pycs | Python.org

 头部,由3个32bit变成4个

第一个32bit是魔数,表示字节码的版本和pyc文件格式

第二个32bit是新的位域,如果这个位域为0,则第三和第四字节还是老的时间戳和文件大小

这个特性是在Python 3.7和3.7以上版本,3.6版本还是3个32bit,这个我在python 3.6.9上测试过。

接着我把上述代码中的

    magic = f.read(4)

改为

    magic = f.read(8)

整个运行正常。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jjinl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值