summaryrefslogtreecommitdiff
path: root/lab
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2021-11-03 19:04:02 -0400
committerNed Batchelder <ned@nedbatchelder.com>2021-11-03 19:04:02 -0400
commit8aa27024bdeb8950d61455b37a18bd508bf37904 (patch)
treeed96a2c7ed70cc7701fdb651a6877e3c0c44eb02 /lab
parentcedd332fd80945d2fb42655a03feb47be6cbaa4c (diff)
downloadpython-coveragepy-git-8aa27024bdeb8950d61455b37a18bd508bf37904.tar.gz
debug(lab): renovate lab/parser.py
Diffstat (limited to 'lab')
-rw-r--r--lab/disgen.py205
-rw-r--r--lab/parser.py67
2 files changed, 40 insertions, 232 deletions
diff --git a/lab/disgen.py b/lab/disgen.py
deleted file mode 100644
index 055f4983..00000000
--- a/lab/disgen.py
+++ /dev/null
@@ -1,205 +0,0 @@
-"""Disassembler of Python bytecode into mnemonics."""
-
-# Adapted from stdlib dis.py, but returns structured information
-# instead of printing to stdout.
-
-import sys
-import collections
-
-from opcode import *
-from opcode import __all__ as _opcodes_all
-
-__all__ = ["dis", "disassemble", "distb", "disco",
- "findlinestarts", "findlabels"] + _opcodes_all
-del _opcodes_all
-
-def dis(x=None):
- for disline in disgen(x):
- if disline.first and disline.offset > 0:
- print()
- print(format_dis_line(disline))
-
-def format_dis_line(disline):
- if disline.first:
- lineno = "%3d" % disline.lineno
- else:
- lineno = " "
- if disline.target:
- label = ">>"
- else:
- label = " "
- if disline.oparg is not None:
- oparg = repr(disline.oparg)
- else:
- oparg = ""
- return "%s %s %4r %-20s %5s %s" % (lineno, label, disline.offset, disline.opcode, oparg, disline.argstr)
-
-def disgen(x=None):
- """Disassemble methods, functions, or code.
-
- With no argument, disassemble the last traceback.
-
- """
- if x is None:
- return distb()
- if hasattr(x, 'im_func'):
- x = x.im_func
- if hasattr(x, 'func_code'):
- x = x.func_code
- if hasattr(x, 'co_code'):
- return disassemble(x)
- else:
- raise TypeError(
- "don't know how to disassemble %s objects" %
- type(x).__name__
- )
-
-def distb(tb=None):
- """Disassemble a traceback (default: last traceback)."""
- if tb is None:
- try:
- tb = sys.last_traceback
- except AttributeError:
- raise RuntimeError("no last traceback to disassemble")
- while tb.tb_next:
- tb = tb.tb_next
- return disassemble(tb.tb_frame.f_code, tb.tb_lasti)
-
-DisLine = collections.namedtuple(
- 'DisLine',
- "lineno first target offset opcode oparg argstr"
- )
-
-def disassemble(co, lasti=-1):
- """Disassemble a code object."""
- code = co.co_code
- labels = findlabels(code)
- linestarts = dict(findlinestarts(co))
- n = len(code)
- i = 0
- extended_arg = 0
- free = None
-
- dislines = []
- lineno = linestarts[0]
-
- while i < n:
- op = byte_from_code(code, i)
- first = i in linestarts
- if first:
- lineno = linestarts[i]
-
- #if i == lasti: print '-->',
- #else: print ' ',
- target = i in labels
- offset = i
- opcode = opname[op]
- i = i+1
- if op >= HAVE_ARGUMENT:
- oparg = byte_from_code(code, i) + byte_from_code(code, i+1)*256 + extended_arg
- extended_arg = 0
- i = i+2
- if op == EXTENDED_ARG:
- extended_arg = oparg*65536
- if op in hasconst:
- argstr = '(' + repr(co.co_consts[oparg]) + ')'
- elif op in hasname:
- argstr = '(' + co.co_names[oparg] + ')'
- elif op in hasjabs:
- argstr = '(-> ' + repr(oparg) + ')'
- elif op in hasjrel:
- argstr = '(-> ' + repr(i + oparg) + ')'
- elif op in haslocal:
- argstr = '(' + co.co_varnames[oparg] + ')'
- elif op in hascompare:
- argstr = '(' + cmp_op[oparg] + ')'
- elif op in hasfree:
- if free is None:
- free = co.co_cellvars + co.co_freevars
- argstr = '(' + free[oparg] + ')'
- else:
- argstr = ""
- else:
- oparg = None
- argstr = ""
- yield DisLine(lineno=lineno, first=first, target=target, offset=offset, opcode=opcode, oparg=oparg, argstr=argstr)
-
-
-def byte_from_code(code, i):
- byte = code[i]
- if not isinstance(byte, int):
- byte = ord(byte)
- return byte
-
-def findlabels(code):
- """Detect all offsets in a bytecode which are jump targets.
-
- Return the list of offsets.
-
- """
- labels = []
- n = len(code)
- i = 0
- while i < n:
- op = byte_from_code(code, i)
- i = i+1
- if op >= HAVE_ARGUMENT:
- oparg = byte_from_code(code, i) + byte_from_code(code, i+1)*256
- i = i+2
- label = -1
- if op in hasjrel:
- label = i+oparg
- elif op in hasjabs:
- label = oparg
- if label >= 0:
- if label not in labels:
- labels.append(label)
- return labels
-
-def findlinestarts(code):
- """Find the offsets in a bytecode which are start of lines in the source.
-
- Generate pairs (offset, lineno) as described in Python/compile.c.
-
- """
- byte_increments = [byte_from_code(code.co_lnotab, i) for i in range(0, len(code.co_lnotab), 2)]
- line_increments = [byte_from_code(code.co_lnotab, i) for i in range(1, len(code.co_lnotab), 2)]
-
- lastlineno = None
- lineno = code.co_firstlineno
- addr = 0
- for byte_incr, line_incr in zip(byte_increments, line_increments):
- if byte_incr:
- if lineno != lastlineno:
- yield (addr, lineno)
- lastlineno = lineno
- addr += byte_incr
- lineno += line_incr
- if lineno != lastlineno:
- yield (addr, lineno)
-
-def _test():
- """Simple test program to disassemble a file."""
- if sys.argv[1:]:
- if sys.argv[2:]:
- sys.stderr.write("usage: python dis.py [-|file]\n")
- sys.exit(2)
- fn = sys.argv[1]
- if not fn or fn == "-":
- fn = None
- else:
- fn = None
- if fn is None:
- f = sys.stdin
- else:
- f = open(fn)
- source = f.read()
- if fn is not None:
- f.close()
- else:
- fn = "<stdin>"
- code = compile(source, fn, "exec")
- dis(code)
-
-if __name__ == "__main__":
- _test()
diff --git a/lab/parser.py b/lab/parser.py
index bbf92508..19cb94a8 100644
--- a/lab/parser.py
+++ b/lab/parser.py
@@ -5,14 +5,14 @@
import collections
+import dis
import glob
import optparse
import os
import re
import sys
import textwrap
-
-import disgen
+import types
from coverage.parser import PythonParser
from coverage.python import get_python_source
@@ -81,7 +81,7 @@ class ParserMain:
if options.dis:
print("Main code:")
- self.disassemble(pyparser.byte_parser)
+ disassemble(pyparser)
arcs = pyparser.arcs()
@@ -120,30 +120,6 @@ class ParserMain:
print("%4d %s%s %s" % (lineno, "".join(marks), a, ltext))
- def disassemble(self, byte_parser):
- """Disassemble code, for ad-hoc experimenting."""
-
- for bp in byte_parser.child_parsers():
- if bp.text:
- srclines = bp.text.splitlines()
- else:
- srclines = None
- print("\n%s: " % bp.code)
- upto = None
- for disline in disgen.disgen(bp.code):
- if disline.first:
- if srclines:
- upto = upto or disline.lineno-1
- while upto <= disline.lineno-1:
- print("{:>100}{}".format("", srclines[upto]))
- upto += 1
- elif disline.offset > 0:
- print("")
- line = disgen.format_dis_line(disline)
- print(f"{line:<70}")
-
- print("")
-
def arc_ascii_art(self, arcs):
"""Draw arcs as ascii art.
@@ -187,6 +163,43 @@ class ParserMain:
return arc_chars
+def all_code_objects(code):
+ """Iterate over all the code objects in `code`."""
+ stack = [code]
+ while stack:
+ # We're going to return the code object on the stack, but first
+ # push its children for later returning.
+ code = stack.pop()
+ stack.extend(c for c in code.co_consts if isinstance(c, types.CodeType))
+ yield code
+
+
+def disassemble(pyparser):
+ """Disassemble code, for ad-hoc experimenting."""
+
+ code = compile(pyparser.text, "", "exec")
+ for code_obj in all_code_objects(code):
+ if pyparser.text:
+ srclines = pyparser.text.splitlines()
+ else:
+ srclines = None
+ print("\n%s: " % code_obj)
+ upto = None
+ for inst in dis.get_instructions(code_obj):
+ if inst.starts_line is not None:
+ if srclines:
+ upto = upto or inst.starts_line - 1
+ while upto <= inst.starts_line - 1:
+ print("{:>100}{}".format("", srclines[upto]))
+ upto += 1
+ elif inst.offset > 0:
+ print("")
+ line = inst._disassemble()
+ print(f"{line:<70}")
+
+ print("")
+
+
def set_char(s, n, c):
"""Set the nth char of s to be c, extending s if needed."""
s = s.ljust(n)