代码:
import ast
globalsDict = {}
fAst = ast.FunctionDef(
name="foo",
args=ast.arguments(args=[], vararg=None, kwarg=None, defaults=[]),
body=[], decorator_list=[])
exprAst = ast.Interactive(body=[fAst])
ast.fix_missing_locations(exprAst)
compiled = compile(exprAst, "<foo>", "single")
eval(compiled, globalsDict, globalsDict)
print globalsDict["foo"]
对于CPython和PyPy,我遇到了一个分段错误。为什么?在
我想你的函数定义不能有一个空的主体。我通过添加no op语句作为函数体测试了您的代码:
分割错误消失;输出为:
^{pr2}$如果我是正确的,这可能是
ast
模块中的一个bug,因为它应该检查空主体。在相关问题 更多 >
编程相关推荐