diff Objects/typeobject.c @ 26780:5efe6b0ac0e6 2.2

Fix SF #658106, Setting __class__ to NoneType Backport Guido's checkin 2.171: Disallow class assignment completely unless both old and new are heap types. This prevents nonsense like 2.__class__ = bool or True.__class__ = int.
author Neal Norwitz <nnorwitz@gmail.com>
date Tue, 24 Dec 2002 14:48:41 +0000
parents 8f743ebe6c66
children 8e21bcf2158a
line wrap: on
line diff
--- a/Objects/typeobject.c	Tue Dec 24 14:45:54 2002 +0000
+++ b/Objects/typeobject.c	Tue Dec 24 14:48:41 2002 +0000
@@ -1702,6 +1702,13 @@
 		return -1;
 	}
 	new = (PyTypeObject *)value;
+	if (!(new->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
+	    !(old->tp_flags & Py_TPFLAGS_HEAPTYPE))
+	{
+		PyErr_Format(PyExc_TypeError,
+			     "__class__ assignment: only for heap types");
+		return -1;
+	}
 	newbase = new;
 	oldbase = old;
 	while (equiv_structs(newbase, newbase->tp_base))
@@ -1718,13 +1725,9 @@
 			     old->tp_name);
 		return -1;
 	}
-	if (new->tp_flags & Py_TPFLAGS_HEAPTYPE) {
-		Py_INCREF(new);
-	}
+	Py_INCREF(new);
 	self->ob_type = new;
-	if (old->tp_flags & Py_TPFLAGS_HEAPTYPE) {
-		Py_DECREF(old);
-	}
+	Py_DECREF(old);
 	return 0;
 }