[Python-Dev] PEP 409 and the stdlib

Ethan Furman ethan at stoneleaf.us
Tue May 21 18:03:13 CEST 2013


On 05/21/2013 04:23 AM, Nick Coghlan wrote:
> On Tue, May 21, 2013 at 5:17 PM, Hrvoje Niksic <hrvoje.niksic at avl.com> wrote:
>> On 05/20/2013 05:15 PM, Ethan Furman wrote:
>>>
>>> 1)  Do nothing and be happy I use 'raise ... from None' in my own
>>> libraries
>>>
>>> 2)  Change the wording of 'During handling of the above exception, another
>>> exception occurred' (no ideas as to what at
>>> the moment)
>>
>>
>> The word "occurred" misleads one to think that, during handling of the real
>> exception, an unrelated and unintended exception occurred.  This is not the
>> case when the "raise" keyword is used.  In that case, the exception was
>> intentionally *converted* from one type to another.  For the "raise" case a
>> wording like the following might work better:
>>
>>      The above exception was converted to the following exception:
>>      ...
>>
>> That makes it clear that the conversion was explicit and (hopefully)
>> intentional, and that the latter exception supersedes the former.
>
> This ship sailed long ago (it was covered by the original exception
> chaining spec in PEP 3134). If you want to deliberately replace an
> exception while retaining the full traceback, you use "raise X from
> Y", and the intro text will change to something like "This exception
> was the direct cause of the following exception:"

I had forgotten about that, Nick, thanks.

So the moral of the story for our library code and replacing exceptions is we should either do

     raise ... from OldException

or

     raise ... from None

depending on the importance of the originating exception.

And, of course, we only make these changes when we're already modifying the module for some other reason.

--
~Ethan~


More information about the Python-Dev mailing list