[issue25537] Call `isinstance` instead of `issubclass` during exception handling
Martin Panter
report at bugs.python.org
Mon Nov 2 22:04:41 EST 2015
Martin Panter added the comment:
[Avoiding UTF-8 error]
This is an interesting idea that never occurred to me. It would be nice to support it if the performance impact isn’t a problem. By the sound of Issue 12029, performance is barely affected until you actually try to catch a “virtual exception”. So the impact of changing from subclass to instance checking shouldn’t be much worse if we do it right.
I agree this would be a new feature for a new release, not for 2.7 or 3.5.
BTW here’s another approach using the same sys.exc_info() hack that also works in Python 3:
>>> def catch_not_implemented():
... '''Helper to catch "API not implemented" exceptions'''
... [_, exc, _] = sys.exc_info()
... if isinstance(exc, NotImplementedError):
... return BaseException # Catch the exception
... if isinstance(exc, EnvironmentError) and exc.errno == ENOSYS:
... return BaseException # Catch the exception
... if isinstance(exc, HTTPError) and exc.code == HTTPStatus.NOT_IMPLEMENTED:
... return BaseException # Catch the exception
... return () # Don't catch the exception
...
>>> try: raise OSError(ENOSYS, "Dummy message")
... except catch_not_implemented() as err: print(repr(err))
...
OSError(38, 'Dummy message')
----------
dependencies: +Catching virtual subclasses in except clauses
nosy: +martin.panter
versions: -Python 2.7, Python 3.5
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25537>
_______________________________________
More information about the Python-bugs-list
mailing list