I’d like to be able to emulate this behaviour in a few places, such as implementing context managers with def __exit__( eg:
class ExampleContextManager:
def __enter__(self):
return self
def __exit__(self, typ, value, traceback):
if typ is None:
return False
if value is None:
value = typ()
if inspect.exceptionmatches(exc, SomeType):
# handle SomeType case
So I’m proposing a python implementation of PyErr_GivenExceptionMatches be made available as inspect.exceptionmatches
Perhaps it should go in operator, since that’s where function versions of other fundamental operations are exposed. Though it’s obviously very inefficient, here’s a method for a pure-Python implementation:
Wouldn’t a simpler Python implementation just duplicate the high-level exception matching logic?
def exception_matches(exc, expected):
"""Return whether or not "raise exc" will be caught by "except expected"."""
if isinstance(exc, type):
return isubclass(exc, expected)
return isinstance(exc, excepted)
Have I missed any cases, or some other issue?
It would be nice to have an official way to do this. The operator module makes sense to me.
Unlike regular instance checks, exception handling skips all of the various hooks - the abc modules, __class__, __instancecheck__() and __subclasscheck__(), etc. Examining the implementation of PyErr_GivenExceptionMatches() though suggests perhaps a better function to expose PyType_IsSubtype(), since the PyErr_GivenExceptionMatches() just recursively handles tuples and verifies they are exceptions.