gh-134786: raise error if Py_TPFLAGS_MANAGED_WEAKREF or Py_TPFLAGS_MANAGED_DICT is used without Py_TPFLAGS_HAVE_GC set (#135863)

This commit is contained in:
Sergey Miryanov 2025-11-02 16:04:49 +05:00 committed by GitHub
parent d12cbf2865
commit da65f38a94
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 72 additions and 2 deletions

View file

@ -8898,6 +8898,13 @@ type_ready_preheader(PyTypeObject *type)
type->tp_name);
return -1;
}
if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC)) {
PyErr_Format(PyExc_SystemError,
"type %s has the Py_TPFLAGS_MANAGED_DICT flag "
"but not Py_TPFLAGS_HAVE_GC flag",
type->tp_name);
return -1;
}
type->tp_dictoffset = -1;
}
if (type->tp_flags & Py_TPFLAGS_MANAGED_WEAKREF) {
@ -8910,6 +8917,13 @@ type_ready_preheader(PyTypeObject *type)
type->tp_name);
return -1;
}
if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC)) {
PyErr_Format(PyExc_SystemError,
"type %s has the Py_TPFLAGS_MANAGED_WEAKREF flag "
"but not Py_TPFLAGS_HAVE_GC flag",
type->tp_name);
return -1;
}
type->tp_weaklistoffset = MANAGED_WEAKREF_OFFSET;
}
return 0;