Skip to content

try-else + with + branch = true with sysmon results in false negative #2289

Description

@asottile

Describe the bug

the combination of things in the title results in coverage treating a line as covered when it is most certainly not! the toy example below is minimized from this file here

To Reproduce

I am using coverage 7.16.1 (with no config) on linux x86_64 -- all pythons below are on the latest released patch version where not specified

import contextlib

def main():
    raise SystemExit()

def example():
    try:
        with contextlib.ExitStack():
            main()
    except SystemExit as e:
        return 1
    else:
        raise RuntimeError('!!!')  # incorrectly covered with sysmon!

example()

in python 3.14 and above I get the following (incorrect) output (notice line 13 is incorrectly marked as covered):

$ venv314/bin/coverage erase
$ venv314/bin/coverage run --branch t.py
$ venv314/bin/coverage report --show-missing --include t.py
Name    Stmts   Miss Branch BrPart  Cover   Missing
---------------------------------------------------
t.py       11      0      0      0   100%
---------------------------------------------------
TOTAL      11      0      0      0   100%

Expected behavior

in python 3.13 and below I get the following (correct) output:

$ venv313/bin/coverage erase
$ venv313/bin/coverage run --branch t.py
$ venv313/bin/coverage report --show-missing --include t.py
Name    Stmts   Miss Branch BrPart  Cover   Missing
---------------------------------------------------
t.py       11      1      0      0    91%   13
---------------------------------------------------
TOTAL      11      1      0      0    91%

Additional context
found while debugging pyflakes coverage

Activity

  1. nedbat commented on Sep 26, 2026

    @nedbat
    Member

    This is fixed in commit 8ad7236.

  2. nedbat commented on Sep 27, 2026

    @nedbat
    Member

    This is now released as part of coverage 7.16.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions