bpo-31908: Fix output of cover files for trace module command-line to… · python/cpython@a607f8b
@@ -79,9 +79,6 @@ def _unsettrace():
79798080PRAGMA_NOCOVER = "#pragma NO COVER"
818182-# Simple rx to find lines with no code.
83-rx_blank = re.compile(r'^\s*(#.*)?$')
84-8582class _Ignore:
8683def __init__(self, modules=None, dirs=None):
8784self._mods = set() if not modules else set(modules)
@@ -284,16 +281,15 @@ def write_results(self, show_missing=True, summary=False, coverdir=None):
284281lnotab = _find_executable_linenos(filename)
285282else:
286283lnotab = {}
287-if lnotab:
288-source = linecache.getlines(filename)
289-coverpath = os.path.join(dir, modulename + ".cover")
290-with open(filename, 'rb') as fp:
291-encoding, _ = tokenize.detect_encoding(fp.readline)
292-n_hits, n_lines = self.write_results_file(coverpath, source,
293-lnotab, count, encoding)
294-if summary and n_lines:
295-percent = int(100 * n_hits / n_lines)
296-sums[modulename] = n_lines, percent, modulename, filename
284+source = linecache.getlines(filename)
285+coverpath = os.path.join(dir, modulename + ".cover")
286+with open(filename, 'rb') as fp:
287+encoding, _ = tokenize.detect_encoding(fp.readline)
288+n_hits, n_lines = self.write_results_file(coverpath, source,
289+lnotab, count, encoding)
290+if summary and n_lines:
291+percent = int(100 * n_hits / n_lines)
292+sums[modulename] = n_lines, percent, modulename, filename
297293298294299295if summary and sums:
@@ -312,6 +308,7 @@ def write_results(self, show_missing=True, summary=False, coverdir=None):
312308313309def write_results_file(self, path, lines, lnotab, lines_hit, encoding=None):
314310"""Return a coverage results file in path."""
311+# ``lnotab`` is a dict of executable lines, or a line number "table"
315312316313try:
317314outfile = open(path, "w", encoding=encoding)
@@ -330,17 +327,13 @@ def write_results_file(self, path, lines, lnotab, lines_hit, encoding=None):
330327outfile.write("%5d: " % lines_hit[lineno])
331328n_hits += 1
332329n_lines += 1
333-elif rx_blank.match(line):
334-outfile.write(" ")
335-else:
336-# lines preceded by no marks weren't hit
337-# Highlight them if so indicated, unless the line contains
330+elif lineno in lnotab and not PRAGMA_NOCOVER in line:
331+# Highlight never-executed lines, unless the line contains
338332# #pragma: NO COVER
339-if lineno in lnotab and not PRAGMA_NOCOVER in line:
340-outfile.write(">>>>>> ")
341-n_lines += 1
342-else:
343-outfile.write(" ")
333+outfile.write(">>>>>> ")
334+n_lines += 1
335+else:
336+outfile.write(" ")
344337outfile.write(line.expandtabs(8))
345338346339return n_hits, n_lines