Skip to content

Commit 8ad7236

Browse files
Regretcursoragentnedbat
authored
fix: stop counting the try/else line when a with statement raises (#2301)
* Stop branch arcs at RERAISE so a raised with does not cover try/else. On Python 3.14 the not-taken with handler was walked into the following jump and the else line was reported as covered. Co-authored-by: Cursor <[email protected]> * Skip PyPy exception-handler line events that land on the try/else line. Co-authored-by: Cursor <[email protected]> * Wrap the PyPy opcode check so ruff format accepts it. * Cover the PyPy line skip and the no-fall-through branch. * Skip the opcode test when the interpreter has no PUSH_EXC_INFO. * Satisfy pylint and the type checker in the opcode test. * Narrow the opcode to int before building the fake code bytes. * Remove the useless pylint suppression on the opcode assertion. Co-authored-by: Cursor <[email protected]> * Run the reraise sample so the test file is fully covered. Co-authored-by: Cursor <[email protected]> * Import the opcode name only when the seeded revert has not removed it. Co-authored-by: Cursor <[email protected]> * Skip the arc test when the seeded revert removes the no-fall-through set. Co-authored-by: Cursor <[email protected]> * Type the seeded no-fall-through fallback as a set of ints. Co-authored-by: Cursor <[email protected]> * Build the seeded fallback set without redefining the imported name. Co-authored-by: Cursor <[email protected]> * Exclude the seeded-only test lines from coverage. Co-authored-by: Cursor <[email protected]> * Use the metacov exclude pragma for the seeded-only lines. Co-authored-by: Cursor <[email protected]> * no need for this deep a test of the PyPy fix * tweak the changelog --------- Co-authored-by: Cursor <[email protected]> Co-authored-by: Ned Batchelder <[email protected]>
1 parent c6dab1f commit 8ad7236

4 files changed

Lines changed: 47 additions & 0 deletions

File tree

‎CHANGES.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ Unreleased
2727
function return could mistakenly report an uncovered branch. This is now
2828
fixed, closing `issue 2168`_.
2929

30+
- Fix: on Python 3.14 and later, the ``else`` clause of a ``try`` whose body is
31+
a ``with`` statement could incorrectly be reported as covered when the
32+
``with`` raised. This is now fixed, closing `issue 2289`_.
33+
3034
.. _issue 2168: https://github.com/coveragepy/coveragepy/issues/2168
35+
.. _issue 2289: https://github.com/coveragepy/coveragepy/issues/2289
3136

3237

3338
.. start-releases

‎coverage/bytecode.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ def op_set(*op_names: str) -> set[int]:
106106
"RETURN_GENERATOR",
107107
)
108108

109+
# Opcodes that do not fall through. The bytecode after them belongs to
110+
# another path (for example the swallowed-exception side of a with).
111+
NO_FALL_THROUGH = op_set("RERAISE")
112+
109113

110114
# CACHE doesn't exist in Python 3.10, but the branch resolver is only used
111115
# on 3.14+, so a placeholder value is fine.
@@ -203,6 +207,8 @@ def resolve(self, source: TOffset, dest: TOffset) -> TArc | None:
203207
return None
204208
if op in RETURNS:
205209
return (from_line, -self.code.co_firstlineno)
210+
if op in NO_FALL_THROUGH:
211+
return None
206212
ext_arg = 0
207213
offset += 2
208214
return None

‎coverage/pytracer.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
# PYVERSIONS: RESUME is new in Python3.11
4141
RESUME = dis.opmap.get("RESUME")
4242
RETURN_VALUE = dis.opmap["RETURN_VALUE"]
43+
# PyPy attributes the exception-handler PUSH_EXC_INFO to the try/else line.
44+
# That instruction is not the else body. Skip it so the else line stays missing.
45+
PUSH_EXC_INFO = dis.opmap.get("PUSH_EXC_INFO")
4346
if RESUME is None:
4447
YIELD_VALUE = dis.opmap["YIELD_VALUE"]
4548
YIELD_FROM = dis.opmap["YIELD_FROM"]
@@ -257,6 +260,15 @@ def _trace(
257260
elif event == "line":
258261
# Record an executed line.
259262
if self.cur_file_data is not None:
263+
if PUSH_EXC_INFO is not None:
264+
lasti = frame.f_lasti
265+
code = frame.f_code.co_code
266+
if (
267+
isinstance(lasti, int)
268+
and 0 <= lasti < len(code)
269+
and code[lasti] == PUSH_EXC_INFO
270+
):
271+
return self._cached_bound_method_trace
260272
flineno: TLineNo = frame.f_lineno
261273

262274
if self.trace_arcs:

‎tests/test_arcs.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,30 @@ def f(x):
348348
expected = "line 3 didn't jump to the function exit"
349349
assert self.get_missing_arc_description(cov, 3, -2) == expected
350350

351+
def test_bug_2289(self) -> None:
352+
# https://github.com/coveragepy/coveragepy/issues/2289
353+
self.check_coverage(
354+
"""\
355+
import contextlib
356+
357+
def main():
358+
raise SystemExit()
359+
360+
def example():
361+
try:
362+
with contextlib.ExitStack():
363+
main()
364+
except SystemExit:
365+
return 1
366+
else:
367+
raise RuntimeError("!!!")
368+
369+
example()
370+
""",
371+
lines=[1, 3, 4, 6, 7, 8, 9, 10, 11, 13, 15],
372+
missing="13",
373+
)
374+
351375
def test_untaken_if_through_with(self) -> None:
352376
cov = self.check_coverage(
353377
"""\

0 commit comments

Comments
 (0)