Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion Lib/test/test_trace.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from pickle import dump
from pickle import dump, load
import sys
from test.support import captured_stdout, requires_resource
from test.support.os_helper import (TESTFN, rmtree, unlink)
Expand Down Expand Up @@ -561,6 +561,28 @@ def f():
self.assertIn('lines cov% module (path)', stdout)
self.assertIn(f'6 100.0% {modulename} ({filename})', stdout)

def test_count_no_report_accumulates_counts(self):
# --no-report must still save the --file counts so they accumulate.
filename = f'{TESTFN}.py'
countsfile = f'{TESTFN}.counts'
with open(filename, 'w', encoding='utf-8') as fd:
self.addCleanup(unlink, filename)
self.addCleanup(unlink, countsfile)
fd.write('for i in range(3):\n pass\n')
argv = ('-m', 'trace', '--count', '--no-report',
'--file', countsfile, filename)
assert_python_ok(*argv, PYTHONIOENCODING='utf-8')
self.assertTrue(os.path.exists(countsfile))
with open(countsfile, 'rb') as fd:
counts = load(fd)[0]
self.assertTrue(counts)
# A second run accumulates into the same file.
assert_python_ok(*argv, PYTHONIOENCODING='utf-8')
with open(countsfile, 'rb') as fd:
accumulated = load(fd)[0]
self.assertEqual(accumulated,
{key: 2 * value for key, value in counts.items()})

def test_run_as_module(self):
assert_python_ok('-m', 'trace', '-l', '--module', 'timeit', '-n', '1')
assert_python_failure('-m', 'trace', '-l', '--module', 'not_a_module_zzz')
Expand Down
7 changes: 6 additions & 1 deletion Lib/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,11 @@ def write_results(self, show_missing=True, summary=False, coverdir=None, *,
n_lines, n_hits, modulename, filename = sums[m]
print(f"{n_lines:5d} {n_hits/n_lines:.1%} {modulename} ({filename})")

self._save_counts()

def _save_counts(self):
"""Save the accumulated counts to ``self.outfile`` if one was given."""
if self.outfile:
# try and store counts and module info into self.outfile
try:
with open(self.outfile, 'wb') as f:
pickle.dump((self.counts, self.calledfuncs, self.callers),
Expand Down Expand Up @@ -744,6 +747,8 @@ def parse_ignore_dir(s):

if not opts.no_report:
results.write_results(opts.missing, opts.summary, opts.coverdir)
else:
results._save_counts()

if __name__=='__main__':
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix the :mod:`trace` command-line tool not saving the ``--file`` counts
when ``--no-report`` is used, which prevented accumulating counts over
several runs. Patch by tonghuaroot.
Loading