我正在尝试创建一个新的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)
调试行证明我正在发送一个int作为co_stacksize ...在Python 3中发生了哪些变化使这不起作用?
编辑: 这是错误(不知道为什么我之前忘了):
TypeError:需要一个整数
答案 0 :(得分:2)
在Python 3上,CodeType
的第二个参数是仅关键字参数的数量co_kwonlyargcount
。这是一个补充,所以后面的所有参数都会移动一个位置。
我从IPython的代码库中挖掘出来,在一个允许修补代码对象的实用程序模块中: https://github.com/ipython/ipython/blob/master/IPython/utils/codeutil.py#L32