Convert python objects to python AST-nodes

社会主义新天地 提交于 2020-04-14 08:21:13

问题


I have a need to dump the modified python object back into source. So I try to find something to convert real python object to python ast.Node (to use later in astor lib to dump source)

Example of usage I want, Python 2:

import ast
import importlib

import astor


m = importlib.import_module('something')

# modify an object
m.VAR.append(123)

ast_nodes = some_magic(m)

source = astor.dump(ast_nodes)

Please help me to find that some_magic


回答1:


There's no way to do what you want, because that's not how ASTs work. When the interpreter runs your code, it will generate an AST out of the source files, and interpret that AST to generate python objects. What happen to those objects once they've been generated has nothing to do with the AST.

It is however possible to get the AST of what generated the object in the first place. The module inspect lets you get the source code of some python objects:

import ast
import importlib
import inspect

m = importlib.import_module('pprint')
s = inspect.getsource(m)
a = ast.parse(s)
print(ast.dump(a))
# Prints the AST of the pprint module

But getsource() is aptly named. If I were to change the value of some variable (or any other object) in m, it wouldn't change its source code.

Even if it was possible to regenerate an AST out of an object, there wouldn't be a single solution some_magic() could return. Imagine I have a variable x in some module, that I reassign in another module:

# In some_module.py
x = 0

# In __main__.py
m = importlib.import_module('some_module')
m.x = 1 + 227

Now, the value of m.x is 228, but there's no way to know what kind of expression led to that value (well, without reading the AST of __main__.py but this would quickly get out of hand). Was it a mere literal? The result of a function call?

If you really have to get a new AST after modifying some value of a module, the best solution would be to transform the original AST by yourself. You can find where your identifier got its value, and replace the value of the assignment with whatever you want. For instance, in my small example x = 0 is represented by the following AST:

Assign(targets=[Name(id='x', ctx=Store())], value=Num(n=0))

And to get the AST matching the reassignment I did in __main__.py, I would have to change the value of the above Assign node as the following:

value=BinOp(left=Num(n=1), op=Add(), right=Num(n=227))

If you'd like to go that way, I recommend you check Python's documentation of the AST node transformer (ast.NodeTransformer), as well as this excellent manual that documents all the nodes you can meet in Python ASTs Green Tree Snakes - the missing Python AST docs.



来源:https://stackoverflow.com/questions/43575099/convert-python-objects-to-python-ast-nodes

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