Python:使用编译/评估时出现分段错误

Alb*_*ert 6 python pypy cpython compilation segmentation-fault

码:

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"]
Run Code Online (Sandbox Code Playgroud)

使用CPython和PyPy,我遇到了分段错误.为什么?


Fer*_*yer 5

我猜你的函数定义必须没有空体.我通过添加一个no-op语句作为函数体来测试你的代码:

fAst = ast.FunctionDef(
    # ...
    body=[ast.Pass()],
    # ...
Run Code Online (Sandbox Code Playgroud)

分段错误消失了; 输出是:

<function foo at 0x022DB3F0>

如果我是正确的,这可能是ast模块中的错误,因为它应该检查空体.