forked from facebookarchive/python-compiler
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompiler_runtest.py
More file actions
41 lines (32 loc) · 977 Bytes
/
compiler_runtest.py
File metadata and controls
41 lines (32 loc) · 977 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
38
39
40
41
#
# Helper script for testsuite - generally, run a file thru compiler and
# disassemble using dis_stable.
#
import sys
import re
import ast
from compiler.pycodegen import compile as py_compile
# 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)
if len(sys.argv) < 2:
print('no filename provided')
sys.exit(1)
peephole = True
if sys.argv[1] == '--peephole':
peephole = True
del sys.argv[1]
text = open_with_coding(sys.argv[1]).read()
codeobj = py_compile(text, sys.argv[1], "exec")
import dis_stable
dis_stable.Disassembler().dump_code(codeobj, file=sys.stdout)