Xavier de Gaye <xdeg...@gmail.com> added the comment:

@Saim wrote
> https://docs.python.org/3/reference/compound_stmts.html#the-try-statement says

This is correct, the disassembly of foo.py below demonstrates that point: 
BEGIN_FINALLY at bytecode index 36 starts the implementation of the 'finally' 
clause that deletes 'err'. But pdb is a line debugger, not a debugger at the 
bytecode level, so when 'foo = 1' is replaced by 'import pdb; pdb.set_trace()', 
the first line where pdb may stop is at line 6 and since foo() last line is 
line 5, pdb stops at the 'return' event for this function.

=======   foo.py   ========
def foo():
    try:
        1/0
    except Exception as err:
        foo = 1

import dis
dis.dis(foo)
===========================
  2           0 SETUP_FINALLY           12 (to 14)

  3           2 LOAD_CONST               1 (1)
              4 LOAD_CONST               2 (0)
              6 BINARY_TRUE_DIVIDE
              8 POP_TOP
             10 POP_BLOCK
             12 JUMP_FORWARD            38 (to 52)

  4     >>   14 DUP_TOP
             16 LOAD_GLOBAL              0 (Exception)
             18 COMPARE_OP              10 (exception match)
             20 POP_JUMP_IF_FALSE       50
             22 POP_TOP
             24 STORE_FAST               0 (err)
             26 POP_TOP
             28 SETUP_FINALLY            8 (to 38)

  5          30 LOAD_CONST               1 (1)
             32 STORE_FAST               1 (foo)
             34 POP_BLOCK
             36 BEGIN_FINALLY
        >>   38 LOAD_CONST               0 (None)
             40 STORE_FAST               0 (err)
             42 DELETE_FAST              0 (err)
             44 END_FINALLY
             46 POP_EXCEPT
             48 JUMP_FORWARD             2 (to 52)
        >>   50 END_FINALLY
        >>   52 LOAD_CONST               0 (None)
             54 RETURN_VALUE
===========================

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue36537>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to