summaryrefslogtreecommitdiffstats
path: root/Lib/trace.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2018-05-01 05:03:29 (GMT)
committerGitHub <noreply@github.com>2018-05-01 05:03:29 (GMT)
commita607f8b9982ac95bb59f8f61e0a50fc5ae29dc14 (patch)
tree0ecfe786c8a7691593ff7dff9e2819c96fcc1655 /Lib/trace.py
parent3d11630ff401cfcdf094cf039cb575332ecaea20 (diff)
downloadcpython-a607f8b9982ac95bb59f8f61e0a50fc5ae29dc14.zip
cpython-a607f8b9982ac95bb59f8f61e0a50fc5ae29dc14.tar.gz
cpython-a607f8b9982ac95bb59f8f61e0a50fc5ae29dc14.tar.bz2
bpo-31908: Fix output of cover files for trace module command-line tool. (GH-4205)
Previously emitted cover files only when --missing option was used. (cherry picked from commit 47ab15470d72367694d7758004067313ae022f0e) Co-authored-by: Michael Selik <mike@selik.org>
Diffstat (limited to 'Lib/trace.py')
-rwxr-xr-xLib/trace.py39
1 files changed, 16 insertions, 23 deletions
diff --git a/Lib/trace.py b/Lib/trace.py
index 9ba9486..d171577 100755
--- a/Lib/trace.py
+++ b/Lib/trace.py
@@ -79,9 +79,6 @@ else:
PRAGMA_NOCOVER = "#pragma NO COVER"
-# Simple rx to find lines with no code.
-rx_blank = re.compile(r'^\s*(#.*)?$')
-
class _Ignore:
def __init__(self, modules=None, dirs=None):
self._mods = set() if not modules else set(modules)
@@ -284,16 +281,15 @@ class CoverageResults:
lnotab = _find_executable_linenos(filename)
else:
lnotab = {}
- if lnotab:
- source = linecache.getlines(filename)
- coverpath = os.path.join(dir, modulename + ".cover")
- with open(filename, 'rb') as fp:
- encoding, _ = tokenize.detect_encoding(fp.readline)
- n_hits, n_lines = self.write_results_file(coverpath, source,
- lnotab, count, encoding)
- if summary and n_lines:
- percent = int(100 * n_hits / n_lines)
- sums[modulename] = n_lines, percent, modulename, filename
+ source = linecache.getlines(filename)
+ coverpath = os.path.join(dir, modulename + ".cover")
+ with open(filename, 'rb') as fp:
+ encoding, _ = tokenize.detect_encoding(fp.readline)
+ n_hits, n_lines = self.write_results_file(coverpath, source,
+ lnotab, count, encoding)
+ if summary and n_lines:
+ percent = int(100 * n_hits / n_lines)
+ sums[modulename] = n_lines, percent, modulename, filename
if summary and sums:
@@ -312,6 +308,7 @@ class CoverageResults:
def write_results_file(self, path, lines, lnotab, lines_hit, encoding=None):
"""Return a coverage results file in path."""
+ # ``lnotab`` is a dict of executable lines, or a line number "table"
try:
outfile = open(path, "w", encoding=encoding)
@@ -330,17 +327,13 @@ class CoverageResults:
outfile.write("%5d: " % lines_hit[lineno])
n_hits += 1
n_lines += 1
- elif rx_blank.match(line):
- outfile.write(" ")
- else:
- # lines preceded by no marks weren't hit
- # Highlight them if so indicated, unless the line contains
+ elif lineno in lnotab and not PRAGMA_NOCOVER in line:
+ # Highlight never-executed lines, unless the line contains
# #pragma: NO COVER
- if lineno in lnotab and not PRAGMA_NOCOVER in line:
- outfile.write(">>>>>> ")
- n_lines += 1
- else:
- outfile.write(" ")
+ outfile.write(">>>>>> ")
+ n_lines += 1
+ else:
+ outfile.write(" ")
outfile.write(line.expandtabs(8))
return n_hits, n_lines