You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Call isinstance instead of issubclass during exception handling #69723
Currently Python2.7 calls PyObject_IsSubclass during exception handling. This allows some virtual-base-class machinery to make Python believe a certain class should match an exception while it in reality does not.
However, this does not necessarily give one really enough granularity when dealing with base libraries which are not as granular in raising exceptions as one would like (the Python base library before PEP-3151 comes to mind).
Currently I used the trick of calling isinstance(sys.exc_info()[1], cls) in the __subclasscheck__, which is sort-of a great work-around.[*].
This method is great for sort-of emulating PEP-3151 in Python2.7, and similar work can be done for other libraries: making the exception classes appear more granular based on properties of the instance of the exception.
My belief is that by calling the isinstance during exception handling, one can use virtual base classes (or should I say virtual base instances) to their fullest potential in writing cleaner code.
I think this is important because exception-handling is already a place where messy code is likely to occur, and we need all the support we can get in making it just a tad less messy.[**]
Note: Python3.5 calls PyType_IsSubtype, which has bpo-12029 open for a change towards PyObject_IsSubclass. As soon as (or before) that's implemented, I'd like to propose a similar change for Python3.5+: call PyObject_IsInstance when the raised exception is an instance.
[*] See https://github.com/sjoerdjob/exhacktion/ for how I use the described hack to somewhat emulate PEP-3151 in Python2.7.
[**] One question that comes to mind is: why not just write a wrapper around the offending library. (1): If it's the base library, almost nobody is going to bother. (2): even if it's not the base library, the wrapper will likely be even more function calls, which may cost performance in both the good and the bad cases, instead of just the bad cases.
This does not sound like the kind of change we would accept for python 2.7. (I also don't understand what the difference is between calling issubclass on the type and isinstance on the instance in this case. But, then, I haven't looked at the code in question.)
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 bpo-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:
>>> defcatch_not_implemented():
... '''Helper to catch "API not implemented" exceptions'''
... [_, exc, _] = sys.exc_info()
... ifisinstance(exc, NotImplementedError):
... returnBaseException# Catch the exception
... ifisinstance(exc, EnvironmentError) and exc.errno ==ENOSYS:
... returnBaseException# Catch the exception
... ifisinstance(exc, HTTPError) and exc.code == HTTPStatus.NOT_IMPLEMENTED:
... returnBaseException# Catch the exception
... return () # Don't catch the exception
...
>>> try: raiseOSError(ENOSYS, "Dummy message")
... except catch_not_implemented() as err: print(repr(err))
...
OSError(38, 'Dummy message')
Ah, now I understand. That is definitely not something that could go into 2.7. It is also a level of change that would need to go to python-ideas first, and possibly even a PEP. Closing this pending such a discussion.
FWIW I don’t see it as a drastic change. If the current patch for bpo-12029 went ahead, I imagine the change for instance checking could look like
@@ PyErr_GivenExceptionMatches()
- /* err might be an instance, so check its class. */- if (PyExceptionInstance_Check(err))- err = PyExceptionInstance_Class(err);
@@ given_exception_matches_inner()
- res = PyObject_IsSubclass(err, exc);+ if (PyExceptionInstance_Check(err)) {+ res = PyObject_IsInstance(err, exc);+ } else {+ res = PyObject_IsSubclass(err, exc);+ }
Activity
sjoerdjob commentedon Nov 2, 2015
Currently Python2.7 calls
PyObject_IsSubclassduring exception handling. This allows some virtual-base-class machinery to make Python believe a certain class should match an exception while it in reality does not.However, this does not necessarily give one really enough granularity when dealing with base libraries which are not as granular in raising exceptions as one would like (the Python base library before PEP-3151 comes to mind).
Currently I used the trick of calling
isinstance(sys.exc_info()[1], cls)in the__subclasscheck__, which is sort-of a great work-around.[*].This method is great for sort-of emulating PEP-3151 in Python2.7, and similar work can be done for other libraries: making the exception classes appear more granular based on properties of the instance of the exception.
My belief is that by calling the
isinstanceduring exception handling, one can use virtual base classes (or should I say virtual base instances) to their fullest potential in writing cleaner code.I think this is important because exception-handling is already a place where messy code is likely to occur, and we need all the support we can get in making it just a tad less messy.[**]
Note: Python3.5 calls
PyType_IsSubtype, which has bpo-12029 open for a change towardsPyObject_IsSubclass. As soon as (or before) that's implemented, I'd like to propose a similar change for Python3.5+: callPyObject_IsInstancewhen the raised exception is an instance.[*] See https://github.com/sjoerdjob/exhacktion/ for how I use the described hack to somewhat emulate PEP-3151 in Python2.7.
[**] One question that comes to mind is: why not just write a wrapper around the offending library. (1): If it's the base library, almost nobody is going to bother. (2): even if it's not the base library, the wrapper will likely be even more function calls, which may cost performance in both the good and the bad cases, instead of just the bad cases.
bitdancer commentedon Nov 2, 2015
This does not sound like the kind of change we would accept for python 2.7. (I also don't understand what the difference is between calling issubclass on the type and isinstance on the instance in this case. But, then, I haven't looked at the code in question.)
vadmium commentedon Nov 3, 2015
[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 bpo-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:
bitdancer commentedon Nov 3, 2015
Ah, now I understand. That is definitely not something that could go into 2.7. It is also a level of change that would need to go to python-ideas first, and possibly even a PEP. Closing this pending such a discussion.
vadmium commentedon Nov 3, 2015
FWIW I don’t see it as a drastic change. If the current patch for bpo-12029 went ahead, I imagine the change for instance checking could look like
bitdancer commentedon Nov 4, 2015
The code change may be minor, but the semantic change is not. It needs discussion.
bitdancer commentedon Nov 4, 2015
Let me rephrase that. The semantic change *may* not be. That's what needs discussion :)