mirror of
https://github.com/python/cpython.git
synced 2025-07-28 21:55:21 +00:00
gh-95388: Deprecate creating immutable types with mutable bases (GH-95533)
This commit is contained in:
parent
000c3874bf
commit
a613fedd6e
5 changed files with 80 additions and 0 deletions
|
@ -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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue