This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Traced line number is wrong for "if not __debug__"
Type: Stage: resolved
Components: Interpreter Core Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Mark.Shannon, nedbat
Priority: normal Keywords:

Created on 2021-01-01 14:43 by nedbat, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg384178 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2021-01-01 14:43
(Using CPython commit 6b1ac809b9)

This program never executes line 4, but the "if not __debug__" is partly attributed to line 4, giving an incorrect trace:

    for value in [True, False]:
        if value:
            if not __debug__:
                1/0 # line 4
        else:
            x = 6

Using a simple trace program (https://github.com/nedbat/coveragepy/blob/master/lab/run_trace.py), it produces this output:

    call <string> 1 @-1
        line <string> 1 @0
        line <string> 2 @8
        line <string> 3 @12
        line <string> 4 @14
        line <string> 1 @4
        line <string> 2 @8
        line <string> 6 @16
        line <string> 1 @4
        return <string> 1 @24

Various simplifications of the program make the problem go away.
msg384223 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2021-01-02 15:19
This might be the same problem as #42810.
msg384232 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2021-01-02 17:36
I think I am finding more examples of the same problem, so I will just add it here:

    x = "hello"
    try:
        3/0
    except ZeroDivisionError:
        if x == 'raise':
            raise ValueError()   # line 6
    f = 7

This shows a trace for line 6, which is never excecuted:

call <string> 1 @-1
    line <string> 1 @0
    line <string> 2 @4
    line <string> 3 @6
    exception <string> 3 @10
    line <string> 4 @24
    line <string> 5 @36
    line <string> 6 @50
    line <string> 7 @52
    return <string> 7 @58
History
Date User Action Args
2022-04-11 14:59:39adminsetgithub: 86969
2021-01-04 18:25:00Mark.Shannonsetstatus: open -> closed
resolution: fixed
stage: resolved
2021-01-02 17:36:27nedbatsetmessages: + msg384232
2021-01-02 15:19:20nedbatsetmessages: + msg384223
2021-01-01 14:43:03nedbatcreate