Skip to content
Snippets Groups Projects
Select Git revision
  • 583591134c2a7c09a288d0a62dd1bd92728621aa
  • main default protected
  • 3.10
  • 3.11
  • revert-15688-bpo-38031-_io-FileIO-opener-crash
  • 3.8
  • 3.9
  • 3.7
  • enum-fix_auto
  • branch-v3.11.0
  • backport-c3648f4-3.11
  • gh-93963/remove-importlib-resources-abcs
  • refactor-wait_for
  • shared-testcase
  • v3.12.0a2
  • v3.12.0a1
  • v3.11.0
  • v3.8.15
  • v3.9.15
  • v3.10.8
  • v3.7.15
  • v3.11.0rc2
  • v3.8.14
  • v3.9.14
  • v3.7.14
  • v3.10.7
  • v3.11.0rc1
  • v3.10.6
  • v3.11.0b5
  • v3.11.0b4
  • v3.10.5
  • v3.11.0b3
  • v3.11.0b2
  • v3.9.13
34 results

exception_handling_notes.txt

Blame
  • exception_handling_notes.txt 5.86 KiB
    Description of exception handling in Python 3.11
    ------------------------------------------------
    
    Python 3.11 uses what is known as "zero-cost" exception handling.
    Prior to 3.11, exceptions were handled by a runtime stack of "blocks".
    
    In zero-cost exception handling, the cost of supporting exceptions is minimized.
    In the common case (where no exception is raised) the cost is reduced
    to zero (or close to zero).
    The cost of raising an exception is increased, but not by much.
    
    The following code:
    
    def f():
        try:
            g(0)
        except:
            return "fail"
    
    compiles as follows in 3.10:
    
      2           0 SETUP_FINALLY            7 (to 16)
    
      3           2 LOAD_GLOBAL              0 (g)
                  4 LOAD_CONST               1 (0)
                  6 CALL_NO_KW               1
                  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
    
    Note the explicit instructions to push and pop from the "block" stack:
    SETUP_FINALLY and POP_BLOCK.
    
    In 3.11, the SETUP_FINALLY and POP_BLOCK are eliminated, replaced with
    a table to determine where to jump to when an exception is raised.
    
      1           0 RESUME                   0
    
      2           2 NOP
    
      3           4 LOAD_GLOBAL              1 (NULL + g)
                 16 LOAD_CONST               1 (0)
                 18 PRECALL                  1
                 22 CALL                     1
                 32 POP_TOP
                 34 LOAD_CONST               0 (None)
                 36 RETURN_VALUE
            >>   38 PUSH_EXC_INFO
    
      4          40 POP_TOP
    
      5          42 POP_EXCEPT
                 44 LOAD_CONST               2 ('fail')
                 46 RETURN_VALUE
            >>   48 COPY                     3
                 50 POP_EXCEPT
                 52 RERAISE                  1
    ExceptionTable:
      4 to 32 -> 38 [0]
      38 to 40 -> 48 [1] lasti