• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#! /usr/bin/env python
2
3import marshal
4import dis
5import types
6
7def dump(obj):
8    print obj
9    for attr in dir(obj):
10        if attr.startswith('co_'):
11            val = getattr(obj, attr)
12            print "\t", attr, repr(val)
13
14def loadCode(path):
15    f = open(path)
16    f.read(8)
17    co = marshal.load(f)
18    f.close()
19    return co
20
21def walk(co, match=None):
22    if match is None or co.co_name == match:
23        dump(co)
24        print
25        dis.dis(co)
26    for obj in co.co_consts:
27        if type(obj) == types.CodeType:
28            walk(obj, match)
29
30def load(filename, codename=None):
31    co = loadCode(filename)
32    walk(co, codename)
33
34if __name__ == "__main__":
35    import sys
36    if len(sys.argv) == 3:
37        filename, codename = sys.argv[1:]
38    else:
39        filename = sys.argv[1]
40        codename = None
41    if filename.endswith('.py'):
42        buf = open(filename).read()
43        co = compile(buf, filename, "exec")
44        walk(co)
45    else:
46        load(filename, codename)
47