Title: `except` doesn't use `isinstance`
Type: Stage:
Components: Interpreter Core Versions: Python 3.2
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, cool-RR, pitrou, terry.reedy
Priority: normal Keywords:

Created on 2010-10-01 22:51 by cool-RR, last changed 2010-10-08 22:30 by terry.reedy. This issue is now closed.

Messages (6)
msg117844 - (view) Author: Ram Rachum (cool-RR) * Date: 2010-10-01 22:51
Is there a reason why `execpt` compares base classes instead of using `isinstance`? This prevents using `__instancecheck__` to override the instance check.
msg117846 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-10-01 23:00
Mainly to protect against potential infinite recursion with isinstance checks. Also, performance is probably better.

Here are the relevant code and comments in PyErr_GivenExceptionMatches() (in Python/errors.c):

        /* PyObject_IsSubclass() can recurse and therefore is
           not safe (see test_bad_getattr in test.pickletester). */
        res = PyType_IsSubtype((PyTypeObject *)err, (PyTypeObject *)exc);
msg117849 - (view) Author: Ram Rachum (cool-RR) * Date: 2010-10-01 23:19
I don't understand the infinite recursion argument. If there's such an infinite recursion, wouldn't it be due to a bug in the user's implementation of __instancecheck__?
msg117874 - (view) Author: Ram Rachum (cool-RR) * Date: 2010-10-02 13:52
Also, how important is the performance of exception checking *after* an exception was raised? I mean, wouldn't it matter only for programs that raise and catch hundreds of exceptions a second?
msg117892 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2010-10-02 18:03
It matters when exceptions are expected or are a normal part of control flow. For example StopIteration.
msg118230 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-10-08 22:30
Questions should be asked on python-list or other discussion forums, not nere. If this were a feature-request, it would need a realistic use-case. If the implied change were requested, I would think it should be rejected as conflicting with the language definition, which to me stipulates that when an exception is raised, nothing happens until it is caught.
Date User Action Args
2010-10-08 22:30:48terry.reedysetstatus: open -> closed
versions: - Python 3.1
nosy: + terry.reedy

messages: + msg118230

resolution: not a bug
2010-10-02 18:03:46benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg117892
2010-10-02 13:52:58cool-RRsetmessages: + msg117874
2010-10-01 23:19:26cool-RRsetmessages: + msg117849
2010-10-01 23:00:35pitrousetnosy: + pitrou

messages: + msg117846
versions: - Python 2.6, Python 2.5, Python 2.7, Python 3.3
2010-10-01 22:51:09cool-RRcreate