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.

classification
Title: Exception in isinstance when __class__ property raises
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Anthony Sottile, blueyed, brett.cannon, serhiy.storchaka
Priority: normal Keywords:

Created on 2018-11-01 16:43 by Anthony Sottile, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg329078 - (view) Author: Anthony Sottile (Anthony Sottile) * Date: 2018-11-01 16:43
This may be intentional, but the behaviour changed between python2 and python3.  Want to make sure it's intentional as we're working (hacking) around this in pytest: https://github.com/pytest-dev/pytest/pull/4284

The actual impact on pytest is the use of `inspect.isclass`

Simplest reproduction:

class C(object):
    @property
    def __class__(self):
        raise AssertionError('fail')

isinstance(C(), type)


In python2.x:

$ python2 t.py
$

In python 3.x:

$ python3.7 t.py
Traceback (most recent call last):
  File "t.py", line 6, in <module>
    isinstance(C(), type)
  File "t.py", line 4, in __class__
    raise AssertionError('fail')
AssertionError: fail


In python2.x it appears there's code which intentionally avoids this case:

https://github.com/python/cpython/blob/ca079a3ea30098aff3197c559a0e32d42dda6d84/Objects/abstract.c#L2906-L2909

Mostly want to see if this is intentional or not, it does feel odd that `inspect.isclass` raises instead of returning `False` in this case, but it's unclear if it's a problem with `isclass` or `isinstance`
msg329080 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-11-01 17:08
Arbitrary exceptions should not be silenced. Imagine that the user pressed Ctrl-C when the __class__ property implemented in Python was executed. The KeyboardInterrupt exception will be silenced in Python 2, and isinstance() will silently return incorrect result.
msg329081 - (view) Author: Anthony Sottile (Anthony Sottile) * Date: 2018-11-01 17:18
arbitrary, sure, but deriving from `Exception` maybe?
msg329083 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-11-01 17:45
MemoryError and RecursionError are subclasses of Exception. And there are may be other exceptions that can be raised by virtually any code and depend on conditions out of your control.
msg329143 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2018-11-02 17:02
I'm with Serhiy that exceptions should not be swallowed up unless there's a very good reason to, and in this instance there isn't if there's a bug in code as that low of a level as debugging that would be atrocious.
History
Date User Action Args
2022-04-11 14:59:07adminsetgithub: 79318
2018-11-02 17:02:12brett.cannonsetstatus: open -> closed

nosy: + brett.cannon
messages: + msg329143

resolution: not a bug
stage: resolved
2018-11-01 17:45:21serhiy.storchakasetmessages: + msg329083
2018-11-01 17:18:45Anthony Sottilesetmessages: + msg329081
2018-11-01 17:08:17serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg329080
2018-11-01 16:49:26blueyedsetnosy: + blueyed
2018-11-01 16:43:59Anthony Sottilecreate