[Python-ideas] Make return inside a finally a SyntaxError

Michael Foord fuzzyman at gmail.com
Sat Jul 18 13:27:46 CEST 2009


2009/7/18 Stephen J. Turnbull <stephen at xemacs.org>

> Greg Ewing writes:
>  > Michael wrote:
>  > > Here are two examples of why allowing return inside a finally block is
>  > > a bad idea:
>  >
>  > Don't do those things, then!
>  >
>  > Nobody is *forcing* you to write returns inside
>  > finally blocks.
>
> +1
>
> Why not just emphasize in the documentation that return (etc) in a
> finally: suite *will* get executed *after* return or an exception is
> raised in the try: suite?


That exceptions can be silently swallowed in a finally block (which is
'expected' and usually intended to propagate exceptions) in the presence of
a return (or a break apparently) is worrying.

Another solution would be to have the exception raised instead of swallowed.
There is a harder migration regarding backwards compatibility though - you
can only warn when the exception is swallowed which may never be seen by the
programmer.

Michael





> "These stunts were performed by
> professionals.  Don't try this at home, kids."
>
> More specifically, maybe there should be an explicit warning that in a
> finally: suite
>
>    if exit_condition:
>        return
>    do_work()
>    # end of suite
>
> has (perhaps surprisingly, YMMV) different semantics from
>
>    if not exit_condition:
>        do_work()
>    # end of suite
>
> WDOT?
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>



-- 
http://www.ironpythoninaction.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20090718/4fd58bc3/attachment.html>


More information about the Python-ideas mailing list