首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >types.CodeType() python调用的参数是什么?

types.CodeType() python调用的参数是什么?
EN

Stack Overflow用户
提问于 2011-07-07 14:43:09
回答 4查看 7.2K关注 0票数 18

我目前正在尝试为python提交自己的“封送”代码,这样我就可以将编译好的python代码存储在上,以便动态地为脚本提供服务。正如大家都可以验证的那样,GAE不支持“封送”,而“泡菜”不能序列化代码对象。

我发现我可以用types.CodeType()构造一个代码对象,但它需要12个参数。

尽管我已经尝试了很多,但是我找不到关于这个调用的任何文档,我真的需要构造代码对象,这样我就可以exec()它了。我的问题是,有人知道这个types.CodeType() “构造函数”的参数是什么吗?或者有什么方法可以自省吗?--我已经使用了定义了这里info()函数,但是它只显示了一般的信息!

快速常见问题:

  • 问:为什么要编译代码?
  • 答:在上,CPU时间花费了很多钱,我可以节省每一点CPU周期。
  • 问:为什么不使用“元帅”?
  • A:那是Google中不支持的模块之一。
  • 问:为什么不使用“泡菜”呢?
  • 答:泡菜不支持代码对象的序列化。

更新

截至2011年7月7日,Google基础设施不允许对代码对象进行实例化,所以我的论点是毫无意义的。希望这将在未来的GAE上得到解决。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-07-07 15:18:36

我去取了代码,找到了这里,并删除了不推荐的“新”模块的依赖项。

代码语言:javascript
运行
复制
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')
票数 5
EN

Stack Overflow用户

发布于 2012-10-13 22:13:38

问题是:

这个types.CodeType()“构造函数”的参数是什么?

来自python关于检查模块的文档

代码语言:javascript
运行
复制
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。

票数 7
EN

Stack Overflow用户

发布于 2011-07-07 15:35:30

code函数PyCode_New在这里(最少)有文档:http://docs.python.org/c-api/code.html --这个函数的C源代码(Python2.7)在这里:http://hg.python.org/cpython/file/b5ac5e25d506/Objects/codeobject.c#l43

代码语言:javascript
运行
复制
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构造函数中,最后六个参数似乎被交换了一些。这是提取Python:http://hg.python.org/cpython/file/b5ac5e25d506/Objects/codeobject.c#l247传递的参数的C代码。

代码语言:javascript
运行
复制
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;

化肥化:

代码语言:javascript
运行
复制
def __init__(self, argcount, nlocals, stacksize, flags, code,
                   consts, names, varnames, filename, name, 
                   firstlineno, lnotab, freevars=None, cellvars=None): # ...
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6612449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档