[issue40180] isinstance(cls_with_metaclass, non_type) raises KeyError

Terry J. Reedy report at bugs.python.org
Sat Apr 4 15:28:49 EDT 2020


New submission from Terry J. Reedy <tjreedy at udel.edu>:

Consider class Object defined as follows:

import types
class Type(type):
    __class__ = property({}.__getitem__, {}.__setitem__)
class Object(metaclass=Type):
    __slots__ = '__class__'

isinstance(Object, ob) is true for type and Type and false for anything else.  But for the examples of the latter that I tried, (list, int, types.CodeType, types.MethodType, see attached tem3.py), it incorrectly raises
  KeyError: <class '__main__.Object'> 

I cannot find the C source for isinstance.  In Python/bltinmodule.c, function builtin_isinstance_impl wraps
  retval = PyObject_IsInstance(obj, class_or_tuple);
but grepping for PyObject_IsInstance in *.c and *.h only returned other calls.

----------
components: Interpreter Core
files: tem3.py
messages: 365774
nosy: terry.reedy
priority: normal
severity: normal
stage: needs patch
status: open
title: isinstance(cls_with_metaclass, non_type) raises KeyError
type: behavior
versions: Python 3.7, Python 3.8, Python 3.9
Added file: https://bugs.python.org/file49034/tem3.py

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


More information about the Python-bugs-list mailing list