[New-bugs-announce] [issue32683] isinstance is calling ob.__getattribute__ as a fallback instead of object.__class__.__get__

Dan Snider report at bugs.python.org
Fri Jan 26 16:10:40 EST 2018


New submission from Dan Snider <mr.assume.away at gmail.com>:

It doesn't even care if the result it gets from ob.__getattribute__("__class__") isn't a type.

Surely `isinstance` is intended to do what it is named and be guaranteed to return True if an object `ob` is an instance of class `cls`. There are already ways to trick `isinstance` into thinking objects are instances of types not present in its __mro__, ie with abstract base classes. All this does is introduce the possibility of bugs.

`inspect.isclass` in particular is affected. If there's some obscure use case for breaking `isinstance` in this manner, surely it shouldn't apply to `inspect.isclass` which according to its documentation, "Return true if the object is a class."

from inspect import isclass
class Liar:
    def __getattribute__(self, attr):
        if attr == '__class__':
            return type
        return object.__getattribute__(self, attr)

>>> islcass(Liar())
True

----------
components: Interpreter Core, Library (Lib)
messages: 310793
nosy: bup
priority: normal
severity: normal
status: open
title: isinstance is calling ob.__getattribute__ as a fallback instead of object.__class__.__get__
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32683>
_______________________________________


More information about the New-bugs-announce mailing list