Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b769e80

Browse files
committed
read in a .pyc file and disassemble the code objects
1 parent d923831 commit b769e80

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

Tools/compiler/dumppyc.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#! /usr/bin/env python
2+
3+
import marshal
4+
import dis
5+
import types
6+
7+
def dump(obj):
8+
print obj
9+
for attr in dir(obj):
10+
print "\t", attr, repr(getattr(obj, attr))
11+
12+
def loadCode(path):
13+
f = open(path)
14+
f.read(8)
15+
co = marshal.load(f)
16+
f.close()
17+
return co
18+
19+
def walk(co, match=None):
20+
if match is None or co.co_name == match:
21+
dump(co)
22+
print
23+
dis.dis(co)
24+
for obj in co.co_consts:
25+
if type(obj) == types.CodeType:
26+
walk(obj, match)
27+
28+
def main(filename, codename=None):
29+
co = loadCode(filename)
30+
walk(co, codename)
31+
32+
if __name__ == "__main__":
33+
import sys
34+
if len(sys.argv) == 3:
35+
filename, codename = sys.argv[1:]
36+
else:
37+
filename = sys.argv[1]
38+
codename = None
39+
main(filename, codename)

0 commit comments

Comments
 (0)