summaryrefslogtreecommitdiff
path: root/lab/show_pyc.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2009-04-02 19:42:04 -0400
committerNed Batchelder <ned@nedbatchelder.com>2009-04-02 19:42:04 -0400
commit4910434d33d0928374bf966c00c07feda5b32d77 (patch)
tree7b8da0180a122d3b261ac19cffa4a6c6c1305507 /lab/show_pyc.py
parent1fadd547372a721a9b0acb4dc710195f85138c59 (diff)
downloadpython-coveragepy-git-4910434d33d0928374bf966c00c07feda5b32d77.tar.gz
A lab directory for experiments in progress.
Diffstat (limited to 'lab/show_pyc.py')
-rw-r--r--lab/show_pyc.py64
1 files changed, 64 insertions, 0 deletions
diff --git a/lab/show_pyc.py b/lab/show_pyc.py
new file mode 100644
index 00000000..a0834e88
--- /dev/null
+++ b/lab/show_pyc.py
@@ -0,0 +1,64 @@
+import dis, marshal, struct, sys, time, types
+
+def show_pyc_file(fname):
+ f = open(fname, "rb")
+ magic = f.read(4)
+ moddate = f.read(4)
+ modtime = time.asctime(time.localtime(struct.unpack('L', moddate)[0]))
+ print "magic %s" % (magic.encode('hex'))
+ print "moddate %s (%s)" % (moddate.encode('hex'), modtime)
+ code = marshal.load(f)
+ show_code(code)
+
+def show_py_file(fname):
+ text = open(fname).read().replace('\r\n', '\n')
+ code = compile(text, fname, "exec")
+ show_code(code)
+
+def show_code(code, indent=''):
+ print "%scode" % indent
+ indent += ' '
+ print "%sargcount %d" % (indent, code.co_argcount)
+ print "%snlocals %d" % (indent, code.co_nlocals)
+ print "%sstacksize %d" % (indent, code.co_stacksize)
+ print "%sflags %04x" % (indent, code.co_flags)
+ show_hex("code", code.co_code, indent=indent)
+ dis.disassemble(code)
+ print "%sconsts" % indent
+ for const in code.co_consts:
+ if type(const) == types.CodeType:
+ show_code(const, indent+' ')
+ else:
+ print " %s%r" % (indent, const)
+ print "%snames %r" % (indent, code.co_names)
+ print "%svarnames %r" % (indent, code.co_varnames)
+ print "%sfreevars %r" % (indent, code.co_freevars)
+ print "%scellvars %r" % (indent, code.co_cellvars)
+ print "%sfilename %r" % (indent, code.co_filename)
+ print "%sname %r" % (indent, code.co_name)
+ print "%sfirstlineno %d" % (indent, code.co_firstlineno)
+ show_hex("lnotab", code.co_lnotab, indent=indent)
+
+def show_hex(label, h, indent):
+ h = h.encode('hex')
+ if len(h) < 60:
+ print "%s%s %s" % (indent, label, h)
+ else:
+ print "%s%s" % (indent, label)
+ for i in range(0, len(h), 60):
+ print "%s %s" % (indent, h[i:i+60])
+
+def show_file(fname):
+ if fname.endswith('pyc'):
+ show_pyc_file(fname)
+ elif fname.endswith('py'):
+ show_py_file(fname)
+ else:
+ print "Odd file:", fname
+
+def main(args):
+ for a in args:
+ show_file(a)
+
+if __name__ == '__main__':
+ main(sys.argv[1:])