You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
New LOAD_ATTR_CLASS_WITH_METACLASS_CHECK does not guard against __class__ reassignment #149239
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.
Activity
Fidget-Spinner commentedon May 1, 2026
A possible fix (also suggested by Codex) is to add to _LOAD_ATTR_CLASS:
which seems correct, as it re-checks the specialization invariant.
pythongh-149239: Deopt `LOAD_ATTR_CLASS_WITH_METACLASS_CHECK` on reas…
markshannon commentedon May 5, 2026
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:
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.