types.CodeType()python调用的参数是什么? [英] What are the arguments to the types.CodeType() python call?

查看:2106
本文介绍了types.CodeType()python调用的参数是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正试图为python卷起我自己的marshal代码,这样我就可以在Google App Engine上存储编译的python代码,以便以动态方式提供脚本。因为你都可以验证,marshal不支持GAE和pickle不能序列化代码对象。

I'm currently trying to roll my own "marshal" code for python so i can store compiled python code on Google App Engine to serve scripts on a dynamic way. As you all can verify, "marshal" isn't supported on GAE and "pickle" can't serialize code objects.

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

I found out i can construct a code object with types.CodeType() but it expects 12 arguments.

找不到任何关于这个调用的文档,我真的需要构造代码对象,所以我可以 exec()它。我的问题是,有没有人知道这个 types.CodeType()构造函数的参数是什么或任何方式自省?使用 info()函数定义了这里,但它吐出只是通用信息!

As much as i've tried, i can't find any documentation on this call and i really need to construct the code object so i can exec() it. My question is, does anyone know what are the parameters for this types.CodeType() "constructor" or any way to introspect it? i have used the info() function defined here but it spits out just generic info!

快速FAQ:


  • 问:为什么要编译代码?

  • A:CPU时间在Google App Engine上花费真实资金,而且每次CPU周期都可以节省计数。

  • marshal?

  • A:这是 Google App Engine中不支持的模块

  • 问:为什么不使用pickle?

  • A:Pickle不支持序列化对象。

  • Q: Why compile the code?
  • A: CPU time costs real money on Google App Engine, and every bit of CPU cycles i can save counts.
  • Q: Why not use "marshal"?
  • A: That's one of the unsupported modules in Google App Engine.
  • Q: Why not use "pickle"?
  • A: Pickle doesn't support serialization of code objects.

UPDATE

Google App Engine基础架构不允许代码对象截至2011年7月7日,所以我的参数在这里是moot。希望以后在GAE上修复。

Google App Engine infrastructure doesn't allow the instantiation of code objects as of 7th July 2011, so my argument here is moot. Hope this gets fixed in the future on GAE.

推荐答案

/book.opensourceproject.org.cn/lamp/python/pythoncook2/opensource/0596007973/pythoncook2-chp-7-sect-6.html>在此处,并删除了已弃用的新模块的依赖关系。 / p>

I went and took the code found here and removed the dependency for the deprecated "new" module.

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')

这篇关于types.CodeType()python调用的参数是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆