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

Serhiy Storchaka report at bugs.python.org
Sun Feb 23 17:13:18 EST 2020


Serhiy Storchaka <storchaka+cpython at gmail.com> added the comment:

It is more complicated. In the following example

try:
    try:
        raise TypeError
    except:
        try:
            try:
                raise OverflowError
            except:
                raise ValueError
        except:
            raise KeyError from None
except BaseException as exc:
    e = exc

the __context__ chain is KeyError -> ValueError -> OverflowError -> TypeError. We want to suppress ValueError and OverflowError, and keep KeyError -> TypeError. I afraid that the chain of exceptions just do not have enough information to do this. Maybe analyzing tracebacks can help, but it looks too complex.

Maybe the simpler solution is to "cut" __context__ when we leave an exception handler. If __suppress_context__ is true and the current handled exception is not __context__, set it as __context__.

Here is a PR which implements this solution. I am not sure that it is correct, and it may miss some corner cases, it is just an example. Pay attention to the ipaddress module: in some cases we want to suppress more than one exception, and currently it is inconvenient.


Maybe we incorrectly handle exception chains. Maybe they should be build in a way similar to tracebacks: initially set __context__ to None and add a handled exception to the end of the chain when leave an exception handler.

----------

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


More information about the Python-bugs-list mailing list