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


Groups > comp.lang.python > #84871

Re: The Most Diabolical Python Antipattern

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.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; 'subject:Python': 0.06; 'expressions': 0.07; 'finally:': 0.07; '"if': 0.09; 'clause': 0.09; 'except:': 0.09; 'exception,': 0.09; 'present,': 0.09; 'try:': 0.09; 'valueerror:': 0.09; 'variant': 0.09; 'python': 0.11; 'jan': 0.12; 'better?': 0.16; 'clause.': 0.16; 'exception:': 0.16; 'systemexit': 0.16; 'unexpected': 0.16; 'valueerror': 0.16; 'valueerror,': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'entered': 0.20; '(or': 0.24; 'pass': 0.26; 'least': 0.26; 'asking': 0.27; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; 'message-id:@mail.gmail.com': 0.30; 'bug?': 0.31; 'supposed': 0.32; 'skip:d 20': 0.34; 'could': 0.34; 'except': 0.35; 'case,': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'depends': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'catch': 0.60; 'ian': 0.60; 'first': 0.61; 'subject:The': 0.64; 'finally': 0.65; 'potentially': 0.81; '2015': 0.84; 'bare': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=Lh8Yu12+BhfjnIOcnABaD/z/IJtlGUWUtLpDykVzlGw=; b=ZOpkiFPrQ4XaN2+BtG3ZQZdd+pO/q7sTHFjvGfDuV2+R3hRfqy+xO8c7l+Fd4zWI7r S/dECIt3PgPF9+2gThjgPmuK1fnHgh4ht0q+k8pDRvbOIkEH7CT5W34zZyTxfhxAv4fy j37xi0nqdCwapQtL2Z/nDUmt56KsKPEPtKUy2AsI2+ncU/aRGnaHEJbbFT3kWPlA/Tp5 8qwC+L6ajwILW+BwOBrDcDKZohdqYtMq6TkcqkKBggOb5WioquxalE4FCNjBMLr9ix/a jqHwu4/iABtz/MTFTJgmLCfoxIhGv8IRmc6sEJAGPmfeFNHAm8e7lKkwIdxwnZnYV2gL jNdg==
X-Received by 10.66.157.5 with SMTP id wi5mr7278795pab.37.1422609136479; Fri, 30 Jan 2015 01:12:16 -0800 (PST)
MIME-Version 1.0
In-Reply-To <87r3uchjyg.fsf@elektro.pacujo.net>
References <madpuv$he7$1@ger.gmane.org> <20150129113238.7eaa986d@bigbox.christie.dr> <mailman.18286.1422571931.18130.python-list@python.org> <87r3uchjyg.fsf@elektro.pacujo.net>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Fri, 30 Jan 2015 02:11:36 -0700
Subject Re: The Most Diabolical Python Antipattern
To Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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.18299.1422609145.18130.python-list@python.org> (permalink)
Lines 47
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1422609145 news.xs4all.nl 2858 [2001:888:2000:d::a6]:49461
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:84871

Show key headers only | View raw


On Thu, Jan 29, 2015 at 11:16 PM, Marko Rauhamaa <marko@pacujo.net> wrote:
> Ian Kelly <ian.g.kelly@gmail.com>:
>
>> At least use "except Exception" instead of a bare except. Do you
>> really want things like SystemExit and KeyboardInterrupt to get turned
>> into 0?
>
> How about:
>
> ==============================
>     try:
>         do_interesting_stuff()
>     except ValueError:
>         try:
>             log_it()
>         except:
>             pass
>         raise
> ==============================

Are you asking if I think this is better? It still swallows arbitrary
exceptions. Why would you want to re-raise the anticipated (and
logged) ValueError instead of the exception that could potentially be
unexpected?

> Surprisingly this variant could raise an unexpected exception:
>
> ==============================
>     try:
>         do_interesting_stuff()
>     except ValueError:
>         try:
>             log_it()
>         finally:
>             raise
> ==============================
>
> A Python bug?

This does what it is supposed to. "If no expressions are present,
raise re-raises the last exception that was active in the current
scope." In this case, what that exception is depends on whether the
finally clause was entered as a result of an exception or fall-through
from the try clause. If you only want to re-raise the ValueError, then
use the first form above. If you only want to re-raise the other
exception, then do so from an except block (or don't catch it in the
first place).

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


Thread

Re: The Most Diabolical Python Antipattern Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-29 15:51 -0700
  Re: The Most Diabolical Python Antipattern Marko Rauhamaa <marko@pacujo.net> - 2015-01-30 08:16 +0200
    Re: The Most Diabolical Python Antipattern Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-01-30 08:10 +0000
      Re: The Most Diabolical Python Antipattern Marko Rauhamaa <marko@pacujo.net> - 2015-01-30 11:02 +0200
        Re: The Most Diabolical Python Antipattern Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-30 02:17 -0700
        Re: The Most Diabolical Python Antipattern Marko Rauhamaa <marko@pacujo.net> - 2015-01-30 12:00 +0200
          Re: The Most Diabolical Python Antipattern Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-30 08:21 -0700
            Re: The Most Diabolical Python Antipattern Marko Rauhamaa <marko@pacujo.net> - 2015-01-30 17:30 +0200
              Re: The Most Diabolical Python Antipattern Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-30 08:42 -0700
                Re: The Most Diabolical Python Antipattern Marko Rauhamaa <marko@pacujo.net> - 2015-01-30 17:56 +0200
                Re: The Most Diabolical Python Antipattern Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-30 09:38 -0700
              Re: The Most Diabolical Python Antipattern Chris Angelico <rosuav@gmail.com> - 2015-01-31 02:55 +1100
    Re: The Most Diabolical Python Antipattern Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-30 02:11 -0700
    Re: The Most Diabolical Python Antipattern Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-01-31 01:28 +0000

csiph-web