[issue40222] "Zero cost" exception handling

Mark Shannon report at bugs.python.org
Wed Apr 21 06:23:21 EDT 2021


Mark Shannon <mark at hotpy.org> added the comment:

The changes to pyc format aren't user visible so shouldn't matter,
but what about the dis output?

Consider this program:

def f():
    try:
        1/0
    except:
        return "fail"

Currently it compiles to:
  2           0 SETUP_FINALLY            7 (to 16)

  3           2 LOAD_CONST               1 (1)
              4 LOAD_CONST               2 (0)
              6 BINARY_TRUE_DIVIDE
              8 POP_TOP
             10 POP_BLOCK
             12 LOAD_CONST               0 (None)
             14 RETURN_VALUE

  4     >>   16 POP_TOP
             18 POP_TOP
             20 POP_TOP

  5          22 POP_EXCEPT
             24 LOAD_CONST               3 ('fail')
             26 RETURN_VALUE

With zero-cost exception handling, it will compile to something like:  
  2           0 NOP
  3           2 LOAD_CONST               1 (1)
              4 LOAD_CONST               2 (0)
              6 BINARY_TRUE_DIVIDE
              8 POP_TOP
             10 LOAD_CONST               0 (None)
             12 RETURN_VALUE

  None       14 PUSH_EXCEPT

  4          16 POP_TOP
             18 POP_TOP
             20 POP_TOP

  5          22 POP_EXCEPT
             24 LOAD_CONST               3 ('fail')
             26 RETURN_VALUE

(There are additional optimizations that should be applied, but those are a separate issue)

The problem is that the exception handling flow is no longer visible.
Should we add it back in somehow, or just append the exception jump table?

----------

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


More information about the Python-bugs-list mailing list