Hes*_*dsi 5 python python-2.7 python-3.x
我在 Python 3.3 中尝试这个
f = open(r'somewhere\my_module.pyc','rb')
contents = f.read()
f.close()
code_obj = marshal.loads(contents[8:])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: bad marshal data (unknown type code)
Run Code Online (Sandbox Code Playgroud)
我收到错误,所以我将contents
变量类型转换为str
def bytes2str(byte_seq):
str_seq = ''
for b in byte_seq:
str_seq += chr(b)
return str_seq
contents = bytes2str(contents)
code_obj = marshal.loads(contents[8:])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' does not support the buffer interface
Run Code Online (Sandbox Code Playgroud)
当我在 Python 2.7 中尝试这个时,我得到一个代码对象。compile
不使用内置函数如何处理这个问题?
在最近版本的 Python 中,文件头似乎.pyc
已更改为 12 个字节,而不是 8 个字节。如果这样做code_obj = marshal.loads(contents[12:])
,您将获得您正在查找的代码对象。
我徒劳地尝试找到有关 PYC 文件格式更改的文档,但到目前为止我还没有任何运气。看起来它是从 Python 3.3 开始的(其中包括对导入机制的大量更改),但我不确定哪一位需要额外的 4 个字节。
归档时间: |
|
查看次数: |
2263 次 |
最近记录: |