-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathl2-pytool.py
More file actions
53 lines (41 loc) · 1.14 KB
/
l2-pytool.py
File metadata and controls
53 lines (41 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import sys
import ast
import dis
import tokenize
from io import BytesIO
def usage():
print("Usage: python pycview.py <source.py> -ast|-ir|-tok")
sys.exit(1)
def load_source(filename):
with open(filename, "r", encoding="utf-8") as f:
return f.read()
def dump_ast(source, filename):
tree = ast.parse(source, filename=filename)
print(ast.dump(tree, indent=2))
def dump_ir(source, filename):
code = compile(source, filename, "exec")
dis.dis(code)
def dump_tokens(source):
"""
Dump lexical tokens produced by Python tokenizer,
including INDENT / DEDENT / NEWLINE / ENDMARKER.
"""
reader = BytesIO(source.encode("utf-8")).readline
for tok in tokenize.tokenize(reader):
print(tok)
def main():
if len(sys.argv) != 3:
usage()
filename = sys.argv[1]
option = sys.argv[2]
if option not in ("-ast", "-ir", "-tok"):
usage()
source = load_source(filename)
if option == "-ast":
dump_ast(source, filename)
elif option == "-ir":
dump_ir(source, filename)
elif option == "-tok":
dump_tokens(source)
if __name__ == "__main__":
main()