[Python-Dev] PEP for allowing 'raise NewException from None' (original) (raw)

Ethan Furman ethan at stoneleaf.us
Sun Jan 29 08:44:32 CET 2012


For those not on the nosy list, here's the latest post to http://bugs.python.org/issue6210:


It looks like agreement is forming around the

 raise ... from None

method. It has been mentioned more than once that having the context saved on the exception would be a Good Thing, and for further debugging (or logging or what-have-you) I must agree.

The patch attached now sets cause to True, leaving context unclobbered. The exception printing routine checks to see if cause is True, and if so simply skips the display of either cause or context, but context can still be queried by later code.

One concern raised was that since it is possible to write (even before this patch)

 raise KeyError from NameError

outside of a try block that some would get into the habit of writing

 raise KeyError from None

as a way of preemptively suppressing implicit context chaining; I am happy to report that this is not an issue, since when that exception is caught and a new exception raised, it is the new exception that controls the display.

In other words:

try: ... raise ValueError from None ... except: ... raise NameError ... Traceback (most recent call last): File "", line 2, in ValueError

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 4, in NameError



More information about the Python-Dev mailing list