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

@ -221,7 +221,7 @@ float_dealloc(PyFloatObject *op)
return;
}
numfree++;
Py_TYPE(op) = (struct _typeobject *)free_list;
Py_SET_TYPE(op, (PyTypeObject *)free_list);
free_list = op;
}
else

View file

@ -52,7 +52,7 @@ PyModuleDef_Init(struct PyModuleDef* def)
if (def->m_base.m_index == 0) {
max_module_number++;
Py_SET_REFCNT(def, 1);
Py_TYPE(def) = &PyModuleDef_Type;
Py_SET_TYPE(def, &PyModuleDef_Type);
def->m_base.m_index = max_module_number;
}
return (PyObject*)def;

View file

@ -144,7 +144,7 @@ PyObject_Init(PyObject *op, PyTypeObject *tp)
return PyErr_NoMemory();
}
Py_TYPE(op) = tp;
Py_SET_TYPE(op, tp);
if (PyType_GetFlags(tp) & Py_TPFLAGS_HEAPTYPE) {
Py_INCREF(tp);
}

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;

View file

@ -882,10 +882,12 @@ PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
if (result != NULL) {
PyWeakReference *prev;
if (PyCallable_Check(ob))
Py_TYPE(result) = &_PyWeakref_CallableProxyType;
else
Py_TYPE(result) = &_PyWeakref_ProxyType;
if (PyCallable_Check(ob)) {
Py_SET_TYPE(result, &_PyWeakref_CallableProxyType);
}
else {
Py_SET_TYPE(result, &_PyWeakref_ProxyType);
}
get_basic_refs(*list, &ref, &proxy);
if (callback == NULL) {
if (proxy != NULL) {