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 (3.10)
Type: Stage: resolved
Components: Interpreter Core Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Mark.Shannon, nedbat, pablogsal, rhettinger
Priority: high Keywords: patch

Created on 2021-01-02 15:09 by nedbat, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 24091 merged Mark.Shannon, 2021-01-04 11:52
Messages (6)
msg384222 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2021-01-02 15:09
(Using CPython commit 6b1ac809b9)

This program never executes line 6, but tracing it claims that it does:

    a = b = x = y = z = 1
    if a == 1:
        if b == 1:
            x = 4
        else:
            y = 6
    else:
        z = 8
    assert (a, b, x, y, z) == (1, 1, 4, 1, 1)

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 @20
        line <string> 3 @28
        line <string> 4 @36
        line <string> 6 @40
        line <string> 9 @52
        return <string> 9 @76
msg384224 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2021-01-02 15:19
This might be the same problem as #42803.
msg384240 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-01-02 21:58
Confirmed.  This regression happened between 3.10 alpha 2 and today's build:

$ python3.10 --version
Python 3.10.0a2
$ python3.10 -m trace --trace tmp19.py
 --- modulename: tmp19, funcname: <module>
tmp19.py(1): a = b = x = y = z = 1
tmp19.py(2): if a == 1:
tmp19.py(3):     if b == 1:
tmp19.py(4):         x = 4
tmp19.py(9): assert (a, b, x, y, z) == (1, 1, 4, 1, 1)

$ py --version
Python 3.10.0a3+
$ py -m trace --trace tmp19.py
 --- modulename: tmp19, funcname: <module>
tmp19.py(1): a = b = x = y = z = 1
tmp19.py(2): if a == 1:
tmp19.py(3):     if b == 1:
tmp19.py(4):         x = 4
tmp19.py(6):         y = 6
tmp19.py(9): assert (a, b, x, y, z) == (1, 1, 4, 1, 1)
msg384252 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-01-03 02:15
Bisecting points at this commit as the culprit:

28b75c80dcc1e17ed3ac1c69362bf8dc164b760a is the first bad commit
commit 28b75c80dcc1e17ed3ac1c69362bf8dc164b760a
Author: Mark Shannon <mark@hotpy.org>
Date:   Wed Dec 23 11:43:10 2020 +0000

    bpo-42246: Don't eliminate jumps to jumps, if it will break PEP 626. (GH-23896)

 Lib/test/test_dis.py                               |  169 +-
 Lib/test/test_sys_settrace.py                      |   64 +
 .../2020-12-22-20-30-11.bpo-42246.7BrPLg.rst       |    1 +
 Python/compile.c                                   |  135 +-
 Python/importlib.h                                 | 1531 +++---
 Python/importlib_external.h                        | 4884 ++++++++++----------
 Python/importlib_zipimport.h                       | 1796 +++----
 7 files changed, 4341 insertions(+), 4239 deletions(-)
 create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-12-22-20-30-11.bpo-42246.7BrPLg.rst
bisect run success
msg384253 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-01-03 02:37
There is something wrong elsewhere as well because even if reverting that commit fix the problem:

❯ git reset --hard upstream/master
HEAD is now at bd2728b1e8 bpo-42806: Fix ast locations of f-strings inside parentheses (GH-24067)

~/github/python/master master*
❯ git revert 28b75c80dcc1e17ed3ac1c69362bf8dc164b760a
Removing Misc/NEWS.d/next/Core and Builtins/2020-12-22-20-30-11.bpo-42246.7BrPLg.rst
mak[master 444e7e11a8] Revert "bpo-42246: Don't eliminate jumps to jumps, if it will break PEP 626. (GH-23896)"
 7 files changed, 4605 insertions(+), 4707 deletions(-)
 delete mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-12-22-20-30-11.bpo-42246.7BrPLg.rst
 rewrite Python/importlib_external.h (87%)
 rewrite Python/importlib_zipimport.h (85%)
e
~/github/python/master master* ⇡
❯ make -j -s
 CC='gcc -pthread' LDSHARED='gcc -pthread -shared    ' OPT='-g -Og -Wall'       _TCLTK_INCLUDES='' _TCLTK_LIBS=''       ./python -E ./setup.py -q build

The following modules found by detect_modules() in setup.py, have been
built by the Makefile instead, as configured by the Setup files:
_abc                  pwd                   time


~/github/python/master master* ⇡
❯ ./python -m trace --trace ./lel.py
 --- modulename: lel, funcname: <module>
lel.py(1): a = b = x = y = z = 1
lel.py(2): if a == 1:
lel.py(3):     if b == 1:
lel.py(4):         x = 4
lel.py(9): assert (a, b, x, y, z) == (1, 1, 4, 1, 1)

Not running the optimize_basic_block function:

diff --git a/Python/compile.c b/Python/compile.c
index 6698b55000..30a0305ef1 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -6074,6 +6074,7 @@ fold_tuple_on_constants(struct instr *inst,
 static int
 optimize_basic_block(basicblock *bb, PyObject *consts)
 {
+    return 0;
     assert(PyList_CheckExact(consts));
     struct instr nop;
     nop.i_opcode = NOP;

makes the problem appear again:

❯ make -j -s
 CC='gcc -pthread' LDSHARED='gcc -pthread -shared    ' OPT='-g -Og -Wall'       _TCLTK_INCLUDES='' _TCLTK_LIBS=''       ./python -E ./setup.py -q build

The following modules found by detect_modules() in setup.py, have been
built by the Makefile instead, as configured by the Setup files:
_abc                  pwd                   time


~/github/python/master master* ⇡
❯ ./python -m trace --trace ./lel.py
 --- modulename: lel, funcname: <module>
lel.py(1): a = b = x = y = z = 1
lel.py(2): if a == 1:
lel.py(3):     if b == 1:
lel.py(4):         x = 4
lel.py(6):         y = 6
lel.py(9): assert (a, b, x, y, z) == (1, 1, 4, 1, 1)
msg384340 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-01-04 18:07
New changeset 127dde591686816e379d1add015304e6b9fb6954 by Mark Shannon in branch 'master':
bpo-42810: Mark jumps at end of if and try statements as artificial. (GH-24091)
https://github.com/python/cpython/commit/127dde591686816e379d1add015304e6b9fb6954
History
Date User Action Args
2022-04-11 14:59:39adminsetgithub: 86976
2021-01-04 18:24:50Mark.Shannonsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-01-04 18:07:05Mark.Shannonsetmessages: + msg384340
2021-01-04 11:52:05Mark.Shannonsetkeywords: + patch
stage: patch review
pull_requests: + pull_request22922
2021-01-03 02:37:42pablogsalsetmessages: + msg384253
2021-01-03 02:15:39pablogsalsetmessages: + msg384252
2021-01-02 21:58:23rhettingersetpriority: normal -> high
nosy: + rhettinger, pablogsal
messages: + msg384240

2021-01-02 15:19:36nedbatsetmessages: + msg384224
2021-01-02 15:09:25nedbatcreate