classification
Title: Incorrect line number in bytecode for try-except-finally
Type: behavior Stage: patch review
Components: Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Mark.Shannon Nosy List: Mark.Shannon, nedbat
Priority: release blocker Keywords: patch

Created on 2020-12-14 07:24 by Mark.Shannon, last changed 2020-12-21 13:53 by Mark.Shannon.

Pull Requests
URL Status Linked Edit
PR 23760 merged Mark.Shannon, 2020-12-14 09:11
PR 23877 merged Mark.Shannon, 2020-12-21 11:27
Messages (4)
msg382958 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2020-12-14 07:24
The following code, when traced, produces a spurious line event for line 5:

a, b, c = 1, 1, 1
try:
    a = 3
except:
    b = 5
finally:
    c = 7
assert a == 3 and b == 1 and c == 7

Bug reported by Ned Batchelder https://gist.github.com/nedbat/6c5dedde9df8d2de13de8a6a39a5f112
msg383448 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2020-12-20 20:45
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.
msg383449 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2020-12-20 20:47
(Rather: line 8 isn't executed, and so should not be traced.)
msg383520 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2020-12-21 13:53
New changeset f2dbfd7e20431f0bcf2b655aa876afec7fe03c6f by Mark Shannon in branch 'master':
bpo-42634: Mark reraise after except blocks as artificial. (GH-23877)
https://github.com/python/cpython/commit/f2dbfd7e20431f0bcf2b655aa876afec7fe03c6f
History
Date User Action Args
2020-12-21 13:53:57Mark.Shannonsetmessages: + msg383520
2020-12-21 11:27:52Mark.Shannonsetstage: resolved -> patch review
pull_requests: + pull_request22740
2020-12-21 10:07:11Mark.Shannonsetresolution: fixed ->
2020-12-20 20:47:27nedbatsetmessages: + msg383449
2020-12-20 20:45:49nedbatsetstatus: closed -> open

messages: + msg383448
2020-12-14 13:04:44Mark.Shannonsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-12-14 09:11:10Mark.Shannonsetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request22616
2020-12-14 07:24:56Mark.Shannoncreate