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


Groups > comp.lang.python > #101035

Re: raise None

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: raise None
Date Thu, 31 Dec 2015 13:12:37 +1100
Lines 42
Message-ID <mailman.98.1451528367.11925.python-list@python.org> (permalink)
References <56847239$0$1590$c3e8da3$5496439d@news.astraweb.com> <mailman.96.1451522295.11925.python-list@python.org> <5684842a$0$1596$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de vFQbvqekC7cyZXzfc90vuQX+2bdZuxQTw4DXlrBVcL5Q==
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'value,': 0.03; 'none,': 0.05; 'none:': 0.05; 'type,': 0.07; 'cc:addr:python-list': 0.09; 'falls': 0.09; 'none"': 0.09; 'none.': 0.09; 'subject:None': 0.09; 'valueerror': 0.09; 'exception': 0.13; 'def': 0.13; 'explicitly': 0.15; 'thu,': 0.15; '39,': 0.16; 'b):': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'function?': 0.16; 'gotcha.': 0.16; 'no-op,': 0.16; 'read-only.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'work,': 0.21; '31,': 0.22; 'exceptions': 0.22; 'bit': 0.23; 'dec': 0.23; '(most': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'error': 0.27; 'compare': 0.27; 'message- id:@mail.gmail.com': 0.27; 'function': 0.28; 'raise': 0.29; 'another': 0.32; 'skip:_ 10': 0.32; 'class': 0.33; "d'aprano": 0.33; 'raising': 0.33; 'steven': 0.33; 'traceback': 0.33; 'file': 0.34; 'received:google.com': 0.35; 'trouble': 0.35; 'something': 0.35; 'level': 0.35; 'but': 0.36; 'instead': 0.36; 'there': 0.36; 'lines': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'say': 0.37; 'received:209.85.213': 0.37; 'doing': 0.38; 'received:209': 0.38; 'self': 0.38; 'end': 0.39; 'along': 0.39; 'save': 0.60; 'today,': 0.62; 'more': 0.63; 'natural': 0.67; '(is': 0.84; 'chrisa': 0.84; 'to:none': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=nEuuq6CGw83vca3rSsYTGnPG0XpAMrnVmThL83inwqs=; b=03Qh6hejYG+IotFk9d26hdg+dW9gZN5NmUD1iAZKV/68+GBe1OAME2c5pjOy+51tO2 naXjx9/VGT780Kbiv7THOTJHrVd+HSPD7GV8x/CamT4AjQQI0sTAUMgEve7FTAjUmwtO lwQBfK0FVfnDE1QlVhntLBivpsjr0Q76XBYfbTu/LEsZhvmKFUhXQxVyJx0w7fzSovsg 8HOIp6AaC9V9ebBp0iDcM/NXJVN2L0GQIkk2OR7mjYUHHd0rSwJ9KqDu/+YOWS7/nOxg N+npEKJG6Cd1mfRaKIkMD8NfXH4mi/GRoiT5K03HYEazDITIwtYYrt1iObIwbyNQ7Iog lnPg==
X-Received by 10.50.60.6 with SMTP id d6mr11771906igr.94.1451527957415; Wed, 30 Dec 2015 18:12:37 -0800 (PST)
In-Reply-To <5684842a$0$1596$c3e8da3$5496439d@news.astraweb.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
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>
Xref csiph.com comp.lang.python:101035

Show key headers only | View raw


On Thu, Dec 31, 2015 at 12:26 PM, Steven D'Aprano <steve@pearwood.info> wrote:
> Traceback (most recent call last):
>   File "spam", line 19, in this
>   File "spam", line 29, in that
>   File "spam", line 39, in other
> ThingyError: ...
>
>
> I think this is a win for debuggability. (Is that a word?) But it's a bit
> annoying to do it today, since you have to save the return result and
> explicitly compare it to None. If "raise None" was a no-op, it would feel
> more natural to just say raise _validate() and trust that if _validate
> falls out the end and returns None, the raise will be a no-op.

(Yes, it is.)

Gotcha. So here's an alternative possibility. Instead of raising None
doing nothing, what you really want is to have _validate signal an
error with one less level of traceback - that is, you want it to raise
an exception from the calling function.

class remove_traceback_level:
    def __enter__(self): return self
    def __exit__(self, type, value, traceback):
        if type is None: return
        tb = traceback
        while tb.tb_next: tb = tb.tb_next
        tb.tb_next = None
        raise value from traceback

def _validate(a, b):
    with remove_traceback_level():
        if condition(a) or condition(b): raise TypeError
        if other_condition(a) or something_else(b): raise ValueError
        if whatever(a): raise SomethingError


The trouble is that this doesn't actually work, because tb_next is
read-only. But is there something along these lines that would make a
function raise exceptions as if it were in another function?

ChrisA

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


Thread

raise None Steven D'Aprano <steve@pearwood.info> - 2015-12-31 11:09 +1100
  Re: raise None Paul Rubin <no.email@nospam.invalid> - 2015-12-30 16:19 -0800
  Validation in Python (was: raise None) Ben Finney <ben+python@benfinney.id.au> - 2015-12-31 11:26 +1100
  Re: raise None Chris Angelico <rosuav@gmail.com> - 2015-12-31 11:38 +1100
    Re: raise None Steven D'Aprano <steve@pearwood.info> - 2015-12-31 12:26 +1100
      Re: raise None Ben Finney <ben+python@benfinney.id.au> - 2015-12-31 12:44 +1100
        Re: raise None Steven D'Aprano <steve@pearwood.info> - 2015-12-31 15:07 +1100
          Re: raise None Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-12-31 12:19 +0000
            Re: raise None Steven D'Aprano <steve@pearwood.info> - 2016-01-01 02:35 +1100
              Re: raise None Chris Angelico <rosuav@gmail.com> - 2016-01-01 02:53 +1100
              Re: raise None Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-12-31 16:46 +0000
                Re: raise None Steven D'Aprano <steve@pearwood.info> - 2016-01-01 04:50 +1100
              Re: raise None "Martin A. Brown" <martin@linux-ip.net> - 2015-12-31 09:30 -0800
              Re: raise None Ben Finney <ben+python@benfinney.id.au> - 2016-01-01 07:18 +1100
                Re: raise None Johannes Bauer <dfnsonfsduifb@gmx.de> - 2016-01-02 12:47 +0100
              Re: raise None Chris Angelico <rosuav@gmail.com> - 2016-01-01 09:48 +1100
                Re: raise None Steven D'Aprano <steve@pearwood.info> - 2016-01-04 16:19 +1100
                Re: raise None Dan Sommers <dan@tombstonezero.net> - 2016-01-04 06:09 +0000
                Re: raise None Rustom Mody <rustompmody@gmail.com> - 2016-01-03 22:39 -0800
              Re: raise None Ben Finney <ben+python@benfinney.id.au> - 2016-01-01 10:27 +1100
                Re: raise None Marko Rauhamaa <marko@pacujo.net> - 2016-01-01 02:29 +0200
                Re: raise None Steven D'Aprano <steve@pearwood.info> - 2016-01-04 16:19 +1100
                Re: raise None Rustom Mody <rustompmody@gmail.com> - 2016-01-03 21:53 -0800
                Re: raise None Rustom Mody <rustompmody@gmail.com> - 2016-01-04 03:55 -0800
                Re: raise None Rustom Mody <rustompmody@gmail.com> - 2016-01-03 21:53 -0800
              Re: raise None Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-12-31 23:36 +0000
              Re: raise None Chris Angelico <rosuav@gmail.com> - 2016-01-01 10:39 +1100
              Re: raise None Chris Angelico <rosuav@gmail.com> - 2016-01-01 10:41 +1100
              Re: raise None Rustom Mody <rustompmody@gmail.com> - 2016-01-03 19:04 -0800
                Re: raise None Chris Angelico <rosuav@gmail.com> - 2016-01-04 14:31 +1100
                Re: raise None Steven D'Aprano <steve@pearwood.info> - 2016-01-04 14:48 +1100
                Re: raise None Chris Angelico <rosuav@gmail.com> - 2016-01-04 14:56 +1100
                Re: raise None Rustom Mody <rustompmody@gmail.com> - 2016-01-03 20:46 -0800
              Re: raise None Christian Gollwitzer <auriocus@gmx.de> - 2016-01-04 08:28 +0100
      Re: raise None Chris Angelico <rosuav@gmail.com> - 2015-12-31 13:12 +1100
      Re: raise None Cameron Simpson <cs@zip.com.au> - 2015-12-31 15:03 +1100
        Re: raise None Steven D'Aprano <steve@pearwood.info> - 2015-12-31 16:12 +1100
          Re: raise None Cameron Simpson <cs@zip.com.au> - 2015-12-31 16:45 +1100
  Re: raise None Terry Reedy <tjreedy@udel.edu> - 2015-12-30 23:00 -0500
  Re: raise None Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-12-31 15:58 +0000
  Re: raise None Johannes Bauer <dfnsonfsduifb@gmx.de> - 2015-12-31 17:17 +0100

csiph-web