Hacking the Python AST
-
Upload
suhas-sg -
Category
Technology
-
view
309 -
download
1
Transcript of Hacking the Python AST
![Page 1: Hacking the Python AST](https://reader033.fdocuments.net/reader033/viewer/2022050900/587dd6941a28ab5b5b8b65ef/html5/thumbnails/1.jpg)
@jargnar
github.com/jargnar
Hacking the Python AST
![Page 2: Hacking the Python AST](https://reader033.fdocuments.net/reader033/viewer/2022050900/587dd6941a28ab5b5b8b65ef/html5/thumbnails/2.jpg)
01010101010101010101010101...
![Page 3: Hacking the Python AST](https://reader033.fdocuments.net/reader033/viewer/2022050900/587dd6941a28ab5b5b8b65ef/html5/thumbnails/3.jpg)
Aprimeroncompilingcomputerlanguages
Front end Middle end Back endC
JAVAARMx86
IR IR
![Page 4: Hacking the Python AST](https://reader033.fdocuments.net/reader033/viewer/2022050900/587dd6941a28ab5b5b8b65ef/html5/thumbnails/4.jpg)
Intel’s developer manual
![Page 5: Hacking the Python AST](https://reader033.fdocuments.net/reader033/viewer/2022050900/587dd6941a28ab5b5b8b65ef/html5/thumbnails/5.jpg)
Aprimeroncompilingcomputerlanguages
Front end Middle end Back endC
JAVAARMx86
IR IR
![Page 6: Hacking the Python AST](https://reader033.fdocuments.net/reader033/viewer/2022050900/587dd6941a28ab5b5b8b65ef/html5/thumbnails/6.jpg)
@jargnar
github.com/jargnar
The Dragon Book
![Page 7: Hacking the Python AST](https://reader033.fdocuments.net/reader033/viewer/2022050900/587dd6941a28ab5b5b8b65ef/html5/thumbnails/7.jpg)
Grammars
S –> ABA –> aA | εB –> b | bB
SAB S–>ABaAB A–>aAaaAB A–>aAaaaAB A–>aAaaaεB A–>εaaab B–>b
A parse of aaabDoes aaab belong to this Grammar?
![Page 8: Hacking the Python AST](https://reader033.fdocuments.net/reader033/viewer/2022050900/587dd6941a28ab5b5b8b65ef/html5/thumbnails/8.jpg)
@jargnar
github.com/jargnar
LL(1) GrammarLeft-to-right scanLeftmost derivation
![Page 9: Hacking the Python AST](https://reader033.fdocuments.net/reader033/viewer/2022050900/587dd6941a28ab5b5b8b65ef/html5/thumbnails/9.jpg)
@jargnar
github.com/jargnar
Let’stakeaquicklookatPython’sGrammar(Grammar/Grammar)
![Page 10: Hacking the Python AST](https://reader033.fdocuments.net/reader033/viewer/2022050900/587dd6941a28ab5b5b8b65ef/html5/thumbnails/10.jpg)
DesignoftheCPython Compiler
Parser/pgen.c
Python/ast.c
Python/compile.c
Python/compile.c
Parse Tree
.pyfile
Byte code
AST CFG
![Page 11: Hacking the Python AST](https://reader033.fdocuments.net/reader033/viewer/2022050900/587dd6941a28ab5b5b8b65ef/html5/thumbnails/11.jpg)
@jargnar
github.com/jargnar
dis
![Page 12: Hacking the Python AST](https://reader033.fdocuments.net/reader033/viewer/2022050900/587dd6941a28ab5b5b8b65ef/html5/thumbnails/12.jpg)
DesignoftheCPython Compiler
Parser/pgen.c
Python/ast.c
Python/compile.c
Python/compile.c
Parse Tree
.pyfile
Byte code
AST CFG
![Page 13: Hacking the Python AST](https://reader033.fdocuments.net/reader033/viewer/2022050900/587dd6941a28ab5b5b8b65ef/html5/thumbnails/13.jpg)
Pythontox86andthenthe01010101s
Parser/pgen.c
Python/ast.c
Python/compile.c
Python/compile.c
Parse Tree
.pyfile
Byte code
AST CFG
Python/ceval.c
![Page 14: Hacking the Python AST](https://reader033.fdocuments.net/reader033/viewer/2022050900/587dd6941a28ab5b5b8b65ef/html5/thumbnails/14.jpg)
@jargnar
github.com/jargnar
![Page 15: Hacking the Python AST](https://reader033.fdocuments.net/reader033/viewer/2022050900/587dd6941a28ab5b5b8b65ef/html5/thumbnails/15.jpg)
AccessingtheAST
a = 26b = 0print(a + b)
example.pyIn [1]: import ast
In [2]: source = open('example.py').read()
In [3]: tree = ast.parse(source)
![Page 16: Hacking the Python AST](https://reader033.fdocuments.net/reader033/viewer/2022050900/587dd6941a28ab5b5b8b65ef/html5/thumbnails/16.jpg)
AccessingtheAST
a = 26b = 0print(a + b)
example.py
Module(body=[Expr(value=Str(s='Docstring')),Assign(
targets=[Name(id='a',ctx=Store())],
value=Num(n=26)),Assign(
targets=[Name(id='b',ctx=Store())],
value=Num(n=0)),Expr(value=Call(
func=Name(id='print',ctx=Load()),
args=[BinOp(left=Name(
id='a',ctx=Load()),
op=Add(),right=Name(
id='b',ctx=Load()))],
keywords=[]))])
![Page 17: Hacking the Python AST](https://reader033.fdocuments.net/reader033/viewer/2022050900/587dd6941a28ab5b5b8b65ef/html5/thumbnails/17.jpg)
Havingalittlefun,now
In [3]: tree = ast.parse('x = 5')
In [4]:print(astunparse.dump(tree))Module(body=[Assign(
targets=[Name(id='x',ctx=Store())],
value=Num(n=5))])
In [5]: tree.body[0].targets[0].idOut[5]: 'x'
In [6]: astunparse.unparse(tree)
x = 5
In [7]:tree.body[0].targets[0].id = 'y'
In [8]:print(astunparse.unparse(tree))
y = 5
![Page 18: Hacking the Python AST](https://reader033.fdocuments.net/reader033/viewer/2022050900/587dd6941a28ab5b5b8b65ef/html5/thumbnails/18.jpg)
@jargnar
github.com/jargnar
Let’sdoabsolutelysillythingswith
ast.NodeVisitorast.NodeTransformer
![Page 19: Hacking the Python AST](https://reader033.fdocuments.net/reader033/viewer/2022050900/587dd6941a28ab5b5b8b65ef/html5/thumbnails/19.jpg)
@jargnar
github.com/jargnar
Evenmoresillythingswith
Flake8
![Page 20: Hacking the Python AST](https://reader033.fdocuments.net/reader033/viewer/2022050900/587dd6941a28ab5b5b8b65ef/html5/thumbnails/20.jpg)
@jargnar
github.com/jargnar
Hacking is a mindset, not a skill.
suhas.org