New submission from Ilya Kamenshchikov <ikamenshchi...@gmail.com>:
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. ---------- components: Library (Lib) messages: 352090 nosy: Ilya Kamenshchikov priority: normal severity: normal status: open title: compile(mode='eval') uninformative error message versions: Python 3.6, Python 3.7, Python 3.8 _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue38131> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com