Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nedbat opened this issue Jan 24, 2016 · 3 comments
Closed
Labels
bug Something isn't working

Comments

@nedbat
Copy link
Owner

nedbat commented Jan 24, 2016

Originally reported by lucasboppre (Bitbucket: lucasboppre, GitHub: Unknown)


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.

#!python

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


@nedbat
Copy link
Owner Author

nedbat commented Jan 24, 2016

Duplicate of #146.

@nedbat
Copy link
Owner Author

nedbat commented Jan 24, 2016

This is now fixed in beta, try it: https://pypi.python.org/pypi/coverage/4.1b2

@nedbat
Copy link
Owner Author

nedbat commented Jan 24, 2016

Original comment by lucasboppre (Bitbucket: lucasboppre, GitHub: Unknown)


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).

@nedbat nedbat closed this as completed Jan 24, 2016
@nedbat nedbat added major bug Something isn't working labels Jun 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant