Skip to content

trace: --count --no-report --file does not save the counts file #152409

Description

@tonghuaroot

Bug report

python -m trace --count --no-report --file FILE script.py exits
successfully but never writes FILE, so counts cannot be accumulated
across runs.

Both options are documented for exactly this workflow:

  • -f, --file FILE — "File to accumulate counts over several runs"
  • -R, --no-report — "Do not generate the coverage report files. Useful
    if you want to accumulate over several runs."

and Doc/library/trace.rst describes --no-report as useful when you
"intend to make several runs with --count, and then produce a single
set of annotated listings at the end."

However the counts are only persisted at the end of
CoverageResults.write_results(), which main() skips entirely under
--no-report:

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

So --no-report suppresses the report and the --file counts. The file
is never created, and because --file is also read back at startup, every
run warns about the file it was supposed to accumulate into:

$ cat prog.py
for i in range(3):
    pass
$ python -m trace --count --no-report --file counts prog.py
Skipping counts file 'counts': [Errno 2] No such file or directory: 'counts'
$ python -m trace --count --no-report --file counts prog.py
Skipping counts file 'counts': [Errno 2] No such file or directory: 'counts'

counts is never written, so accumulation never happens. The same command
without --no-report does write counts (alongside the .cover report).

Suggested fix

Move the counts-saving step out of write_results() into a small
CoverageResults.save_counts() helper and call it on the --no-report
path as well, so --file is honoured regardless of --no-report.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions