diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2015-04-20 12:21:15 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2015-04-20 12:21:15 -0400 |
commit | 046722d4b0a89968cba973c3bfce5a7665853c7b (patch) | |
tree | ebeccdfc74b8cd412bdf8d20f2318aab7eb09cd9 /lab | |
parent | a1c737c03dbb0c1dfee85d436ddcaeafc7a5dbfe (diff) | |
parent | 30a6a037158eebf062c7da735e0cb905a489d21c (diff) | |
download | python-coveragepy-046722d4b0a89968cba973c3bfce5a7665853c7b.tar.gz |
Merge issue-324 fix
Diffstat (limited to 'lab')
-rw-r--r-- | lab/parser.py | 18 | ||||
-rw-r--r-- | lab/run_trace.py | 32 | ||||
-rw-r--r-- | lab/sample.py | 5 | ||||
-rw-r--r-- | lab/trace_sample.py | 57 |
4 files changed, 41 insertions, 71 deletions
diff --git a/lab/parser.py b/lab/parser.py index 1783468..662183a 100644 --- a/lab/parser.py +++ b/lab/parser.py @@ -9,8 +9,8 @@ from optparse import OptionParser import disgen from coverage.misc import CoverageException -from coverage.files import get_python_source from coverage.parser import ByteParser, PythonParser +from coverage.python import get_python_source opcode_counts = collections.Counter() @@ -82,7 +82,7 @@ class ParserMain(object): self.disassemble(bp, histogram=options.histogram) arcs = bp._all_arcs() - if options.chunks and not options.dis: + if options.chunks:# and not options.dis: chunks = bp._all_chunks() if options.recursive: print("%6d: %s" % (len(chunks), filename)) @@ -116,7 +116,7 @@ class ParserMain(object): m2 = 'C' if lineno in cp.excluded: m3 = 'x' - a = arc_chars.get(lineno, '').ljust(arc_width) + a = arc_chars[lineno].ljust(arc_width) print("%4d %s%s%s%s%s %s" % (lineno, m0, m1, m2, m3, a, ltext) ) @@ -162,12 +162,12 @@ class ParserMain(object): dictionary mapping line numbers to ascii strings to draw for that line. """ - arc_chars = {} + arc_chars = collections.defaultdict(str) for lfrom, lto in sorted(arcs): if lfrom < 0: - arc_chars[lto] = arc_chars.get(lto, '') + 'v' + arc_chars[lto] += 'v' elif lto < 0: - arc_chars[lfrom] = arc_chars.get(lfrom, '') + '^' + arc_chars[lfrom] += '^' else: if lfrom == lto - 1: # Don't show obvious arcs. @@ -176,7 +176,7 @@ class ParserMain(object): l1, l2 = lfrom, lto else: l1, l2 = lto, lfrom - w = max([len(arc_chars.get(l, '')) for l in range(l1, l2+1)]) + w = max(len(arc_chars[l]) for l in range(l1, l2+1)) for l in range(l1, l2+1): if l == lfrom: ch = '<' @@ -184,11 +184,11 @@ class ParserMain(object): ch = '>' else: ch = '|' - arc_chars[l] = arc_chars.get(l, '').ljust(w) + ch + arc_chars[l] = arc_chars[l].ljust(w) + ch arc_width = 0 if arc_chars: - arc_width = max([len(a) for a in arc_chars.values()]) + arc_width = max(len(a) for a in arc_chars.values()) else: arc_width = 0 diff --git a/lab/run_trace.py b/lab/run_trace.py new file mode 100644 index 0000000..3822a80 --- /dev/null +++ b/lab/run_trace.py @@ -0,0 +1,32 @@ +"""Run a simple trace function on a file of Python code.""" + +import os, sys + +nest = 0 + +def trace(frame, event, arg): + global nest + + if nest is None: + # This can happen when Python is shutting down. + return None + + print "%s%s %s %d @%d" % ( + " " * nest, + event, + os.path.basename(frame.f_code.co_filename), + frame.f_lineno, + frame.f_lasti, + ) + + if event == 'call': + nest += 1 + if event == 'return': + nest -= 1 + + return trace + +the_program = sys.argv[1] + +sys.settrace(trace) +execfile(the_program) diff --git a/lab/sample.py b/lab/sample.py deleted file mode 100644 index bb62848..0000000 --- a/lab/sample.py +++ /dev/null @@ -1,5 +0,0 @@ -a, b = 1, 0 -if a or b or fn(): - # Hey - a = 3 -d = 4 diff --git a/lab/trace_sample.py b/lab/trace_sample.py deleted file mode 100644 index 3f81919..0000000 --- a/lab/trace_sample.py +++ /dev/null @@ -1,57 +0,0 @@ -import os, sys - -global nest -nest = 0 - -def trace(frame, event, arg): - #if event == 'line': - global nest - - print "%s%s %s %d" % ( - " " * nest, - event, - os.path.basename(frame.f_code.co_filename), - frame.f_lineno, - ) - - if event == 'call': - nest += 1 - if event == 'return': - nest -= 1 - - return trace - -def trace2(frame, event, arg): - #if event == 'line': - global nest - - print "2: %s%s %s %d" % ( - " " * nest, - event, - os.path.basename(frame.f_code.co_filename), - frame.f_lineno, - ) - - if event == 'call': - nest += 1 - if event == 'return': - nest -= 1 - - return trace2 - -sys.settrace(trace) - -def bar(): - print "nar" - -a = 26 -def foo(n): - a = 28 - sys.settrace(sys.gettrace()) - bar() - a = 30 - return 2*n - -print foo(a) -#import sample -#import littleclass |