After nearly two years, I think this is finally ready: coverage.py can use sys.monitoring to more efficiently measure branch coverage.
I would love for people to try it, but it’s a little involved at the moment:
- You need to have your own build of Python from the main branch on GitHub, because the CPython side of the work landed after 3.14 alpha 5. Alpha 6 is supposed to arrive within a week, so that will make it easier.
- I haven’t released a version of coverage.py to PyPI with this code yet, so you also need to install coverage from GitHub:
% python3 -m pip install git+https://github.com/nedbat/coveragepy
Once you have both of those things, set the environment variable
COVERAGE_CORE=sysmon
and run coverage as you usually do. If all goes
well, it should be faster. Please let me know!
Feedback is welcome in GitHub issues or in the #coverage-py channel in the Python Discord server.
This has been a long journey, starting when I first commented on PEP 669 that underpins this work. Mark Shannon and I have had many back and forths about the behavior of sys.monitoring, finally landing on something that would work for us both.
For the curious: traditionally coverage.py relied on sys.settrace. Python calls my recording function for every line of Python executed. It’s simple and effective, but inefficient. After I’ve been told a line was executed once, I don’t need to be told again, but settrace keeps calling my function. The new sys.monitoring that arrived in Python 3.12 lets me disable an event once it’s fired, so after the first ping there’s no overhead to running that same code multiple times.
It took a while to iron out the event behavior that lets us measure branches as well as lines, but Python 3.14.0 after alpha 5 has it, so we’re finally able to announce coverage.py support for people to try out.
Comments
Add a comment: