1 """Results of coverage measurement."""
5 from coverage
.backward
import iitems
, set, sorted # pylint: disable=W0622
6 from coverage
.misc
import format_lines
, join_regex
, NoSource
7 from coverage
.parser
import CodeParser
10 class Analysis(object):
11 """The results of analyzing a code unit."""
13 def __init__(self
, cov
, code_unit
):
15 self
.code_unit
= code_unit
17 self
.filename
= self
.code_unit
.filename
18 actual_filename
, source
= self
.find_source(self
.filename
)
20 self
.parser
= CodeParser(
21 text
=source
, filename
=actual_filename
,
22 exclude
=self
.coverage
._exclude
_regex
('exclude')
24 self
.statements
, self
.excluded
= self
.parser
.parse_source()
26 # Identify missing statements.
27 executed
= self
.coverage
.data
.executed_lines(self
.filename
)
28 exec1
= self
.parser
.first_lines(executed
)
29 self
.missing
= self
.statements
- exec1
31 if self
.coverage
.data
.has_arcs():
32 self
.no_branch
= self
.parser
.lines_matching(
33 join_regex(self
.coverage
.config
.partial_list
),
34 join_regex(self
.coverage
.config
.partial_always_list
)
36 n_branches
= self
.total_branches()
37 mba
= self
.missing_branch_arcs()
38 n_partial_branches
= sum(
39 [len(v
) for k
,v
in iitems(mba
) if k
not in self
.missing
]
41 n_missing_branches
= sum([len(v
) for k
,v
in iitems(mba
)])
43 n_branches
= n_partial_branches
= n_missing_branches
= 0
44 self
.no_branch
= set()
46 self
.numbers
= Numbers(
48 n_statements
=len(self
.statements
),
49 n_excluded
=len(self
.excluded
),
50 n_missing
=len(self
.missing
),
51 n_branches
=n_branches
,
52 n_partial_branches
=n_partial_branches
,
53 n_missing_branches
=n_missing_branches
,
56 def find_source(self
, filename
):
57 """Find the source for `filename`.
59 Returns two values: the actual filename, and the source.
61 The source returned depends on which of these cases holds:
63 * The filename seems to be a non-source file: returns None
65 * The filename is a source file, and actually exists: returns None.
67 * The filename is a source file, and is in a zip file or egg:
70 * The filename is a source file, but couldn't be found: raises
76 base
, ext
= os
.path
.splitext(filename
)
78 '.py': ['.py', '.pyw'],
81 try_exts
= TRY_EXTS
.get(ext
)
85 for try_ext
in try_exts
:
86 try_filename
= base
+ try_ext
87 if os
.path
.exists(try_filename
):
88 return try_filename
, None
89 source
= self
.coverage
.file_locator
.get_zip_data(try_filename
)
91 return try_filename
, source
92 raise NoSource("No source for code: '%s'" % filename
)
94 def missing_formatted(self
):
95 """The missing line numbers, formatted nicely.
97 Returns a string like "1-2, 5-11, 13-14".
100 return format_lines(self
.statements
, self
.missing
)
103 """Were arcs measured in this result?"""
104 return self
.coverage
.data
.has_arcs()
106 def arc_possibilities(self
):
107 """Returns a sorted list of the arcs in the code."""
108 arcs
= self
.parser
.arcs()
111 def arcs_executed(self
):
112 """Returns a sorted list of the arcs actually executed in the code."""
113 executed
= self
.coverage
.data
.executed_arcs(self
.filename
)
114 m2fl
= self
.parser
.first_line
115 executed
= [(m2fl(l1
), m2fl(l2
)) for (l1
,l2
) in executed
]
116 return sorted(executed
)
118 def arcs_missing(self
):
119 """Returns a sorted list of the arcs in the code not executed."""
120 possible
= self
.arc_possibilities()
121 executed
= self
.arcs_executed()
125 and p
[0] not in self
.no_branch
127 return sorted(missing
)
129 def arcs_unpredicted(self
):
130 """Returns a sorted list of the executed arcs missing from the code."""
131 possible
= self
.arc_possibilities()
132 executed
= self
.arcs_executed()
133 # Exclude arcs here which connect a line to itself. They can occur
134 # in executed data in some cases. This is where they can cause
135 # trouble, and here is where it's the least burden to remove them.
141 return sorted(unpredicted
)
143 def branch_lines(self
):
144 """Returns a list of line numbers that have more than one exit."""
145 exit_counts
= self
.parser
.exit_counts()
146 return [l1
for l1
,count
in iitems(exit_counts
) if count
> 1]
148 def total_branches(self
):
149 """How many total branches are there?"""
150 exit_counts
= self
.parser
.exit_counts()
151 return sum([count
for count
in exit_counts
.values() if count
> 1])
153 def missing_branch_arcs(self
):
154 """Return arcs that weren't executed from branch lines.
156 Returns {l1:[l2a,l2b,...], ...}
159 missing
= self
.arcs_missing()
160 branch_lines
= set(self
.branch_lines())
162 for l1
, l2
in missing
:
163 if l1
in branch_lines
:
169 def branch_stats(self
):
170 """Get stats about branches.
172 Returns a dict mapping line numbers to a tuple:
173 (total_exits, taken_exits).
176 exit_counts
= self
.parser
.exit_counts()
177 missing_arcs
= self
.missing_branch_arcs()
179 for lnum
in self
.branch_lines():
180 exits
= exit_counts
[lnum
]
182 missing
= len(missing_arcs
[lnum
])
185 stats
[lnum
] = (exits
, exits
- missing
)
189 class Numbers(object):
190 """The numerical results of measuring coverage.
192 This holds the basic statistics from `Analysis`, and is used to roll
193 up statistics across files.
196 # A global to determine the precision on coverage percentages, the number
199 _near0
= 1.0 # These will change when _precision is changed.
202 def __init__(self
, n_files
=0, n_statements
=0, n_excluded
=0, n_missing
=0,
203 n_branches
=0, n_partial_branches
=0, n_missing_branches
=0
205 self
.n_files
= n_files
206 self
.n_statements
= n_statements
207 self
.n_excluded
= n_excluded
208 self
.n_missing
= n_missing
209 self
.n_branches
= n_branches
210 self
.n_partial_branches
= n_partial_branches
211 self
.n_missing_branches
= n_missing_branches
213 def set_precision(cls
, precision
):
214 """Set the number of decimal places used to report percentages."""
215 assert 0 <= precision
< 10
216 cls
._precision
= precision
217 cls
._near
0 = 1.0 / 10**precision
218 cls
._near
100 = 100.0 - cls
._near
0
219 set_precision
= classmethod(set_precision
)
221 def _get_n_executed(self
):
222 """Returns the number of executed statements."""
223 return self
.n_statements
- self
.n_missing
224 n_executed
= property(_get_n_executed
)
226 def _get_n_executed_branches(self
):
227 """Returns the number of executed branches."""
228 return self
.n_branches
- self
.n_missing_branches
229 n_executed_branches
= property(_get_n_executed_branches
)
231 def _get_pc_covered(self
):
232 """Returns a single percentage value for coverage."""
233 if self
.n_statements
> 0:
234 pc_cov
= (100.0 * (self
.n_executed
+ self
.n_executed_branches
) /
235 (self
.n_statements
+ self
.n_branches
))
239 pc_covered
= property(_get_pc_covered
)
241 def _get_pc_covered_str(self
):
242 """Returns the percent covered, as a string, without a percent sign.
244 Note that "0" is only returned when the value is truly zero, and "100"
245 is only returned when the value is truly 100. Rounding can never
246 result in either "0" or "100".
250 if 0 < pc
< self
._near
0:
252 elif self
._near
100 < pc
< 100:
255 pc
= round(pc
, self
._precision
)
256 return "%.*f" % (self
._precision
, pc
)
257 pc_covered_str
= property(_get_pc_covered_str
)
259 def pc_str_width(cls
):
260 """How many characters wide can pc_covered_str be?"""
262 if cls
._precision
> 0:
263 width
+= 1 + cls
._precision
265 pc_str_width
= classmethod(pc_str_width
)
267 def __add__(self
, other
):
269 nums
.n_files
= self
.n_files
+ other
.n_files
270 nums
.n_statements
= self
.n_statements
+ other
.n_statements
271 nums
.n_excluded
= self
.n_excluded
+ other
.n_excluded
272 nums
.n_missing
= self
.n_missing
+ other
.n_missing
273 nums
.n_branches
= self
.n_branches
+ other
.n_branches
274 nums
.n_partial_branches
= (
275 self
.n_partial_branches
+ other
.n_partial_branches
277 nums
.n_missing_branches
= (
278 self
.n_missing_branches
+ other
.n_missing_branches
282 def __radd__(self
, other
):
283 # Implementing 0+Numbers allows us to sum() a list of Numbers.
286 return NotImplemented