-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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
gh-92678: Fix MRO calculation for C extension types with manual __dict__ #95242
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fix MRO calculation issue which caused C extension types manually managing | ||
their own ``__dict__`` to fail in Python 3.11. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2241,6 +2241,12 @@ extra_ivars(PyTypeObject *type, PyTypeObject *base) | |
type->tp_weaklistoffset + sizeof(PyObject *) == t_size && | ||
type->tp_flags & Py_TPFLAGS_HEAPTYPE) | ||
t_size -= sizeof(PyObject *); | ||
if (!(type->tp_flags & Py_TPFLAGS_MANAGED_DICT) && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to check for if (FIELD_AT_OFFSET(dictoffset, t_size)) {
t_size -= sizeof(PyObject *);
}
if (FIELD_AT_OFFSET(weaklistoffset, t_size)) {
t_size -= sizeof(PyObject *);
}
if (FIELD_AT_OFFSET(dictoffset, t_size)) {
t_size -= sizeof(PyObject *);
} |
||
type->tp_dictoffset && base->tp_dictoffset == 0 && | ||
type->tp_dictoffset + sizeof(PyObject *) == t_size && | ||
type->tp_flags & Py_TPFLAGS_HEAPTYPE) { | ||
t_size -= sizeof(PyObject *); | ||
} | ||
Comment on lines
+2244
to
+2249
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This reintroduces the old code pre-3.11, but guards it with a check for not 8319114#diff-1decebeef15f4e0b0ce106c665751ec55068d4d1d1825847925ad4f528b5b872L2254 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition is a bit arcane. Could you capture it in a variable or use a macro or inline function so we can name what we are checking for? Currently is a bit difficult to decipher what happens when all of these are true or false There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So funny thing is I also found the old code arcane and couldn't really understand it well. From my rough guesstimation
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a really cursed example of Hyrum's law. The |
||
return t_size != b_size; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is is correct to check
Py_TPFLAGS_MANAGED_DICT
during class creation?It is set during class creation: https://github.com/python/cpython/blob/main/Objects/typeobject.c#L2989
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think setting the flag happens before the MRO check. https://github.com/python/cpython/blob/main/Objects/typeobject.c#L3174-L3186
(
type_new_descriptors
thenPyType_Ready
).(
PyType_Ready
->type_ready
->type_ready_mro
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you factor out the check into a macro, the code would be a bit clearer.