Skip to content

Commit f653aae

Browse files
HanpuLinedbat
andauthored
fix: loop completion arcs on Python 3.14 (#2281)
* Fix loop completion arcs on Python 3.14 Fixes #2168. * Limit loop completion fix to C tracer * Fix loop completion arcs in Python tracer * Test PyTracer return handling directly * no need for pragma in a file that's completely unmeasured * we don't test tracers this closely * make the change description more concise * remove now-unused imports * re-trigger build --------- Co-authored-by: Caitlyn Lye <[email protected]> Co-authored-by: Ned Batchelder <[email protected]>
1 parent cc1ced4 commit f653aae

4 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎CHANGES.rst‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ upgrading your version of coverage.py.
2323
Unreleased
2424
----------
2525

26-
Nothing yet.
26+
- Fix: on Python 3.14 and later, a ``for`` loop completing immediately before a
27+
function return could mistakenly report an uncovered branch. This is now
28+
fixed, closing `issue 2168`_.
29+
30+
.. _issue 2168: https://github.com/coveragepy/coveragepy/issues/2168
2731

2832

2933
.. start-releases

‎coverage/ctracer/tracer.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,19 @@ CTracer_handle_return(CTracer *self, PyFrameObject *frame)
771771
real_return = !(is_yield || is_yield_from);
772772
#endif
773773
if (real_return) {
774+
#if PY_VERSION_HEX >= 0x030E0000
775+
/* Python 3.14 can report a new line only on the return event. */
776+
int return_line = PyFrame_GetLineNumber(frame);
777+
if (
778+
self->pcur_entry->file_tracer == Py_None &&
779+
return_line != self->pcur_entry->last_line
780+
) {
781+
if (CTracer_record_pair(self, self->pcur_entry->last_line, return_line) < 0) {
782+
goto error;
783+
}
784+
self->pcur_entry->last_line = return_line;
785+
}
786+
#endif
774787
int first = MyFrame_BorrowCode(frame)->co_firstlineno;
775788
if (CTracer_record_pair(self, self->pcur_entry->last_line, -first) < 0) {
776789
goto error;

‎coverage/pytracer.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ def _trace(
292292
else:
293293
real_return = True
294294
if real_return:
295+
# Python 3.14 can report a new line only on the return event.
296+
if env.PYVERSION >= (3, 14) and frame.f_lineno != self.last_line:
297+
cast(set_TArc, self.cur_file_data).add((self.last_line, frame.f_lineno))
298+
self.last_line = frame.f_lineno
295299
first = frame.f_code.co_firstlineno
296300
cast(set_TArc, self.cur_file_data).add((self.last_line, -first))
297301

‎tests/test_arcs.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,23 @@ def test_nested_loop(self) -> None:
505505
branchz_missing="",
506506
)
507507

508+
def test_bug_2168_loop_completion_before_return(self) -> None:
509+
self.check_coverage(
510+
"""\
511+
def foo(*x):
512+
for i in x:
513+
if i:
514+
break
515+
pass
516+
517+
foo()
518+
foo(0)
519+
foo(1)
520+
""",
521+
branchz="23 25 32 34",
522+
branchz_missing="",
523+
)
524+
508525
def test_break(self) -> None:
509526
self.check_coverage(
510527
"""\

0 commit comments

Comments
 (0)