问题
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