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

incorrect branch reporting with __debug__ #522

Closed
nedbat opened this issue Sep 22, 2016 · 8 comments
Closed

incorrect branch reporting with __debug__ #522

nedbat opened this issue Sep 22, 2016 · 8 comments
Labels
bug Something isn't working

Comments

@nedbat
Copy link
Owner

nedbat commented Sep 22, 2016

Originally reported by John Theodore Goetz (Bitbucket: theodoregoetz, GitHub: theodoregoetz)


Coverage report indicates a missing branch with __debug__ within an if/else block. Note if a pass is added after the if __debug__ then coverage is correctly shown as 100%.

#!python

for value in [True, False]:
    if value:
        if __debug__:
            x = 1
        #pass  # required for coverage to show 100%
    else:
        x = 2

Running coverage in both __debug__ and with python -O (appending the results):

#!bash

python -mcoverage erase
python -mcoverage run --branch prog.py
python -O -mcoverage run --branch --append prog.py
python -mcoverage report -m

Results in the following:

#!bash

Name       Stmts   Miss Branch BrPart  Cover   Missing
------------------------------------------------------
prog2.py       4      0      4      1    88%   2->1

@nedbat
Copy link
Owner Author

nedbat commented Dec 13, 2016

Original comment by Loic Dachary (Bitbucket: dachary, GitHub: dachary)


Reproduced the issue as instructed at 24aff3d7bfd5 (bb) with python-2.7.12 and python-3.5

@nedbat
Copy link
Owner Author

nedbat commented Dec 19, 2016

Original comment by Loic Dachary (Bitbucket: dachary, GitHub: dachary)


As of 813a3f0 (bb) the coverage shows 100%, the bug has been fixed and this issue can be closed

@nedbat
Copy link
Owner Author

nedbat commented Jan 8, 2017

Original comment by Loic Dachary (Bitbucket: dachary, GitHub: dachary)


@nedbat is there anything I can do before this issue can be closed ?

@nedbat
Copy link
Owner Author

nedbat commented Jan 15, 2017

Using the latest code, I still see the behavior originally reported. Leaving open.

@nedbat
Copy link
Owner Author

nedbat commented Jan 15, 2017

A simpler case will show the problem:

$ cat bug522.py
one = 1
for value in [True, False]:
    if value:
        if one:
            x = 1
    else:
        x = 2

for value in [True, False]:
    if value:
        if 1:
            x = 1
    else:
        x = 2
$ python -m coverage run --branch bug522.py
$ python -m coverage report -m
Name        Stmts   Miss Branch BrPart  Cover   Missing
-------------------------------------------------------
bug522.py      10      0     10      2    90%   4->2, 10->9

The "if 1:" statement doesn't appear in the compiled bytecode, so it's being removed from consideration, which makes the branch be attributed to 10->9 rather than 11->9.

@nedbat
Copy link
Owner Author

nedbat commented Jan 15, 2017

We treat constant expressions specially in while loops, but not in if's.

@nedbat
Copy link
Owner Author

nedbat commented Jan 16, 2017

Fixed in 7d90fa43b338 (bb).

@nedbat
Copy link
Owner Author

nedbat commented Jan 17, 2017

This was shipped in 4.3.2.

@nedbat nedbat closed this as completed Jan 17, 2017
@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