Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #65169

Re: Try-except-finally paradox

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!border2.nntp.ams2.giganews.com!border1.nntp.ams2.giganews.com!backlog3.nntp.ams3.giganews.com!backlog3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <self@gkayaalp.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'algorithm': 0.04; 'context': 0.07; 'finally:': 0.07; 'clause': 0.09; 'except:': 0.09; 'executes': 0.09; 'exist,': 0.09; 'jessica': 0.09; 'reached.': 0.09; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'jan': 0.12; 'wrote': 0.14; '"finally"': 0.16; '(true)': 0.16; 'clause.': 0.16; 'discussion.': 0.16; 'executed.': 0.16; 'received:72.5': 0.16; 'received:72.5.230': 0.16; 'received:72.5.230.103': 0.16; 'received:sender1.zohomail.com': 0.16; 'received:zohomail.com': 0.16; 'reedy': 0.16; 'return,': 0.16; 'wrote:': 0.18; 'seems': 0.21; '>>>': 0.22; 'appears': 0.22; 'code,': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'header :User-Agent:1': 0.23; 'exists': 0.24; 'mind.': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; "i've": 0.25; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'rest': 0.29; 'am,': 0.29; 'raise': 0.29; 'code': 0.31; 'assert': 0.31; 'block,': 0.31; 'explained': 0.31; 'raised': 0.31; 'writes:': 0.31; 'this.': 0.32; 'run': 0.32; 'quite': 0.32; 'checking': 0.33; 'except': 0.35; 'something': 0.35; 'but': 0.35; 'false': 0.36; 'keyword': 0.36; 'should': 0.36; 'example,': 0.37; 'so,': 0.37; 'rather': 0.38; 'does': 0.39; 'dave': 0.60; 'most': 0.60; 'from:charset:utf-8': 0.61; "you're": 0.61; 'first': 0.61; "you'll": 0.62; 'information': 0.63; 'happen': 0.63; 'chance': 0.65; 'finally': 0.65; 'finish': 0.65; 'within': 0.65; 'topic,': 0.81; 'truth': 0.81; 'returns.': 0.84; 'angel': 0.91
From Göktuğ Kayaalp <self@gkayaalp.com>
To Terry Reedy <tjreedy@udel.edu>
Subject Re: Try-except-finally paradox
References <9314ac52-a2be-4382-94ef-2c291f32be1a@googlegroups.com> <lcdf0c$bnl$2@ger.gmane.org> <lcfc58$bep$1@ger.gmane.org>
Date Sat, 01 Feb 2014 03:58:19 +0200
In-Reply-To <lcfc58$bep$1@ger.gmane.org> (Terry Reedy's message of "Fri, 31 Jan 2014 00:26:05 -0500")
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)
MIME-Version 1.0
Content-Type text/plain
X-ZohoMailClient External
X-Zoho-Virus-Status 2
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.6247.1391223064.18130.python-list@python.org> (permalink)
Lines 106
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1391223064 news.xs4all.nl 2858 [2001:888:2000:d::a6]:50955
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:65169
X-Original-Bytes 7004

Show key headers only | View raw


Terry Reedy <tjreedy@udel.edu> writes:

I do not have any information on the topic, but I *imagine* that the
when RETURN_VALUE opcode is evaluated within the context of an except
block, it triggers a check for whether a corresponding finally block
exists and should it exist, it is triggered, much like a callback.
So, my *imaginary* algorithm works like this:

return:
  if within an except block EB:
    if EB has a correspoinding final block FB
      run FB
    else
      do return
  else
    do return

In Jessica's example, as the finally block executes a RETURN_VALUE
opcode, and as this is *probably* a jump to the caller, the rest of the
code does not get a chance to be executed.

As I have said earlier, I by no means assert the truth of this idea I've
explained here, but it seems quite reasonable to me.

          gk

> On 1/30/2014 7:05 AM, Dave Angel wrote:
>>   Jessica Ross <deathweasel@gmail.com> Wrote in message:
>>> I found something like this in a StackOverflow discussion.
>>>>>> def paradox():
>>> ...     try:
>>> ...             raise Exception("Exception raised during try")
>>> ...     except:
>>> ...             print "Except after try"
>>> ...             return True
>>> ...     finally:
>>> ...             print "Finally"
>>> ...             return False
>>> ...     return None
>>> ...
>>>>>> return_val = paradox()
>>> Except after try
>>> Finally
>>>>>> return_val
>>> False
>>>
>>> I understand most of this.
>>> What I don't understand is why this returns False rather than True. Does the finally short-circuit the return in the except block?
>>>
>>
>> The finally has to happen before any return inside the try or the
>>   except.  And once you're in the finally clause you'll finish it
>>   before resuming the except clause.  Since it has a return,  that
>>   will happen before the other returns. The one in the except block
>>   will never get reached.
>>
>> It's the only reasonable behavior., to my mind.
>
> Checking with the disassembled code, it appears that the except return
> happens first and is then caught and the value over-written
>
>   2           0 SETUP_FINALLY           45 (to 48)
>               3 SETUP_EXCEPT            16 (to 22)
>
>   3           6 LOAD_GLOBAL              0 (Exception)
>               9 LOAD_CONST               1 ('Exception raised during try')
>              12 CALL_FUNCTION            1 (1 positional, 0 keyword pair)
>              15 RAISE_VARARGS            1
>              18 POP_BLOCK
>              19 JUMP_FORWARD            22 (to 44)
>
>   4     >>   22 POP_TOP
>              23 POP_TOP
>              24 POP_TOP
>
>   5          25 LOAD_GLOBAL              1 (print)
>              28 LOAD_CONST               2 ('Except after try')
>              31 CALL_FUNCTION            1 (1 positional, 0 keyword pair)
>              34 POP_TOP
>
>   6          35 LOAD_CONST               3 (True)
>              38 RETURN_VALUE
>              39 POP_EXCEPT
>              40 JUMP_FORWARD             1 (to 44)
>              43 END_FINALLY
>         >>   44 POP_BLOCK
>              45 LOAD_CONST               0 (None)
>
>   8     >>   48 LOAD_GLOBAL              1 (print)
>              51 LOAD_CONST               4 ('Finally')
>              54 CALL_FUNCTION            1 (1 positional, 0 keyword pair)
>              57 POP_TOP
>
>   9          58 LOAD_CONST               5 (False)
>              61 RETURN_VALUE
>              62 END_FINALLY
>
>  10          63 LOAD_CONST               0 (None)
>              66 RETURN_VALUE
>
>
>
>
> -- 
> Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Try-except-finally paradox Jessica Ross <deathweasel@gmail.com> - 2014-01-29 21:56 -0800
  Re: Try-except-finally paradox Ian Kelly <ian.g.kelly@gmail.com> - 2014-01-29 23:23 -0700
  Re: Try-except-finally paradox Andrew Berg <robotsondrugs@gmail.com> - 2014-01-30 00:33 -0600
    Re: Try-except-finally paradox Rotwang <sg552@hotmail.co.uk> - 2014-01-30 18:12 +0000
      Re: Try-except-finally paradox Ethan Furman <ethan@stoneleaf.us> - 2014-01-30 10:30 -0800
  Re: Try-except-finally paradox wxjmfauth@gmail.com - 2014-01-29 22:59 -0800
  Re:Try-except-finally paradox Dave Angel <davea@davea.name> - 2014-01-30 07:05 -0500
  Re: Try-except-finally paradox Chris Angelico <rosuav@gmail.com> - 2014-01-31 00:02 +1100
  Re: Try-except-finally paradox MRAB <python@mrabarnett.plus.com> - 2014-01-30 13:11 +0000
  Re: Try-except-finally paradox Chris Angelico <rosuav@gmail.com> - 2014-01-31 00:19 +1100
  Re: Try-except-finally paradox Terry Reedy <tjreedy@udel.edu> - 2014-01-31 00:26 -0500
  Re: Try-except-finally paradox Göktuğ Kayaalp <self@gkayaalp.com> - 2014-02-01 03:58 +0200

csiph-web