This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
SlipCover is a fast, near-zero-overhead Python code coverage tool. Unlike traditional coverage tools that use Python's tracing facilities, SlipCover uses just-in-time bytecode instrumentation (Python <3.12) or the sys.monitoring API (Python 3.12+) to track executed code with minimal overhead.
# Install in development/editable mode
pip install -e .
# Run all tests
pytest
# Run a single test file
pytest tests/test_coverage.py
# Run a specific test
pytest tests/test_coverage.py::test_function_name
# Run with pytest-forked (Unix only, useful for isolation)
pytest --forked
# Clean build artifacts
make clean
# Run benchmarks
make bench# Run a script with coverage
python -m slipcover myscript.py
# Run with a module (e.g., pytest)
python -m slipcover -m pytest
# Enable branch coverage
python -m slipcover --branch myscript.py
# Output JSON format
python -m slipcover --json --out coverage.json myscript.py-
src/slipcover/slipcover.py: MainSlipcoverclass that manages instrumentation and coverage collection. Contains version-specific code paths for Python <3.12 (bytecode rewriting) vs 3.12+ (sys.monitoring). -
src/slipcover/bytecode.py: Bytecode manipulation utilities (Editorclass) for inserting probe calls into Python bytecode. Only used on Python <3.12. -
src/slipcover/branch.py: AST-based pre-instrumentation for branch coverage. Thepreinstrument()function inserts branch markers before compilation. -
src/slipcover/importer.py: Custom import machinery (ImportManager,SlipcoverMetaPathFinder,SlipcoverLoader) that intercepts module loading to instrument code. Also containsFileMatcherfor source file filtering andwrap_pytest()for pytest integration. -
src/probe.cxx: C++ extension module providing low-overhead probe signaling for Python <3.12. Not used on 3.12+ (pure Python there).
The codebase has significant branching based on Python version:
- Python 3.12+: Uses
sys.monitoringAPI for coverage (no bytecode rewriting, no C++ extension) - Python <3.12: Uses bytecode instrumentation via the
probeC++ extension
Many functions have if sys.version_info >= (3,12): blocks with different implementations.
- Script/module execution:
__main__.pyparses args, createsSlipcoverandFileMatcherinstances - Import interception:
ImportManagerinstalls a meta path finder that wraps module loaders - Instrumentation: When matching modules load, their bytecode is instrumented via
Slipcover.instrument() - Branch coverage (optional): AST pre-instrumentation via
branch.preinstrument()adds branch markers before compilation - Collection: Probes signal line/branch execution to the
Slipcoverinstance - De-instrumentation (Python <3.12): Once coverage is recorded, probes can be disabled to reduce overhead
- Reporting:
get_coverage()returns JSON-compatible coverage data; output can be text, JSON, or XML
Tests are in tests/ and use pytest:
test_coverage.py: End-to-end coverage functionality teststest_instrumentation.py: Bytecode instrumentation teststest_bytecode.py: Low-level bytecode editor teststest_branch.py: Branch coverage and AST pre-instrumentation teststest_importer.py: Import machinery tests