Message352090
While trying to construct a valid ast node programmatically, I have tried following:
import ast
tree = ast.BinOp(left=ast.Num(n=2), right=ast.Num(n=2), op=ast.Add())
expr = ast.Expression(body=[tree])
ast.fix_missing_locations(expr)
exe = compile(expr, filename="", mode="eval")
print(eval(exe))
Unfortunately this gives unhelpful error message:
>>> exe = compile(expr, filename="", mode="eval")
TypeError: required field "lineno" missing from expr
Turns out I need to make body of ast.Expression not a list, but a node:
expr = ast.Expression(body=tree) # works
Confusion also comes from naming the field "body", which takes value of a list for ast.Module and some others. |
|
Date |
User |
Action |
Args |
2019-09-12 10:21:59 | Ilya Kamenshchikov | set | recipients:
+ Ilya Kamenshchikov |
2019-09-12 10:21:59 | Ilya Kamenshchikov | set | messageid: <1568283719.06.0.851695472479.issue38131@roundup.psfhosted.org> |
2019-09-12 10:21:59 | Ilya Kamenshchikov | link | issue38131 messages |
2019-09-12 10:21:58 | Ilya Kamenshchikov | create | |
|