[issue39725] unrelated `from None` exceptions lose prior exception information

Ethan Furman report at bugs.python.org
Sun Feb 23 13:24:47 EST 2020


Ethan Furman <ethan at stoneleaf.us> added the comment:

I think the way forward is going to be a recursive walk back to the first exception, and if __suppress_context__ is true for any exception then only the previous exception is omitted.

For example, the above example had the following chain:

Exception    __context__    __cause__    __suppress_context__
---------    -----------    ---------    --------------------
TypeError:     None           None          False
KeyError:      TypeError      None          False
KeyError:      KeyError       None          True

When walking the stack to decide which items get printed, the final KeyError is printed (obviously, as it's the last one), but because its __suppress_context__ is True then the immediately preceding exception, the first KeyError, is skipped; however, the first KeyError's __suppress_context__ is False, so we do print its __context__, which is the TypeError.  Giving us a traceback similar to:

---------------------------------------------------------------------
Traceback (most recent call last):
TypeError: str expected, not type

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
KeyError: 'NEW_VARIABLE'
---------------------------------------------------------------------

Which is entirely correct.

In cases where NewException has it's __cause__ set, we should print that as normal, then keep following the NewException.__context__ chain (with some kind of verbage or visual indicator between the __context__ and the __cause__).

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39725>
_______________________________________


More information about the Python-bugs-list mailing list