Python:使用compile时出现分段错误/

2024-05-16 17:40:27 发布

您现在位置:Python中文网/ 问答频道 /正文

代码:

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,我遇到了一个分段错误。为什么?在



Tags: 代码importreportnonefooargsbodypypy
1条回答
网友
1楼 · 发布于 2024-05-16 17:40:27

我想你的函数定义不能有一个空的主体。我通过添加no op语句作为函数体测试了您的代码:

fAst = ast.FunctionDef(
    # ...
    body=[ast.Pass()],
    # ...

分割错误消失;输出为:

^{pr2}$

如果我是正确的,这可能是ast模块中的一个bug,因为它应该检查空主体。在

相关问题 更多 >