bpo-39573: Add Py_SET_TYPE() function (GH-18394)

Add Py_SET_TYPE() function to set the type of an object.
This commit is contained in:
Victor Stinner 2020-02-07 09:17:07 +01:00 committed by GitHub
parent daa9756cb6
commit d2ec81a8c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 77 additions and 48 deletions

View file

@ -4097,9 +4097,10 @@ object_set_class(PyObject *self, PyObject *value, void *closure)
}
if (compatible_for_assignment(oldto, newto, "__class__")) {
if (newto->tp_flags & Py_TPFLAGS_HEAPTYPE)
if (newto->tp_flags & Py_TPFLAGS_HEAPTYPE) {
Py_INCREF(newto);
Py_TYPE(self) = newto;
}
Py_SET_TYPE(self, newto);
if (oldto->tp_flags & Py_TPFLAGS_HEAPTYPE)
Py_DECREF(oldto);
return 0;
@ -5334,8 +5335,9 @@ PyType_Ready(PyTypeObject *type)
NULL when type is &PyBaseObject_Type, and we know its ob_type is
not NULL (it's initialized to &PyType_Type). But coverity doesn't
know that. */
if (Py_TYPE(type) == NULL && base != NULL)
Py_TYPE(type) = Py_TYPE(base);
if (Py_TYPE(type) == NULL && base != NULL) {
Py_SET_TYPE(type, Py_TYPE(base));
}
/* Initialize tp_bases */
bases = type->tp_bases;