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

Created on 2020-12-14 07:24 by Mark.Shannon, last changed 2021-02-01 23:38 by Mark.Shannon. This issue is now closed.

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 (7)
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
msg386107 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-02-01 20:50
Friendly reminder that this issue is currently blocking the 3.10a5 release. If you are ok with waiting for the next release to include the fix, please say so here or drop me an email/
msg386116 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2021-02-01 21:31
This problem no longer appears with master (commit 9eb11a139f).
msg386121 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-02-01 23:38
Sorry. I forgot the close the issue when it was fixed.
History
Date User Action Args
2021-02-01 23:38:55Mark.Shannonsetstatus: open -> closed
resolution: fixed
messages: + msg386121

stage: patch review -> resolved
2021-02-01 21:31:22nedbatsetmessages: + msg386116
2021-02-01 20:50:29pablogsalsetnosy: + pablogsal
messages: + msg386107
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 -> (no value)
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