Generate ast from constituent elements without using ast.parse in python

二次信任 提交于 2021-01-28 08:58:51

问题


Using the python ast module, it is possible to generate a simple abstract syntax tree as follows:

import ast
module = ast.parse('x=3')

This generates a Module object for which the source code can be retrieved using the astor library as follows:

import astor
astor.to_source(module)

Generating an output of

'x = 3\n'

Is it possible to constitute the exact same module object from its constituent elements without using the ast.parse method such that the astor.to_source method can generate the same source code? if so how?


回答1:


I think I just found it. using ast.dump one can inspect the contents of the tree as follows:

import astor, ast
module = ast.parse('x=3')
ast.dump(module)

This results in the following output which reveals the underlying structure:

"Module(body=[Assign(targets=[Name(id='x', ctx=Store())], value=Num(n=3))])"

We can make use of this information to build the same tree from scratch, and then use astor to recover the source:

module = ast.Module(body=[ast.Assign(targets=[ast.Name(id='x', ctx=ast.Store())], value=ast.Num(n=3))])
astor.to_source(module)

Which outputs the following:

'x = 3\n'

There is one problem however, since executing this new tree results in an error:

exec(compile(module, filename='<ast>', mode="exec"))

Traceback (most recent call last): File "", line 1, in TypeError: required field "lineno" missing from stmt

To fix this, line numbers must be added to each node using the ast.fix_missing_locations method.



来源:https://stackoverflow.com/questions/60791084/generate-ast-from-constituent-elements-without-using-ast-parse-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!