Skip to content

Commit a2baa66

Browse files
committed
refactor: nicer branch_trails code
1 parent 0b15dae commit a2baa66

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

coverage/bytecode.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ class InstructionWalker:
6363
First, in strict sequence to visit all the instructions in the object.
6464
This is `walk(follow_jumps=False)`. Second, we want to follow jumps to
6565
understand how execution will flow: `walk(follow_jumps=True)`.
66-
6766
"""
6867

6968
def __init__(self, code: CodeType) -> None:
@@ -98,7 +97,8 @@ def walk(
9897
offset += 2
9998

10099

101-
TBranchTrails = dict[TOffset, dict[Optional[TArc], set[TOffset]]]
100+
TBranchTrailsOneSource = dict[Optional[TArc], set[TOffset]]
101+
TBranchTrails = dict[TOffset, TBranchTrailsOneSource]
102102

103103

104104
def branch_trails(code: CodeType) -> TBranchTrails:
@@ -117,7 +117,9 @@ def branch_trails(code: CodeType) -> TBranchTrails:
117117
arc from the original instruction's line to the new source line.
118118
119119
"""
120-
the_trails: TBranchTrails = collections.defaultdict(lambda:collections.defaultdict(set))
120+
the_trails: TBranchTrails = collections.defaultdict(
121+
lambda: collections.defaultdict(set)
122+
)
121123
iwalker = InstructionWalker(code)
122124
for inst in iwalker.walk(follow_jumps=False):
123125
if not inst.jump_target:
@@ -131,7 +133,10 @@ def branch_trails(code: CodeType) -> TBranchTrails:
131133
if from_line is None:
132134
continue
133135

134-
def walk_one_branch(start_at: TOffset) -> tuple[Optional[TArc], set[TOffset]]:
136+
def add_one_branch_trail(
137+
trails: TBranchTrailsOneSource,
138+
start_at: TOffset,
139+
) -> None:
135140
# pylint: disable=cell-var-from-loop
136141
inst_offsets: set[TOffset] = set()
137142
to_line = None
@@ -146,17 +151,15 @@ def walk_one_branch(start_at: TOffset) -> tuple[Optional[TArc], set[TOffset]]:
146151
to_line = -code.co_firstlineno
147152
break
148153
if to_line is not None:
149-
return (from_line, to_line), inst_offsets
154+
trails[(from_line, to_line)].update(inst_offsets)
150155
else:
151-
return None, set()
156+
trails[None] = set()
152157

153158
# Calculate two trails: one from the next instruction, and one from the
154159
# jump_target instruction.
155-
trails = collections.defaultdict(set)
156-
arc, offsets = walk_one_branch(start_at=inst.offset + 2)
157-
trails[arc].update(offsets)
158-
arc, offsets = walk_one_branch(start_at=inst.jump_target)
159-
trails[arc].update(offsets)
160+
trails: TBranchTrailsOneSource = collections.defaultdict(set)
161+
add_one_branch_trail(trails, start_at=inst.offset + 2)
162+
add_one_branch_trail(trails, start_at=inst.jump_target)
160163
the_trails[inst.offset] = trails
161164

162165
# Sometimes we get BRANCH_RIGHT or BRANCH_LEFT events from instructions

tests/helpers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,16 +396,15 @@ def all_our_source_files() -> Iterator[tuple[Path, str]]:
396396
397397
Produces a stream of (filename, file contents) tuples.
398398
"""
399-
print(f"all_our_source_files: {coverage.__file__ = }")
400399
cov_dir = Path(coverage.__file__).parent.parent
401400
if ".tox" in cov_dir.parts:
402401
# We are in a tox-installed environment, look above the .tox dir to
403402
# also find the uninstalled source files.
404403
cov_dir = Path(os.fspath(cov_dir).partition(".tox")[0])
405404

406-
print(f"all_our_source_files: {os.path.abspath(cov_dir) = }")
407405
# To run against all the files in the tox venvs:
408406
# for source_file in cov_dir.rglob("*.py"):
409407
for sub in [".", "benchmark", "ci", "coverage", "lab", "tests"]:
408+
assert (cov_dir / sub).is_dir()
410409
for source_file in (cov_dir / sub).glob("*.py"):
411410
yield (source_file, source_file.read_text(encoding="utf-8"))

tests/test_arcs.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2129,7 +2129,10 @@ def func():
21292129
branchz_missing="29 38 45 56 5. 9A 9.",
21302130
)
21312131

2132-
@pytest.mark.skipif(env.PYVERSION[:2] == (3, 9), reason="not sure why it isn't fixed on 3.9")
2132+
@pytest.mark.skipif(
2133+
env.PYVERSION[:2] == (3, 9),
2134+
reason="CPython fix not backported to 3.9: https://github.com/python/cpython/issues/93061",
2135+
)
21332136
def test_bug_1999(self) -> None:
21342137
self.check_coverage("""\
21352138
import asyncio

0 commit comments

Comments
 (0)