◐ Shell
clean mode source ↗

Instruction-Offsets vs Byte-Offsets in dis.dis output NOT A BUG

Please ignore this issue. It was caused by my running the 3.7 library dis in the 3.10 environment.

Bug report

This is new to version 3.10
When disassembling code code block with jumps, bytecode for the jump shows the wrong target instruction number, and/or the bytecode with the wrong address flagged with >>.

To reproduce, here is an example, I have interspersed the source code with the disassembled output:



try: x = 1
  3           4 SETUP_FINALLY           15 (to 21)  --- (to 2 * 15 = 30)
              6 SETUP_FINALLY            4 (to 12)  --- (to 8 + 2 * 4 = 16)
              8 LOAD_CONST               1 (1)
             10 STORE_NAME               1 (x)
        >>   12 POP_BLOCK                           --- no >>
             14 JUMP_FORWARD             6 (to 22)  --- (to 16 + 2 * 6 = 28)

 except: x = 2
  4          16 POP_TOP                             --- >> (target of 6)
             18 POP_TOP
             20 POP_TOP
        >>   22 LOAD_CONST               2 (2)      --- no >>
             24 STORE_NAME               1 (x)
             26 POP_EXCEPT
        >>   28 POP_BLOCK                           --- >> (target of 14), not >> (target of 48).

finally: x = 3
  5          30 LOAD_CONST               3 (3)
             32 STORE_NAME               1 (x)
             34 JUMP_FORWARD             3 (to 39)  --- (to 36 + 2 * 3 = 42)
             36 LOAD_CONST               3 (3)      --- >> (target of 4)
             38 STORE_NAME               1 (x)
             40 CONTINUE_LOOP            0


if x == 3:
  7          42 LOAD_NAME                1 (x)      --- >> (target of 34)
             44 LOAD_CONST               3 (3)
             46 COMPARE_OP               2 (==)
             48 POP_JUMP_IF_FALSE       28          --- (to 28 * 2 = 56)

	x = 4
  8          50 LOAD_CONST               4 (4)
             52 STORE_NAME               1 (x)
             54 JUMP_FORWARD             2 (to 58)  --- (to 56 + 2 * 2 = 60)

else:
 	x = 5
10          56 LOAD_CONST               5 (5)
        >>   58 STORE_NAME               1 (x)      --- no >>

import sys
 14          60 LOAD_CONST               6 (0)      --- >> (target of 54)
             62 LOAD_CONST               7 (None)
             64 IMPORT_NAME              2 (sys)
             66 STORE_NAME               2 (sys)

Compare with output from 3.9. The compiled bytecodes are slightly different, but the jump addresses and labelled indictors are correct.
In my opinion, though, the true oparg values (17, etc.), which are shown in 3.10, should be shown instead of the doubled values (34, etc.), which are shown in 3.9. I would consider this a bug in 3.9.

  3           4 SETUP_FINALLY           34 (to 40)
              6 SETUP_FINALLY            8 (to 16)
              8 LOAD_CONST               1 (1)
             10 STORE_NAME               1 (x)
             12 POP_BLOCK
             14 JUMP_FORWARD            16 (to 32)

  4     >>   16 POP_TOP
             18 POP_TOP
             20 POP_TOP
             22 LOAD_CONST               2 (2)
             24 STORE_NAME               1 (x)
             26 POP_EXCEPT
             28 JUMP_FORWARD             2 (to 32)
             30 RERAISE
        >>   32 POP_BLOCK

  5          34 LOAD_CONST               3 (3)
             36 STORE_NAME               1 (x)
             38 JUMP_FORWARD             6 (to 46)
        >>   40 LOAD_CONST               3 (3)
             42 STORE_NAME               1 (x)
             44 RERAISE

  7     >>   46 LOAD_NAME                1 (x)
             48 LOAD_CONST               3 (3)
             50 COMPARE_OP               2 (==)
             52 POP_JUMP_IF_FALSE       60

  8          54 LOAD_CONST               4 (4)
             56 STORE_NAME               1 (x)
             58 JUMP_FORWARD             4 (to 64)

 10     >>   60 LOAD_CONST               5 (5)
             62 STORE_NAME               1 (x)

 14     >>   64 LOAD_CONST               6 (0)
             66 LOAD_CONST               7 (None)
             68 IMPORT_NAME              2 (sys)
             70 STORE_NAME               2 (sys)

Your environment

  • CPython versions tested on: Python 3.10.0 (tags/v3.10.0:b494f59, Oct 4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)] on win32
  • Operating system and architecture: Windows