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: Nested if/else gets phantom else trace again (3.10)
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Mark.Shannon, Sergey.Kirpichev, nedbat, pablogsal
Priority: normal Keywords: 3.10regression, patch

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

Pull Requests
URL Status Linked Edit
PR 27656 merged Mark.Shannon, 2021-08-07 15:17
PR 27673 merged Mark.Shannon, 2021-08-09 09:38
Messages (4)
msg399003 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2021-08-05 13:11
Note: this is very similar to https://bugs.python.org/issue42810
This was originally reported against coverage.py: https://github.com/nedbat/coveragepy/issues/1205

---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

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

def func():
    if A:
        if B:
            if C:
                if D:
                    return False
        else:
            return False
    elif E and F:
        return True

A = B = True
C = False

func()
-------------------------

This produces this trace output:

3.10.0rc1 (default, Aug  3 2021, 15:03:55) [Clang 12.0.0 (clang-1200.0.32.29)]
call 13: def func():
line 14:     if A:
line 15:         if B:
line 16:             if C:
line 21:     elif E and F:
retu 21:     elif E and F:

The elif on line 21 is not executed, and should not be traced.

Also, if I change line 21 to `elif E:`, then the trace changes to:

3.10.0rc1 (default, Aug  3 2021, 15:03:55) [Clang 12.0.0 (clang-1200.0.32.29)]
call 13: def func():
line 14:     if A:
line 15:         if B:
line 16:             if C:
line 22:         return True
retu 22:         return True
msg399251 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-08-09 09:19
New changeset b854557b49083d8625a433eb36aacb0c87d67c52 by Mark Shannon in branch 'main':
bpo-44840: Compiler: Move duplication of exit blocks with no line numbers to after CFG optimization. (GH-27656)
https://github.com/python/cpython/commit/b854557b49083d8625a433eb36aacb0c87d67c52
msg399253 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-08-09 09:54
New changeset 762ef85f441cdec002cb4e812b9e77ae5033e571 by Mark Shannon in branch '3.10':
bpo-44840: Compiler: Move duplication of exit blocks with no line numbers to after CFG optimization. (GH-27656) (#27673)
https://github.com/python/cpython/commit/762ef85f441cdec002cb4e812b9e77ae5033e571
msg399255 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2021-08-09 10:30
This fix looks good, thanks.
History
Date User Action Args
2022-04-11 14:59:48adminsetgithub: 89003
2021-08-09 11:45:36pablogsalsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-08-09 10:30:07nedbatsetmessages: + msg399255
2021-08-09 09:54:51pablogsalsetnosy: + pablogsal
messages: + msg399253
2021-08-09 09:38:24Mark.Shannonsetpull_requests: + pull_request26161
2021-08-09 09:19:28Mark.Shannonsetmessages: + msg399251
2021-08-07 15:17:32Mark.Shannonsetkeywords: + patch
stage: patch review
pull_requests: + pull_request26150
2021-08-05 13:44:24Sergey.Kirpichevsetnosy: + Sergey.Kirpichev
2021-08-05 13:11:40nedbatcreate