-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcompiler_runtest.py
More file actions
37 lines (28 loc) · 853 Bytes
/
compiler_runtest.py
File metadata and controls
37 lines (28 loc) · 853 Bytes
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
#
# Helper script for testsuite - generally, run a file thru compiler and
# disassemble using dis_stable.
#
import sys
import re
import ast
import compiler.pycodegen
# https://www.python.org/dev/peps/pep-0263/
coding_re = re.compile(rb"^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)")
def open_with_coding(fname):
with open(fname, "rb") as f:
l = f.readline()
m = coding_re.match(l)
if not m:
l = f.readline()
m = coding_re.match(l)
encoding = "utf-8"
if m:
encoding = m.group(1).decode()
return open(fname, encoding=encoding)
text = open_with_coding(sys.argv[1]).read()
node = ast.parse(text, sys.argv[1], "exec")
node.filename = sys.argv[1]
gen = compiler.pycodegen.ModuleCodeGenerator(node)
codeobj = gen.getCode()
import dis_stable
dis_stable.dis(codeobj)