[New-bugs-announce] [issue42696] Duplicated unused bytecodes at end of function

Ned Batchelder report at bugs.python.org
Sun Dec 20 16:29:18 EST 2020


New submission from Ned Batchelder <ned at nedbatchelder.com>:

(Using CPython commit c95f8bc270.)

This program has extra bytecodes:

    def f():
        for i in range(10):
            break
        return 17

The dis output is:

      1           0 LOAD_CONST               0 (<code object f at 0x109cf7450, file "afterbreak.py", line 1>)
                  2 LOAD_CONST               1 ('f')
                  4 MAKE_FUNCTION            0
                  6 STORE_NAME               0 (f)
                  8 LOAD_CONST               2 (None)
                 10 RETURN_VALUE

    Disassembly of <code object f at 0x109cf7450, file "afterbreak.py", line 1>:
      2           0 LOAD_GLOBAL              0 (range)
                  2 LOAD_CONST               1 (10)
                  4 CALL_FUNCTION            1
                  6 GET_ITER
                  8 FOR_ITER                 8 (to 18)
                 10 STORE_FAST               0 (i)

      3          12 POP_TOP

      4          14 LOAD_CONST               2 (17)
                 16 RETURN_VALUE
            >>   18 LOAD_CONST               2 (17)
                 20 RETURN_VALUE

The break has something to do with it, because if I change the Python to:

    def f():
        for i in range(10):
            a = 1
        return 17

then the dis output is:

      1           0 LOAD_CONST               0 (<code object f at 0x10bf8f450, file "afterbreak.py", line 1>)
                  2 LOAD_CONST               1 ('f')
                  4 MAKE_FUNCTION            0
                  6 STORE_NAME               0 (f)
                  8 LOAD_CONST               2 (None)
                 10 RETURN_VALUE

    Disassembly of <code object f at 0x10bf8f450, file "afterbreak.py", line 1>:
      2           0 LOAD_GLOBAL              0 (range)
                  2 LOAD_CONST               1 (10)
                  4 CALL_FUNCTION            1
                  6 GET_ITER
            >>    8 FOR_ITER                 8 (to 18)
                 10 STORE_FAST               0 (i)

      3          12 LOAD_CONST               2 (1)
                 14 STORE_FAST               1 (a)
                 16 JUMP_ABSOLUTE            8

      4     >>   18 LOAD_CONST               3 (17)
                 20 RETURN_VALUE

----------
messages: 383460
nosy: Mark.Shannon, nedbat
priority: normal
severity: normal
status: open
title: Duplicated unused bytecodes at end of function
versions: Python 3.10

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42696>
_______________________________________


More information about the New-bugs-announce mailing list