summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1997-01-17 20:05:04 (GMT)
committerGuido van Rossum <guido@python.org>1997-01-17 20:05:04 (GMT)
commitbd30795192f1e74ef1fae06eefc1f225193f49fb (patch)
treeb6c512defb84ed41ebce9e271ec118a812fa19fb /Lib
parentc905fffa1524694ca5600c301230b4b707469e82 (diff)
downloadcpython-bd30795192f1e74ef1fae06eefc1f225193f49fb.zip
cpython-bd30795192f1e74ef1fae06eefc1f225193f49fb.tar.gz
cpython-bd30795192f1e74ef1fae06eefc1f225193f49fb.tar.bz2
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.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/dis.py35
1 files changed, 25 insertions, 10 deletions
diff --git a/Lib/dis.py b/Lib/dis.py
index 2324c8c..50e6c1d 100644
--- a/Lib/dis.py
+++ b/Lib/dis.py
@@ -3,18 +3,31 @@
import sys
import string
-def dis():
- tb = sys.last_traceback
- while tb.tb_next: tb = tb.tb_next
- distb(tb)
-
-def distb(tb):
+def dis(x=None):
+ if not x:
+ distb()
+ 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=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)
-def disco(co):
- disassemble(co, -1)
-
-def disassemble(co, lasti):
+def disassemble(co, lasti=-1):
code = co.co_code
labels = findlabels(code)
n = len(code)
@@ -46,6 +59,8 @@ def disassemble(co, lasti):
print '(' + cmp_op[oparg] + ')',
print
+disco = disassemble
+
def findlabels(code):
labels = []
n = len(code)