Context manager inside try/except reports incomplete coverage with --branch

Issue #470 duplicate
Lucas Boppre Niehues created an issue

It looks to me as if all branches have been taken, but coverage disagrees. Problem disappears with context manager is removed or value is returned directly.

def soft_read(filename):
    try:
        with open(filename, 'r') as f:
            value = f.read()   # line 4, partial coverage
    except:
        value = ''
    return value

assert soft_read('missing file.txt') == ''
assert len(soft_read(__file__))

Line 4 was executed, but never jumped to line 5

Comments (3)

  1. Lucas Boppre Niehues reporter

    Thanks! I checked all open issues for duplicates, and searched the closed ones, but couldn't find because I thought it had something to do with the try/except. Upgrading to beta fixed the problem (pip install --pre --upgrade coverage).

  2. Log in to comment