Files
youknowdot 1ffba21557 Synchronize regression harness helpers with Python 3.15 (#9297)
Regenerate nine canonical modules using scripts/update_lib at CPython
3.15.0 d220409a13b77de7a6c30abe16ea90db01528edc. Keep the discovery
package split out until its missing test family is available.

AI-assisted source selection, synchronization, review and bounded
validation using OpenAI Codex. No native capability claims are added.

Assisted-by: Codex:unknown
2026-10-22 18:14:03 +09:00

49 lines
1.2 KiB
Python
Vendored

"""A minimal hook for gathering line coverage of the standard library.
Designed to be used with -Xpresite=test.cov:enable which means:
* it can't import anything besides `sys` to avoid tainting gathered coverage
* filenames are not normalized
To get gathered coverage back, look for 'test.cov' in `sys.modules`
instead of importing directly. That way you can determine if the module
was already in use.
If you need to disable the hook, call the `disable()` function.
"""
import sys
mon = sys.monitoring
FileName = str
LineNo = int
Location = tuple[FileName, LineNo]
coverage: set[Location] = set()
# `types` and `typing` aren't imported to avoid invalid coverage
def add_line(
code: "types.CodeType",
lineno: int,
) -> "typing.Literal[sys.monitoring.DISABLE]":
coverage.add((code.co_filename, lineno))
return mon.DISABLE
def enable():
mon.use_tool_id(mon.COVERAGE_ID, "regrtest coverage")
mon.register_callback(mon.COVERAGE_ID, mon.events.LINE, add_line)
mon.set_events(mon.COVERAGE_ID, mon.events.LINE)
def disable():
mon.set_events(mon.COVERAGE_ID, 0)
mon.register_callback(mon.COVERAGE_ID, mon.events.LINE, None)
mon.free_tool_id(mon.COVERAGE_ID)
if __name__ == "__main__":
enable()