types.CodeType() Python 调用的参数是什么?
我现在正在尝试自己写一段“marshal”代码,用于在Google App Engine上存储编译后的Python代码,以便动态地提供脚本。大家都知道,“marshal”在GAE上不支持,而“pickle”又无法序列化代码对象。
我发现可以用types.CodeType()
来构造一个代码对象,但它需要12个参数。
我试了很多次,找不到关于这个调用的任何文档,我真的需要构造这个代码对象,好让我可以用exec()
来执行它。我的问题是,有没有人知道这个types.CodeType()
“构造器”的参数是什么,或者有什么方法可以查看它的内容?我用过info()
函数,具体定义可以在这里找到,但它只给出了一些通用的信息!
快速问答:
- 问:为什么要编译代码?
- 答:在Google App Engine上,CPU时间是要花钱的,我能节省的每一毫秒都很重要。
- 问:为什么不使用“marshal”?
- 答:这是Google App Engine上不支持的模块之一。
- 问:为什么不使用“pickle”?
- 答:因为pickle不支持序列化代码对象。
更新
截至2011年7月7日,Google App Engine的基础设施不允许实例化代码对象,所以我在这里的论点已经没有意义了。希望将来GAE能解决这个问题。
4 个回答
这个C语言的API函数PyCode_New的文档可以在这里找到:http://docs.python.org/c-api/code.html,而这个函数的C源代码(适用于Python 2.7)可以在这里查看:http://hg.python.org/cpython/file/b5ac5e25d506/Objects/codeobject.c#l43
PyCodeObject *
PyCode_New(int argcount, int nlocals, int stacksize, int flags,
PyObject *code, PyObject *consts, PyObject *names,
PyObject *varnames, PyObject *freevars, PyObject *cellvars,
PyObject *filename, PyObject *name, int firstlineno,
PyObject *lnotab)
不过,在Python的构造函数中,最后六个参数似乎有点顺序颠倒了。下面这段C代码是用来提取Python传入的参数的:http://hg.python.org/cpython/file/b5ac5e25d506/Objects/codeobject.c#l247
if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code",
&argcount, &nlocals, &stacksize, &flags,
&code,
&PyTuple_Type, &consts,
&PyTuple_Type, &names,
&PyTuple_Type, &varnames,
&filename, &name,
&firstlineno, &lnotab,
&PyTuple_Type, &freevars,
&PyTuple_Type, &cellvars))
return NULL;
用Python风格表达:
def __init__(self, argcount, nlocals, stacksize, flags, code,
consts, names, varnames, filename, name,
firstlineno, lnotab, freevars=None, cellvars=None): # ...
提问内容是:
这个 types.CodeType() “构造函数”的参数是什么?
关于 inspect 模块 的 Python 文档中提到:
co_argcount: number of arguments (not including * or ** args)
co_code: string of raw compiled bytecode
co_consts: tuple of constants used in the bytecode
co_filename: name of file in which this code object was created
co_firstlineno: number of first line in Python source code
co_flags: bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
co_lnotab: encoded mapping of line numbers to bytecode indices
co_name: name with which this code object was defined
co_names: tuple of names of local variables
co_nlocals: number of local variables
co_stacksize: virtual machine stack space required
co_varnames: tuple of names of arguments and local variables
这篇博客文章有更详细的解释:http://tech.blog.aknin.name/2010/07/03/pythons-innards-code-objects/
注意:这篇博客文章讨论的是 Python 3,而上面引用的 Python 文档是 Python 2.7。
我去找了在这里找到的代码,然后把不再使用的“new”模块的依赖给去掉了。
import types, copy_reg
def code_ctor(*args):
# delegate to new.code the construction of a new code object
return types.CodeType(*args)
def reduce_code(co):
# a reductor function must return a tuple with two items: first, the
# constructor function to be called to rebuild the argument object
# at a future de-serialization time; then, the tuple of arguments
# that will need to be passed to the constructor function.
if co.co_freevars or co.co_cellvars:
raise ValueError, "Sorry, cannot pickle code objects from closures"
return code_ctor, (co.co_argcount, co.co_nlocals, co.co_stacksize,
co.co_flags, co.co_code, co.co_consts, co.co_names,
co.co_varnames, co.co_filename, co.co_name, co.co_firstlineno,
co.co_lnotab)
# register the reductor to be used for pickling objects of type 'CodeType'
copy_reg.pickle(types.CodeType, reduce_code)
if __name__ == '__main__':
# example usage of our new ability to pickle code objects
import cPickle
# a function (which, inside, has a code object, of course)
def f(x): print 'Hello,', x
# serialize the function's code object to a string of bytes
pickled_code = cPickle.dumps(f.func_code)
# recover an equal code object from the string of bytes
recovered_code = cPickle.loads(pickled_code)
# build a new function around the rebuilt code object
g = types.FunctionType(recovered_code, globals( ))
# check what happens when the new function gets called
g('world')