[Python-bugs-list] [ python-Feature Requests-722498 ] assert could raise other Exceptions

SourceForge.net noreply@sourceforge.net
Mon, 12 May 2003 10:51:14 -0700


Feature Requests item #722498, was opened at 2003-04-16 09:08
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=722498&group_id=5470

Category: Python Interpreter Core
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Gregory Smith (gregsmith)
Assigned to: Nobody/Anonymous (nobody)
Summary: assert could raise other Exceptions

Initial Comment:

how about having 'assert' check its second parameter,
and if it is an instance of Exception, raise that instead
of AssertionError:

  try:
         n = int(argv[i])
         assert 0 <= n <  10, ValueError("n out of range")
   except ValueError,e:
         print "Bad parameter:", str(e)



This is a fair bit tidier than

      if not ( 0 <= n < 10 ): raise ValueError, "n out
of range"

you could also do 'except (ValueError,
AssertionError)", but
the 'except' might not  be so close by, it would be nice to
have assert raise something else.

Having written this, I just realized that assert is
supposed
to go away when '-O' is selected (I never use that, myself)
so maybe this is not quite as useful -- but still quite
free of downside. 

I know nobody wants a new keyword, how about

     assert >> ValueError  , 0 <= n < 10 , n out of range

... which would do the same as the first example but
not be deleted by -O









----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2003-05-12 12:51

Message:
Logged In: YES 
user_id=80475

I concur with Martin.  While handy, this idea is not 
consistent with the intended purpose for assertions.  Also, 
I don't find the >> notation to be attractive.

I recommend leaving this one out.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2003-04-18 11:26

Message:
Logged In: YES 
user_id=21627

I don't like this idea. Assertions are something that you
can take out without changing the interface (and indeed -O
takes them out). They are supposed to never happen, not even
when an application uses a library incorrectly.

If you need to perform correctness checks on parameters,
those checks belong to the API of the function, and should
be documented and implemented with a proper if statement.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=722498&group_id=5470