fix a segfault when setting __class__ in __del__ #5283
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index cf5e2a9..304066f 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -928,6 +928,9 @@
 			assert(base);
 		}
 
+		/* Extract the type again; tp_del may have changed it */
+		type = Py_TYPE(self);
+
 		/* Call the base tp_dealloc() */
 		assert(basedealloc);
 		basedealloc(self);
@@ -1009,6 +1012,9 @@
 		}
 	}
 
+	/* Extract the type again; tp_del may have changed it */
+	type = Py_TYPE(self);
+
 	/* Call the base tp_dealloc(); first retrack self if
 	 * basedealloc knows about gc.
 	 */