Skip to content

Commit adc857a

Browse files
committed
move risky things out of the constructor and into the method called in a try catch block
1 parent 76c8865 commit adc857a

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

coverage/jsonreport.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ def __init__(self, coverage):
2020
self.config = self.coverage.config
2121
self.total = Numbers()
2222
self.report_data = {}
23-
self.data = self.coverage.get_data()
24-
self.has_arcs = self.data.has_arcs()
2523

2624
def report(self, morfs, outfile=None):
2725
"""Generate a json report for `morfs`.
@@ -32,17 +30,19 @@ def report(self, morfs, outfile=None):
3230
3331
"""
3432
outfile = outfile or sys.stdout
35-
self.data.set_query_contexts(self.config.report_contexts)
33+
coverage_data = self.coverage.get_data()
34+
coverage_data.set_query_contexts(self.config.report_contexts)
3635
self.report_data["meta"] = {
3736
"version": __version__,
3837
"timestamp": datetime.datetime.now().isoformat(),
39-
"branch_coverage": self.data.has_arcs(),
38+
"branch_coverage": coverage_data.has_arcs(),
4039
"show_contexts": self.config.json_show_contexts,
4140
}
4241

4342
measured_files = {}
4443
for file_reporter, analysis in get_analysis_to_report(self.coverage, morfs):
4544
measured_files[file_reporter.relative_filename()] = self.report_one_file(
45+
coverage_data,
4646
file_reporter,
4747
analysis
4848
)
@@ -56,7 +56,7 @@ def report(self, morfs, outfile=None):
5656
'missing_lines': self.total.n_missing,
5757
}
5858

59-
if self.data.has_arcs():
59+
if coverage_data.has_arcs():
6060
self.report_data["totals"].update({
6161
'num_branches': self.total.n_branches,
6262
'num_partial_branches': self.total.n_partial_branches,
@@ -70,7 +70,7 @@ def report(self, morfs, outfile=None):
7070

7171
return self.total.n_statements and self.total.pc_covered
7272

73-
def report_one_file(self, file_reporter, analysis):
73+
def report_one_file(self, coverage_data, file_reporter, analysis):
7474
"""Extract the relevant report data for a single file"""
7575
nums = analysis.numbers
7676
self.total += nums
@@ -89,7 +89,7 @@ def report_one_file(self, file_reporter, analysis):
8989
reported_file['contexts'] = analysis.data.contexts_by_lineno(
9090
file_reporter.filename
9191
)
92-
if self.data.has_arcs():
92+
if coverage_data.has_arcs():
9393
reported_file['summary'].update({
9494
'num_branches': nums.n_branches,
9595
'num_partial_branches': nums.n_partial_branches,

coverage/xmlreport.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ def __init__(self, coverage):
4444
self.source_paths.add(files.canonical_filename(src))
4545
self.packages = {}
4646
self.xml_out = None
47-
self.data = coverage.get_data()
48-
self.has_arcs = self.data.has_arcs()
4947

5048
def report(self, morfs, outfile=None):
5149
"""Generate a Cobertura-compatible XML report for `morfs`.
@@ -57,6 +55,7 @@ def report(self, morfs, outfile=None):
5755
"""
5856
# Initial setup.
5957
outfile = outfile or sys.stdout
58+
has_arcs = self.coverage.get_data().has_arcs()
6059

6160
# Create the DOM that will store the data.
6261
impl = xml.dom.minidom.getDOMImplementation()
@@ -73,7 +72,7 @@ def report(self, morfs, outfile=None):
7372

7473
# Call xml_file for each file in the data.
7574
for fr, analysis in get_analysis_to_report(self.coverage, morfs):
76-
self.xml_file(fr, analysis)
75+
self.xml_file(fr, analysis, has_arcs)
7776

7877
xsources = self.xml_out.createElement("sources")
7978
xcoverage.appendChild(xsources)
@@ -102,7 +101,7 @@ def report(self, morfs, outfile=None):
102101
xclasses.appendChild(class_elt)
103102
xpackage.setAttribute("name", pkg_name.replace(os.sep, '.'))
104103
xpackage.setAttribute("line-rate", rate(lhits, lnum))
105-
if self.has_arcs:
104+
if has_arcs:
106105
branch_rate = rate(bhits, bnum)
107106
else:
108107
branch_rate = "0"
@@ -117,7 +116,7 @@ def report(self, morfs, outfile=None):
117116
xcoverage.setAttribute("lines-valid", str(lnum_tot))
118117
xcoverage.setAttribute("lines-covered", str(lhits_tot))
119118
xcoverage.setAttribute("line-rate", rate(lhits_tot, lnum_tot))
120-
if self.has_arcs:
119+
if has_arcs:
121120
xcoverage.setAttribute("branches-valid", str(bnum_tot))
122121
xcoverage.setAttribute("branches-covered", str(bhits_tot))
123122
xcoverage.setAttribute("branch-rate", rate(bhits_tot, bnum_tot))
@@ -138,7 +137,7 @@ def report(self, morfs, outfile=None):
138137
pct = 100.0 * (lhits_tot + bhits_tot) / denom
139138
return pct
140139

141-
def xml_file(self, fr, analysis):
140+
def xml_file(self, fr, analysis, has_arcs):
142141
"""Add to the XML report for a single file."""
143142

144143
# Create the 'lines' and 'package' XML elements, which
@@ -182,7 +181,7 @@ def xml_file(self, fr, analysis):
182181
# executed? If so, that should be recorded here.
183182
xline.setAttribute("hits", str(int(line not in analysis.missing)))
184183

185-
if self.has_arcs:
184+
if has_arcs:
186185
if line in branch_stats:
187186
total, taken = branch_stats[line]
188187
xline.setAttribute("branch", "true")
@@ -198,7 +197,7 @@ def xml_file(self, fr, analysis):
198197
class_lines = len(analysis.statements)
199198
class_hits = class_lines - len(analysis.missing)
200199

201-
if self.has_arcs:
200+
if has_arcs:
202201
class_branches = sum(t for t, k in branch_stats.values())
203202
missing_branches = sum(t - k for t, k in branch_stats.values())
204203
class_br_hits = class_branches - missing_branches
@@ -208,7 +207,7 @@ def xml_file(self, fr, analysis):
208207

209208
# Finalize the statistics that are collected in the XML DOM.
210209
xclass.setAttribute("line-rate", rate(class_hits, class_lines))
211-
if self.has_arcs:
210+
if has_arcs:
212211
branch_rate = rate(class_br_hits, class_branches)
213212
else:
214213
branch_rate = "0"

0 commit comments

Comments
 (0)