This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author serhiy.storchaka
Recipients nedbat, serhiy.storchaka
Date 2018-09-16.14:28:56
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1537108136.62.0.956365154283.issue34705@psf.upfronthosting.co.za>
In-reply-to
Content
This is a side effect of specific optimization. If the return value is constant, it is pushed on the stack after executing the finally code (see LOAD_CONST at offset 14 below). But other opcodes at this line (POP_BLOCK and CALL_FINALLY) are executed after executing the finally code. Thus it looks like the line 4 is executed twice, but actually different opcodes marked with the same line are executed before and after executing the finally code.

Disassembly of <code object return_from_finally at 0x7feff78897c0, file "<stdin>", line 1>:
  2           0 SETUP_FINALLY           16 (to 18)

  3           2 LOAD_GLOBAL              0 (print)
              4 LOAD_CONST               1 ('returning')
              6 CALL_FUNCTION            1
              8 POP_TOP

  4          10 POP_BLOCK
             12 CALL_FINALLY             4 (to 18)
             14 LOAD_CONST               2 (17)
             16 RETURN_VALUE

  6     >>   18 LOAD_GLOBAL              0 (print)
             20 LOAD_CONST               3 ('finally')
             22 CALL_FUNCTION            1
             24 POP_TOP
             26 END_FINALLY
             28 LOAD_CONST               0 (None)
             30 RETURN_VALUE

The benefit of this optimization is that it can make the stack smaller. This decreases the memory consumption of the Python function frame by one pointer and speeds up the Python function frame creation time (one pointer assignment less). It is tiny, but I think it is worth to keep it.

I don't know what is the right solution here.
History
Date User Action Args
2018-09-16 14:28:56serhiy.storchakasetrecipients: + serhiy.storchaka, nedbat
2018-09-16 14:28:56serhiy.storchakasetmessageid: <1537108136.62.0.956365154283.issue34705@psf.upfronthosting.co.za>
2018-09-16 14:28:56serhiy.storchakalinkissue34705 messages
2018-09-16 14:28:56serhiy.storchakacreate