Python: getting segmentation error when using compile / eval

the 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 get a segmentation error. Why?


+6
source share
1 answer

I assume that your function definition should not have an empty body. I checked your code by adding the no-op operator as the function body:

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

And the segmentation error has disappeared; exit:

  <function foo at 0x022DB3F0> 

If I'm right, this could be a mistake in the ast module, since it should check for an empty body.

+5
source

Source: https://habr.com/ru/post/893287/


All Articles