Code:
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"]
With both CPython and PyPy, I am getting a segmentation fault. Why?
I guess that your function definition must not have an empty body. I tested your code by adding a no-op statement as the function body:
fAst = ast.FunctionDef(
# ...
body=[ast.Pass()],
# ...
And the segmentation fault is gone; output is:
<function foo at 0x022DB3F0>
If I am correct, this could be a bug in the ast
module, since it should check for the empty body.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With