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

Add Py_SET_TYPE() function to set the type of an object.
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index 26e238c..dfc5b19 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -221,7 +221,7 @@
             return;
         }
         numfree++;
-        Py_TYPE(op) = (struct _typeobject *)free_list;
+        Py_SET_TYPE(op, (PyTypeObject *)free_list);
         free_list = op;
     }
     else
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c
index da329b4..0a59326 100644
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -52,7 +52,7 @@
     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;
diff --git a/Objects/object.c b/Objects/object.c
index 5806542..503fb86 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -144,7 +144,7 @@
         return PyErr_NoMemory();
     }
 
-    Py_TYPE(op) = tp;
+    Py_SET_TYPE(op, tp);
     if (PyType_GetFlags(tp) & Py_TPFLAGS_HEAPTYPE) {
         Py_INCREF(tp);
     }
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 5b8d5a2..e6a84b0 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -4097,9 +4097,10 @@
     }
 
     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 @@
        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;
diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c
index d104b64..18c737e 100644
--- a/Objects/weakrefobject.c
+++ b/Objects/weakrefobject.c
@@ -882,10 +882,12 @@
         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) {