Serhiy Storchaka <storchaka+cpyt...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue39725>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to