This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Michael McCoy
Recipients Michael McCoy
Date 2018-04-13.07:39:33
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1523605174.57.0.682650639539.issue33271@psf.upfronthosting.co.za>
In-reply-to
Content
Exception handling matches subtypes, not subclasses


# Example


    from abc import ABC
    
    class MyException(Exception, ABC):
        pass
    
    class OtherException(Exception):
        pass
    
    MyException.register(OtherException)
    
    try:
        raise OtherException
    except MyException:
        print("Correct: Caught MyException")
    except Exception:
        print("Wrong: Caught something else")
        
    # "Wrong: Caught something else"


# Background and evidence of bug-ness

Issue 2534 [1] (10 years ago!) introduced the behavior, but only in the Python 3 patch [2]. During code review, the correct function call was used [3], but the function's name got switched in the final python3 patch without any comment.

The current Python 2 code uses `PyObject_IsSubclass`, and produces the correct behavior in the example above (using `__metaclass__ = ABCMeta`, of course). This leads me to strongly suspect that this is a bug, not a feature. The note below regarding unittest for further evidence that this code has eight legs.

Given the ancient nature of this bug, it affects all versions of python3.

[1] https://bugs.python.org/issue2534
[2] https://bugs.python.org/file11257/isinstance3k-2.patch
[3] https://codereview.appspot.com/483/diff/1/21#newcode114
[4] https://github.com/python/cpython/blob/2.7/Python/errors.c#L119


# Solution

Coming very soon in a PR on Github, but in short, we do the following:

  1. Switch `PyType_IsSubtype` to  `PyObject_IsSubclass`.
  2. Revert the changes made to remove “dead code” in https://bugs.python.org/issue31091. The code was dead because the wrong function was used—the PR left only the bug.
  3. Add tests. Note that `unittest`’s `self.assertRaises` function uses `issubclass` and does not alert to this bug. (Different symptom, same cause.)


# Note

This bug has nothing to do with the `abc` package, beyond being a simple way to generate the error.


-Mike

Gitub: mbmccoy
History
Date User Action Args
2018-04-13 07:39:34Michael McCoysetrecipients: + Michael McCoy
2018-04-13 07:39:34Michael McCoysetmessageid: <1523605174.57.0.682650639539.issue33271@psf.upfronthosting.co.za>
2018-04-13 07:39:34Michael McCoylinkissue33271 messages
2018-04-13 07:39:33Michael McCoycreate