diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2014-05-18 18:32:33 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2014-05-18 18:32:33 -0400 |
commit | 32782e38316c8bb3cf9d6f8b5c62081177399ffe (patch) | |
tree | 7e4d577568e4bbbbbd6594798241eed9df0927b6 /coverage/annotate.py | |
parent | 7c003bb14493f170fca65e2b39d3c962635ad064 (diff) | |
download | python-coveragepy-git-32782e38316c8bb3cf9d6f8b5c62081177399ffe.tar.gz |
Modernize annotate.py
Diffstat (limited to 'coverage/annotate.py')
-rw-r--r-- | coverage/annotate.py | 78 |
1 files changed, 35 insertions, 43 deletions
diff --git a/coverage/annotate.py b/coverage/annotate.py index 8074df91..dae9f4cf 100644 --- a/coverage/annotate.py +++ b/coverage/annotate.py @@ -47,52 +47,44 @@ class AnnotateReporter(Reporter): `cu` is the CodeUnit for the file to annotate. """ - filename = cu.filename - source = cu.source_file() - if self.directory: - dest_file = os.path.join(self.directory, cu.flat_rootname()) - dest_file += ".py,cover" - else: - dest_file = filename + ",cover" - dest = open(dest_file, 'w') - statements = sorted(analysis.statements) missing = sorted(analysis.missing) excluded = sorted(analysis.excluded) - lineno = 0 - i = 0 - j = 0 - covered = True - while True: - line = source.readline() - if line == '': - break - lineno += 1 - while i < len(statements) and statements[i] < lineno: - i += 1 - while j < len(missing) and missing[j] < lineno: - j += 1 - if i < len(statements) and statements[i] == lineno: - covered = j >= len(missing) or missing[j] > lineno - if self.blank_re.match(line): - dest.write(' ') - elif self.else_re.match(line): - # Special logic for lines containing only 'else:'. - if i >= len(statements) and j >= len(missing): - dest.write('! ') - elif i >= len(statements) or j >= len(missing): + if self.directory: + dest_file = os.path.join(self.directory, cu.flat_rootname()) + dest_file += ".py,cover" + else: + dest_file = cu.filename + ",cover" + + with open(dest_file, 'w') as dest: + i = 0 + j = 0 + covered = True + source = cu.source_file().read() + for lineno, line in enumerate(source.splitlines(True), start=1): + while i < len(statements) and statements[i] < lineno: + i += 1 + while j < len(missing) and missing[j] < lineno: + j += 1 + if i < len(statements) and statements[i] == lineno: + covered = j >= len(missing) or missing[j] > lineno + if self.blank_re.match(line): + dest.write(' ') + elif self.else_re.match(line): + # Special logic for lines containing only 'else:'. + if i >= len(statements) and j >= len(missing): + dest.write('! ') + elif i >= len(statements) or j >= len(missing): + dest.write('> ') + elif statements[i] == missing[j]: + dest.write('! ') + else: + dest.write('> ') + elif lineno in excluded: + dest.write('- ') + elif covered: dest.write('> ') - elif statements[i] == missing[j]: - dest.write('! ') else: - dest.write('> ') - elif lineno in excluded: - dest.write('- ') - elif covered: - dest.write('> ') - else: - dest.write('! ') - dest.write(line) - source.close() - dest.close() + dest.write('! ') + dest.write(line) |