-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-121654: Add PyType_Freeze() function #122457
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
Changes from 1 commit
2d24aba
f7bd310
f7d75ac
1fcda68
beea5e3
0155362
1c1fa7e
e0cc4be
a9f1f01
0f98b64
2752daa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4643,9 +4643,14 @@ check_basicsize_includes_size_and_offsets(PyTypeObject* type) | |
} | ||
|
||
static int | ||
check_immutable_bases(const char *type_name, PyObject *bases) | ||
check_immutable_bases(const char *type_name, PyObject *bases, int skip_first) | ||
{ | ||
for (Py_ssize_t i=0; i<PyTuple_GET_SIZE(bases); i++) { | ||
Py_ssize_t i = 0; | ||
if (skip_first) { | ||
// When testing the MRO, skip the type itself | ||
i = 1; | ||
} | ||
for (; i<PyTuple_GET_SIZE(bases); i++) { | ||
PyTypeObject *b = (PyTypeObject*)PyTuple_GET_ITEM(bases, i); | ||
if (!b) { | ||
return -1; | ||
|
@@ -4818,7 +4823,7 @@ _PyType_FromMetaclass_impl( | |
* and only heap types can be mutable.) | ||
*/ | ||
if (spec->flags & Py_TPFLAGS_IMMUTABLETYPE) { | ||
if (check_immutable_bases(spec->name, bases) < 0) { | ||
if (check_immutable_bases(spec->name, bases, 0) < 0) { | ||
goto finally; | ||
} | ||
} | ||
|
@@ -11190,8 +11195,17 @@ add_operators(PyTypeObject *type, PyTypeObject *def) | |
int | ||
PyType_Freeze(PyTypeObject *type) | ||
{ | ||
PyObject *bases = type->tp_bases; | ||
if (check_immutable_bases(type->tp_name, bases) < 0) { | ||
// gh-121654: Check the __mro__ instead of __bases__ | ||
PyObject *mro = type_get_mro(type, NULL); | ||
if (!PyTuple_Check(mro)) { | ||
Py_DECREF(mro); | ||
PyErr_SetString(PyExc_TypeError, "unable to get the type MRO"); | ||
return -1; | ||
} | ||
Comment on lines
+11342
to
+11346
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. How difficult to add test for this case? 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'm not sure that it's possible to create a type with a MRO which is not a tuple. Using a metaclass, it's possible to override the MRO, but internally, Python converts I added this check since |
||
|
||
int check = check_immutable_bases(type->tp_name, mro, 1); | ||
Py_DECREF(mro); | ||
if (check < 0) { | ||
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.
Sorry for the necro-posting.
IIUC only improperly constructed tuples can store NULL items. Is this such a case? If so, is the following check redundant?
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 don't know, I just moved the code. The test was already there before.
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.
Got it, thanks!