![](/img/trans.png)
[英]What are the arguments to the types.CodeType() python call?
[英]TypeError on CodeType creation in Python 3
我正在尝试创建一个新的CodeType,以下代码在Python 2.7中可以正常运行,但是在Python 3.2中却出现错误:
def newCode(co_argcount = 0, co_nlocals = 0, co_stacksize = 0, co_flags = 0x0000,
co_code = bytes(), co_consts = (), co_names = (), co_varnames = (),
filename = "<string>", name = "", firstlineno = 0, co_lnotab = bytes(),
co_freevars = (), co_cellvars = ()):
"""wrapper for CodeType so that we can remember the synatax"""
print(type(co_stacksize))
return types.CodeType(co_argcount, co_nlocals, co_stacksize,
co_flags, co_code, co_consts, co_names, co_varnames,
filename, name, firstlineno, co_lnotab, co_freevars, co_cellvars)
用法:
return newCode(co_code = code, co_stacksize = size, co_consts = consts)
调试行证明我以co_stacksize的形式发送了一个int ...在Python 3中做了哪些更改以使其不起作用?
编辑:这是错误(不知道为什么我以前忘记了):
TypeError:必须为整数
在Python 3上, CodeType
的第二个参数是仅关键字参数co_kwonlyargcount
。 这是一个附加项,因此所有以后的参数将移动一个位置。
我在一个允许腌制代码对象的实用程序模块中,从IPython的代码库中挖了出来: https : //github.com/ipython/ipython/blob/master/IPython/utils/codeutil.py#L32
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.