Problem with Unittest:

Francis Avila francisgavila at yahoo.com
Tue May 13 18:33:07 EDT 2003


"Terry Reedy" <tjreedy at udel.edu> wrote in message
news:88ycnaV9r5MLqlyjXTWcog at comcast.com...
>
> "Francis Avila" <francisgavila at yahoo.com> wrote in message
> news:vc23a17s1t30e3 at corp.supernews.com...
> > If you've gotten into the habit of raising exception instances
> rather than
> > classes, break it right now, or you'll be plagued with problems like
> this.
>
> Bad advice, I believe.  When you raise an exception class, it is
> converted to an exception instance which is tested against the classes
> listed in except statements with isinstance().  It has been suggested
> on PyDev that raising instances directly might someday (Python 3.0) be
> required.  IE, when raise string is disallowed, so might be raise
> class.
>
> TJR
>

But that seems completely backwards to me.  I thought raising instances was
depreciated (ok, "discouraged") when we switched over to exception classes
instead of strings.  And so now we raise classes instead of (for now only
conceptually) instances of string objects.  It would seem instead that
raising *instances* (including all the now-builtin types) would be
depreciated, and only classes could be raised, which are implicitly
instantiated before being tossed up.  Is the suggestion really that every
raise statement be

raise Exception(arg)

instead of

raise Exception, arg

as we have now?  I *never* see anyone raise a class instance explicitly, and
won't except statements *require* a class rather than a instance?


In any case, I was completely wrong about what the OP's problem was: the
entire issue was in the arguments to assertRaises, as Matt Brubrek
explained.  Still, I thought I recalled getting burned by trying to raise an
instance instead of a class? Maybe I am mis-remembering, or it was some
other problem I didn't notice.

(What I mean by "raising an x" is that x is the expression in the raise
statement, not that x is what is actually being associated with the
traceback.  I'm getting the impression that I'm not using this phrase the
same way everyone else is....)
--
Francis Avila





More information about the Python-list mailing list