gh-95388: Deprecate creating immutable types with mutable bases (GH-95533)

This commit is contained in:
Petr Viktorin 2022-08-04 16:13:45 +02:00 committed by GitHub
parent 000c3874bf
commit a613fedd6e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 80 additions and 0 deletions

View file

@ -3676,6 +3676,32 @@ PyType_FromMetaclass(PyTypeObject *metaclass, PyObject *module,
goto finally;
}
/* If this is an immutable type, check if all bases are also immutable,
* and (for now) fire a deprecation warning if not.
* (This isn't necessary for static types: those can't have heap bases,
* and only heap types can be mutable.)
*/
if (spec->flags & Py_TPFLAGS_IMMUTABLETYPE) {
for (int i=0; i<PyTuple_GET_SIZE(bases); i++) {
PyTypeObject *b = (PyTypeObject*)PyTuple_GET_ITEM(bases, i);
if (!b) {
goto finally;
}
if (!_PyType_HasFeature(b, Py_TPFLAGS_IMMUTABLETYPE)) {
if (PyErr_WarnFormat(
PyExc_DeprecationWarning,
0,
"Creating immutable type %s from mutable base %s is "
"deprecated, and slated to be disallowed in Python 3.14.",
spec->name,
b->tp_name))
{
goto finally;
}
}
}
}
/* Calculate the metaclass */
if (!metaclass) {