[issue42634] Incorrect line number in bytecode for try-except-finally

Ned Batchelder report at bugs.python.org
Sun Dec 20 15:45:49 EST 2020


Ned Batchelder <ned at nedbatchelder.com> added the comment:

I checked on this with CPython commit c95f8bc270.  The code above is fixed, but this code has a similar problem:

a, b, c = 1, 1, 1
try:
    try:
        a = 4/0         # ZeroDivisionError
    except ValueError:
        b = 6
    except IndexError:
        a = 8           # Line 8
    finally:
        c = 10
except ZeroDivisionError:
    pass
assert a == 1 and b == 1 and c == 10


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 @10
    line <string> 3 @12
    line <string> 4 @16
    exception <string> 4 @20
    line <string> 5 @28
    line <string> 7 @48
    line <string> 8 @68
    line <string> 10 @78
    line <string> 11 @88
    line <string> 12 @100
    line <string> 13 @106
    return <string> 13 @136

Line 8 should never be executed.

----------
status: closed -> open

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42634>
_______________________________________


More information about the Python-bugs-list mailing list