More user friedly interface:

dis() still disassembles the last frame of the lats stack trace.

dis(x) disassembles x, which may be a code object, function, or method.

disassemble(co, [lasti]) disassembles a code object; the lasti
argument is now optional.

disco(...) is an alias for disassemble(...), for backward compatibility.
This commit is contained in:
Guido van Rossum 1997-01-17 20:05:04 +00:00
parent c905fffa15
commit bd30795192
1 changed files with 24 additions and 9 deletions

View File

@ -3,18 +3,31 @@
import sys import sys
import string import string
def dis(): def dis(x=None):
tb = sys.last_traceback if not x:
while tb.tb_next: tb = tb.tb_next distb()
distb(tb) else:
if hasattr(x, 'im_func'):
x = x.im_func
if hasattr(x, 'func_code'):
x = x.func_code
if hasattr(x, 'co_code'):
disassemble(x)
else:
raise ValueError, \
"don't know how to disassemble %s objects" % \
type(x).__name__
def distb(tb): def distb(tb=None):
if not tb:
try:
tb = sys.last_traceback
except AttributeError:
raise RuntimeError, "no last traceback to disassemble"
while tb.tb_next: tb = tb.tb_next
disassemble(tb.tb_frame.f_code, tb.tb_lasti) disassemble(tb.tb_frame.f_code, tb.tb_lasti)
def disco(co): def disassemble(co, lasti=-1):
disassemble(co, -1)
def disassemble(co, lasti):
code = co.co_code code = co.co_code
labels = findlabels(code) labels = findlabels(code)
n = len(code) n = len(code)
@ -46,6 +59,8 @@ def disassemble(co, lasti):
print '(' + cmp_op[oparg] + ')', print '(' + cmp_op[oparg] + ')',
print print
disco = disassemble
def findlabels(code): def findlabels(code):
labels = [] labels = []
n = len(code) n = len(code)