classification
Title: Final "pass" is traced incorrectly in 3.9 (and before)
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.9, Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: Mark.Shannon, lukasz.langa, nedbat
Priority: normal Keywords:

Created on 2021-07-19 12:03 by nedbat, last changed 2021-07-19 18:24 by nedbat. This issue is now closed.

Messages (4)
msg397791 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2021-07-19 12:03
A simple function with a last "pass" statement gets traced incorrectly, attributing the return to the pass instead of the actual last statement executed:

--- 8< --------------------------
import linecache, sys

def trace(frame, event, arg):
    # The weird globals here is to avoid a NameError on shutdown...
    if frame.f_code.co_filename == globals().get("__file__"):
        lineno = frame.f_lineno
        print("{} {}: {}".format(event[:4], lineno, linecache.getline(__file__, lineno).rstrip()))
    return trace

def wrong_loop(x):
    if x:
        if x:
            print(4)
    else:
        pass

print(sys.version)
sys.settrace(trace)

wrong_loop(8)
----------------------------------

On 3.9 and before, this produces:

3.9.5 (default, May  5 2021, 06:50:43)
[Clang 12.0.0 (clang-1200.0.32.29)]
call 10: def wrong_loop(x):
line 11:     if x:
line 12:         if x:
line 13:             print(4)
4
line 15:         pass
retu 15:         pass

Partly I'm writing this issue to record the problem, but partly to get a decision: will there be fixes made to 3.9 (or before) for issues like this?
msg397798 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-07-19 13:43
I say no, for a couple of reasons.

1. PEP 626 only applies to 3.10 onwards
2. The bytecode optimizer in 3.9 doesn't understand line numbers.
Changing it would be a lot of effort and likely to introduce more bugs than it fixes.

Ultimately it is Łukasz's decision, though.
msg397817 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-07-19 17:14
Thanks for reporting, Ned. I agree with Mark though.

We've done 7 releases of Python 3.9 already. The later in the release cycle for a given Python version, the less it's clear if it's "worth" performing complex fixes. Most importantly because introducing a large functional change at this point is risky and we try hard not to disrupt users of stable versions. Moreover, our experience shows that later bugfix releases see decreasing adoption because by default downstream distributors keep their original early bugfix versions of Python with only security- and build-related fixes ported from later releases in the same series.
msg397830 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2021-07-19 18:24
This is the right call.  Thanks!
History
Date User Action Args
2021-07-19 18:24:26nedbatsetmessages: + msg397830
2021-07-19 17:14:03lukasz.langasetstatus: open -> closed
resolution: wont fix
messages: + msg397817

stage: resolved
2021-07-19 13:43:52Mark.Shannonsetnosy: + lukasz.langa
messages: + msg397798
2021-07-19 12:03:13nedbatcreate