[Python-Dev] PEP 409 - final?
Ethan Furman
ethan at stoneleaf.us
Wed Feb 1 19:48:33 CET 2012
Guido van Rossum wrote:
> Hm... Reading this draft, I like the idea of using "raise X from
> None", but I still have one quibble. It seems the from clause sets
> __cause__, and __cause__ can indicate three things: (1) print
> __cause__ (explicitly set), (2) print __context__ (default), (3) print
> neither (raise X from None). For (1), __cause__ must of course be a
> traceback object.
Actually, for (1) __cause__ is an exception object, not a traceback.
> The PEP currently proposes to use two special
> values: False for (2), None for (3). To me, this has a pretty strong
> code smell, and I don't want this pattern to be enshrined in a PEP as
> an example for all to follow. (And I also don't like "do as I say,
> don't do as I do." :-)
My apologies for my ignorance, but is the code smell because both False
and None evaluate to bool(False)? I suppose we could use True for (2)
to indicate that __context__ should be printed, leaving None for (3)...
but having __context__ at None and __cause__ at True could certainly be
confusing (the default case when no chaining is in effect).
> Can we think of a different special value to distinguish between (2)
> and (3)? Ideally one that doesn't change the nice "from None" idiom,
> which I actually like as a way to spell this.
How about this:
Exception Life Cycle
====================
Stage 1 - brand new exception
-----------------------------
raise ValueError()
* __context__ is None
* __cause__ is None
Stage 2 - exception caught, exception raised
--------------------------------------------
try:
raise ValueError()
except Exception:
raise CustomError()
* __context__ is previous exception
* __cause__ is True
Stage 3 - exception raised from [exception | None]
--------------------------------------------------
try:
raise ValueError()
except Exception:
raise CustomError() from [OtherException | None]
* __context__ is previous exception
* __cause__ is [OtherException | None]
>
> Sorry that life isn't easier,
Where would be the fun without the challenge?
~Ethan~
More information about the Python-Dev
mailing list