Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call isinstance instead of issubclass during exception handling #69723

Closed
sjoerdjob mannequin opened this issue Nov 2, 2015 · 7 comments
Closed

Call isinstance instead of issubclass during exception handling #69723

sjoerdjob mannequin opened this issue Nov 2, 2015 · 7 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@sjoerdjob
Copy link
Mannequin

sjoerdjob mannequin commented Nov 2, 2015

BPO 25537
Nosy @bitdancer, @vadmium

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2015-11-03.15:10:48.579>
created_at = <Date 2015-11-02.21:10:26.217>
labels = ['interpreter-core', 'type-feature']
title = 'Call `isinstance` instead of `issubclass` during exception handling'
updated_at = <Date 2015-11-04.01:53:04.853>
user = 'https://bugs.python.org/sjoerdjob'

bugs.python.org fields:

activity = <Date 2015-11-04.01:53:04.853>
actor = 'r.david.murray'
assignee = 'none'
closed = True
closed_date = <Date 2015-11-03.15:10:48.579>
closer = 'r.david.murray'
components = ['Interpreter Core']
creation = <Date 2015-11-02.21:10:26.217>
creator = 'sjoerdjob'
dependencies = []
files = []
hgrepos = []
issue_num = 25537
keywords = []
message_count = 7.0
messages = ['253946', '253951', '253963', '253996', '254030', '254035', '254036']
nosy_count = 3.0
nosy_names = ['r.david.murray', 'martin.panter', 'sjoerdjob']
pr_nums = []
priority = 'normal'
resolution = 'later'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue25537'
versions = ['Python 3.6']

@sjoerdjob
Copy link
Mannequin Author

sjoerdjob mannequin commented Nov 2, 2015

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.

@sjoerdjob sjoerdjob mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels Nov 2, 2015
@bitdancer
Copy link
Member

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
Copy link
Member

vadmium commented 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:

>>> 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')

@bitdancer
Copy link
Member

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
Copy link
Member

vadmium commented 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

@@ 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);
+    }

@bitdancer
Copy link
Member

The code change may be minor, but the semantic change is not. It needs discussion.

@bitdancer
Copy link
Member

Let me rephrase that. The semantic change *may* not be. That's what needs discussion :)

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants