Skip to content

New LOAD_ATTR_CLASS_WITH_METACLASS_CHECK does not guard against __class__ reassignment #149239

Open

Description

@Fidget-Spinner

Bug description:

This was found by Codex. Consider the following:

import enum
import _testinternalcapi

class ColorMeta(enum.EnumType):
    pass

class Color(enum.IntEnum, metaclass=ColorMeta):
    RED = 1

red = Color.RED

class Descriptor(enum.IntEnum):
    RED = 1

    def __get__(self, obj, owner):
        return "descr"

def get_enum_member():
    return Color.RED

for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
    assert get_enum_member() == 1

red.__class__ = Descriptor

for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
    assert get_enum_member() == "descr"

This passes without specializing interpreter, but fails with. @kumaraditya303

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

Activity

  1. Fidget-Spinner commented on May 1, 2026

    @Fidget-Spinner
    MemberAuthor

    A possible fix (also suggested by Codex) is to add to _LOAD_ATTR_CLASS:

    PyTypeObject *descr_type = Py_TYPE(descr);
    EXIT_IF((descr_type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) == 0 && descr_type != (PyTypeObject *)owner);

    which seems correct, as it re-checks the specialization invariant.

  2. added a commit that references this issue on May 2, 2026
  3. markshannon commented on May 5, 2026

    @markshannon
    Member

    This is a specific case of reassigning the __class__ of an object.
    We are mostly resilient to this, as we usually end up needing to check the class of an object before using it, but there are a few cases where we don't handle it well. Like this one.

    In the JIT, we assume that the class of an object is unchanged, even across escaping calls, which is probably wrong as well.

    What we could do is this:

    • Add a flag to each class, to indicate if one of its instances has ever had its class changed
    • When an instance of a class has its class changed, for that class:
      • If the flag is already set, do nothing
      • Otherwise, set the flag and change the original class's version number.
    • When optimizing:
      • Don't optimize classes (or their instances) if the flag is set
      • Rely on the type version number/watcher to de-optimize when needed.

    Not having the flag, and just changing the version number, would be correct. But it would risk doing a lot of (optimize, change class, deoptimize) cycles, until the class ran out of version numbers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions