-
Notifications
You must be signed in to change notification settings - Fork 3
/
dis_stable.py
79 lines (65 loc) · 2.3 KB
/
dis_stable.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
#
# Dissassemble code objects:
# a) recursively (like dis.dis() in Python3.7 behaves);
# b) providing stable references to internal code objects (by replacing
# memory address with incrementing number);
# c) besides disassembly, also dump other fields of code objects.
# Useful for comparing disassembly outputs from different runs.
#
from __future__ import print_function
import re
import dis as _dis
id_map = {}
id_cnt = 0
def get_co_id(co):
global id_cnt
addr = id(co)
if addr in id_map:
return id_map[addr]
id_map[addr] = id_cnt
id_cnt += 1
return id_cnt - 1
def co_repr(co):
return '<code object %s at #%d, file "%s", line %d>' % (co.co_name, get_co_id(co), co.co_filename, co.co_firstlineno)
def disassemble(co, lasti=-1, file=None):
"""Disassemble a code object."""
cell_names = co.co_cellvars + co.co_freevars
linestarts = dict(_dis.findlinestarts(co))
consts = [co_repr(x) if hasattr(x, "co_code") else x for x in co.co_consts]
_dis._disassemble_bytes(co.co_code, lasti, co.co_varnames, co.co_names,
consts, cell_names, linestarts, file=file)
def dis(co):
print(co_repr(co))
disassemble(co)
print("co_argcount:", co.co_argcount)
print("co_kwonlyargcount:", co.co_kwonlyargcount)
# print("co_stacksize:", co.co_stacksize)
# print("co_flags:", hex(co.co_flags))
print("co_consts:", tuple([co_repr(x) if hasattr(x, "co_code") else x for x in co.co_consts]))
print("co_names:", co.co_names)
print("co_varnames:", co.co_varnames)
print("co_cellvars:", co.co_cellvars)
print("co_freevars:", co.co_freevars)
print()
for c in co.co_consts:
if hasattr(c, "co_code"):
dis(c)
# 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 __name__ == "__main__":
import sys
with open_with_coding(sys.argv[1]) as f:
co = compile(f.read(), sys.argv[1], "exec")
dis(co)