How to change Python source code to add new AST node?

For example, if I were to create a node called "Something" that I wanted to use in the Python AST tree, where and what changes would I need to add to the Python source code to do this?

I know that I have to start with Python.asdl where the AST grammar is defined. Then I have to go to ast.c.

Unfortunately, I'm not sure where exactly I have to make changes to the ast.c file in order to implement node.

Also, for simplicity's sake, let's say that I just want the node to be substitute, which means that it has nothing to do but insert itself into the tree.

+4
source share
1 answer

No need to dive in Python.asdl ! Then you need to go after the ASDL parser, and then you need to figure out what to do with it (although this is a worthy exercise in itself). You can stay in Python thanks to the amazing ast module.

Be sure to check out Elia Bendersky 's article on this subject .

Here is an example that he gives where the AST is built, and eval from scratch!

 import ast node = ast.Expression(ast.BinOp( ast.Str('xy'), ast.Mult(), ast.Num(3))) fixed = ast.fix_missing_locations(node) codeobj = compile(fixed, '<string>', 'eval') print eval(codeobj) 

ast.NodeTransformer

Check out the ast.NodeTransformer class if you want to convert an existing AST.

Here is an example from the postwhich blog above that changes the values โ€‹โ€‹of the strings to be added with str:

 class MyTransformer(ast.NodeTransformer): def visit_Str(self, node): return ast.Str('str: ' + node.s) 
+1
source

Source: https://habr.com/ru/post/1395354/


All Articles