将PyCodeObject保存为pyc

#!/usr/bin/env python

import py_compile
import imp
import os

def pycodeobject2pyc(pyobj, pycfile):
    with open(pycfile, 'wb') as fc: 
        fc.write('\0\0\0\0')
        timestamp = long(os.fstat(fc.fileno()).st_mtime)
        py_compile.wr_long(fc, timestamp)
        marshal.dump(pyobj, fc)
        fc.flush()
        fc.seek(0, 0)
        MAGIC = imp.get_magic()
        fc.write(MAGIC)
标签: 无
返回文章列表 文章二维码
本页链接的二维码
打赏二维码