-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
bpo-41295: Reimplement the Carlo Verre "hackcheck" #21528
Changes from 3 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,3 @@ | ||
Resolve a regression in CPython 3.8.4 where defining "__setattr__" in a | ||
multi-inheritance setup and calling up the hierarchy chain could fail | ||
if builtins/extension types were involved in the base types. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5961,14 +5961,26 @@ hackcheck(PyObject *self, setattrofunc func, const char *what) | |
return 1; | ||
} | ||
assert(PyTuple_Check(mro)); | ||
Py_ssize_t i, n; | ||
n = PyTuple_GET_SIZE(mro); | ||
for (i = 0; i < n; i++) { | ||
|
||
/* Find the (base) type that defined the type's slot function. */ | ||
PyTypeObject *defining_type = type; | ||
Py_ssize_t i; | ||
for (i = PyTuple_GET_SIZE(mro) - 1; i >= 0; i--) { | ||
PyTypeObject *base = (PyTypeObject*) PyTuple_GET_ITEM(mro, i); | ||
if (base->tp_setattro == func) { | ||
/* 'func' is the earliest non-Python implementation in the MRO. */ | ||
if (base->tp_setattro == slot_tp_setattro) { | ||
/* Ignore Python classes: | ||
they never define their own C-level setattro. */ | ||
} | ||
else if (base->tp_setattro == type->tp_setattro) { | ||
defining_type = base; | ||
break; | ||
} else if (base->tp_setattro != slot_tp_setattro) { | ||
} | ||
} | ||
|
||
/* Reject calls that jump over intermediate C-level overrides. */ | ||
PyTypeObject *base = defining_type; | ||
while (base && base->tp_setattro != func) { | ||
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 wonder if this would read better if you wrote it as
(That for-loop is IMO a more idiomatic way to iterate over a linked list.) |
||
if (base->tp_setattro != slot_tp_setattro) { | ||
/* 'base' is not a Python class and overrides 'func'. | ||
Its tp_setattro should be called instead. */ | ||
PyErr_Format(PyExc_TypeError, | ||
|
@@ -5977,9 +5989,8 @@ hackcheck(PyObject *self, setattrofunc func, const char *what) | |
type->tp_name); | ||
return 0; | ||
} | ||
base = base->tp_base; | ||
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. (If you follow my suggestion this should go of course.) |
||
} | ||
/* Either 'func' is not in the mro (which should fail when checking 'self'), | ||
or it's the right slot function to call. */ | ||
return 1; | ||
} | ||
|
||
|
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'd add a
_
between multi and inherit.