1

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?

asked Mar 21, 2020 at 17:32

1 Answer 1

2

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.

answered Mar 21, 2020 at 18:58
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.