[Python-Dev] PEP for allowing 'raise NewException from None'

Ethan Furman ethan at stoneleaf.us
Sat Jan 28 19:56:57 CET 2012


Nick Coghlan wrote:
> On Sat, Jan 28, 2012 at 10:33 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
>> So the question is:
>>
>>  - should 'raise ... from ...' be legal outside a try block?
>>
>>  - should 'raise ... from None' be legal outside a try block?
> 
> Given that it would be quite a bit of work to make it illegal, my
> preference is to leave it alone.
> 
> I believe that means there's only one open question. Should "raise ex
> from None" be syntactic sugar for:
> 
> 1. clearing the current thread's exception state (as I believe Ethan's
> patch currently does), thus meaning that __context__ and __cause__
> both end up being None
> 2. setting __cause__ to None (so that __context__ still gets set
> normally, as it is now when __cause__ is set to a specific exception),
> and having __cause__ default to a *new* sentinel object that indicates
> "use __context__"
> 
> I've already stated my own preference in favour of 2 - that approach
> means developers that think about it can explicitly change exception
> types such that the context isn't displayed by default, but
> application and framework developers remain free to insert their own
> exception handlers that *always* report the full exception stack.

The reasoning behind choice two makes a lot of sense.  My latest effort 
(I should be able to get the patch posted within two days) involves 
creating a new dummy exception, SuppressContext, and 'raise ... from 
None' sets cause to it; the printing logic checks to see if cause is 
SuppressContext, and if so, prints neither context nor cause.

Not exactly how Nick describes it, but as far as I've gotten in my 
Python core hacking skills.  ;)

~Ethan~


More information about the Python-Dev mailing list