Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isinstance(cls_with_metaclass, non_type) raises KeyError #84361

Open
terryjreedy opened this issue Apr 4, 2020 · 5 comments
Open

isinstance(cls_with_metaclass, non_type) raises KeyError #84361

terryjreedy opened this issue Apr 4, 2020 · 5 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@terryjreedy
Copy link
Member

BPO 40180
Nosy @terryjreedy, @serhiy-storchaka, @isidentical
Files
  • tem3.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2020-04-04.19:28:49.901>
    labels = ['interpreter-core', '3.8', 'type-bug', '3.7', '3.9']
    title = 'isinstance(cls_with_metaclass, non_type) raises KeyError'
    updated_at = <Date 2020-04-04.22:54:24.596>
    user = 'https://github.com/terryjreedy'

    bugs.python.org fields:

    activity = <Date 2020-04-04.22:54:24.596>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2020-04-04.19:28:49.901>
    creator = 'terry.reedy'
    dependencies = []
    files = ['49034']
    hgrepos = []
    issue_num = 40180
    keywords = []
    message_count = 5.0
    messages = ['365774', '365776', '365791', '365797', '365801']
    nosy_count = 3.0
    nosy_names = ['terry.reedy', 'serhiy.storchaka', 'BTaskaya']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = 'needs patch'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue40180'
    versions = ['Python 3.7', 'Python 3.8', 'Python 3.9']

    @terryjreedy
    Copy link
    Member Author

    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.

    @terryjreedy terryjreedy added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Apr 4, 2020
    @isidentical
    Copy link
    Sponsor Member

    What would you expect in this case? Objects/abstract.c:2429 is where the isinstance code. If only returning False would be enough, something like this (untested) would be enough

    --- a/Objects/abstract.c
    +++ b/Objects/abstract.c
    @@ -2436,6 +2436,10 @@ object_isinstance(PyObject *inst, PyObject *cls)
             retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls);
             if (retval == 0) {
                 retval = _PyObject_LookupAttrId(inst, &PyId___class__, &icls);
    +            if (retval == NULL && PyErr_Occurred()) {
    +                PyErr_Clear();
    +                retval = 0;
    +            }

    @serhiy-storchaka
    Copy link
    Member

    All works as expected to me. Your __class__ attribute raise an arbitrary exception, and it is expected, that the code which uses it passes it to you.

    I am against silencing all exceptions. It may hide bugs.

    It is even documented. See What's New in Python 3.8:

    • The CPython interpreter can swallow exceptions in some circumstances.
      In Python 3.8 this happens in fewer cases. In particular, exceptions
      raised when getting the attribute from the type dictionary are no longer
      ignored. (Contributed by Serhiy Storchaka in :issue:`35459`.)

    @terryjreedy
    Copy link
    Member Author

    (Serhiy posted while I wrote this.)
    Sorry, I should have quoted the doc. " If object is not an object of the given type, the function always returns False." Raising instead is a bug -- even of the object itself is somewhat buggy.

    My knowledge of the C codebase is insufficient to review, let alone write, non-trivial changes. But I was curious where and why Object itself was being used as a key. I presume
    retval = _PyObject_LookupAttrId(inst, &PyId___class__, &icls);
    is roughly equivalent to Object.__class__ and indeed, that raises the KeyError. I presume the why has something to do with the interaction between metaclasses, properties, and slots. So if the details are correct, your patch should plug this hole. Can you submit a PR with added test?

    Issue background. The example is from Dan Snider, OP of bpo-38689. When he typed 'Object(' into IDLE, IDLE tried to pop up a calltip. But inspect.signature(Object) failed with the (unexpected and undocumented) KeyError when it called isinstance(Object, types.MethodTypes), and IDLE froze. Given Python's flexibility, it can be hard to write code that works with any user code thrown at it.

    @serhiy-storchaka
    Copy link
    Member

    Sorry, I should have quoted the doc. " If object is not an object of the given type, the function always returns False." Raising instead is a bug -- even of the object itself is somewhat buggy.

    You take it too literally. It does not mean that the function always returns a value. It can also raise an exception. If you press Ctrl-C it may raise an exception. If there is no memory to create some temporary objects, it may raise an exception. If you turn of the computer, it may neither return a value nor raise an exception.

    You created a class whose __class__ attribute always raises an exception. What do you expect to get when you use this attribute? {}.__getitem__ always raise a KeyError, because an empty dict does not contain any key.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants