Python的原始代码在运行前都会被先编译成字节码,并把编译的结果保存到一个一个的PyCodeObject中,pyc 文件即是把PyCodeObject从内存中以marshal格式保存到文件后的结果。
下面我们来通过测试和工具来了解下pyc文件到底有些什么东西。
先写个简单的测试程序:
test.py
import dis
myglobal = True
def add(a):
b = 1
a += b
return a
class world:
def __init__(self):
pass
def sayHello(self):
print 'hello,world'
w = world()
w.sayHello()
在这个例子里,全局变量,函数,类都有了,然后我们用下面的命令把它编译成pyc文件:
python -m compileall test.py
然后我们通过下面的代码分析一下test.pyc:
showfile.py
import dis, marshal, struct, sys, time, types
def show_file(fname):
f = open(fname, "rb")
magic = f.read(4)
moddate = f.read(4)
modtime = time.asctime(time.localtime(struct.unpack('L', moddate)[0]))
print "magic %s" % (magic.encode('hex'))
print "moddate %s (%s)" % (moddate.encode('hex'), modtime)
code = marshal.load(f)
show_code(code)
def show_code(code, indent=''):
old_indent = indent
print "%s<code>" % indent
indent += ' '
print "%s<argcount> %d </argcount>" % (indent, code.co_argcount)
print "%s<nlocals> %d</nlocals>" % (indent, code.co_nlocals)
print "%s<stacksize> %d</stacksize>" % (indent, code.co_stacksize)
print "%s<flags> %04x</flags>" % (indent, code.co_flags)
show_hex("code", code.co_code, indent=indent)
print "%s<dis>" % indent
dis.disassemble(code)
print "%s</dis>" % indent
print "%s<names> %r</names>" % (indent, code.co_names)
print "%s<varnames> %r</varnames>" % (indent, code.co_varnames)
print "%s<freevars> %r</freevars>" % (indent, code.co_freevars)
print "%s<cellvars> %r</cellvars>" % (indent, code.co_cellvars)
print "%s<filename> %r</filename>" % (indent, code.co_filename)
print "%s<name> %r</name>" % (indent, code.co_name)
print "%s<firstlineno> %d</firstlineno>" % (indent, code.co_firstlineno)
print "%s<consts>" % indent
for const in code.co_consts:
if type(const) == types.CodeType:
show_code(const, indent+' ')
else:
print " %s%r" % (indent, const)
print "%s</consts>" %