FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add Coverage.clear_data() for lightweight data reset by pctablet505 · Pull Request #2235 · coveragepy/coveragepy · GitHub

Add Coverage.clear_data() for lightweight data reset - #2235

Open
pctablet505 wants to merge 2 commits into
coveragepy:mainfrom
pctablet505:clear-data-2139
Open

Add Coverage.clear_data() for lightweight data reset#2235
pctablet505 wants to merge 2 commits into
coveragepy:mainfrom
pctablet505:clear-data-2139

Conversation

Copy link
Copy Markdown

Summary

erase() clears collected coverage data, but it also sets things up so
the next start() reruns _init_for_start() — rebuilding Core,
Collector, and InOrOut, and re-registering the atexit/sigterm
handlers. For code that starts/stops measurement on the same
Coverage instance many times in a row and only needs a clean slate
of data between cycles (tracing configuration doesn't change), that
reinitialization is pure overhead.

This adds Coverage.clear_data(): it closes and replaces the current
CoverageData, but leaves the existing collector/tracer setup alone
so the next start() doesn't redo the expensive part.

Closes #2139, which includes a benchmark showing the reinitialization
overhead and the speedup from avoiding it.

Changes

  • coverage/control.py: new Coverage.clear_data() method, plus a
    small _close_data() helper it shares the "close and forget the
    current CoverageData" logic with.
  • tests/test_api.py: tests that clear_data() gives a fresh data set,
    that repeated cycles behave like the existing erase()-based test,
    and that the collector/core/inorout objects from the first start()
    are still the ones in use after a clear_data() + start() cycle.
  • CHANGES.rst: entry under "Unreleased".

Test plan

  • pytest tests/test_api.py tests/test_data.py tests/test_collector.py — all passing
  • ruff format --check, pylint, and mypy --strict all clean on the touched files
  • Ran the benchmark script from the issue with clear_data() substituted for the manual workaround; confirms the same order-of-magnitude speedup and no extra atexit-callback accumulation

pctablet505 marked this pull request as ready for review July 17, 2026 12:47
erase() clears collected data but also forces the next start() to
rerun _init_for_start(), rebuilding Core, Collector, and InOrOut and
re-registering atexit/sigterm handlers. For code that repeatedly
starts/stops measurement on the same Coverage instance and only needs
a clean slate of data between cycles, this reinitialization is pure
overhead.

clear_data() closes and replaces the current CoverageData, keeping
the existing collector/tracer setup intact for the next start().

Closes coveragepy#2139.
clear_data() closed the current CoverageData and made a fresh one, but
the closed object stayed referenced in _data_to_close for the life of
the process. Over many start/stop/clear cycles (e.g. long fuzzer runs)
these closed, empty objects piled up, partly defeating the point of the
lighter-weight reset. _close_data() now drops the object from
_data_to_close when it forgets it.

Also fill in the versionadded placeholder for Coverage.clear_data().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: lightweight clear_data() for repeated collection without rerunning _init_for_start()

1 participant


Back | FazBrowse Home | New Git URL