OLD | NEW |
(Empty) | |
| 1 # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 |
| 2 # For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt |
| 3 |
| 4 import dis, marshal, struct, sys, time, types |
| 5 |
| 6 def show_pyc_file(fname): |
| 7 f = open(fname, "rb") |
| 8 magic = f.read(4) |
| 9 moddate = f.read(4) |
| 10 modtime = time.asctime(time.localtime(struct.unpack('<L', moddate)[0])) |
| 11 print "magic %s" % (magic.encode('hex')) |
| 12 print "moddate %s (%s)" % (moddate.encode('hex'), modtime) |
| 13 code = marshal.load(f) |
| 14 show_code(code) |
| 15 |
| 16 def show_py_file(fname): |
| 17 text = open(fname).read().replace('\r\n', '\n') |
| 18 show_py_text(text, fname=fname) |
| 19 |
| 20 def show_py_text(text, fname="<string>"): |
| 21 code = compile(text, fname, "exec") |
| 22 show_code(code) |
| 23 |
| 24 def show_code(code, indent=''): |
| 25 print "%scode" % indent |
| 26 indent += ' ' |
| 27 print "%sargcount %d" % (indent, code.co_argcount) |
| 28 print "%snlocals %d" % (indent, code.co_nlocals) |
| 29 print "%sstacksize %d" % (indent, code.co_stacksize) |
| 30 print "%sflags %04x" % (indent, code.co_flags) |
| 31 show_hex("code", code.co_code, indent=indent) |
| 32 dis.disassemble(code) |
| 33 print "%sconsts" % indent |
| 34 for const in code.co_consts: |
| 35 if type(const) == types.CodeType: |
| 36 show_code(const, indent+' ') |
| 37 else: |
| 38 print " %s%r" % (indent, const) |
| 39 print "%snames %r" % (indent, code.co_names) |
| 40 print "%svarnames %r" % (indent, code.co_varnames) |
| 41 print "%sfreevars %r" % (indent, code.co_freevars) |
| 42 print "%scellvars %r" % (indent, code.co_cellvars) |
| 43 print "%sfilename %r" % (indent, code.co_filename) |
| 44 print "%sname %r" % (indent, code.co_name) |
| 45 print "%sfirstlineno %d" % (indent, code.co_firstlineno) |
| 46 show_hex("lnotab", code.co_lnotab, indent=indent) |
| 47 |
| 48 def show_hex(label, h, indent): |
| 49 h = h.encode('hex') |
| 50 if len(h) < 60: |
| 51 print "%s%s %s" % (indent, label, h) |
| 52 else: |
| 53 print "%s%s" % (indent, label) |
| 54 for i in range(0, len(h), 60): |
| 55 print "%s %s" % (indent, h[i:i+60]) |
| 56 |
| 57 def show_file(fname): |
| 58 if fname.endswith('pyc'): |
| 59 show_pyc_file(fname) |
| 60 elif fname.endswith('py'): |
| 61 show_py_file(fname) |
| 62 else: |
| 63 print "Odd file:", fname |
| 64 |
| 65 def main(args): |
| 66 if args[0] == '-c': |
| 67 show_py_text(" ".join(args[1:]).replace(";", "\n")) |
| 68 else: |
| 69 for a in args: |
| 70 show_file(a) |
| 71 |
| 72 if __name__ == '__main__': |
| 73 main(sys.argv[1:]) |
OLD | NEW |