Sat, 10 Oct 2015 12:44:52 +0200
Merged with coverage.py update.
4489
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
1 | # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
2 | # For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
3 | |
3495 | 4 | """Control of and utilities for debugging.""" |
5 | ||
4489
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
6 | import inspect |
3495 | 7 | import os |
4489
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
8 | import sys |
3495 | 9 | |
10 | ||
11 | # When debugging, it can be helpful to force some options, especially when | |
12 | # debugging the configuration mechanisms you usually use to control debugging! | |
13 | # This is a list of forced debugging options. | |
14 | FORCED_DEBUG = [] | |
15 | ||
4489
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
16 | # A hack for debugging testing in sub-processes. |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
17 | _TEST_NAME_FILE = "" # "/tmp/covtest.txt" |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
18 | |
3495 | 19 | |
20 | class DebugControl(object): | |
21 | """Control and output for debugging.""" | |
22 | ||
23 | def __init__(self, options, output): | |
24 | """Configure the options and output file for debugging.""" | |
25 | self.options = options | |
26 | self.output = output | |
27 | ||
4489
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
28 | def __repr__(self): |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
29 | return "<DebugControl options=%r output=%r>" % (self.options, self.output) |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
30 | |
3495 | 31 | def should(self, option): |
32 | """Decide whether to output debug information in category `option`.""" | |
33 | return (option in self.options or option in FORCED_DEBUG) | |
34 | ||
35 | def write(self, msg): | |
36 | """Write a line of debug output.""" | |
37 | if self.should('pid'): | |
38 | msg = "pid %5d: %s" % (os.getpid(), msg) | |
39 | self.output.write(msg+"\n") | |
4489
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
40 | if self.should('callers'): |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
41 | dump_stack_frames(self.output) |
3495 | 42 | self.output.flush() |
43 | ||
4489
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
44 | def write_formatted_info(self, header, info): |
3495 | 45 | """Write a sequence of (label,data) pairs nicely.""" |
4489
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
46 | self.write(info_header(header)) |
3495 | 47 | for line in info_formatter(info): |
48 | self.write(" %s" % line) | |
49 | ||
50 | ||
4489
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
51 | def info_header(label): |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
52 | """Make a nice header string.""" |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
53 | return "--{0:-<60s}".format(" "+label+" ") |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
54 | |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
55 | |
3495 | 56 | def info_formatter(info): |
57 | """Produce a sequence of formatted lines from info. | |
58 | ||
59 | `info` is a sequence of pairs (label, data). The produced lines are | |
60 | nicely formatted, ready to print. | |
61 | ||
62 | """ | |
4489
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
63 | info = list(info) |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
64 | if not info: |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
65 | return |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
66 | label_len = max(len(l) for l, _d in info) |
3495 | 67 | for label, data in info: |
68 | if data == []: | |
69 | data = "-none-" | |
4489
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
70 | if isinstance(data, (list, set, tuple)): |
3495 | 71 | prefix = "%*s:" % (label_len, label) |
72 | for e in data: | |
73 | yield "%*s %s" % (label_len+1, prefix, e) | |
74 | prefix = "" | |
75 | else: | |
76 | yield "%*s: %s" % (label_len, label, data) | |
3499
f2d4b02c7e88
Modified the Python2 coverage files to include the Python2 eflags line and fixed an issue in both variants.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
3495
diff
changeset
|
77 | |
4489
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
78 | |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
79 | def short_stack(): # pragma: debugging |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
80 | """Return a string summarizing the call stack. |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
81 | |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
82 | The string is multi-line, with one line per stack frame. Each line shows |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
83 | the function name, the file name, and the line number: |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
84 | |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
85 | ... |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
86 | start_import_stop : /Users/ned/coverage/trunk/tests/coveragetest.py @95 |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
87 | import_local_file : /Users/ned/coverage/trunk/tests/coveragetest.py @81 |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
88 | import_local_file : /Users/ned/coverage/trunk/coverage/backward.py @159 |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
89 | ... |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
90 | |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
91 | """ |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
92 | stack = inspect.stack()[:0:-1] |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
93 | return "\n".join("%30s : %s @%d" % (t[3], t[1], t[2]) for t in stack) |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
94 | |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
95 | |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
96 | def dump_stack_frames(out=None): # pragma: debugging |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
97 | """Print a summary of the stack to stdout, or some place else.""" |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
98 | out = out or sys.stdout |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
99 | out.write(short_stack()) |
d0d6e4ad31bd
Updated coverage to 4.0 (breaks with Python 3.2 support).
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3499
diff
changeset
|
100 | out.write("\n") |
4491
0d8612e24fef
Merged with coverage.py update.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
4489
diff
changeset
|
101 | |
0d8612e24fef
Merged with coverage.py update.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
4489
diff
changeset
|
102 | # |
0d8612e24fef
Merged with coverage.py update.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
4489
diff
changeset
|
103 | # eflag: FileType = Python2 |